[cfe-commits] r44525 - /cfe/trunk/Sema/SemaDecl.cpp

Anders Carlsson andersca at mac.com
Sun Dec 2 17:01:29 PST 2007


Author: andersca
Date: Sun Dec  2 19:01:28 2007
New Revision: 44525

URL: http://llvm.org/viewvc/llvm-project?rev=44525&view=rev
Log:
Handle initializing vector elements correctly. Emit just one warning if there are excess initializers, instead of one per initializer.

Modified:
    cfe/trunk/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sun Dec  2 19:01:28 2007
@@ -459,13 +459,18 @@
     // Set DeclType, used below to recurse (for multi-dimensional arrays).
     DeclType = CAT->getElementType();
   } else if (DeclType->isScalarType()) {
-    Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, 
-         IList->getSourceRange());
-    maxElementsAtThisLevel = 1;
+    if (const VectorType *VT = DeclType->getAsVectorType())
+      maxElementsAtThisLevel = VT->getNumElements();
+    else {
+      Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init, 
+           IList->getSourceRange());
+      maxElementsAtThisLevel = 1;
+    }
   } 
   // The empty init list "{ }" is treated specially below.
   unsigned numInits = IList->getNumInits();
   if (numInits) {
+    bool DidWarn = false;
     for (unsigned i = 0; i < numInits; i++) {
       Expr *expr = IList->getInit(i);
       
@@ -478,9 +483,12 @@
         totalInits--;    // decrement the total number of initializers.
         
         // Check if we have space for another initializer.
-        if ((nInitsAtLevel > maxElementsAtThisLevel) || (totalInits < 0))
+        if (((nInitsAtLevel > maxElementsAtThisLevel) || (totalInits < 0)) &&
+            !DidWarn) {
           Diag(expr->getLocStart(), diag::warn_excess_initializers, 
                expr->getSourceRange());
+          DidWarn = true;
+        }
       }
     }
     if (nInitsAtLevel < maxElementsAtThisLevel) // fill the remaining elements.
@@ -533,6 +541,12 @@
                           isStatic, maxElements, hadError);
     return hadError;
   }
+  if (const VectorType *VT = DeclType->getAsVectorType()) {
+    int maxElements = VT->getNumElements();
+    CheckConstantInitList(DeclType, InitList, VT->getElementType(),
+                          isStatic, maxElements, hadError);
+    return hadError;
+  }
   if (DeclType->isScalarType()) { // C99 6.7.8p11: Allow "int x = { 1, 2 };"
     int maxElements = 1;
     CheckConstantInitList(DeclType, InitList, DeclType, isStatic, maxElements, 





More information about the cfe-commits mailing list