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

            Bug ID: 101573
           Summary: uninitialized warning could not appear correclty in
                    -O0 optimisation level
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shen980630 at gmail dot com
  Target Milestone: ---

Hi there,

with the code example below, -O0 -Wunitialized could not display the
uninitialized warning correctly while -O1 -Wunitialized could.

==============
int main() {
    int a;
    for(; a < 5; ++a)
        ;
    return 0;
}
==============

> gcc-12 -O0 -Wuninitialized test.c

produces no warning

> gcc-12 -O1 -Wuninitialized test.c

produces:
test.c: In function 'main':
test.c:3:13: warning: 'a' is used uninitialized [-Wuninitialized]
    3 |     for(; a < 5; ++a)
      |

For comparison, clang with -O0 could correctly display this warning.
> clang -O0 -Wuninitialized test.c

produces:

test.c:3:11: warning: variable 'a' is uninitialized when used here
[-Wuninitialized]
    for(; a < 5; ++a)
          ^
test.c:2:10: note: initialize the variable 'a' to silence this warning
    int a;
         ^
          = 0
1 warning generated.

This behaviour happens in all the versions from gcc-7 to gcc-12 and the latest
version we checked is gcc (GCC) 12.0.0 20210520 (experimental), it also happens
with higher optimisation levels, -O2, -O3, -Os.

Thanks.

Reply via email to