-Wmaybe-uninitialized

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

-Wmaybe-uninitialized

Manfred
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);
}
Reply | Threaded
Open this post in threaded view
|

Re: -Wmaybe-uninitialized

Martin Sebor-2
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
Reply | Threaded
Open this post in threaded view
|

Re: -Wmaybe-uninitialized

Manuel López-Ibáñez
On 02/11/2018 20:14, Martin Sebor wrote:
> 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.

This is infamous https://gcc.gnu.org/PR18501

You can see everything that has been tried and discussed there.

Nobody has been able to fix it in the last 14 years.

The solution that Clang uses is to do a limited data flow analysis in the FE
and warn from the FE.

Any help welcome:
https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps

Cheers,

Manuel.