Handle initializing vector elements correctly. Emit just one warning if there are excess initializers, instead of one per initializer.

llvm-svn: 44525
This commit is contained in:
Anders Carlsson 2007-12-03 01:01:28 +00:00
parent 4b6d965611
commit 5dd106b212
1 changed files with 18 additions and 4 deletions

View File

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