Overkilling "cross variable initialization" in C ++?

I noticed that g++ complains too much about crossed initialization , and I wonder why these false positive errors cannot be removed simply by looking at the form of the SSA program at compile time.

Let me give you a very simple example:

 #include <cstdlib> int main () { goto end; int i = 0; // unused variable declaration end: return EXIT_SUCCESS; } 

When compiling with g++ -Wall -Wextra -o example1 example1.cc ( g++ 4.8.1), the compiler generates the following error message:

 example1.cc: In function 'int main()': example1.cc:10:2: error: jump to label 'end' [-fpermissive] end: ^ example1.cc:6:8: error: from here [-fpermissive] goto end; ^ example1.cc:8:7: error: crosses initialization of 'int i' int i = 0; ^ example1.cc:8:7: warning: unused variable 'i' [-Wunused-variable] 

Thus, this will throw an error when there is really no risk, because the variable is not used (the compiler obviously has both information and cannot combine it to infer that the error is false positive).

More strangely, I was hoping that LLVM would be more effective at analyzing the program. So, I tried clang++ (LLVM) on this simple example using clang++ -Wall -Wextra -o example1 example1.cc ( clang++ 3.4). And I got the same error message:

 example1.cc:8:7: warning: unused variable 'i' [-Wunused-variable] int i = 0; ^ example1.cc:6:3: error: goto into protected scope goto end; ^ example1.cc:8:7: note: jump bypasses variable initialization int i = 0; ^ 1 warning and 1 error generated. 

So, I am sure that I am missing something important, a problem that makes the detection of this false positive more difficult than me. But I do not know what it is. Or maybe the C ++ specification specifically says that it should be like that.

If anyone has an idea, feel free to share!

Change I also compiled the same code in C ( gcc or clang ), and everything went fine only if the warning about i is an unused variable. Thus, this reinforces the fact that it is more likely to be related to the C ++ specification, rather than the problem of detecting this problem at compile time.

+7
c ++ g ++ clang ++
source share
2 answers

There is nothing wrong with compilers. Your code is poorly formed according to the standard.

In your particular case, the requirement of the standard may not be necessary, and the transition may be allowed, and the compiler may create valid code. However, this only happens because the initialization of the variable int i has no side effects.

You can make your code valid by simply including the jump section in your area:

 #include <cstdlib> int main () { goto end; { int i = 0; // unused variable declaration } end: // cannot use i here, as it not defined. return EXIT_SUCCESS; } 
+10
source share

This is forbidden because you could potentially name destructors for objects that are not properly constructed. It is clear that int does not have a constructor or destructor, but makes it "fair" for all types of objects. And technically, something on the end: label could use i , and making the rule strict, it prevents the machine from checking every single code (which becomes a β€œstopping problem”).

+8
source share

All Articles