How to turn on (literally) ALL of GCC's warnings?
Solution 1
You can't.
The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the page you link to though, for instance some language-specific options are on the pages for C++ options or Obj-C options. To find them all you're better off looking at the Options Summary
Turning on everything would include -Wdouble-promotion
which is only relevant on CPUs with a 32-bit single-precision floating-point unit which implements float
in hardware, but emulates double
in software. Doing calculations as double
would use the software emulation and be slower. That's relevant for some embedded CPUs, but completely irrelevant for modern desktop CPUs with hardware support for 64-bit floating-point.
Another warning that's not usually useful is -Wtraditional
, which warns about perfectly well formed code that has a different meaning (or doesn't work) in traditional C, e.g. "string " "concatenation"
, or ISO C function definitions! Do you really care about compatibility with 30 year old compilers? Do you really want a warning for writing int inc(int i) { return i+1; }
?
I think -Weffc++
is too noisy to be useful, it's based on the outdated first edition of Effective C++ and warns about constructs which are perfectly valid C++ (and for which the guidelines changed in later editions of the book.) I don't want to be warned that I haven't initialized a std::string
member in my constructor; it has a default constructor that does exactly what I want, why should I write m_str()
to call it? The -Weffc++
warnings that would be helpful are too difficult for the compiler to detect accurately (giving false negatives), and the ones that aren't useful, such as initializing all members explicitly, just produce too much noise, giving false positives.
Luc Danton provided a great example of useless warnings from -Waggregate-return
that almost certainly never makes sense for C++ code.
i.e. you don't really want all warnings, you just think you do.
Go through the manual, read about them, decide which you might want to enable, try them. Reading your compiler's manual is a Good ThingTM anyway, taking a short cut and enabling warnings you don't understand is not a very good idea, especially if it's to avoid having to RTFM.
Edit: See also -Wall-all to enable all warnings which is closed as WONTFIX.
Edit 2: in response to DevSolar's complaint about makefiles needing to use different warnings depending on compiler version, if -Wall -Wextra
isn't suitable then it's not difficult to use compiler-specific and version-specific CFLAGS:
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))
Solution 2
I would agree with the previous answers that it is probably not beneficial to enable literally all warnings, however GCC does provide a reasonably convenient way to achieve this. The command
gcc -Q --help=warning
provides a list of all supported warning options with information on whether they are active. This can by the way be used to find out which options are (not) enabled by e.g. -Wall
and -Wextra
gcc -Wall -Wextra -Q --help=warning
To enable all the warnings you can use some regex to extract the command line parameters
gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n'
For my current GCC this gives:
-Wabi -Wabi-tag -Waddress -Waggregate-return -Waggressive-loop-optimizations -Waliasing -Walign-commons -Wampersand -Warray-bounds -Warray-temporaries -Wassign-intercept -Wattributes -Wbad-function-cast -Wbool-compare -Wbuiltin-macro-redefined -Wc++-compat -Wc++0x-compat -Wc++14-compat -Wc-binding-type -Wc90-c99-compat -Wc99-c11-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wcharacter-truncation -Wchkp -Wclobbered -Wcomment -Wcompare-reals -Wconditionally-supported -Wconversion -Wconversion-extra -Wconversion-null -Wcoverage-mismatch -Wcpp -Wctor-dtor-privacy -Wdate-time -Wdeclaration-after-statement -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated -Wdeprecated-declarations -Wdesignated-init -Wdisabled-optimization -Wdiscarded-array-qualifiers -Wdiscarded-qualifiers -Wdiv-by-zero -Wdouble-promotion -Weffc++ -Wempty-body -Wendif-labels -Wenum-compare -Wextra -Wfloat-equal -Wformat-contains-nul -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-signedness -Wformat-y2k -Wformat-zero-length -Wfree-nonheap-object -Wfunction-elimination -Wignored-qualifiers -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wimplicit-interface -Wimplicit-procedure -Wincompatible-pointer-types -Winherited-variadic-ctor -Winit-self -Winline -Wint-conversion -Wint-to-pointer-cast -Wintrinsic-shadow -Wintrinsics-std -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch -Wjump-misses-init -Wline-truncation -Wliteral-suffix -Wlogical-not-parentheses -Wlogical-op -Wlong-long -Wmain -Wmaybe-uninitialized -Wmemset-transposed-args -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers -Wmissing-include-dirs -Wmissing-parameter-type -Wmissing-prototypes -Wmultichar -Wnarrowing -Wnested-externs -Wnoexcept -Wnon-template-friend -Wnon-virtual-dtor -Wnonnull -Wodr -Wold-style-cast -Wold-style-declaration -Wold-style-definition -Wopenmp-simd -Woverflow -Woverlength-strings -Woverloaded-virtual -Woverride-init -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpedantic -Wpmf-conversions -Wpointer-arith -Wpointer-sign -Wpointer-to-int-cast -Wpragmas -Wproperty-assign-default -Wprotocol -Wreal-q-constant -Wrealloc-lhs -Wrealloc-lhs-all -Wredundant-decls -Wreorder -Wreturn-local-addr -Wreturn-type -Wselector -Wsequence-point -Wshadow -Wshadow-ivar -Wshift-count-negative -Wshift-count-overflow -Wsign-compare -Wsign-promo -Wsized-deallocation -Wsizeof-array-argument -Wsizeof-pointer-memaccess -Wstack-protector -Wstrict-null-sentinel -Wstrict-prototypes -Wstrict-selector-match -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types -Wsuggest-override -Wsurprising -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum -Wsync-nand -Wsynth -Wsystem-headers -Wtabs -Wtarget-lifetime -Wtraditional -Wtraditional-conversion -Wtrampolines -Wtrigraphs -Wtype-limits -Wundeclared-selector -Wundef -Wunderflow -Wuninitialized -Wunknown-pragmas -Wunsafe-loop-optimizations -Wunsuffixed-float-constants -Wunused -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-dummy-argument -Wunused-function -Wunused-label -Wunused-local-typedefs -Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable -Wuse-without-only -Wuseless-cast -Wvarargs -Wvariadic-macros -Wvector-operation-performance -Wvirtual-move-assign -Wvla -Wvolatile-register-var -Wwrite-strings -Wzero-as-null-pointer-constant -Wzerotrip -frequire-return-statement
This can now be used to call the GCC, i.e.
gcc $(gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n')
Note however that this results in warnings due to some warning options only being available for certain languages (e.g. C++
). These could be avoided by using some more regex to only include the options allowed for the current language or by adding an appropriate -Wno-whatever
at the end of the call.
Solution 3
It's simply impossible to program with all warnings enabled (unless you are going to ignore them, but then, why bother?). For example, let's assume you use following set of flags: -Wstrict-prototypes -Wtraditional
.
Even with two warnings enabled, the following program would complain.
/tmp $ cat main.c
int main(int argc, char **argv) {
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c: In function ‘main’:
main.c:1:5: warning: traditional C rejects ISO C style function definitions [-Wtraditional]
int main(int argc, char **argv) {
^
You may think "well, I'm going to use old style prototypes then". Nope, this won't work.
/tmp $ cat main.c
int main(argc, argv)
int argc;
char **argv;
{
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
int main(argc, argv)
^
And no, not specifying any prototype is also wrong, as the compiler will also complain.
/tmp $ cat main.c
int main() {
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
int main() {
^
If you define any functions inside your program, you cannot use all flags, because the compiler will complain about any imaginable function definition.
For C++, this is possible (the -Wtraditional
flag doesn't exist), and very simple programs can be compiled. To enable all warnings, use following list of warnings (probably some warnings are duplicated, because I didn't bother to filter warnings enabled by -Wall
).
-Wabi -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Weffc++ -Wstrict-null-sentinel -Wno-non-template-friend -Wold-style-cast -Woverloaded-virtual -Wno-pmf-conversions -Wsign-promo -Wextra -Wall -Waddress -Waggregate-return -Warray-bounds -Wno-attributes -Wno-builtin-macro-redefined -Wc++0x-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wclobbered -Wcomment -Wconversion -Wcoverage-mismatch -Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization -Wno-div-by-zero -Wempty-body -Wenum-compare -Wno-endif-labels -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wignored-qualifiers -Winit-self -Winline -Wno-int-to-pointer-cast -Wno-invalid-offsetof -Winvalid-pch -Wunsafe-loop-optimizations -Wlogical-op -Wlong-long -Wmain -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wno-mudflap -Wno-multichar -Wnonnull -Wno-overflow -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wsign-conversion -Wstack-protector -Wstrict-aliasing=1 -Wstrict-overflow=5 -Wswitch -Wswitch-default -Wswitch-enum -Wsync-nand -Wsystem-headers -Wtrigraphs -Wtype-limits -Wundef -Wuninitialized -Wunknown-pragmas -Wno-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvla -Wvolatile-register-var -Wwrite-strings
Solution 4
Someone has created a set of tools for determining the complete set of warnings for a given GCC or Clang version.
For GCC, copying from the full list of warnings provided by this tool for your compiler version appears to be the only way to ensure that all warnings are turned on, since (unlike Clang) GCC does not provide -Weverything
.
The tool appears to parse the actual c.opt
file in the GCC source code, so its results should be definitive.
The repository also contains text files with the warning lists generated for most GCC and Clang versions (currently Clang 3.2 through 3.7 and GCC 3.4 through 5.3).
https://github.com/barro/compiler-warnings
Solution 5
Gcc 4.3+ now has -Q --help=warnings, you can even specify --help=warnings,C to just print out the C related warnings.
I just wrote a m4 module to take advantage of this (also supports clang's -Weverything), see wget_manywarnings.m4
How to use it is pretty simple, basically the module turns every warn flag on. And you remove warnings as needed - some are really very verbose. Example: configure.ac
If you don't use autotools, you'll find the code to turn on all disabled warnings in the m4 module, which basically is the gcc call piped through awk:
flags="-Wall -Wextra -Wformat=2 "$(gcc -Wall -Wextra -Wformat=2 -Q --help=warning,C|awk '{ if (($2 == "[disabled]" || $2 == "") && $1!~/=/ && $1~/^-W/&& $1!="-Wall") print $1 }'
Related videos on Youtube
user541686
Updated on July 08, 2022Comments
-
user541686 almost 2 years
I would like to enable -- literally -- ALL of the warnings that GCC has. (You'd think it would be easy...)
You'd think
-Wall
might do the trick, but nope! Still need-Wextra
.You'd think
-Wextra
might do the trick, but nope! Not all of the warnings listed here (for example,-Wshadow
) are enabled by this. And I still have no idea if this list is comprehensive.
How do I tell GCC to enable (no if's, and's, or but's!) all the warnings it has?
-
Arafangion almost 12 yearsIncluding mutually incompatible warnings? Why?
-
user541686 almost 12 years@Arafangion: Like which ones? I haven't heard of those before.
-
Arafangion almost 12 yearsWell, for example, I usually tell GCC to be pedantic and confirm to C99, or C89 depending on the project. Obviously, using C99 constructs is going to cause more warnings in C89 (if it compiles at all). Are you suggesting that you want to enable all warnings, for all the C standards that GCC supports? And what about those other "warning" flags that actually disable warnings? Please clarify the question and state WHY you want them enabled, this is a pointless excercise otherwise.
-
user541686 almost 12 years@Arafangion: I don't understand what's "unclear" about the question -- yes, I'd like to turn on all warnings that are applicable to my code, no matter how pedantic. As for the reason, it's very simple: I have found some of the warnings that are not turned on by -Wall or -Wextra to be helpful, and so I want to try out the rest to see if I can improve my code. It's as simple as that.
-
Some programmer dude almost 12 yearsWhy not just add all the warnings that the document doesn't say is turned on be e.g.
-Wall
and-Wextra
? Personally I use-Wall -Wextra -Weffc++ -Wconversion -Wsign-conversion -Woverloaded-virtual
, it covers most cases. -
Some programmer dude almost 12 yearsBy the way, what is it you want the compiler to warn about? It's easier to answer that specific question. Also, for a complete list of warning options, there is only one definitive source: The actual source code for the compiler!
-
user541686 almost 12 years@JoachimPileborg: "What is it you want the compiler to warn about?" Potential errors and/or bad style? I mean, if I knew all the exact warnings then I would've just turned them on manually, no need to pose the question. If the answer is indeed "you have to look at the source code to find all of them" then please post it as the answer!
-
Alexandre Hamez almost 12 yearsclang 3.1 offers
-Weverything
. -
jogojapan almost 12 yearsAlmost a duplicate (and many useful answers): stackoverflow.com/questions/5088460/…
-
Arafangion almost 12 years@JoachimPileborg: Turning on -Weffc++ is probably not going to be helpful with a ANSI C codebase. C++ is a different language.
-
Some programmer dude almost 12 years@Arafangion Well, the question is tagged C++ so... :)
-
Arafangion almost 12 years@JoachimPileborg: Ah, it is now! Well, people should point out that g++ is to be used, too many people regard "GCC" as the C compiler, rather than "Gnu Compiler Collection".
-
Kyle Strand about 8 years@JoachimPileborg And now there is (finally) an automated way to extract the warnings from the source: github.com/barro/compiler-warnings
-
alfC over 4 yearsAfter
-Wall
and-Wextra
(and perhaps-Wpedantic
) is arguable what other warnings to activate. The best advise I can think of is to compile (and make sure the code works) with-O3
. That is, optimization exposes bugs. -
Akib Azmain over 3 yearsYou are going to be buried under warning if you somehow managed to enable all warnings
-
user541686 almost 12 years"And I still have no idea if this list is comprehensive" ... yes, I can certainly grep that page, but the question is, is it comprehensive?
-
paddy almost 12 yearsI don't know... You might have to pour through the GCC source code. Are you trying to make your life as a programmer exceedingly difficult, or is there a good reason why you want to see every conceivable warning? =)
-
user541686 almost 12 yearsI'd like to see GCC's diagnosis of my code -- I find it to be really helpful. But obviously, if I already knew about all the warnings and which ones are useful (and which ones aren't), then there would've been nothing to ask. There's no real way for me to tell unless I try them (and e.g. I found the shadowing one helpful, so it's not like they're useless just because they're turned off).
-
DevSolar almost 12 years"Go through the manual, read about them, decide which you might want to enable, try them." The problem here are the steps you left out: "Re-visit the manual for each and every compiler version and adapt your list of warnings because they are a-changing. Make your Makefiles check the exact compiler version and use a different list of warnings for each of them." We have maintainer-maintained levels of optimizations; why couldn't they be bothered with providing the same service for warnings?
-
Jonathan Wakely almost 12 years@DevSolar, new warnings are documented in the release notes for each version (e.g. 4.5, 4.6) so you only need to read them when upgrading your compiler to find out about new warnings, and surely you can be bothered to read the documentation for your compilers, not doing so is simply unprofessional and lazy.
-
Jonathan Wakely almost 12 yearsThere has been discussion of something like
-W1
,-W2
(e.g. see the long thread starting at gcc.gnu.org/ml/gcc/2012-04/msg00087.html) but they "couldn't be bothered" because noone has proposed a design or done the work yet, and there aren't infinite developer resources to work on every user's pet list of demands. It's open source, why don't you do it? Patches welcome. -
DevSolar almost 12 years@JonathanWakely: I have my projects, and GCC isn't among them. I point to a weakness in their product. Either they fix it or they accept the blame for not doing so, but it's not up to me to fix it for them, OSS be damned.
-
Jonathan Wakely almost 12 years@DevSolar, AFAIK no user ever requested
-Wn
, it was (semi-)proposed by GCC devs in discussion on the GCC list. How are the devs to know people want it if noone's ever requested it? Are we meant to be psychic? Scour every post on the internet to find what people consider to be weaknesses? If you want something, ask for it, don't bitch about it. -
Jonathan Wakely almost 12 yearsBesides which, the maintainers already do maintain levels of warnings, they decide what should be in
-Wall
and what should be in-Wextra
, all that's missing is-Wover9000
for "everything else". Most new warnings get included in-Wall
, or are more refined controls over existing warnings (e.g. instead of-Wunused
you can use-Wunused-but-set-variable -Wunused-but-set-parameter
to only get some of the-Wunused
warnings.) What exactly are you complaining that they don't bother to do? -
DevSolar almost 12 years@JonathanWakely: "If you want something, ask for it, don't bitch about it." -- I am not obliged to participate in the GCC project in order to critizise it, especially not if #31573 has been flagged as WONTFIX already. That puts this subject from the "ask about it" into the "bitch about it" ballpark.
-
Jonathan Wakely almost 12 years@DevSolar, my answer already explains why
-Wall-all
is not a good idea. As I said two comments up, what else is missing? Criticism is fine, but don't assume it's because noone could be bothered to do something, especially when you can't be bothered to open an enhancement request instead of complaining. -
DevSolar almost 12 years@JonathanWakely: Your answer explains why you and other GCC maintainers think that -Wall-all is not a good idea. The question, feature requests you yourself linked, and my comments explain why others do think it would be a good idea. My comment also explained why I don't bother to open an enhancement request that would be a duplicate to one already closed as WONTFIX. That's just as it is.
-
David Stone almost 10 years
-Weverything
is the better solution, I think, than the gcc strategy of not giving such an option. I use that flag with clang because my philosophy is that I want all warnings on by default (because someone thought it was helpful enough to add to the compiler), and if I don't like it, I turn off that warning specifically. The point is that you don't know about warnings that don't trigger, but you do know about warnings you don't want that do trigger, and they are easily turned off. -
user541686 about 9 yearsI never bothered to check this until now, but actually, it's not impossible... try
int main(int, char **); int main(argc, argv) int argc; char **argv; { (void)argc; (void)argv; return 0; }
-
Kyle Strand over 8 yearsIn defense of avoiding reading the manual: I am honestly not convinced that there aren't many, many more useful things to read, such as Effective C++ or several useful SO posts, and I have limited time and memory. At some point, if your tools require you to imbibe a fire-hydrant of information in order to use them properly, then they're no longer helping you.
-
Jonathan Wakely over 8 years@KyleStrand, sure, in which case just use
-Wall
or-Wall -Wextra
and rely on the maintainers to have enabled all the generally useful warnings with those options, which is the whole point of those options. If it's not included in one of those then you probably don't need it. If you think you're missing out on something useful then read the manual. And you don't need to read the entire manual to know about possibly interesting warnings anyway, only gcc.gnu.org/onlinedocs/gcc/Option-Summary.html -
Kyle Strand over 8 years@JonathanWakely Yeah, but they're paltry. The easiest way to see what warnings might be relevant to your code is to see which warnings are triggered by your code; at that point you can see a relevant, real-life example of the potentially-dangerous code before deciding whether or not to disable the warning. This can be easily done with Clang's
-Weverything
option, but is impossible to do with GCC. -
Jonathan Wakely over 8 yearsI linked to the Options Summary page in my answer, which groups all options into one page. GCC code review policies do not allow new options without documentation, so the docs should be comprehensive.
-
Kyle Strand about 8 yearsFYI, there's finally a way to figure out the complete list of warnings for a given version of GCC: github.com/barro/compiler-warnings
-
Valentin H over 7 yearsI'm afraid it is not practical. Gcc has shown me warnings from std lib.
-
Haatschii about 7 years@ValentinHeinitz As I said I don't think it is beneficial to enable literally all warnings, but this is was what OP asked for. However I think by explicitly removing some problematic warnings already mentioned in other answers (e.g. by adding respective -Wno-whatever at the end of the call), this can be of practical use.
-
Kyle Strand almost 6 years@ValentinHeinitz You can prevent gcc from issuing warnings for system/std/3rd-party headers by using
-isystem
instead of-I
for the relevant directories. -
TFuto over 5 years'Anyone who just turns on everything is probably either doing so because they're clueless because or a pointy-haired boss said "no warnings."' - You might be clueless what you DON'T know, and scanning through warnings (and selectively grep/grep -v) you can find a relevant warning that had never occurred to you, because you are not a GCC/Clang developer yourself... I found C++ code problems even in Stroustrup's own C++ files! E.g. do a -Weverything on his std_lib_facilities.h as the easiest demonstration.
-
TFuto over 5 yearsThis should be the accepted answer as this actually and directly answers the question.
-
Jetski S-type over 5 yearsThis is the answer. Use the "top level" list, and add all the arguments that are on the top level (not indented/nested). github.com/Barro/compiler-warnings/blob/master/gcc/…
-
Marc Glisse over 5 yearsEven with this trivial program I can still get "warning: stack usage is 16 bytes [-Wstack-usage=]" ;-)
-
Escape0707 over 4 years@JonathanWakely Actually, sir, I can't reproduce the ambiguity using example code from cppreference.com without
-pedantic
. I read it in this SO answer. And I'm just a C++ Beginner reading C++ Primer 5th ed. How could I even know I need to read though the whole document of warning options to experiment the ambiguity out? I think I should try outclang
for a period of time now. -
user997112 almost 3 yearsCan someone clarify: you can enable every GCC warnings but also add additional flags to disable individual flags/checks?
-
Haatschii almost 3 years@user997112 Yes, that is correct. Note that the order of the arguments is relevant. E.g.
gcc -Wabi -Wno-abi -Q --help=warning
will show-Wabi
as disabled, whilegcc -Wno-abi -Wabi -Q --help=warning
will show-Wabi
as enabled. -
Chris Mountford over 2 yearsOP stated somewhere that the purpose was to learn about style rules and to better inform the choice of what warnings to activate and what changes to make in the code.
-
alfC about 2 years
gcc 11
deemed that "-Wtraditional
is valid for C/ObjC but not for C++".