【问题标题】:How to query GCC warnings for C++?如何查询 C++ 的 GCC 警告?
【发布时间】:2019-06-25 11:31:47
【问题描述】:

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

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

向调用添加警告标志会将它们包含在结果中:

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

但是,似乎调用是从 C 的角度完成的,我不知道如何从 C++ 的角度进行。如果调用包含纯 C++ 警告,例如:

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

并且仍然显示警告为禁用:

  -Wnon-virtual-dtor                    [disabled]

请注意,无论调用是使用g++ 还是gcc 完成,都会发生这种情况。

与纯 C 相同的 -Wbad-function-cast 以预期的方式运行:

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

[disabled][enabled] 之间没有额外的消息和报告的警告状态更改。同样,无论是使用g++ 还是gcc

我使用的是 GCC 版本 7.3.0。尽管该问题似乎适用于许多(如果不是全部)版本。 It can be observed through Compiler Explorer.

那么,有没有办法针对给定的语言进行此查询?

【问题讨论】:

    标签: gcc gcc-warning


    【解决方案1】:

    是的,你的观察是正确的。

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

    但请注意,这是可行的:

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

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

    // 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 编译器,因为

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

      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");
        }
    

    【讨论】:

    • 我提交了一个错误:gcc.gnu.org/bugzilla/show_bug.cgi?id=91011.
    • 顺便说一句,虚拟源文件技巧不适用于我的 7.3.0。从Compiler Explorer 来看,它不适用于任何版本,包括当前的顶级 9.1。它仅适用于中继版本。此外,需要现有文件使其解决方法有些不完善。
    • @AdamBadura “此外,需要现有文件使其解决方法有些不完善。” — 作为另一种解决方法,您可以调用特定语言前端 (cc1plus)直接地。我认为,gcc --print-file-name cc1plus 应该是获取可执行文件路径的可移植方式。 “我提交了一个错误:...” — 太好了!我认为,我们应该在那里继续讨论以将其保留在一个地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多