]> granicus.if.org Git - clang/commitdiff
Count the number of initializable members correctly in structs/unions
authorEli Friedman <eli.friedman@gmail.com>
Sun, 25 May 2008 14:03:31 +0000 (14:03 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sun, 25 May 2008 14:03:31 +0000 (14:03 +0000)
with unnamed members.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51557 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaInit.cpp
test/Sema/array-init.c

index 6eb072eebafaefb381bb125d8f856f44bd41debf..43adc2a82f94ee3a9b376885a5b3b2a288604b5d 100644 (file)
@@ -38,9 +38,13 @@ int InitListChecker::numArrayElements(QualType DeclType) {
 
 int InitListChecker::numStructUnionElements(QualType DeclType) {
   RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
+  int InitializableMembers = 0;
+  for (int i = 0; i < structDecl->getNumMembers(); i++)
+    if (structDecl->getMember(i)->getIdentifier())
+      ++InitializableMembers;
   if (structDecl->getKind() == Decl::Union)
-    return std::min(structDecl->getNumMembers(), 1);
-  return structDecl->getNumMembers() - structDecl->hasFlexibleArrayMember();
+    return std::min(InitializableMembers, 1);
+  return InitializableMembers - structDecl->hasFlexibleArrayMember();
 }
 
 void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, 
index 69255569ddc75eeea657224c1d36fddfe9e4432e..c1a789ea5b35b89ee71557cca0edcf4bc02088ec 100644 (file)
@@ -216,3 +216,9 @@ void varArray() {
 // PR2151
 int emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct extension}} expected-error{{initializer for aggregate with no elements}}
 
+int noNamedInit() {
+struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
+}
+struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
+int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
+