[cfe-commits] r51555 - in /cfe/trunk: lib/Sema/SemaInit.cpp test/Sema/array-init.c

Eli Friedman eli.friedman at gmail.com
Sun May 25 06:22:36 PDT 2008


Author: efriedma
Date: Sun May 25 08:22:35 2008
New Revision: 51555

URL: http://llvm.org/viewvc/llvm-project?rev=51555&view=rev
Log:
Move the error checking for variable-sized objects so we don't 
double-report errors; fixes PR2362.


Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Sema/array-init.c

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Sun May 25 08:22:35 2008
@@ -28,22 +28,9 @@
 }
 
 int InitListChecker::numArrayElements(QualType DeclType) {
-  int maxElements;
-  if (DeclType->isIncompleteArrayType()) {
-    // FIXME: use a proper constant
-    maxElements = 0x7FFFFFFF;
-  } else if (const VariableArrayType *VAT =
-             DeclType->getAsVariableArrayType()) {
-    // Check for VLAs; in standard C it would be possible to check this
-    // earlier, but I don't know where clang accepts VLAs (gcc accepts
-    // them in all sorts of strange places).
-    SemaRef->Diag(VAT->getSizeExpr()->getLocStart(),
-                  diag::err_variable_object_no_init,
-                  VAT->getSizeExpr()->getSourceRange());
-    hadError = true;
-    maxElements = 0x7FFFFFFF;
-  } else {
-    const ConstantArrayType *CAT = DeclType->getAsConstantArrayType();
+  // FIXME: use a proper constant
+  int maxElements = 0x7FFFFFFF;
+  if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) {
     maxElements = static_cast<int>(CAT->getSize().getZExtValue());
   }
   return maxElements;
@@ -101,8 +88,10 @@
 
   CheckListElementTypes(IList, T, Index);
   IList->setType(T);
+  if (hadError)
+    return;
 
-  if (!hadError && (Index < IList->getNumInits())) {
+  if (Index < IList->getNumInits()) {
     // We have leftover initializers
     if (IList->getNumInits() > 0 &&
         SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
@@ -119,7 +108,7 @@
     }
   }
 
-  if (!hadError && T->isScalarType())
+  if (T->isScalarType())
     SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, 
                   IList->getSourceRange());
 }
@@ -165,8 +154,7 @@
   } else if (ElemType->isScalarType()) {
     CheckScalarType(IList, ElemType, Index);
   } else if (expr->getType()->getAsRecordType() &&
-             SemaRef->Context.typesAreCompatible(
-                  IList->getInit(Index)->getType(), ElemType)) {
+             SemaRef->Context.typesAreCompatible(expr->getType(), ElemType)) {
     Index++;
     // FIXME: Add checking
   } else {
@@ -230,6 +218,17 @@
       return;
     }
   }
+  if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) {
+    // Check for VLAs; in standard C it would be possible to check this
+    // earlier, but I don't know where clang accepts VLAs (gcc accepts
+    // them in all sorts of strange places).
+    SemaRef->Diag(VAT->getSizeExpr()->getLocStart(),
+                  diag::err_variable_object_no_init,
+                  VAT->getSizeExpr()->getSourceRange());
+    hadError = true;
+    return;
+  }
+
   int maxElements = numArrayElements(DeclType);
   QualType elementType = DeclType->getAsArrayType()->getElementType();
   int numElements = 0;

Modified: cfe/trunk/test/Sema/array-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-init.c?rev=51555&r1=51554&r2=51555&view=diff

==============================================================================
--- cfe/trunk/test/Sema/array-init.c (original)
+++ cfe/trunk/test/Sema/array-init.c Sun May 25 08:22:35 2008
@@ -208,3 +208,7 @@
 int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
 int u2 = {{3}}; //expected-error{{too many braces around scalar initializer}}
 
+// PR2362
+void varArray() {
+  int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
+}





More information about the cfe-commits mailing list