Bug 48801 - uninitialized warning for variables used only in loop body and initialized in the first iteration
Summary: uninitialized warning for variables used only in loop body and initialized in...
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.0
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2011-04-28 08:56 UTC by Ángel
Modified: 2013-11-20 01:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-04-28 10:16:52


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ángel 2011-04-28 08:56:13 UTC
Consider the code:

int loop_cond, some_value;

void foo(int arg);
void f() {
     int load, variable;
     
     load = 1;
     while (loop_cond) {
       if (load) {
          variable = some_value;
          load = 0;
       }

       foo(variable);
     }     
}


gcc -Wall -O1 initialise-warn.c
initialise-warn.c: In function `f':
initialise-warn.c:5: warning: 'variable' might be used uninitialized in this fun
ction

variable is only used inside the loop, and always set at the beginning of its first iteration.
The warning is gone if using a conditional instead of a loop, so it seems that it could easily detect that it is initialised in this case, too. Perhaps it was optimizing the constant with an if, but the load variable is local and can only be changed inside the function, so it has no extra restrictions.


The warning appears in gcc 4.5.2 and 4.6.0
The false positive was probably always misreported.
Comment 1 Richard Biener 2011-04-28 10:16:52 UTC
Confirmed.
Comment 2 Jeffrey A. Law 2013-11-20 01:10:11 UTC
gcc-4.8 and the trunk seem to handle this just fine.