]> granicus.if.org Git - clang/commitdiff
Refactor and fix checking for initialization of flexible array members. The old...
authorEli Friedman <eli.friedman@gmail.com>
Tue, 23 Aug 2011 22:24:57 +0000 (22:24 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 23 Aug 2011 22:24:57 +0000 (22:24 +0000)
Fixes PR10648 and another similar accepts-invalid bug.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138398 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
lib/Sema/SemaInit.cpp
test/Sema/flexible-array-init.c

index 0ded2fb3e40251ec63d3ad793edbf0a8135339e2..3d8fbd37c1287f94b0a98f21b629c9c536ae7a4e 100644 (file)
@@ -2716,8 +2716,8 @@ def ext_flexible_array_in_struct : Extension<
   "%0 may not be nested in a struct due to flexible array member">;
 def ext_flexible_array_in_array : Extension<
   "%0 may not be used as an array element due to flexible array member">;
-def err_flexible_array_init_nonempty : Error<
-  "non-empty initialization of flexible array member inside subobject">;
+def err_flexible_array_init : Error<
+  "initialization of flexible array member is not allowed">;
 def ext_flexible_array_empty_aggregate_ms : Extension<
   "flexible array member %0 in otherwise empty %select{struct|class}1 "
   "is a Microsoft extension">, InGroup<Microsoft>;
index 62032e8b65c0925d42d6423b60ee4e4e0532b98f..9f43aa2f05453b312795a61bed34c38e94f2a446 100644 (file)
@@ -5740,27 +5740,6 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
     VDecl->setType(DclT);
     Init->setType(DclT);
   }
-
-  
-  // If this variable is a local declaration with record type, make sure it
-  // doesn't have a flexible member initialization.  We only support this as a
-  // global/static definition.
-  if (VDecl->hasLocalStorage())
-    if (const RecordType *RT = VDecl->getType()->getAs<RecordType>())
-      if (RT->getDecl()->hasFlexibleArrayMember()) {
-        // Check whether the initializer tries to initialize the flexible
-        // array member itself to anything other than an empty initializer list.
-        if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
-          unsigned Index = std::distance(RT->getDecl()->field_begin(),
-                                         RT->getDecl()->field_end()) - 1;
-          if (Index < ILE->getNumInits() &&
-              !(isa<InitListExpr>(ILE->getInit(Index)) &&
-                cast<InitListExpr>(ILE->getInit(Index))->getNumInits() == 0)) {
-            Diag(VDecl->getLocation(), diag::err_nonstatic_flexible_variable);
-            VDecl->setInvalidDecl();
-          }
-        }
-      }
   
   // Check any implicit conversions within the expression.
   CheckImplicitConversions(Init, VDecl->getLocation());
index a61b1d11fc5a9542eda2a154a41a4342150c6f1a..2d47cda0e0f43dab0ba5a1d71ff8ac74cec87f14 100644 (file)
@@ -249,6 +249,9 @@ class InitListChecker {
                                InitListExpr *ILE, bool &RequiresSecondPass);
   void FillInValueInitializations(const InitializedEntity &Entity,
                                   InitListExpr *ILE, bool &RequiresSecondPass);
+  bool CheckFlexibleArrayInit(const InitializedEntity &Entity,
+                              Expr *InitExpr, FieldDecl *Field,
+                              bool TopLevelObject);
 public:
   InitListChecker(Sema &S, const InitializedEntity &Entity,
                   InitListExpr *IL, QualType &T);
@@ -1113,6 +1116,43 @@ void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
   }
 }
 
+bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
+                                             Expr *InitExpr,
+                                             FieldDecl *Field,
+                                             bool TopLevelObject) {
+  // Handle GNU flexible array initializers.
+  unsigned FlexArrayDiag;
+  if (isa<InitListExpr>(InitExpr) &&
+      cast<InitListExpr>(InitExpr)->getNumInits() == 0) {
+    // Empty flexible array init always allowed as an extension
+    FlexArrayDiag = diag::ext_flexible_array_init;
+  } else if (SemaRef.getLangOptions().CPlusPlus) {
+    // Disallow flexible array init in C++; it is not required for gcc
+    // compatibility, and it needs work to IRGen correctly in general.
+    FlexArrayDiag = diag::err_flexible_array_init;
+  } else if (!TopLevelObject) {
+    // Disallow flexible array init on non-top-level object
+    FlexArrayDiag = diag::err_flexible_array_init;
+  } else if (Entity.getKind() != InitializedEntity::EK_Variable) {
+    // Disallow flexible array init on anything which is not a variable.
+    FlexArrayDiag = diag::err_flexible_array_init;
+  } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) {
+    // Disallow flexible array init on local variables.
+    FlexArrayDiag = diag::err_flexible_array_init;
+  } else {
+    // Allow other cases.
+    FlexArrayDiag = diag::ext_flexible_array_init;
+  }
+  
+  SemaRef.Diag(InitExpr->getSourceRange().getBegin(),
+               FlexArrayDiag)
+    << InitExpr->getSourceRange().getBegin();
+  SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
+    << Field;
+
+  return FlexArrayDiag != diag::ext_flexible_array_init;
+}
+
 void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
                                             InitListExpr *IList,
                                             QualType DeclType,
@@ -1239,24 +1279,11 @@ void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
       Index >= IList->getNumInits())
     return;
 
-  // Handle GNU flexible array initializers.
-  if (!TopLevelObject &&
-      (!isa<InitListExpr>(IList->getInit(Index)) ||
-       cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
-    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
-                  diag::err_flexible_array_init_nonempty)
-      << IList->getInit(Index)->getSourceRange().getBegin();
-    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
-      << *Field;
+  if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field,
+                             TopLevelObject)) {
     hadError = true;
     ++Index;
     return;
-  } else {
-    SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
-                 diag::ext_flexible_array_init)
-      << IList->getInit(Index)->getSourceRange().getBegin();
-    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
-      << *Field;
   }
 
   InitializedEntity MemberEntity =
@@ -1567,16 +1594,10 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
         Invalid = true;
       }
 
-      // Handle GNU flexible array initializers.
-      if (!Invalid && !TopLevelObject &&
-          cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
-        SemaRef.Diag(DIE->getSourceRange().getBegin(),
-                      diag::err_flexible_array_init_nonempty)
-          << DIE->getSourceRange().getBegin();
-        SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
-          << *Field;
+      // Check GNU flexible array initializer.
+      if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field,
+                                             TopLevelObject))
         Invalid = true;
-      }
 
       if (Invalid) {
         ++Index;
index 12f5d4f5d6059d93fa4cc57691c7c1088abede69..78fc7c5e0df3700cd15f81ce2d008f06e47358cb 100644 (file)
@@ -7,9 +7,7 @@ struct one {
 struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{flexible array initialization is a GNU extension}}
 
 void test() {
-  struct one x3 = {5, {1, 2, 3}}; // \
-   // expected-warning{{flexible array initialization is a GNU extension}} \
-   // expected-error {{non-static initialization of a variable with flexible array member}}
+  struct one x3 = {5, {1, 2, 3}}; // expected-error{{initialization of flexible array member is not allowed}}
   struct one x3a = { 5 };
   struct one x3b = { .a = 5 };
   struct one x3c = { 5, {} }; // expected-warning{{use of GNU empty initializer extension}} \
@@ -19,22 +17,23 @@ void test() {
 
 struct foo { 
   int x; 
-  int y[]; // expected-note 6 {{initialized flexible array member 'y' is here}}
+  int y[]; // expected-note 8 {{initialized flexible array member 'y' is here}}
 }; 
 struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
      
 struct foo a = { 1, { 2, 3, 4 } };        // expected-warning{{flexible array initialization is a GNU extension}}
-struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{non-empty initialization of flexible array member inside subobject}}
+struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{initialization of flexible array member is not allowed}}
 struct bar c = { { 1, { } } };            // // expected-warning{{flexible array initialization is a GNU extension}} \
               // expected-warning{{use of GNU empty initializer extension}} \
               // expected-warning{{zero size arrays are an extension}}
 struct foo d[1] = { { 1, { 2, 3, 4 } } };  // expected-warning{{'struct foo' may not be used as an array element due to flexible array member}} \
-              // expected-error{{non-empty initialization of flexible array member inside subobject}}
+              // expected-error{{initialization of flexible array member is not allowed}}
 
-struct foo desig_foo = { .y = {2, 3, 4} };
+struct foo desig_foo = { .y = {2, 3, 4} }; // expected-warning{{flexible array initialization is a GNU extension}}
 struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
-  // expected-warning{{zero size arrays are an extension}}
-struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}}
+  // expected-warning{{zero size arrays are an extension}} \
+  // expected-warning{{flexible array initialization is a GNU extension}}
+struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{initialization of flexible array member is not allowed}}
 struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
 
 struct point {
@@ -68,13 +67,25 @@ struct Y {
 // PR8217
 struct PR8217a {
   int  i;
-  char v[];
+  char v[]; // expected-note 2 {{initialized flexible array member 'v' is here}}
 };
 
 void PR8217() {
-  struct PR8217a foo1 = { .i = 0, .v = "foo" }; // expected-error {{non-static initialization of a variable with flexible array member}}
+  struct PR8217a foo1 = { .i = 0, .v = "foo" }; // expected-error {{initialization of flexible array member is not allowed}}
   struct PR8217a foo2 = { .i = 0 };
-  struct PR8217a foo3 = { .i = 0, .v = { 'b', 'a', 'r', '\0' } }; // expected-error {{non-static initialization of a variable with flexible array member}}
+  struct PR8217a foo3 = { .i = 0, .v = { 'b', 'a', 'r', '\0' } }; // expected-error {{initialization of flexible array member is not allowed}}
   struct PR8217a bar;
 }
 
+typedef struct PR10648 {
+ unsigned long n;
+ int v[]; // expected-note {{initialized flexible array member 'v' is here}}
+} PR10648;
+int f10648() { 
+  return (PR10648){2, {3, 4}}.v[1]; // expected-error {{initialization of flexible array member is not allowed}}
+}
+
+struct FlexWithUnnamedBitfield { int : 10; int x; int y[]; }; // expected-note {{initialized flexible array member 'y' is here}}
+void TestFlexWithUnnamedBitfield() {
+  struct FlexWithUnnamedBitfield x = {10, {3}}; // expected-error {{initialization of flexible array member is not allowed}}
+}