如何隐藏#pragma消息的额外输出

时间:2015-05-15 08:51:10

标签: gcc c-preprocessor pragma

当前状态

在gcc bugtracker中提交的bug(包括简单的测试用例):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语句来输出builddate和hostname(注意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 /等消息,同时保持所需的输出?

或者我只是以错误的方式做这件事,是否有更好的方法来打印消息?

2 个答案:

答案 0 :(得分:4)

简短回答

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

长答案

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

我发现能够用香草(从源代码构建)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

答案 1 :(得分:1)

使用 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))
                                                       ^