Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some GCC warning flags not belong to the C++ language and yet work in C++?

Tags:

c++

gcc

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?

like image 284
Adam Badura Avatar asked Jun 14 '19 08:06

Adam Badura


People also ask

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.

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors.

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 .

Where do I find compiler flags in GCC?

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.

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.

How do I remove a GCC warning when a flag is specified?

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:


1 Answers

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

like image 92
Vladislav Ivanishin Avatar answered Sep 30 '22 09:09

Vladislav Ivanishin