https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98900

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |24639
                 CC|                            |msebor at gcc dot gnu.org
          Component|c                           |middle-end
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
            Summary|Wrong static analyses       |misleading -Wuninitialized
                   |warning when optimisation   |using a variable outside
                   |is on                       |its lifetime with -O2
   Last reconfirmed|                            |2021-01-31
           Keywords|                            |diagnostic

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning could be clearer but at the point in the pipeline it's issued there
isn't enough information in the IL to differentiate the specific nature of the
problem (variable used after the end of its lifetime) from a plain
uninitialized read:

$ gcc -O1 -S -Wall -fdump-tree-uninit=/dev/stdout pr98850.c

;; Function main (main, funcdef_no=0, decl_uid=1943, cgraph_uid=1,
symbol_order=1) (executed once)

test1.c: In function ‘main’:
test1.c:11:7: warning: ‘x’ is used uninitialized [-Wuninitialized]
int main ()
{
  int x;

  <bb 2> [local count: 1073741824]:
  g = x_3(D);
  return 0;

}

But after changing the example to the one below doesn't make the warning more
informative even though the IL now has sufficient information (the CLOBBER) to
make it clear the variable isn't uninitialized at the time it's read but rather
its lifetime has ended.  So with that I agree that things can be improved.

$ cat pr98850.c && gcc -O1 -S -Wall -fdump-tree-uninit=/dev/stdout pr98850.c
int g = 10;

void f (int*);

int main()
{
    int* p = 0;
    {
        int x = 15;
        p = &x;
        f (p);
    }

    g = *p;
}

;; Function main (main, funcdef_no=0, decl_uid=1945, cgraph_uid=1,
symbol_order=1) (executed once)

pr98850.c: In function ‘main’:
pr98850.c:14:9: warning: ‘x’ is used uninitialized [-Wuninitialized]
   14 |     g = *p;
      |         ^~
pr98850.c:9:13: note: ‘x’ declared here
    9 |         int x = 15;
      |             ^
int main ()
{
  int x;
  int _1;

  <bb 2> [local count: 1073741824]:
  x = 15;
  f (&x);
  x ={v} {CLOBBER};
  _1 = x;
  g = _1;
  return 0;

}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639
[Bug 24639] [meta-bug] bug to track all Wuninitialized issues

Reply via email to