27

Compiler warnings generally provide useful hints for common programming errors. It is often recommended to turn compiler warnings into errors (e.g. here). In my experience, this is useful to prevent warnings from getting ignored, especially on larger code bases or in automated build systems. However, some warnings are useful as warnings and cannot be turned into errors, either because they can happen to be false positives or because the code base cannot be adapted at once but it takes some transition period.

As a workaround, I could compile the same code twice:

  1. One run with most or all warnings enabled (e.g. -Wextra), but without turning them into errors (i.e., without -Werror)
  2. A second run with a smaller set of warnings, but with pedantic settings (i.e., with the -Werror command line option)

This way the code project would benefit from more warnings than it could currently fix.

Is there a way to do this directly, i.e., with a single compilation step, not two?

I have read some mentions of the -Wno-error=[name] option in another question and it barely mentioned in the latest clang command line reference, but I lack details. In case those provide a solution for my question, can you please elaborate what it does and/or provide a link to where it is documented?

4
  • 1
    -Werror means turn all warnings into errors. Not to be confused with -pedantic-errors which means "give errors for standard C violations". Pedantic is a poorly chosen name, it's only pedantic by the standard set by some loony aimlessly hacking away in GNU C with wild & crazy non-standard extensions just for the heck of non-conformance and non-portability. Commented Apr 9 at 10:56
  • This question is similar to: G++ raise compile errors instead of warnings for narrowing conversions. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 9 at 13:52
  • 1
    @mediocrevegetable1: No, those questions are very different. The one you mention is about G++, mine is primarily about clang. Also, in my question I asked for documentation, which is not covered by the question you mentioned nor the answers to it. Also, I wanted to know more about the -Wno-error=[warning-name] which is not mentioned in the question you linked. Commented Apr 9 at 14:45
  • 2
    If anything, it would be better to mark the other question as a duplicate of this one. Except for the specific name of the warning it asks about, conversion, it is fully covered by this one. Commented Apr 10 at 20:11

5 Answers 5

34

From the documentation:

-Werror=foo to turn warning "foo" into error,
-Wno-error=bar to turn warning "bar" into warning even with -Werror.

Now you have to browse the warnings list to select the ones you want to treat specifically.

Sign up to request clarification or add additional context in comments.

Comments

18

Simply -Werror=[warning-name] works with both gcc and clang: https://godbolt.org/z/s84rT39cT

Note that it doesn't seem to allow multiple warnings, you may need to repeat -Werror= for every warning you want as error.

gcc documentation: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Werror_003d clang documentation: https://clang.llvm.org/docs/UsersManual.html#cmdoption-Werror

Comments

11

For Visual Studio you can use the warning number like: /we4326

https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170

For example, /we4326 causes warning number C4326 to be treated as an error by the compiler.

Comments

8

Yes, this is possible. With GCC for example you can use -Werror= to "Turn the specified warning into an error.".

To quote the entire relevant part of the documentation:

-Werror=

Turn 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.

Clang supports the same thing, but I do not know if MSVC does.

Comments

5

An alternative may be to use #pragma, which also has the benefit of targetting specific lines of code rather that potentially missing other errors. Viewing the documentation something like this should work:

int main() { #pragma GCC diagnostic push #pragma GCC diagnostic warning "-Woverflow" unsigned char x = 9999u; #pragma GCC diagnostic pop } 

See godbolt.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.