diff mbox

Allow compounds with empty initializer in pedantic mode (PR c/59351)

Message ID 20131202201045.GI9986@redhat.com
State New
Headers show

Commit Message

Marek Polacek Dec. 2, 2013, 8:10 p.m. UTC
We triggered an assert on attached testcase, because when building the
compound literal with empty initial value complete_array_type returns
3, but we assert it returns 0.  It returns 3 only in the pedantic mode,
where empty initializer braces are forbidden.  Since we already gave
a warning, I think we could loosen the assert a bit and allow
empty initial values at that point.  sizeof on such compound literal
then yields zero, which I think is correct.
The assert exists even in GCC 4.0.  

Regtested/botstrapped on x86_64-linux, ok for trunk and 4.8 and
perhaps even 4.7?

2013-12-02  Marek Polacek  <polacek@redhat.com>

	PR c/59351
c/
	* c-decl.c (build_compound_literal): Allow compound literals with
	empty initial value.
testsuite/
	* gcc.dg/pr59351.c: New test.


	Marek

Comments

Joseph Myers Dec. 2, 2013, 11:49 p.m. UTC | #1
On Mon, 2 Dec 2013, Marek Polacek wrote:

> Regtested/botstrapped on x86_64-linux, ok for trunk and 4.8 and
> perhaps even 4.7?
> 
> 2013-12-02  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c/59351
> c/
> 	* c-decl.c (build_compound_literal): Allow compound literals with
> 	empty initial value.
> testsuite/
> 	* gcc.dg/pr59351.c: New test.

OK.
Jeff Law Dec. 3, 2013, 8:03 p.m. UTC | #2
On 12/02/13 13:10, Marek Polacek wrote:
> We triggered an assert on attached testcase, because when building the
> compound literal with empty initial value complete_array_type returns
> 3, but we assert it returns 0.  It returns 3 only in the pedantic mode,
> where empty initializer braces are forbidden.  Since we already gave
> a warning, I think we could loosen the assert a bit and allow
> empty initial values at that point.  sizeof on such compound literal
> then yields zero, which I think is correct.
> The assert exists even in GCC 4.0.
>
> Regtested/botstrapped on x86_64-linux, ok for trunk and 4.8 and
> perhaps even 4.7?
>
> 2013-12-02  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/59351
> c/
> 	* c-decl.c (build_compound_literal): Allow compound literals with
> 	empty initial value.
> testsuite/
> 	* gcc.dg/pr59351.c: New test.
I was going to ask for some details about where we detect and warn to 
review that code as well, but given your test verifies the warning as 
well, we're good to go.

OK for the trunk.  Branch maintainers have final say for their branches.

Thanks,

Jeff
Marek Polacek Dec. 3, 2013, 8:13 p.m. UTC | #3
On Tue, Dec 03, 2013 at 01:03:43PM -0700, Jeff Law wrote:
> On 12/02/13 13:10, Marek Polacek wrote:
> >We triggered an assert on attached testcase, because when building the
> >compound literal with empty initial value complete_array_type returns
> >3, but we assert it returns 0.  It returns 3 only in the pedantic mode,
> >where empty initializer braces are forbidden.  Since we already gave
> >a warning, I think we could loosen the assert a bit and allow
> >empty initial values at that point.  sizeof on such compound literal
> >then yields zero, which I think is correct.
> >The assert exists even in GCC 4.0.
> >
> >Regtested/botstrapped on x86_64-linux, ok for trunk and 4.8 and
> >perhaps even 4.7?
> >
> >2013-12-02  Marek Polacek  <polacek@redhat.com>
> >
> >	PR c/59351
> >c/
> >	* c-decl.c (build_compound_literal): Allow compound literals with
> >	empty initial value.
> >testsuite/
> >	* gcc.dg/pr59351.c: New test.
> I was going to ask for some details about where we detect and warn
> to review that code as well, but given your test verifies the
> warning as well, we're good to go.

Yeah, the patch should really only fix the ICE.

> OK for the trunk.  Branch maintainers have final say for their branches.

Note that I already put this into trunk and after ack by Jakub into
4.7/4.8 branches as well. 

	Marek
diff mbox

Patch

--- gcc/c/c-decl.c.mp3	2013-12-02 20:23:27.947224366 +0100
+++ gcc/c/c-decl.c	2013-12-02 20:25:56.618779873 +0100
@@ -4693,7 +4693,9 @@  build_compound_literal (location_t loc,
     {
       int failure = complete_array_type (&TREE_TYPE (decl),
 					 DECL_INITIAL (decl), true);
-      gcc_assert (!failure);
+      /* If complete_array_type returns 3, it means that the
+         initial value of the compound literal is empty.  Allow it.  */
+      gcc_assert (failure == 0 || failure == 3);
 
       type = TREE_TYPE (decl);
       TREE_TYPE (DECL_INITIAL (decl)) = type;
--- gcc/testsuite/gcc.dg/pr59351.c.mp3	2013-12-02 20:29:05.612345428 +0100
+++ gcc/testsuite/gcc.dg/pr59351.c	2013-12-02 20:48:47.298751979 +0100
@@ -0,0 +1,8 @@ 
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -Wpedantic" } */
+
+unsigned int
+foo (void)
+{
+  return sizeof ((int[]) {}); /* { dg-warning "ISO C forbids empty initializer braces" } */
+}