简体   繁体   中英

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

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?

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 eg language-independent options that are marked with the CL_COMMON flag. You do see options that apply to multiple languages, but not all, however (eg 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.)

Inspired by the answer from Vladislav Ivanishin , mainly the concept of "common" flags, I have done split of warning messages from the GCC I'm using (6.4.1):

  1. common - 58
  2. C++ - 141
  3. C - 133
  4. Objective C - 135
  5. Objective C++ - 141
  6. Fortran - 36
  7. Ada - 2
  8. Go - 2
  9. Java - 5

Overall there are 250 messages and all are covered by one or more from the above languages. However, I'm not sure whether this is a complete list of supported languages - I don't know how to check which languages are supported by an arbitrary version of GCC.

The -Waggregate-return from the question belongs to the "common" category which is why it is produced for the C++ language as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM