]> granicus.if.org Git - clang/commitdiff
Fix a bug in semantic analysis involving anonymous structs and flexible arrays.
authorEli Friedman <eli.friedman@gmail.com>
Tue, 7 Feb 2012 05:00:47 +0000 (05:00 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 7 Feb 2012 05:00:47 +0000 (05:00 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149966 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/anonymous-struct-union.c

index c44b474e8052b6d743b93ed03b54a5764d12d739..ffc7c618727c0ab81a4d022bd291ee10deeadfc1 100644 (file)
@@ -9171,11 +9171,23 @@ void Sema::ActOnFields(Scope* S,
   if (EnclosingDecl->isInvalidDecl())
     return;
 
-  // Verify that all the fields are okay.
+  RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
+
+  // Start counting up the number of named members; make sure to include
+  // members of anonymous structs and unions in the total.
   unsigned NumNamedMembers = 0;
+  if (Record) {
+    for (RecordDecl::decl_iterator i = Record->decls_begin(),
+                                   e = Record->decls_end(); i != e; i++) {
+      if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*i))
+        if (IFD->getDeclName())
+          ++NumNamedMembers;
+    }
+  }
+
+  // Verify that all the fields are okay.
   SmallVector<FieldDecl*, 32> RecFields;
 
-  RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
   bool ARCErrReported = false;
   for (llvm::ArrayRef<Decl *>::iterator i = Fields.begin(), end = Fields.end();
        i != end; ++i) {
index d88abc3c3b9e9385ceb6a6c72ed5ba5e4fa17cee..e0822901b0ed01f6632d365c4d20ac05bb14f9d4 100644 (file)
@@ -102,3 +102,9 @@ typedef struct {
   int x;
 } a_struct;
 int tmp = (a_struct) { .x = 0 }; // expected-error {{initializing 'int' with an expression of incompatible type 'a_struct'}}
+
+// This example comes out of the C11 standard; make sure we don't accidentally reject it.
+struct s {
+  struct { int i; };
+  int a[];
+};