2

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!

Johnney
  • 127
  • 1
  • 9
  • 1
    I believe its `-Werror=...`, where `...` is the name of the flag that enables the warning, which should be included in the warning message. – HolyBlackCat Apr 28 '22 at 06:29
  • [this answer](https://stackoverflow.com/a/1047003/12257925) might help you – DiantArts Apr 28 '22 at 06:31
  • What compiler are you using? – Gabriel Staples Apr 28 '22 at 06:44
  • If this was the gcc or clang compiler, I believe it would tell you which flag was the flag in question, at the end of the warning, and in square brackets. Ex: `warning: blah blah [-Wblah-blah]`, so that you could know to disable `blah-blah` if you want. I don't see that here. I don't know what your compiler is. – Gabriel Staples Apr 28 '22 at 06:51
  • 1
    Do you really mean specifically for the pre-processor only (i.e. running `cpp` stand-alone rather than the compiler driver? That would be unusual. Your fragment/example is unhelpful, showing an example warning that does not appear to relate to the code. Provide a real warning and real and complete code that will generate it so we can be sure we know what you require. – Clifford Apr 28 '22 at 06:51
  • @user3386109, your (now-deleted) comment was really helpful. Thanks! I referenced you in my answer. – Gabriel Staples Apr 28 '22 at 07:05
  • I have not seen the comment... Could you please repost ? – Johnney Apr 28 '22 at 07:06
  • @Johnney, it was just that in the clang compiler, the warning of interest is `-Wmacro-redefined`. I've updated my answer with that. But, most importantly, you still haven't answered which compiler you're using, nor if you are running the `cpp` C preprocessor utility directly instead of a compiler. Clifford has an excellent question. – Gabriel Staples Apr 28 '22 at 07:08
  • you are right, I am running cpp preprocessor directly. So here should i try -Werror = -Wmacro-redefined ? – Johnney Apr 28 '22 at 07:10
  • What does `cpp --version` tell you? Now we need to know which brand of `cpp` you're running. Also, I've never run `cpp` in my life. Normally, if you want to stop the compilation stage early, I just call `gcc` with some flags to indicate that. Ex: `man gcc` tells me to use `gcc -c` to stop before linking, so you just get .o object files. Or, if you want to see all preprocessed intermediate .i files, I run `gcc -save-temps=obj`, [as I explain here](https://stackoverflow.com/a/56482777/4561887). – Gabriel Staples Apr 28 '22 at 07:13
  • I am using cpp from gcc-6 version as follows :- /home/toolroot/CCACHE/ccache_3_7_6_nv/install_dir/GCC/gcc-6.4.0-2/bin/cpp – Johnney Apr 28 '22 at 07:21

1 Answers1

2

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
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Okay...In my case what would be the -Werror=???> , I could not find exact string to placed , Inroder to just enable referined warnings as errors. – Johnney Apr 28 '22 at 06:42
  • 1
    @Johnney, I don't know what a "redefined" warning is. I don't recall that one. Can you copy and paste the *exact* warning or error you're seeing please, into your question? – Gabriel Staples Apr 28 '22 at 06:43
  • If we have same macro/variable defined with different values at two different files, If we try to preprocess those files , CPP complains with warnings variable has been redefined. I am just using sample CPP (from gcc compiler) command to preprocessing the files. – Johnney Apr 28 '22 at 06:51
  • @Johnney, thanks. What does "CPP" mean? To me, that means "C Plus Plus" (C++), but you have tagged this question C. Also, C++ never complains. It's a standard written in a huge huge huge document for sale. A compiler complains, however. So we need to know which compiler you have and have a minimal runnable example that we can test ourselves. – Gabriel Staples Apr 28 '22 at 06:54
  • 1
    Sorry. CPP i mean c pre processer – Johnney Apr 28 '22 at 07:01