This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C PATCH] Enable initializing statics with COMPOUND_LITERAL_EXPR in C99 (PR c/63567)


Building Linux kernel failed with 'error: initializer element is not
constant', because they're initializing objects with static storage
duration with (T){ ...} - and that isn't permitted in gnu99/gnu11.

I think the Right Thing is to allow some latitude here and enable it
even in gnu99/gnu11 unless -pedantic.  In gnu89, this will work as
before even with -pedantic.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2014-10-17  Marek Polacek  <polacek@redhat.com>

	PR c/63567
	* c-typeck.c (digest_init): Allow initializing objects with static
	storage duration with compound literals in non-pedantic mode.

	* gcc.dg/pr63567.c: New test.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 5c0697a..8ddf368 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -6676,7 +6676,7 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype,
 	inside_init = convert (type, inside_init);
 
       if (require_constant
-	  && (code == VECTOR_TYPE || !flag_isoc99)
+	  && (code == VECTOR_TYPE || !pedantic || !flag_isoc99)
 	  && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
 	{
 	  /* As an extension, allow initializing objects with static storage
diff --git gcc/testsuite/gcc.dg/pr63567.c gcc/testsuite/gcc.dg/pr63567.c
index e69de29..cf942ef 100644
--- gcc/testsuite/gcc.dg/pr63567.c
+++ gcc/testsuite/gcc.dg/pr63567.c
@@ -0,0 +1,11 @@
+/* PR c/63567 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+/* Allow initializing objects with static storage duration with
+   compound literals even in non-pedantic gnu99/gnu11.  This is
+   being used in Linux kernel.  */
+
+struct T { int i; };
+struct S { struct T t; };
+static struct S s = (struct S) { .t = { 42 } };

	Marek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]