How can I turn on (literally) ALL of GCC's warnings?

Viewed 88728

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! You 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 don't have any 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?

8 Answers

I gathered information from the other postings and one-by-one tested the warnings in the test for a C++ library.

Using the list from Haatschii and his/her method of getting the full list for GCC 11:

gcc -Wall -Wextra -Wpedantic -Q --help=warning

Of all these warnings some do not apply to C++, so here it is a list of warnings and some minimal comments that work for the tests of my C++ project.

Take into account, that:

  1. Some of these warnings are by default already turned on without adding any option.
  2. I don't claim to know what some warnings actually mean.
  3. I don't recommend using or not using any particular warning.
  4. Some are commented, and that doesn't mean anything. Comment or uncomment them as you need. (I commented the ones that were not useful for my project.)
  5. Some will not work on GCC 10.
  6. The list is given as-is, it may contain errors or typos.

The list is basically in alphabetical order, with some minimal bunching to save vertical space. As a bonus, it is formatted for use in a CMake project.

Now the list:

target_compile_options(
    target
    PRIVATE
        $<CXX_COMPILER_ID:GNU>:
            -Werror
            -Wall
            -Wextra  # (activates -Wunknown-pragmas)
            -Wextra-semi
            -Wpedantic
            # -Wabi=13 -Wabi-tag (maybe important when linking with very old libraries)
            -Waddress -Waddress-of-packed-member
            # -Waggregate-return (disallow return classes or structs, seems a C-compatibility warning)
            -Waggressive-loop-optimizations
            -Walloc-zero  # -Walloc-size-larger-than=
            -Walloca  # -Walloca-larger-than=
            -Wanalyzer-double-fclose -Wanalyzer-double-free -Wanalyzer-exposure-through-output-file -Wanalyzer-file-leak -Wanalyzer-free-of-non-heap -Wanalyzer-malloc-leak -Wanalyzer-mismatching-deallocation -Wanalyzer-null-argument -Wanalyzer-null-dereference -Wanalyzer-possible-null-argument -Wanalyzer-possible-null-dereference -Wanalyzer-shift-count-negative -Wanalyzer-shift-count-overflow -Wanalyzer-stale-setjmp-buffer -Wanalyzer-tainted-array-index -Wanalyzer-too-complex -Wanalyzer-unsafe-call-within-signal-handler -Wanalyzer-use-after-free -Wanalyzer-use-of-pointer-in-stale-stack-frame -Wanalyzer-write-to-const -Wanalyzer-write-to-string-literal
            -Warith-conversion
            -Warray-bounds -Warray-parameter #  -Warray-bounds=<0,2> -Warray-parameter=<0,2>
            -Wattribute-warning -Wattributes
            -Wbool-compare -Wbool-operation
            -Wbuiltin-declaration-mismatch -Wbuiltin-macro-redefined
            -Wc++0x-compat -Wc++14-compat -Wc++17-compat -Wc++1z-compat -Wc++20-compat -Wc++2a-compat
            -Wcannot-profile
            -Wcast-align -Wcast-align=strict -Wcast-function-type -Wcast-qual
            -Wcatch-value  # -Wcatch-value=<0, 3>
            -Wchar-subscripts -Wclobbered -Wcomment  # (same as -Wcomments)
            -Wconditionally-supported
            -Wconversion -Wconversion-null
            -Wcoverage-mismatch -Wcpp
            # -Wctad-maybe-unsupported  (make ctad explicitly necessary)
            -Wctor-dtor-privacy
            -Wdangling-else -Wdate-time
            -Wdelete-incomplete -Wdelete-non-virtual-dtor
            -Wdeprecated -Wdeprecated-copy -Wdeprecated-copy-dtor -Wdeprecated-declarations -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion
            -Wdisabled-optimization -Wdiv-by-zero -Wdouble-promotion
            -Wduplicated-branches -Wduplicated-cond
            # -Weffc++ (doesn't allow some advanced techniques, such as CRTP)
            -Wempty-body -Wendif-labels
            -Wenum-compare -Wenum-conversion
            -Wexpansion-to-defined
            -Wexceptions
            # -Wextra (see above)
            -Wfloat-conversion # -Wfloat-equal (disallows float equality)
            -Wformat-truncation -Wformat=2 -Wformat-diag -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-signedness -Wformat-truncation -Wformat-y2k -Wformat-zero-length  # -Wformat-overflow=<0,2>
            -Wframe-address  # -Wframe-larger-than=<byte-size>
            -Wfree-nonheap-object -Whsa -Wif-not-aligned
            -Wignored-attributes -Wignored-qualifiers
            -Wimplicit-fallthrough#=3  # -Wimplicit-fallthrough=<0,5>
            -Winit-self
            # -Winline
            -Wint-in-bool-context -Wint-to-pointer-cast
            -Winvalid-imported-macros
            -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch
            -Wignored-qualifiers
            # -Wlarger-than=<byte-size>  # (disallow large objects types? in executable)
            -Wliteral-suffix
            -Wlogical-not-parentheses -Wlogical-op
            -Wlong-long -Wlto-type-mismatch -Wmain -Wmaybe-uninitialized
            -Wmemset-elt-size -Wmemset-transposed-args
            -Wmisleading-indentation
            -Wmismatched-dealloc -Wmismatched-new-delete -Wmismatched-tags
            -Wmissing-attributes -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wmissing-profile
            -Wmultichar
            # -Wmultiple-inheritance (disallows composition by inheritance)
            -Wmultistatement-macros
            # -Wnamespaces (disallows use of namespaces, seems a C-tool)
            -Wnarrowing
            -Wnoexcept -Wnoexcept-type
            -Wnormalized  #=nfc -Wnormalized=[none|id|nfc|nfkc]
            -Wnon-template-friend
            -Wnon-virtual-dtor
            -Wnonnull -Wnonnull-compare -Wnull-dereference
            -Wodr -Wold-style-cast -Wopenmp-simd -Woverflow
            -Woverlength-strings -Woverloaded-virtual
            -Wpacked -Wpacked-bitfield-compat -Wpacked-not-aligned
            # -Wpadded (disallows structs that need padding for alignment)
            -Wparentheses
            # -Wpedantic (see above)
            -Wpessimizing-move -Wplacement-new  #=1  -Wplacement-new=<0,2>
            -Wpmf-conversions
            -Wpointer-arith -Wpointer-compare
            -Wpragmas -Wprio-ctor-dtor -Wpsabi
            -Wrange-loop-construct -Wredundant-decls -Wredundant-move -Wredundant-tags -Wregister
            -Wreturn-local-addr -Wreturn-type
            -Wrestrict -Wreorder
            -Wscalar-storage-order -Wsequence-point
            -Wshadow -Wshadow-compatible-local -Wshadow-local -Wshadow=compatible-local -Wshadow=local
            -Wshift-count-negative -Wshift-count-overflow -Wshift-negative-value -Wshift-overflow  #=1 -Wshift-overflow=<0,2>
            -Wsign-compare -Wsign-conversion -Wsign-promo
            -Wsized-deallocation
            -Wsizeof-array-argument -Wsizeof-array-div -Wsizeof-pointer-div -Wsizeof-pointer-memaccess
            -Wstack-protector  # -Wstack-usage=<byte-size>
            -Wstrict-aliasing  #=3  -Wstrict-aliasing=<0,3>
            -Wstrict-null-sentinel  #=1  -Wstrict-overflow=<0,5>
            -Wstrict-overflow  #=1  -Wstrict-overflow=<0,5>
            -Wstring-compare
            -Wstringop-overflow  #=2  -Wstringop-overflow=<0,4>
            -Wstringop-overread -Wstringop-truncation
            -Wsubobject-linkage
            -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=malloc -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types
            -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum -Wswitch-outside-range -Wswitch-unreachable
            -Wsync-nand -Wsynth
            # -Wsystem-headers (expects system headers to be warning-compliant which they are not)
            -Wtautological-compare
            # -Wtemplates (disallows templates, C-tool)
            -Wtrampolines -Wtrigraphs -Wtsan
            -Wtype-limits -Wundef -Wuninitialized
            -Wno-unknown-pragmas  # (see above) -Wunknown-pragmas (other compilers need their own pragmas for their warnings)
            -Wunreachable-code -Wunsafe-loop-optimizations
            -Wunused -Wunused-parameter -Wunused-but-set-variable
            # -Wunused-const-variable  #=2
            -Wunused-function -Wunused-label -Wunused-local-typedefs -Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable
            -Wuseless-cast
            -Wvarargs -Wvariadic-macros -Wvector-operation-performance -Wvexing-parse -Wvirtual-inheritance -Wvirtual-move-assign
            -Wvla -Wvla-parameter
            -Wvolatile -Wvolatile-register-var
            -Wwrite-strings
            -Wzero-as-null-pointer-constant
        >
)
Related