From: Eli Friedman Date: Tue, 7 Feb 2012 05:00:47 +0000 (+0000) Subject: Fix a bug in semantic analysis involving anonymous structs and flexible arrays. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11e70d7fe2eb11874c3619ad26dc2b525b81074f;p=clang Fix a bug in semantic analysis involving anonymous structs and flexible arrays. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149966 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index c44b474e80..ffc7c61872 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -9171,11 +9171,23 @@ void Sema::ActOnFields(Scope* S, if (EnclosingDecl->isInvalidDecl()) return; - // Verify that all the fields are okay. + RecordDecl *Record = dyn_cast(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(*i)) + if (IFD->getDeclName()) + ++NumNamedMembers; + } + } + + // Verify that all the fields are okay. SmallVector RecFields; - RecordDecl *Record = dyn_cast(EnclosingDecl); bool ARCErrReported = false; for (llvm::ArrayRef::iterator i = Fields.begin(), end = Fields.end(); i != end; ++i) { diff --git a/test/Sema/anonymous-struct-union.c b/test/Sema/anonymous-struct-union.c index d88abc3c3b..e0822901b0 100644 --- a/test/Sema/anonymous-struct-union.c +++ b/test/Sema/anonymous-struct-union.c @@ -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[]; +};