【问题标题】:How to hide extra output from #pragma message如何隐藏#pragma 消息的额外输出
【发布时间】:2015-05-15 08:51:10
【问题描述】:

当前状态

在 gcc bugtracker 中提交的错误(包括简单的测试用例):https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66234


我目前正在将一些代码移植到新平台和工具链,其中包括从 gcc 4.7.2 升级到 gcc 4.9.2(或者更具体地说,从 OSELAS toolchains 的 2012 版升级到 2014 版 - 我已经还使用 gcc 4.6.4(有效)和 gcc 4.8.3(无效)重现了我的主机上的行为。

在构建应用程序的过程中,我使用了一些#pragma message 语句来输出构建日期和主机名(注意BUILDTAGBUILDHOST 也被存储为常量供应用程序使用):

#define BUILDTAG (__DATE__  " "  __TIME__)
#define BUILDHOST BUILT_ON

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#pragma message "Setting builddate to: " STR(BUILDTAG)
#pragma message "Building on: " STR(BUILDHOST)

旧工具链 (gcc 4.7.2 / 4.6.4) 输出以下内容,这正是我想要的:

note: #pragma message: Setting builddate to: ("May 15 2015" " " 10:35:12")
note: #pragma message: Building on: my-host

但是,新的工具链(gcc 4.9.2 / 4.8.3)给了我:

note: #pragma message: Setting builddate to: ("May 15 2015" " " "10:39:35")
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
                       ^
note: in expansion of macro 'STR'
#pragma message "Setting builddate to: " STR(BUILDTAG)
                                         ^
note: #pragma message: Building on: my-host
#pragma message "Building on: " STR(BUILDHOST)
                                             ^
note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
                       ^
note: in expansion of macro 'STR'
#pragma message "Building on: " STR(BUILDHOST)
                                ^

(请注意,我已从两个列表的输出中删除了文件路径/位置。)

当然,我想要的输出就在那里,但还有很多额外的东西。是否有隐藏额外的in definition of macro/in expansion of macro/等消息,同时保持所需的输出?

或者我只是做错了,有没有更好的方法来打印消息?

【问题讨论】:

    标签: gcc c-preprocessor pragma


    【解决方案1】:

    简答

    要隐藏额外的输出,请传递在 GCC 4.8 版中添加的 -ftrack-macro-expansion=0-fno-diagnostics-show-caret 选项。

    长答案

    这看起来像是 GCC 中的一个错误,您可能需要报告它。它看起来与this bug 相同或相关。

    我发现能够使用 vanilla(从源代码构建)GCC 版本 4.9.2 重现它:

    $ cat test.c 
    #define BUILDTAG (__DATE__  " "  __TIME__)
    #define BUILDHOST BUILT_ON
    
    #define STR_HELPER(x) #x
    #define STR(x) STR_HELPER(x)
    
    #pragma message "Setting builddate to: " STR(BUILDTAG)
    #pragma message "Building on: " STR(BUILDHOST)
    $ g++ -c test.c
    test.c:7:1: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:12")
     #pragma message "Setting builddate to: " STR(BUILDTAG)
     ^
    test.c:4:24: note: in definition of macro 'STR_HELPER'
     #define STR_HELPER(x) #x
                            ^
    test.c:7:42: note: in expansion of macro 'STR'
     #pragma message "Setting builddate to: " STR(BUILDTAG)
                                              ^
    test.c:8:46: note: #pragma message: Building on: BUILT_ON
     #pragma message "Building on: " STR(BUILDHOST)
                                                  ^
    test.c:4:24: note: in definition of macro 'STR_HELPER'
     #define STR_HELPER(x) #x
                            ^
    test.c:8:33: note: in expansion of macro 'STR'
     #pragma message "Building on: " STR(BUILDHOST)
                                     ^
    

    请注意,它只发生在 C++ 预处理器中(即g++ 而不是gcc),如果您通过-ftrack-macro-expansion=0,它就会消失:

    $ gcc -c test.c
    test.c:7:9: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:17")
     #pragma message "Setting builddate to: " STR(BUILDTAG)
             ^
    test.c:8:9: note: #pragma message: Building on: BUILT_ON
     #pragma message "Building on: " STR(BUILDHOST)
             ^
    $ g++ -c test.c -ftrack-macro-expansion=0
    test.c:7:42: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:35")
     #pragma message "Setting builddate to: " STR(BUILDTAG)
                                              ^
    test.c:8:33: note: #pragma message: Building on: BUILT_ON
     #pragma message "Building on: " STR(BUILDHOST)
                                     ^
    

    change log for GCC 4.8 中,它显示-ftrack-macro-expansion=2 现在默认传递,这解释了为什么4.7.2 版本中不存在此行为。

    如果您想获得与 4.7.2 相同的输出,也可以传递 -fno-diagnostics-show-caret(GCC 4.8 中也添加):

    $ g++ -c test.c -ftrack-macro-expansion=0 -fno-diagnostics-show-caret
    test.c:7:42: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:39:48")
    test.c:8:33: note: #pragma message: Building on: BUILT_ON
    

    【讨论】:

    • 是的,添加 -ftrack-macro-expansion=0 -fno-diagnostics-show-caret 工作正常,但我怀疑它可能会隐藏一些我在调试宏时确实想看到的消息(幸运的是,我不必经常这样做)。但是现在,解决方法有效,我在这里为 gcc 提交了一个错误:gcc.gnu.org/bugzilla/show_bug.cgi?id=66234
    • +1 谢谢! gcc 太冗长了。它可能对棘手的情况有所帮助,但对于有经验的程序员来说,这是一场噩梦,其中一条错误行就可以完成这项工作!
    【解决方案2】:

    使用 g++ 8.3.1 版。

    发现将消息放在括号内会减少输出消息的数量。

    例子:

    #define XSTR(x) STR(x)
    #define STR(x) #x
    #pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
    

    输出是:

    <filename>:17:53: note: #pragma message: BOOST_VERSION: 106600
     #pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
                                                         ^
    <filename>:16:17: note: in definition of macro ‘STR’
     #define STR(x) #x
                     ^
    <filename>:17:35: note: in expansion of macro ‘XSTR’
     #pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
                                       ^~~~
    

    几乎相同的代码,但括号中的消息:

    #define XSTR(x) STR(x)
    #define STR(x) #x
    #pragma message ("BOOST_VERSION: " XSTR(BOOST_VERSION))
    

    输出变为:

    <filename>:17:55: note: #pragma message: BOOST_VERSION: 106600
     #pragma message ("BOOST_VERSION: " XSTR(BOOST_VERSION))
                                                           ^
    

    【讨论】:

    • 而且似乎只需要 -fno-diagnostics-show-caret 之后清除其余的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2012-07-01
    相关资源
    最近更新 更多