Search code examples
c++gccclangcompiler-warningssuppress-warnings

Automatically know if a GCC/Clang warning comes from Wall or Wextra?


I wonder if there's some clever, automatic way of knowing if a particular compiler warning (e.g. -Wunused-parameter) comes from the group -Wall, -Wextra, or another group, for both GCC and Clang.

Use case: we want to enable:

-Wall -Wextra -pedantic

However, some of the pedantic warnings are unapplicable to us and we want to disable them, example:

-Wall -Wextra -pedantic -Wno-c++20-designator

Now, we want to be 100% sure that we are not disabling anything from -Wall nor -Wextra. How do we ensure that -Wc++20-designator is not part of either? Of course one can go and check the documentation, but this is a tedious process when you have many such warnings or when you upgrade the compiler and get new warnings.

Our use case to ensure that all -Wall, -Wextra warnings will always be active, regardless of the disabled warnings from -pedantic.

Thanks!


Solution

  • There's no specific command to get a direct answer to the question "which warning group does a given warning come from?", but it's possible to infer this information automatically by querying the compiler which warnings are enabled and checking if the warning we are interested in is part of the list of enabled warnings.

    This is done as follows:

    • GCC: gcc -Q -Wall --help=warnings | grep enabled | grep unused-parameter (credits to @dratenik).
    • Clang: this functionality is not baked into the compiler, unlike GCC, so we need to use the diagtool tool: diagtool show-enabled -Wall foo.cpp | grep unused-parameter.

    Bonus: Unrelated to the original question but related to the original use case: to be sure that all of Wall and Wextra are enabled, regardless of which warnings are disabled, the solution (only works for Clang) is to put Wall Wextra at the very end:

    clang -Wno-unused-parameter -Wall -Wextra
    

    In this case, if we accidentally disable the unused-parameter warning, Wall will be applied after, re-enabling the warning.