-Werror causes the compiler to stop at #warning. What can I do to prevent this?

First, I want him to focus on warnings. But I also want to print some informational messages (for example, "Come back and implement it!").

Unfortunately, my compiler does not support #info , #message , #pragma message() , etc.

I know -Wno-error=<something> , but my google-foo is weak and I cannot find <something> for #warning . I tried -Wno-error=warning and it just says no -Wwarning . Same thing with warn .

Any suggestions?

For what it's worth, I'm using the Tensilica xtensa compiler, xt-xcc, which seems to be a derivative of gnu or, at least, uses the gnu frontend. This is version 8.0.0.

+7
c gcc c-preprocessor embedded preprocessor-directive
source share
5 answers

I am not familiar with the Tensilica xtensa compiler ( xt-xcc ), but with standard gcc you can use

  #pragma GCC diagnostic warning "-Wcpp" 

make #warning a simple warning question (no error due to -Werror ). To make the effect temporary, you can use this #pragma and #warning between #pragma GCC diagnostic push and #pragma GCC diagnostic pop .

When I compile a file containing the following

  #pragma GCC diagnostic push #pragma GCC diagnostic warning "-Wcpp" #warning one #pragma GCC diagnostic pop #warning two 

with -Werror using gcc 4.6.1 ( gcc -c -Werror warning-test.c ), I get the following output:

  warning-test.c:3:2: warning: #warning one [-Wcpp] warning-test.c:6:2: error: #warning two [-Werror=cpp] cc1: all warnings being treated as errors 

When I delete the second #warning , compilation is not interrupted by an error.

You can also replace the -Werror compiler options with -Werror -Wno-error=cpp . I don't know what other effects include the cpp warning categories (and you may have a #warning in some other place that you want to catch as an error), so temporarily disable the error for a specific #warning and restore settings right away has become a more accurate method to meet your requirements.

Edit (2016): Using gcc versions 4.8.4 and 4.9.2 gives almost the same behavior (only the original line is printed additionally). But using gcc version 5.0.1 (the preliminary version included with Ubuntu 15.04) will give two warnings if the -Werror=cpp option is not enabled. Thus, it seems that -Werror with the newer gcc no longer implies -Werror=cpp , as before, and must be provided separately if necessary.

+3
source share

What happened with:

 #warning "Come back and implement this" #warning "Do not do that!" #warning "Must fix this before release" 

Typically, the compiler will include an argument in - or the material after - #warning in the error or warning message.

And, as a rule, if the compiler detects something that requires a warning, it reports this quite clearly.


Given the requirements, I think the only way to handle this is to protect the #warning directives ...

 #ifdef DO_WARNINGS #warning "Some warning" #endif /* DO_WARNINGS */ 

In most cases, you compile without -DDO_WARNINGS ; when you need to check the #warning warnings (with -Werror ), then you turn on -DDO_WARNINGS in the end, acknowledging that the compilation will fail. Remember that make -k will do as much as possible even if there are separate compilation errors.


Section 5.52.9 of GCC 4.4.1 Guidelines (in part):

5.52.9 Diagnostic Pragmas

GCC allows the user to selectively enable or disable certain types of diagnostics and change the type of diagnostics. For example, a project policy might require that all sources be compiled using "-Werror", but certain files may have exceptions that allow certain types of warnings to be used. Or, a project can selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

 #pragma GCC diagnostic kind option 

Changes the location of the diagnostic. Please note that not all diagnostic changes are subject to change; at the moment, only warnings (usually controlled by "-W ...) may not be all of them. Use the -fdiagnostics-show-option parameter to determine which diagnostics are controlled and which option controls them. kind is a" mistake to cure this Diagnostics as an error, "a warning for its treatment is a warning (even if" -Werror is valid ") or" ignored if the diagnosis is ignored. option is a double-quoted string that matches the command line option.

 #pragma GCC diagnostic warning "-Wformat" #pragma GCC diagnostic error "-Wformat" #pragma GCC diagnostic ignored "-Wformat" 

Please note that these pragmas override any command line options. Also, although it is syntactically valid to place these pragmas anywhere in your sources, the supported location for them is before any data or functions are defined. otherwise, it can lead to unpredictable results depending on how the optimizer manages your sources. If the same option is specified several times, the last one is the one that is valid. This pragma is not intended to be a general-purpose replacement for command line options, but to implement strict project policy management.

GCC also offers a simple mechanism for printing messages at compile time.

 #pragma message string 

Prints a string as a compilation message in compilation. A message is informational only and is neither a compilation warning nor an error.

 #pragma message "Compiling " __FILE__ "..." 
Line

can be bracketed and printed with location information.

I'm not sure if you like editing #warning lines in #pragma message lines. This will help you solve this problem - and it's worse than adding conditional compilation around #warning in that #pragma message can be supported by fewer compilers. It depends on your portability requirements.

+1
source share

Given that #warning is presumably handled by a preprocessor, you can run the preprocessor separately without -Werror, and then run the compiler with preprocessing locked on the processor output.

To do this, run the .c file through a preprocessor with all the normal parameters except -Werror, and generate the output as a .i file (.ii for C ++). The compiler recognizes these files as not subject to pre-processing, so you can compile them with -Werror and assuming that the preprocessor disables #warning and is not processed by the compiler itself, this may solve your problem.

I have not tested this; when I ran into the same problem, I just decided to live with it and not use -Werror. The solution turned out to be more complicated than the problem!

+1
source share

If your compiler supports it, you can try using the constructor attribute to determine the function that starts when the program starts (before main ), which displays a message to standard output:

 #define TOKENPASTE(x, y) TOKENPASTE2(x, y) #define TOKENPASTE2(x, y) x ## y #define WARNING(message) \ static void TOKENPASTE(_print_warning, __LINE__)() __attribute__((constructor)); \ static void TOKENPASTE(_print_warning, __LINE__)() \ { \ puts(message); \ } WARNING("fix this before ship") // prints out a message at runtime before main 

This will cause the message to be printed at runtime instead of compile time, which is almost as good, especially if you have no other options. The only limitation is that you must use this in a global scope outside the function definition.

0
source share

Unfortunately, there is no answer for my particular toolchain, or so engineers at Tensilica say so. They do not support #message or #pramga message () or know how to suppress #warning as an error in the presence of -Werror.

The GCC toolchain allows you to use -Wno-error = [code] to say: β€œthis warning is NOT an error,” but I have not found a list that matches #warning for any code that takes (or even a list of codes that it may be)

I can try to find the time to delve into the standard GCC command lines and the source code of the pre-processor to try to find a list of what might be equal to -Wno-error =, or is there -Werror = code that matches #warning. If I do, I will return and clarify the answer.

0
source share

All Articles