From: David Majnemer Date: Wed, 24 Sep 2014 11:04:09 +0000 (+0000) Subject: Sema: Inherit the flexible array property from struct fields X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc251c9c6ceff6dc1cdbc3101bf583396d3a5392;p=clang Sema: Inherit the flexible array property from struct fields A record which contains a flexible array member is itself a flexible array member. A struct which contains such a record should also consider itself to be a flexible array member. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218378 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 71017f4f9d..277816be42 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -12704,8 +12704,7 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, continue; } // Okay, we have a legal flexible array member at the end of the struct. - if (Record) - Record->setHasFlexibleArrayMember(true); + Record->setHasFlexibleArrayMember(true); } else if (!FDTy->isDependentType() && RequireCompleteType(FD->getLocation(), FD->getType(), diag::err_field_incomplete)) { @@ -12714,11 +12713,11 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, EnclosingDecl->setInvalidDecl(); continue; } else if (const RecordType *FDTTy = FDTy->getAs()) { - if (FDTTy->getDecl()->hasFlexibleArrayMember()) { - // If this is a member of a union, then entire union becomes "flexible". - if (Record && Record->isUnion()) { - Record->setHasFlexibleArrayMember(true); - } else { + if (Record && FDTTy->getDecl()->hasFlexibleArrayMember()) { + // A type which contains a flexible array member is considered to be a + // flexible array member. + Record->setHasFlexibleArrayMember(true); + if (!Record->isUnion()) { // If this is a struct/class and this is not the last element, reject // it. Note that GCC supports variable sized arrays in the middle of // structures. @@ -12730,8 +12729,6 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, // other structs as an extension. Diag(FD->getLocation(), diag::ext_flexible_array_in_struct) << FD->getDeclName(); - if (Record) - Record->setHasFlexibleArrayMember(true); } } } diff --git a/test/SemaCXX/flexible-array-test.cpp b/test/SemaCXX/flexible-array-test.cpp index e58f19a62e..2d74fa5245 100644 --- a/test/SemaCXX/flexible-array-test.cpp +++ b/test/SemaCXX/flexible-array-test.cpp @@ -14,6 +14,12 @@ void QMap::insert(const Key &, const T &avalue) v = avalue; } +struct Rec { + union { // expected-warning-re {{variable sized type '{{.*}}' not at the end of a struct or class is a GNU extension}} + int u0[]; + }; + int x; +} rec; struct inotify_event {