Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make warn_unused_result applied to all function with GCC

Is it possible to tell GCC to use warn_unused_result flag for all function even not having the corresponding attribute? Because if I can forget to check for return value, I can also forget to add the GCC specific attribute.

I've seen it to be possible with some other compilers.

like image 290
PoltoS Avatar asked Nov 11 '12 18:11

PoltoS


People also ask

How do I enable all warnings in GCC?

For GCC, copying from the full list of warnings provided by this tool for your compiler version appears to be the only way to ensure that all warnings are turned on, since (unlike Clang) GCC does not provide -Weverything . The tool appears to parse the actual c.

Which GCC flag is used to enable all compiler warning?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

How does GCC treat warning errors?

You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning. Unfortunately, in this case there isn't any specific option that covers that warning.

What is pedantic GCC?

The -pedantic option tells GCC to issue warnings in such cases; -pedantic-errors says to make them errors instead. This does not mean that all non-ISO constructs get warnings or errors. See Options to Request or Suppress Warnings, for more detail on these and related command-line options.

How to suppress the-wunused warning in GCC?

These functions changed semantics in GCC 4.4. Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration). To suppress this warning use the unused attribute (see Variable Attributes ). This warning is also enabled by -Wunused together with -Wextra .

Why does GCC issue warnings when I use a common variable?

To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe. See Common Variable Attributes . Warn for cases where adding an attribute may be beneficial.

Do not warn when a function does not use its return value?

Do not warn if a caller of a function marked with attribute warn_unused_result (see Function Attributes) does not use its return value. The default is -Wunused-result . Warn whenever a local or static variable is unused aside from its declaration. This option implies -Wunused-const-variable=1 for C, but not for C++.

How do I eliminate the warning-wparentheses in GCC for IF statements?

When there is the potential for this confusion, GCC issues a warning when this flag is specified. To eliminate the warning, add explicit braces around the innermost if statement so there is no way the else can belong to the enclosing if. The resulting code looks like this: This warning is enabled by -Wparentheses .


1 Answers

No, you can only tell gcc to ignore all warn_unused_result flags with -Wno-unused-result but the respective -Wunused-result only sets the default (to warn only on flags). Compiling with -Wall -Wextra -pedantic should have triggered a warning if it can be activated but it doesn't, so it cannot.

Besides that, I wonder why you would want this, it is not that uncommon to ignore the result of functions, and all libraries are likely to produce tons of warnings.

like image 93
bitmask Avatar answered Sep 21 '22 02:09

bitmask