]> granicus.if.org Git - clang/commitdiff
Sema: Inherit the flexible array property from struct fields
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 24 Sep 2014 11:04:09 +0000 (11:04 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 24 Sep 2014 11:04:09 +0000 (11:04 +0000)
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

lib/Sema/SemaDecl.cpp
test/SemaCXX/flexible-array-test.cpp

index 71017f4f9dfe6f6ac8a9462093a07e805e990ced..277816be4291ab19fc9448a37ff3addd4a69a831 100644 (file)
@@ -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<RecordType>()) {
-      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);
           }
         }
       }
index e58f19a62eca93c9380aa71cf1e570ff51f624b8..2d74fa52452ebd128b9cd85a3c529ddee85392df 100644 (file)
@@ -14,6 +14,12 @@ void QMap<Key, T>::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
 {