Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide extra output from #pragma message

Current status

Bug filed in gcc bugtracker (includes simple testcase): https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66234


I'm currently porting some code to a new platform and toolchain, which includes an upgrade from gcc 4.7.2 to gcc 4.9.2 (or more specifically, from release 2012 to 2014 of the OSELAS toolchains - I've also reproduced the behavior on my host machine with gcc 4.6.4 (works) and gcc 4.8.3 (does not work)).

During the build of the application I use some #pragma message statements to output the builddate and hostname (note that BUILDTAG and BUILDHOST are also stored as constants to be used by the application afterwards):

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

The old toolchains (gcc 4.7.2 / 4.6.4) output the following, which is exactly what I want:

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

However, the new toolchains (gcc 4.9.2 / 4.8.3) gives me:

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

(Note that I've removed the file path/location from the output in both listings.)

Sure, the output that I want is there, but there's just a whole lot of extra stuff also. Is there anyway to hide the additional in definition of macro / in expansion of macro / etc. messages while keeping the desired output?

Or am I just doing this the wrong way, and is there a better method to print out the messages?

like image 432
sonicwave Avatar asked May 15 '15 08:05

sonicwave


2 Answers

Short answer

To hide the extra output, pass the -ftrack-macro-expansion=0 and -fno-diagnostics-show-caret options which were added in GCC version 4.8.

Long answer

This looks like a bug in GCC, which you may want to report. It looks the same or related to this bug.

I found was able to reproduce it with vanilla (built from source) GCC version 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)
                                 ^

Notice that it only occurs with the C++ pre-processor (i.e. g++ instead of gcc), and goes away if you pass -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)
                                 ^

In the change log for GCC 4.8 it shows that -ftrack-macro-expansion=2 is now passed by default which explains why this behavior wasn't present in version 4.7.2.

You can also pass -fno-diagnostics-show-caret (which was also added in GCC 4.8) if you want to have the same output as 4.7.2:

$ 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
like image 197
Tim Avatar answered Oct 25 '22 21:10

Tim


Using g++ version 8.3.1 .

Found that placing the message inside parenthesis reduces the number of output messages.

Example:

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

Output is:

<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)
                                   ^~~~

Almost the same code, but with the message in parenthesis:

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

Output becomes:

<filename>:17:55: note: #pragma message: BOOST_VERSION: 106600
 #pragma message ("BOOST_VERSION: " XSTR(BOOST_VERSION))
                                                       ^
like image 31
Nadav Aharoni Avatar answered Oct 25 '22 20:10

Nadav Aharoni