[cfe-commits] r40475 - in /cfe/trunk: AST/Type.cpp Sema/SemaExpr.cpp Sema/SemaStmt.cpp clang.xcodeproj/project.pbxproj include/clang/AST/Type.h test/Parser/promote_types_in_proto.c

Steve Naroff snaroff at apple.com
Tue Jul 24 14:46:40 PDT 2007


Author: snaroff
Date: Tue Jul 24 16:46:40 2007
New Revision: 40475

URL: http://llvm.org/viewvc/llvm-project?rev=40475&view=rev
Log:

Fix Sema::ParseCallExpr()...it wasn't doing the default array/function promotions on it's argument types.

This resulted in the following errors when compiling promote_types_in_proto.c test...

[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang test/Parser/promote_types_in_proto.c 
test/Parser/promote_types_in_proto.c:7:24: error: incompatible types passing 'char *[]' to function expecting 'char *const []'
        arrayPromotion(argv);
        ~~~~~~~~~~~~~~ ^~~~
test/Parser/promote_types_in_proto.c:8:27: error: incompatible types passing 'void (char *const [])' to function expecting 'void (char *const [])'
        functionPromotion(arrayPromotion);
        ~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~
2 diagnostics generated.

When fixing this, noticed that both ParseCallExpr() and ParseReturnStmt() were prematurely comparing types for
equivalence. This is incorrect (since the expr. promotions haven't been done yet). To fix this, I moved the
check "down" to Sema::CheckAssignmentConstraints().

I also converted Type::isArrayType() to the modern API (since I needed it). Still more Type predicates to 
convert.

Added:
    cfe/trunk/test/Parser/promote_types_in_proto.c
Modified:
    cfe/trunk/AST/Type.cpp
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/AST/Type.h

Modified: cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Type.cpp?rev=40475&r1=40474&r2=40475&view=diff

==============================================================================
--- cfe/trunk/AST/Type.cpp (original)
+++ cfe/trunk/AST/Type.cpp Tue Jul 24 16:46:40 2007
@@ -84,8 +84,16 @@
   return 0;
 }
 
-bool Type::isArrayType() const {
-  return isa<ArrayType>(CanonicalType);
+const ArrayType *Type::isArrayType() const {
+  // If this is directly a reference type, return it.
+  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
+    return ATy;
+  
+  // If this is a typedef for an array type, strip the typedef off without
+  // losing all typedef information.
+  if (isa<ArrayType>(CanonicalType))
+    return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
+  return 0;
 }
 
 bool Type::isStructureType() const {

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=40475&r1=40474&r2=40475&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Tue Jul 24 16:46:40 2007
@@ -433,10 +433,15 @@
       
       QualType lhsType = proto->getArgType(i);
       QualType rhsType = argExpr->getType();
-      
-      if (lhsType == rhsType) // common case, fast path...
-        continue;
+
+      // C99 6.7.5.3p7
+      if (const ArrayType *ary = lhsType->isArrayType())
+        lhsType = Context.getPointerType(ary->getElementType());
         
+      // C99 6.7.5.3p8
+      if (lhsType->isFunctionType())
+        lhsType = Context.getPointerType(lhsType);
+
       AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
                                                                       argExpr);
       SourceLocation l = argExpr->getLocStart();
@@ -638,7 +643,7 @@
   }
   if (t->isFunctionType())
     promoteExprToType(e, Context.getPointerType(t));
-  else if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
+  else if (const ArrayType *ary = t->isArrayType())
     promoteExprToType(e, Context.getPointerType(ary->getElementType()));
 }
 
@@ -793,6 +798,9 @@
 ///
 Sema::AssignmentCheckResult
 Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
+  if (lhsType == rhsType) // common case, fast path...
+    return Compatible;
+
   if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
     if (lhsType->isVectorType() || rhsType->isVectorType()) {
       if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
@@ -1066,9 +1074,6 @@
            lhsType.getAsString(), lex->getSourceRange());
       return QualType();
   }
-  if (lhsType == rhsType) // common case, fast path...
-    return lhsType;
-  
   AssignmentCheckResult result;
   
   if (compoundType.isNull())

Modified: cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaStmt.cpp?rev=40475&r1=40474&r2=40475&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Tue Jul 24 16:46:40 2007
@@ -318,9 +318,6 @@
   // we have a non-void function with an expression, continue checking
   QualType rhsType = RetValExp->getType();
 
-  if (lhsType == rhsType) // common case, fast path...
-    return new ReturnStmt(RetValExp);
-
   // C99 6.8.6.4p3(136): The return statement is not an assignment. The 
   // overlap restriction of subclause 6.5.16.1 does not apply to the case of 
   // function return.  

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=40475&r1=40474&r2=40475&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Tue Jul 24 16:46:40 2007
@@ -191,7 +191,7 @@
 		1A869AA70BA21ABA008DA07A /* LiteralSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = LiteralSupport.cpp; sourceTree = "<group>"; };
 		84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
 		84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
 		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=40475&r1=40474&r2=40475&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Tue Jul 24 16:46:40 2007
@@ -35,6 +35,7 @@
   class PointerType;
   class ReferenceType;
   class VectorType;
+  class ArrayType;
   
 /// QualType - For efficiency, we don't store CVR-qualified types as nodes on
 /// their own: instead each reference to a type stores the qualifiers.  This
@@ -237,7 +238,7 @@
   bool isDerivedType() const;
   const PointerType *isPointerType() const;
   const ReferenceType *isReferenceType() const;
-  bool isArrayType() const;
+  const ArrayType *isArrayType() const;
   bool isStructureType() const;   
   bool isUnionType() const;
   

Added: cfe/trunk/test/Parser/promote_types_in_proto.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/promote_types_in_proto.c?rev=40475&view=auto

==============================================================================
--- cfe/trunk/test/Parser/promote_types_in_proto.c (added)
+++ cfe/trunk/test/Parser/promote_types_in_proto.c Tue Jul 24 16:46:40 2007
@@ -0,0 +1,9 @@
+// RUN: clang %s
+void functionPromotion(void f(char *const []));
+void arrayPromotion(char * const argv[]);
+
+int whatever(int argc, char *argv[])
+{
+        arrayPromotion(argv);
+        functionPromotion(arrayPromotion);
+}





More information about the cfe-commits mailing list