[cfe-commits] r54583 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/tentative-decls.c

Steve Naroff snaroff at apple.com
Sat Aug 9 09:04:45 PDT 2008


Author: snaroff
Date: Sat Aug  9 11:04:40 2008
New Revision: 54583

URL: http://llvm.org/viewvc/llvm-project?rev=54583&view=rev
Log:
Fix Sema::MergeVarDecl() to better handle type compatibility. The previous code was trying to handle arrays specially (which didn't work for pointers to array). Removed local helper function areEquivalentArrayTypes(), replacing it's use with the more general ASTContext::typesAreCompatible() predicate.

Even though the test case this fixes is in "tentative-decls.c", this bug didn't have anything to do with our handling of tentative definitions (which is what I first expected). In any event, this is a tricky area of the spec.


Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/tentative-decls.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=54583&r1=54582&r2=54583&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Aug  9 11:04:40 2008
@@ -378,38 +378,6 @@
   return New;
 }
 
-/// equivalentArrayTypes - Used to determine whether two array types are 
-/// equivalent.
-/// We need to check this explicitly as an incomplete array definition is
-/// considered a VariableArrayType, so will not match a complete array 
-/// definition that would be otherwise equivalent.
-static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType,
-                                    ASTContext &Context) {
-  const ArrayType *NewAT = Context.getAsArrayType(NewQType);
-  const ArrayType *OldAT = Context.getAsArrayType(OldQType);
-
-  if (!NewAT || !OldAT)
-    return false;
-  
-  // If either (or both) array types in incomplete we need to strip off the
-  // outer VariableArrayType.  Once the outer VAT is removed the remaining
-  // types must be identical if the array types are to be considered 
-  // equivalent.
-  // eg. int[][1] and int[1][1] become
-  //     VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
-  // removing the outermost VAT gives
-  //     CAT(1, int) and CAT(1, int)
-  // which are equal, therefore the array types are equivalent.
-  if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
-    if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
-      return false;
-    NewQType = Context.getCanonicalType(NewAT->getElementType());
-    OldQType = Context.getCanonicalType(OldAT->getElementType());
-  }
-  
-  return NewQType == OldQType;
-}
-
 /// Predicate for C "tentative" external object definitions (C99 6.9.2).
 bool Sema::isTentativeDefinition(VarDecl *VD) {
   if (VD->isFileVarDecl())
@@ -471,8 +439,7 @@
   // Verify the types match.
   QualType OldCType = Context.getCanonicalType(Old->getType());
   QualType NewCType = Context.getCanonicalType(New->getType());
-  if (OldCType != NewCType &&
-      !areEquivalentArrayTypes(NewCType, OldCType, Context)) {
+  if (OldCType != NewCType && !Context.typesAreCompatible(OldCType, NewCType)) {
     Diag(New->getLocation(), diag::err_redefinition, New->getName());
     Diag(Old->getLocation(), diag::err_previous_definition);
     return New;

Modified: cfe/trunk/test/Sema/tentative-decls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/tentative-decls.c?rev=54583&r1=54582&r2=54583&view=diff

==============================================================================
--- cfe/trunk/test/Sema/tentative-decls.c (original)
+++ cfe/trunk/test/Sema/tentative-decls.c Sat Aug  9 11:04:40 2008
@@ -24,6 +24,9 @@
 int i4;
 extern int i4;
 
+int (*pToArray)[];
+int (*pToArray)[8];
+
 void func() {
   extern int i1; // expected-error{{previous definition is here}}
   static int i1; // expected-error{{static declaration of 'i1' follows non-static declaration}}





More information about the cfe-commits mailing list