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.
c ++ g ++ clang ++
perror
source share