On 11/02/2018 09:23 AM, Manfred wrote:
The following was posted on comp.lanc.c++ by Ralf Goertz: https://groups.google.com/d/msg/comp.lang.c++/Tb5Ir3A71nw/szKUC5j8AQAJ #include <iostream> int main(int argc, char *argv[]) { int x; if(argc > 1) x = 1; std::cout << x << std::endl; } Despite the description of -Wmaybe-uninitialized, even with optimizing compilation, neither gcc 8.2.1 nor gcc 7.3.1 report the expected warning (with "c++ -Wall -O{1,3}"). Are we missing something? Thanks in advance. PS. BTW the trivial C translation behaves the same with "cc -Wall -O{1,3}": #include <stdio.h> int main(int argc, char *argv[]) { int x; if(argc > 1) x = 1; printf("%d\n", x); }
The code is optimized early on the assumption that x cannot be used uninitialized (the test is folded to true). The uninitialized warning runs much later and doesn't see the eliminated uninitialized path. It's tempting to say it's a bug that GCC folds the code without warning about the uninitialized path. The CPP pass obviously sees that the value is uninitialized in the else branch and makes a decision to optimize based on it, so it seems that it should be able to issue a warning for it. The question is how noisy implementing it there (as well) would be. Martin