]> granicus.if.org Git - clang/commitdiff
Handle initializing vector elements correctly. Emit just one warning if there are...
authorAnders Carlsson <andersca@mac.com>
Mon, 3 Dec 2007 01:01:28 +0000 (01:01 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 3 Dec 2007 01:01:28 +0000 (01:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44525 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/SemaDecl.cpp

index 71f2f192260e4a06c0cb32b7b346b077c85b99e5..920d9917d6f8fac839abd22578f612d8f35c1539 100644 (file)
@@ -459,13 +459,18 @@ void Sema::CheckConstantInitList(QualType DeclType, InitListExpr *IList,
     // 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 @@ void Sema::CheckConstantInitList(QualType DeclType, InitListExpr *IList,
         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 @@ bool Sema::CheckInitializer(Expr *&Init, QualType &DeclType, bool isStatic) {
                           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,