The general recommendation is to try to compile with as many warnings checks enabled, and explicitly disable some warnings where needed.
Enabling all warnings for GCC is a bit more complicated, so the minimum
recommended would be to pass the build with clang -Wall -Weverything
.
From the GCC 10 manual:
This option turns on the following warning flags:
-Waddress
-Warray-bounds=1
(only with ‘-O2’)-Wbool-compare
-Wbool-operation
-Wc++11-compat
-Wc++14-compat
-Wcatch-value
(C++ and Objective-C++ only)-Wchar-subscripts
-Wcomment
-Wduplicate-decl-specifier
(C and Objective-C only)-Wenum-compare
(in C/ObjC; this is on by default in C++)-Wenum-conversion
in C/ObjC;-Wformat
-Wformat-overflow
-Wformat-truncation
-Wint-in-bool-context
-Wimplicit
(C and Objective-C only)-Wimplicit-int
(C and Objective-C only)-Wimplicit-function-declaration
(C and Objective-C only)-Winit-self
(only for C++)-Wlogical-not-parentheses
-Wmain
(only for C/ObjC and unless ‘-ffreestanding’)-Wmaybe-uninitialized
-Wmemset-elt-size
-Wmemset-transposed-args
-Wmisleading-indentation
(only for C/C++)-Wmissing-attributes
-Wmissing-braces
(only for C/ObjC)-Wmultistatement-macros
-Wnarrowing
(only for C++)-Wnonnull
-Wnonnull-compare
-Wopenmp-simd
-Wparentheses
-Wpessimizing-move
(only for C++)-Wpointer-sign
-Wreorder
-Wrestrict
-Wreturn-type
-Wsequence-point
-Wsign-compare
(only in C++)-Wsizeof-pointer-div
-Wsizeof-pointer-memaccess
-Wstrict-aliasing
-Wstrict-overflow=1
-Wswitch
-Wtautological-compare
-Wtrigraphs
-Wuninitialized
-Wunknown-pragmas
-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wvolatile-register-var
-Wzero-length-bounds
This enables some extra warning flags that are not enabled by ‘-Wall’.
-Wclobbered
-Wcast-function-type
-Wdeprecated-copy
(C++ only)-Wempty-body
-Wignored-qualifiers
-Wimplicit-fallthrough=3
((attribute ((fallthrough));))-Wmissing-field-initializers
-Wmissing-parameter-type
(C only)-Wold-style-declaration
(C only)-Woverride-init
-Wsign-compare
(C only)-Wstring-compare
-Wredundant-move
(only for C++)-Wtype-limits
-Wuninitialized
-Wshift-negative-value
(in C++03 and in C99 and newer)-Wunused-parameter
(only with ‘-Wunused’ or ‘-Wall’)-Wunused-but-set-parameter
(only with ‘-Wunused’ or ‘-Wall’)
-Waggregate-return
-Warith-conversion
<-- since 10.x-Wbad-function-cast
(C and Objective-C only)-Wc++-compat
(C and Objective-C only)-Wcast-align
-Wcast-qual
-Wconversion
-Wdouble-promotion
(C, C++, Objective-C and Objective-C++ only)-Wduplicate-decl-specifier
(C and Objective-C only)-Wduplicated-branches
-Wduplicated-cond
-Wfloat-conversion
-Wfloat-equal
-Wformat-nonliteral
-Wformat-overflow=2
-Wformat-security
-Wformat-signedness
-Wformat-truncation=2
-Wformat-y2k
-Wformat=2
-Wlogical-op
-Wmissing-declarations
-Wmissing-include-dirs
-Wmissing-prototypes
(C and Objective-C only)-Wnested-externs
(C and Objective-C only)-Wnull-dereference
-Wold-style-definition
-Wpacked
-Wpadded
-Wpointer-arith
-Wredundant-decls
-Wshadow
-Wshift-overflow=2
-Wsign-conversion
-Wstrict-prototypes
(C and Objective-C only)-Wswitch-default
-Wswitch-enum
-Wundef
-Wuninitialized
-Wvla
And for C++
-Wabi-tag
(C++ and Objective-C++ only) <- too noisy-Wcomma-subscript
(C++ and Objective-C++ only) <-- since 10.x-Wctor-dtor-privacy
(C++ and Objective-C++ only)-Wextra-semi
(C++, Objective-C++ only)-Wmismatched-tags
(C++ and Objective-C++ only) <-- since 10.x, fails on 10.1-Wnoexcept
(C++ and Objective-C++ only)-Wnon-virtual-dtor
(C++ and Objective-C++ only)-Wold-style-cast
(C++ and Objective-C++ only)-Woverloaded-virtual
(C++ and Objective-C++ only)-Wplacement-new=2
(C++, Objective-C++ only)-Wredundant-tags
(C++ and Objective-C++ only) <-- since 10.x-Wregister
(C++ and Objective-C++ only)-Wsign-promo
(C++ and Objective-C++ only)-Wstrict-null-sentinel
(C++ and Objective-C++ only)-Wsuggest-final-methods
-Wsuggest-final-types
-Wsuggest-override
-Wuseless-cast
(C++ and Objective-C++ only)-Wvolatile
(C++ and Objective-C++ only) <-- since 10.x-Wzero-as-null-pointer-constant
(C++ and Objective-C++ only)
Do not enable it, since it'll complain about #include_next
.
On RISC-V 10.1 it fails with note: replace the class-key with 'struct' in system headers.
Silencing GCC warnings is strightforward:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic pop
There are a few GCC specific warnings, that are not accepted by clang, for example:
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wuseless-cast"
#pragma GCC diagnostic ignored "-Wsuggest-final-types"
#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#if __GNUC__ >= 10
#pragma GCC diagnostic ignored "-Wredundant-tags"
#endif
#endif
For header files included in both C and C++ sources, it might be
necessary to disable the -Wold-style-cast
warning:
#pragma GCC diagnostic push
#if defined(__cplusplus)
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#pragma GCC diagnostic pop
Other warnings:
#pragma GCC diagnostic ignored "-Wpedantic"
For clang things are easier, to enable all possible checks use:
-Wall
-Weverything
(includes -pedantic)-Werror
There are many common warnings with GCC, and silencing them with #pragma GCC
is ok, but there are several clang specific warnings, that need conditional
compiling like:
#pragma GCC diagnostic push
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif
#pragma GCC diagnostic pop
Other warnings:
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wunused-macros"
#pragma clang diagnostic ignored "-Wempty-translation-unit"
#pragma clang diagnostic ignored "-Wdeprecated-volatile"