如何查询C ++的GCC警告? [英] How to query GCC warnings for C++?

查看:154
本文介绍了如何查询C ++的GCC警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC允许使用以下语法查询特定于C ++语言的可用警告标志:

GCC allows querying available warning flags specific for C++ language with the syntax:

g++ -Q --help=warning,c++

将警告标志添加到呼叫中会将其包括在结果中:

Adding warning flags to the call includes them in the result:

g++ -Wall -Q --help=warning,c++

但是,似乎是从C的角度完成调用的,而从C ++的角度来看我不知道该怎么做.如果调用包含仅C ++的警告,例如:

However, it seems the call is done from the C point of view and I don't know how to do it from the C++ point of view. If the call includes C++-only warning, like:

g++ -Wnon-virtual-dtor -Q --help=warning,c++

输出包含一条消息:

cc1: warning: command line option ‘-Wnon-virtual-dtor’ is valid for C++/ObjC++ but not for C

并仍将警告显示为已禁用:

and still shows the warning as disabled:

  -Wnon-virtual-dtor                    [disabled]

请注意,无论使用g++还是gcc进行呼叫,都会发生这种情况.

Note, that this happens regardless of whether the call is done using g++ or gcc.

仅C语言-Wbad-function-cast的行为与预期相同:

The same with C-only -Wbad-function-cast behaves in an expected way:

gcc -Wbad-function-cast -Q --help=warning,c

没有额外的消息,并且报告的警告状态在[disabled][enabled]之间发生了变化.同样,无论使用的是g++还是gcc.

There is no extra message and reported warning status changes between [disabled] and [enabled]. Again, regardless of whether g++ or gcc is used.

我正在使用GCC版本7.3.0.尽管该问题似乎适用于许多(如果不是全部)版本. 可以通过编译器资源管理器进行观察.

I'm using GCC version 7.3.0. Although the issue seems to apply to many if not all versions. It can be observed through Compiler Explorer.

那么,有没有一种方法可以针对给定的语言进行此查询?

So, is there a way to do this query with respect to given language?

推荐答案

是的,您的观察是正确的.

Yes, your observations are correct.

这可能不是预期的行为,如果您关心此功能,则建议向上游报告.

Probably this is not the intended behavior, and if you care about this feature, then I suggest reporting it upstream.

请注意,这可行,但是:

Note that this works, however:

touch 1.cc
g++ -Wnon-virtual-dtor -Q --help=warning,c++ 1.cc

即如果存在带有适当扩展名的输入文件,则会调用正确的编译器适当的可执行文件:cc1plus,而不是cc1.如果不存在输入文件,则后者是默认设置.我做了一些快速调试,这是怎么发生的:

I.e. if there's an input file with a proper extension, then the correct compiler proper executable is invoked: cc1plus, not cc1. The latter is the default if no input files are present. I did some quick debugging, and here's how that happens:

// gcc.c:
driver::do_spec_on_infiles () const
{
  ...
  for (i = 0; (int) i < n_infiles; i++)
    {
      ...
      /* Figure out which compiler from the file's suffix.  */

      input_file_compiler
        = lookup_compiler (infiles[i].name, input_filename_length,
                           infiles[i].language);

      if (input_file_compiler)
        {
          ...
              value = do_spec (input_file_compiler->spec);

并且input_file_compiler那时是C编译器,因为

And input_file_compiler at that point is the C compiler, because

p n_infiles
$9 = 1
(gdb) p infiles[0]
$10 = {name = 0x4cbfb0 "help-dummy", language = 0x4cbfae "c", incompiler = 0x58a920, compiled = false, preprocessed = false}

以下是创建伪文件的方式(同一文件中的功能process_command):

Here's how the dummy file got created (function process_command in the same file):

  if (n_infiles == 0
      && (print_subprocess_help || print_help_list || print_version))
    {
      /* Create a dummy input file, so that we can pass
         the help option on to the various sub-processes.  */
      add_infile ("help-dummy", "c");
    }

这篇关于如何查询C ++的GCC警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆