Bug 54804 - -Wuninitialized fails to warn about uninitialized local union
Summary: -Wuninitialized fails to warn about uninitialized local union
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2012-10-04 00:27 UTC by Paul Eggert
Modified: 2012-10-04 08:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Eggert 2012-10-04 00:27:53 UTC
This problem stems from a proposed change to Bison to work
around a GCC bug.  I'd rather get the GCC bug fixed, so I'm
filing this bug report.

Bison-generated parsers currently contain code that GCC incorrectly
generates a -Wuninitialized warning for.  In trying to fix this, I
discovered that GCC sometimes does not warn when it should.  Compile
the following program with 'gcc -O2 -Wall -S'.

   union YYSTYPE { int ival; };
   union YYSTYPE
   yyparse (void)
   {
     union YYSTYPE yylval;
     return yylval;
   }


   struct s { int ival; };
   struct s
   yyparse_with_struct (void)
   {
     struct s xxlval;
     return xxlval;
   }

Here's what I observe, for the above program:

$ gcc -O2 -Wall -S t.i
t.i: In function 'yyparse_with_struct':
t.i:15:6: warning: 'xxlval.ival' is used uninitialized in this function [-Wuninitialized]

There should be a warning for yylval, but the warning is missing.
The (correct) warning for xxlval suggests that the problem has
to do with unions, not structs.
Comment 1 Manuel López-Ibáñez 2012-10-04 08:07:35 UTC
Confirmed. early SRA creates a scalar replacement for the struct but not for the union:

;; Function yyparse (yyparse, funcdef_no=0, decl_uid=1710, cgraph_uid=0)
yyparse ()
{
  union YYSTYPE yylval;
  union YYSTYPE D.1720;

<bb 2>:
  [pr54804.c : 6:6] D.1720 = yylval;
  yylval ={v} {CLOBBER};
  [pr54804.c : 6:6] return D.1720;
}
;; Function yyparse_with_struct (yyparse_with_struct, funcdef_no=1, decl_uid=1716, cgraph_uid=1)

Created a replacement for xxlval offset: 0, size: 32: xxlval$ival

Symbols to be put in SSA form
{ xxlval$ival }
Incremental SSA update started at block: 0
Number of blocks in CFG: 3
Number of blocks to update: 2 ( 67%)


yyparse_with_struct ()
{
  int xxlval$ival;
  struct s xxlval;
  struct s D.1723;

<bb 2>:
  [pr54804.c : 15:6] [pr54804.c : 15] MEM[(struct s *)&D.1723] = xxlval$ival_3(D);
  [pr54804.c : 15:6] return D.1723;
}

Alternatively, one may see the problem as Wuninitialized not working with anything that leaves in memory (structs/union/arrays) unless SRA is applied. There are a few PRs about that.