[cfe-commits] r46799 - in /cfe/trunk: AST/ASTContext.cpp test/Sema/compare.c

Eli Friedman eli.friedman at gmail.com
Tue Feb 5 20:53:23 PST 2008


Author: efriedma
Date: Tue Feb  5 22:53:22 2008
New Revision: 46799

URL: http://llvm.org/viewvc/llvm-project?rev=46799&view=rev
Log:
Be a bit stricter about array type compatibility.


Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/test/Sema/compare.c

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=46799&r1=46798&r2=46799&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Tue Feb  5 22:53:22 2008
@@ -1627,14 +1627,18 @@
 }
 
 bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
-  QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
-  QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
-  
+  // Compatible arrays must have compatible element types
+  QualType ltype = lhs->getAsArrayType()->getElementType();
+  QualType rtype = rhs->getAsArrayType()->getElementType();
+
   if (!typesAreCompatible(ltype, rtype))
     return false;
-    
-  // FIXME: If both types specify constant sizes, then the sizes must also be 
-  // the same. Even if the sizes are the same, GCC produces an error.
+
+  // Compatible arrays must be the same size
+  if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
+    if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
+      return RCAT->getSize() == LCAT->getSize();
+
   return true;
 }
 

Modified: cfe/trunk/test/Sema/compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/compare.c?rev=46799&r1=46798&r2=46799&view=diff

==============================================================================
--- cfe/trunk/test/Sema/compare.c (original)
+++ cfe/trunk/test/Sema/compare.c Tue Feb  5 22:53:22 2008
@@ -1,4 +1,4 @@
-// RUN: clang -fsyntax-only -verify %s
+// RUN: clang -fsyntax-only -pedantic -verify %s
 
 int test(char *C) { // nothing here should warn.
   return C != ((void*)0);
@@ -10,3 +10,8 @@
 {
     return a == b;
 }
+
+int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
+  int d = (a == c);
+  return a == b; // expected-warning {{comparison of distinct pointer types}}
+}





More information about the cfe-commits mailing list