On Wed, 2023-08-30 at 14:25 +0200, Mathieu Malaterre via Gcc-help wrote: > Hi all, > > Consider the following silly c++ code (*), it compiles nicely using > g++ 13.2 no warnings using either: > > % g++ -c -O2 -Wall -Wextra alt.cxx > > or > > % g++ -c -O0 -Wall -Wextra alt.cxx > > Is it possible to trigger a warning for this code (maybe I missed some > gcc options) ? Analyzer does a better job for such a "complex" pattern: $ gcc -Wall -Wextra t.cc -fanalyzer -S t.cc: In function 'void k()': t.cc:8:21: warning: use of uninitialized value 'di' [CWE-457] [-Wanalyzer-use-of-uninitialized-value] 8 | long actual = cl(di); | ~~^~~~ 'void k()': events 1-3 | | 7 | d di; | | ^~ | | | | | (1) region created on stack here | | (2) capacity: 64 bytes | 8 | long actual = cl(di); | | ~~~~~~ | | | | | (3) use of uninitialized value 'di' here | Unfortunately analyzer is still C-only (it may work for simple C++ test cases but it's still not suitable for real C++ programs) as at now. > Thanks ! > > > (*) > % cat alt.cxx > #include <cstdlib> > struct d { > long cj[sizeof(long)]; > }; > long cl(d b) { return b.cj[0]; } > void k() { > d di; > long actual = cl(di); > if (actual == 0) > std::exit(42); > } > -- Xi Ruoyao <xry111@xxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University