]> granicus.if.org Git - clang/commitdiff
GNU allows structs with flexible array members to be placed inside
authorDouglas Gregor <dgregor@apple.com>
Tue, 10 Feb 2009 21:49:46 +0000 (21:49 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 10 Feb 2009 21:49:46 +0000 (21:49 +0000)
arrays and other structs/unions as an extension. Downgrade our error
to a warning. Fixes PR3540.

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

include/clang/Basic/DiagnosticSemaKinds.def
lib/Sema/SemaType.cpp
test/Sema/array-constraint.c
test/Sema/flexible-array-init.c

index 9c879f740aa263b5a9efe0e80ca4c744061fffe0..5dfe9d6f35b7fdcd7e2161a2bec402305a5e7e27 100644 (file)
@@ -651,7 +651,7 @@ DIAG(err_flexible_array_empty_struct, ERROR,
      "flexible array %0 not allowed in otherwise empty struct")
 DIAG(ext_flexible_array_in_struct, EXTENSION,
      "%0 may not be nested in a struct due to flexible array member")
-DIAG(err_flexible_array_in_array, ERROR,
+DIAG(ext_flexible_array_in_array, EXTENSION,
      "%0 may not be used as an array element due to flexible array member")
 DIAG(err_flexible_array_init_nonempty, ERROR,
      "non-empty initialization of flexible array member inside subobject")
index 25fd5c39fe3a0d6ebe5b52f2f30f07f701cbc100..e898782f31d80c0f97c3caf47e4ba16217f18245 100644 (file)
@@ -399,12 +399,9 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
         D.setInvalidType(true);
       } else if (const RecordType *EltTy = T->getAsRecordType()) {
         // If the element type is a struct or union that contains a variadic
-        // array, reject it: C99 6.7.2.1p2.
-        if (EltTy->getDecl()->hasFlexibleArrayMember()) {
-          Diag(DeclType.Loc, diag::err_flexible_array_in_array) << T;
-          T = Context.IntTy;
-          D.setInvalidType(true);
-        }
+        // array, accept it as a GNU extension: C99 6.7.2.1p2.
+        if (EltTy->getDecl()->hasFlexibleArrayMember())
+          Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
       } else if (T->isObjCInterfaceType()) {
         Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
       }
index d35b0acdfe7500dad4ad73669519f524e8590441..06b5d747abc2cc18905510a9bba5c68dd187874f 100644 (file)
@@ -20,7 +20,7 @@ struct vari {
   int b[];
 };
 
-struct vari *func(struct vari a[]) { // expected-error {{'struct vari' may not be used as an array element due to flexible array member}}
+struct vari *func(struct vari a[]) { // expected-warning {{'struct vari' may not be used as an array element due to flexible array member}}
   return a;
 }
 
index 99ef66abe99afa296c684c7033c4abecc2c69b91..909b856d171bca761787b66fd1f8220d1798522e 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: clang -fsyntax-only -verify %s
+// RUN: clang -fsyntax-only -pedantic -verify %s
 struct one {
   int a;
   int values[];
@@ -12,17 +12,21 @@ void test() {
 
 struct foo { 
   int x; 
-  int y[]; // expected-note 3 {{initialized flexible array member 'y' is here}}
+  int y[]; // expected-note 4 {{initialized flexible array member 'y' is here}}
 }; 
-struct bar { struct foo z; };
+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 } };        // Valid.
 struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{non-empty initialization of flexible array member inside subobject}}
-struct bar c = { { 1, { } } };            // Valid.
-struct foo d[1] = { { 1, { 2, 3, 4 } } };  // expected-error{{'struct foo' may not be used as an array element due to flexible array member}}
+struct bar c = { { 1, { } } };            // Valid. \
+              // 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}}
 
 struct foo desig_foo = { .y = {2, 3, 4} };
-struct bar desig_bar = { .z.y = { } };
+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}}
 struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
 
@@ -36,3 +40,19 @@ struct polygon {
 };
 struct polygon poly = { 
   .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
+
+// PR3540
+struct X {
+  int a;
+  int b;
+  char data[];
+};
+
+struct Y {
+  int a:4;
+  int b:4;
+  int c;
+  int d;
+  int e;
+  struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}}
+};