Johnney
Johnney

Reputation: 147

How do we disable specific warnings from C preprocessing

I have enabled -Werror for C pre processing command to treat warnings as errors. Basically i wanted to only treat redefined warnings as errors. And all other warnings should not be treated as errors.

  In file included from /home/test/version24/test.spec:35:0,
                 /test/release/base_version.spec:93:0: warning: "CD_MK_FILE" redefined
 #define CD_MK_FILE                4

In file included from /home/test/version23/mss_litter.spec:19:0,
                 from 
/test/release/base_version23.spec:0: note: this is the location of the previous definition
 #define CD_MK_FILE                3

Is there any flag in to treat only redefined warnings as errors. Thank you!

Upvotes: 3

Views: 1904

Answers (1)

Gabriel Staples
Gabriel Staples

Reputation: 52499

Answer to the specific question

How to only treat macro-redefined warnings as errors, and let all other warnings not be treated as errors:

  1. In the gcc compiler: you can't.
  2. In the LLVM clang compiler: use -Werror=macro-redefined.

Generic answer to just the title of the question: how do we enable or disable specific compiler warnings or errors?

For gcc/g++ and clang compilers, try:

# To disable -Werror just for warning -Wwhatever
-Wno-error=whatever

# To **enable** errors just for this one -Wwhatever warning
-Werror=whatever

For the clang compiler, you'd use -Wno-error=macro-redefined, to disable that error for the -Wmacro-redefined warning only, and you'd use -Werror=macro-redefined to enable an error only for that warning.

See here: https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefined. Thanks for the comment under the question, @user3386109!

See the list of all possible warnings and errors here:

  1. For the gcc compiler: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  2. For the clang compiler (which is gcc-compatible, by design): https://clang.llvm.org/docs/DiagnosticsReference.html

For example, from the gcc link above:

-Werror=

Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.

Other References:

  1. The comment by @HolyBlackCat
  2. [my Q&A] How can I disable a C/C++ -Werror build error in Bazel? (AKA: how to turn OFF specific warnings already turned on by -Wall -Werror)

Related:

  1. In case you ever need to control just a few lines of code, this is incredibly useful too: How to disable GCC warnings for a few lines of code

Upvotes: 4

Related Questions