[cfe-commits] r64392 - in /cfe/trunk: lib/Sema/SemaInit.cpp test/Sema/designated-initializers.c

Douglas Gregor dgregor at apple.com
Thu Feb 12 11:00:39 PST 2009


Author: dgregor
Date: Thu Feb 12 13:00:39 2009
New Revision: 64392

URL: http://llvm.org/viewvc/llvm-project?rev=64392&view=rev
Log:
Fix a bug with designated initializers where we were stepping out of a
union subobject initialization before checking whether the next
initiailizer was actually a designated initializer. This led to
spurious "excess elements in union initializer" errors. Thanks to
rdivacky for reporting the bug!


Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Sema/designated-initializers.c

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=64392&r1=64391&r2=64392&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Thu Feb 12 13:00:39 2009
@@ -742,14 +742,13 @@
     return;
   }
 
-
-
   // If structDecl is a forward declaration, this loop won't do
   // anything except look at designated initializers; That's okay,
   // because an error should get printed out elsewhere. It might be
   // worthwhile to skip over the rest of the initializer, though.
   RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
   RecordDecl::field_iterator FieldEnd = RD->field_end();
+  bool InitializedSomething = false;
   while (Index < IList->getNumInits()) {
     Expr *Init = IList->getInit(Index);
 
@@ -768,11 +767,7 @@
                                      true, TopLevelObject))
         hadError = true;
 
-      // Abort early for unions: the designator handled the
-      // initialization of the appropriate field.
-      if (DeclType->isUnionType())
-        break;
-
+      InitializedSomething = true;
       continue;
     }
 
@@ -781,6 +776,10 @@
       break;
     }
 
+    // We've already initialized a member of a union. We're done.
+    if (InitializedSomething && DeclType->isUnionType())
+      break;
+
     // If we've hit the flexible array member at the end, we're done.
     if (Field->getType()->isIncompleteArrayType())
       break;
@@ -793,11 +792,11 @@
 
     CheckSubElementType(IList, Field->getType(), Index,
                         StructuredList, StructuredIndex);
+    InitializedSomething = true;
 
     if (DeclType->isUnionType()) {
       // Initialize the first field within the union.
       StructuredList->setInitializedFieldInUnion(*Field);
-      break;
     }
 
     ++Field;

Modified: cfe/trunk/test/Sema/designated-initializers.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/designated-initializers.c?rev=64392&r1=64391&r2=64392&view=diff

==============================================================================
--- cfe/trunk/test/Sema/designated-initializers.c (original)
+++ cfe/trunk/test/Sema/designated-initializers.c Thu Feb 12 13:00:39 2009
@@ -172,3 +172,17 @@
   .f.arr[3] = 1,
   .arr = { &f }
 };
+
+typedef unsigned char u_char;
+typedef unsigned short u_short;
+
+union wibble {
+        u_char  arr1[6];
+        u_short arr2[3];
+};
+
+const union wibble wobble = { .arr2[0] = 0xffff,
+                              .arr2[1] = 0xffff,
+                              .arr2[2] = 0xffff };
+
+const union wibble wobble2 = { .arr2 = {4, 5, 6}, 7 }; // expected-error{{excess elements in union initializer}}





More information about the cfe-commits mailing list