From: Anders Carlsson Date: Mon, 3 Dec 2007 01:01:28 +0000 (+0000) Subject: Handle initializing vector elements correctly. Emit just one warning if there are... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f0049e65763ed4ace041fc7bbece9814067003a8;p=clang Handle initializing vector elements correctly. Emit just one warning if there are excess initializers, instead of one per initializer. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44525 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 71f2f19226..920d9917d6 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -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,