As alk summed up, use these flags:
-pedantic -Wall -Wextra -Wconversion
First, I think you don't want to use the -ansi
flag, as suggested in Should I use "-ansi" or explicit "-std=..." as compiler flags?
Secondly, -Wextra
seems to be quite useful too, as suggested in -Wextra how useful is it really?
Thirdly, -Wconversion
seems also useful, as suggested in Can I make GCC warn on passing too-wide types to functions?
Fourthly, -pedantic
is also helpul,
as suggested in What is the purpose of using -pedantic in GCC/G++ compiler?.
Lastly, enabling -Wall
should be fine in this case, so I am pretty doubtful about what you said.
Example with gcc:
Georgioss-MacBook-Pro:~ gsamaras$ cat main.c
int main(void)
{
int x = 1;
int y = x+ ++x;
return 0;
}
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:4:16: warning: unsequenced modification and access to 'x' [-Wunsequenced]
int y = x+ ++x;
~ ^
main.c:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Georgioss-MacBook-Pro:~ gsamaras$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Example with g++, same version:
Georgioss-MacBook-Pro:~ gsamaras$ cp main.c main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:4:16: warning: unsequenced modification and access to 'x'
[-Wunsequenced]
int y = x+ ++x;
~ ^
main.cpp:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Relevant answer of mine, that Wall saves the day once more with a similar problem.
int y = x+ ++x;
->int y = 2*x + 1
– Halettagcc
, to learn what flags it supports, and which of those would be useful to you? – Watford-ansi
does not make your code more safe or more robust, it just makes it use an outdated version of the dialect. Use-std=<version>
for compliance with a standard of your choosing, for instance-std=c99
or-std=c++11
. – Krystlekrystyna-Wall
is more than enough, sometimes-Wextra
as well. Beyond that, manually picking specific context-dependent warnings can make sense. Consider-Werror
for dev build as well, if you want to enforce a no-warning policy. – Krystlekrystyna-pedantic -Wall -Wextra -Wconversion
helps you, but does not avoid any run-time error, which you probably would never be able to achieve using static code analysis. – Avig++
:-std=c++14 -O3 -ggdb3 -march=core2 -fno-common -pipe -Werror -Wall -Wextra -Wvla -Wreorder -Wnon-virtual-dtor -Wpacked -Wcast-align -Wenum-compare -Wpointer-arith -Wunused -Wuninitialized -Winit-self -Winvalid-pch -Woverlength-strings -Woverloaded-virtual -Woverflow -Wsequence-point -Wno-unknown-pragmas -Wtrigraphs -finput-charset=UTF-8 -Wpacked-bitfield-compat -Wlogical-op -Wsync-nand -Wstrict-null-sentinel -Wnoexcept
– Longoria