I was exploring warnings offered by GCC using the gcc -Q --help=warning
syntax. (See 3.2 Options Controlling the Kind of Output for more details on that.)
What occurred to me is that many (109 out of 250 with GCC version 6.4.1) warnings are not classified as C++. By that I mean they will not show up when doing a restricted query gcc -Q --help=warning,c++
. (Out of curiosity, 81 warnings are classified as neither C++ nor C.)
Yet, at least some of those warnings do work in C++. As an example take -Waggregate-return
. (See it on Compiler Explorer.)
The -Waggregate-return
is disabled by default and I do know it is probably of little use anyway (see Confusion in regards to purpose/behavior of -Waggregate-return?). However, it is just an example, maybe there are some useful flags in those 109 of the same case.
So, why do some GCC warning flags not belong to the C++ language and yet work in C++? What is the rule here?
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
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.
GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.
If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors.
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 .
Documentation for compiler flags is available in the GCC manual. Those flags (which start with -Wl) are passed to the linker and are described in the documentation for ld . -D_GLIBCXX_ASSERTIONS enables additional C++ standard library hardening.
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.
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:
It's a bug in either documentation
--help={class|[^]qualifier}[,...]
Print (on the standard output) a description of the command-line options understood by the compiler that fit into all specified classes and qualifiers. These are the
supported classes:
...
language
Display the options supported for language, where language is the name of one of the languages supported in this version of GCC.
or implementation (I think, it's the latter.) So go ahead and file a bug if you like. Please be sure to post a link to the problem report here.
Specifically, you don't see any Common e.g. language-independent options that are marked with the CL_COMMON
flag. You do see options that apply to multiple languages, but not all, however (e.g. if they have both CL_C
and CL_CXX
flags; CL_COMMON
is a separate flag whose value is not composed of values of individual language flags).
The code responsible for that is around gcc/opts.c:1360
:
print_filtered_help (unsigned int include_flags,
unsigned int exclude_flags,
unsigned int any_flags,
unsigned int columns,
struct gcc_options *opts,
unsigned int lang_mask)
unsigned int lang_mask)
...
if (include_flags == 0
|| ((option->flags & include_flags) != include_flags))
{
if ((option->flags & any_flags) == 0)
continue;
}
(the caller passes 0 for any_flags
, so the inner check always succeeds; it's not the point here.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With