How to hide extra output from #pragma message - gcc

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?

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

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

Related

gcc define function-like macro using -D argument for printf

This is quite similar to GCC define function-like macros using -D argument but I couldn't find a relation to my use case.
Consider the following code, main.c:
#include <stdio.h>
const char greeting[] = "hello world";
//#define printf(fmt, ...) (0)
int main() {
printf("%s!\n", greeting);
return 0;
}
If I compile and run this, it works as expected:
$ gcc -Wall -g main.c -o main.exe
$ ./main.exe
hello world!
$
Ok, now I want to How to disable printf function? so I uncomment the #define in the code; I get some warnings, but things again work as as expected (as there is no printout):
$ gcc -Wall -g main.c -o main.exe
main.c: In function 'main':
main.c:5:26: warning: statement with no effect [-Wunused-value]
5 | #define printf(fmt, ...) (0)
| ^
main.c:8:5: note: in expansion of macro 'printf'
8 | printf("%s!\n", greeting);
| ^~~~~~
$ ./main.exe
$
Now, go back to the example as originally posted - that is, comment the #define like - and let's try to set that define via the command-line -D argument:
$ gcc -Wall -D'printf(fmt, ...)=(0)' -g main.c -o main.exe
<command-line>: error: expected identifier or '(' before numeric constant
main.c: In function 'main':
<command-line>: warning: statement with no effect [-Wunused-value]
main.c:8:5: note: in expansion of macro 'printf'
8 | printf("%s!\n", greeting);
| ^~~~~~
Well, the command line argument -D'printf(fmt, ...)=(0)' causes the compilation to fail.
Is there any way I can format this macro somehow, so I can set it via the gcc command line using the -D argument? (Bonus: can it be formulated somehow, so it does not raise warnings like "statement with no effect")
EDIT: contents of C:/msys64/mingw64/include/stdio.h lines 366 to 372:
366 __mingw_ovr
367 __attribute__((__format__ (gnu_printf, 1, 2))) __MINGW_ATTRIB_NONNULL(1)
368 int printf (const char *__format, ...)
369 {
370 int __retval;
371 __builtin_va_list __local_argv; __builtin_va_start( __local_argv, __format );
372 __retval = __mingw_vfprintf( stdout, __format, __local_argv );
373 __builtin_va_end( __local_argv );
374 return __retval;
375 }
When you use:
#define printf(fmt, ...) (0)
The preprocessor turns the code into this:
int main() {
(0);
return 0;
}
That free standing (0) is not allowed. However if you define it this way:
#define printf(fmt, ...)
You can also use the command line definition:
"-Dprintf(fmt, ...)="
Everything works.

Have GAS generate instruction from inline assembly?

I'm trying to assemble a file that uses ARM's CRC instruction. The assembler is producing an error Error: selected processor does not support 'crc32b w1,w0,w0'.
There are runtime checks in place, so we are safe with the instruction. The technique works fine on i686 and x86_64. For example, I can assemble a file that uses Intel CRC intrinsics or SHA Intrinsics without -mcrc or -msha (and on a machine without the features).
Here is the test case:
$ cat test.cxx
#include <arm_neon.h>
#define GCC_INLINE_ATTRIB __attribute__((__gnu_inline__, __always_inline__, __artificial__))
#if defined(__GNUC__) && !defined(__ARM_FEATURE_CRC32)
__inline unsigned int GCC_INLINE_ATTRIB
CRC32B(unsigned int crc, unsigned char v)
{
unsigned int r;
asm ("crc32b %w2, %w1, %w0" : "=r"(r) : "r"(crc), "r"((unsigned int)v));
return r;
}
#else
// Use the intrinsic
# define CRC32B(a,b) __crc32b(a,b)
#endif
int main(int argc, char* argv[])
{
return CRC32B(argc, argc);
}
And here is the result:
$ g++ test.cxx -c
/tmp/ccqHBPUf.s: Assembler messages:
/tmp/ccqHBPUf.s:23: Error: selected processor does not support `crc32b w1,w0,w0'
Placing the ASM code in a source file and compiling with different options is not feasible because CRC32B will be used in C++ header files, too.
How do I get GAS to assemble the instruction?
GCC's configuration and options are the reason we are trying to do things this way. User's don't read manuals, so they won't add -march=armv8-a+crc+crypto -mtune=cortex-a53 to CFLAGS and CXXFLAGS.
In addition, distros compile to a "least capable" machine, so we want the hardware acceleration routines available. When the library is provided by a distro like Linaro, both code paths (software CRC and hardware accelerated CRC) will be available.
The machine is a LeMaker HiKey, which is ARMv8/Aarch64. It has an A53 processor with CRC and Crypto (CRC and Crypto is optional under the architecture):
$ cat /proc/cpuinfo
Processor : AArch64 Processor rev 3 (aarch64)
processor : 0
...
processor : 7
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: AArch64
GCC lacks most of the usual defines one expects to be present by default:
$ g++ -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
#define __aarch64__ 1
#define __AARCH64_CMODEL_SMALL__ 1
#define __AARCH64EL__ 1
Using GCC's -march=native does not work on ARM:
$ g++ -march=native -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
cc1: error: unknown value ‘native’ for -march
And Clang:
$ clang++ -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
#define __AARCH64EL__ 1
#define __ARM_64BIT_STATE 1
#define __ARM_ACLE 200
#define __ARM_ALIGN_MAX_STACK_PWR 4
#define __ARM_ARCH 8
#define __ARM_ARCH_ISA_A64 1
#define __ARM_ARCH_PROFILE 'A'
#define __ARM_FEATURE_CLZ 1
#define __ARM_FEATURE_DIV 1
#define __ARM_FEATURE_FMA 1
#define __ARM_FEATURE_UNALIGNED 1
#define __ARM_FP 0xe
#define __ARM_FP16_FORMAT_IEEE 1
#define __ARM_FP_FENV_ROUNDING 1
#define __ARM_NEON 1
#define __ARM_NEON_FP 0xe
#define __ARM_PCS_AAPCS64 1
#define __ARM_SIZEOF_MINIMAL_ENUM 4
#define __ARM_SIZEOF_WCHAR_T 4
#define __aarch64__ 1
GCC version:
$ gcc -v
...
gcc version 4.9.2 (Debian/Linaro 4.9.2-10)
GAS version:
$ as -v
GNU assembler version 2.24 (aarch64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
This answer came from Jiong Wang on the Binutils mailing list. It bypasses GAS's architectural requirements and plays well with GCC:
__inline unsigned int GCC_INLINE_ATTRIB
CRC32W(unsigned int crc, unsigned int val)
{
#if 1
volatile unsigned int res;
asm ("\n"
"\t" ".set reg_x0, 0\n"
"\t" ".set reg_x1, 1\n"
"\t" ".set reg_x2, 2\n"
"\t" ".set reg_x3, 3\n"
"\t" ".set reg_x4, 4\n"
"\t" ".set reg_x5, 5\n"
"\t" ".set reg_x6, 6\n"
"\t" ".set reg_x7, 7\n"
"\t" "#crc32w %w0, %w1, %w2\n"
"\t" ".inst 0x1ac04800 | (reg_%2 << 16) | (reg_%1 << 5) | (reg_%0)\n"
: "=r"(res) : "r"(crc), "r"(val)
);
return res;
#else
volatile unsigned int res;
asm (".cpu generic+fp+simd+crc+crypto \n"
"crc32w %w0, %w1, %w2 \n"
: "=r"(res) : "r"(crc), "r"(val));
return res;
#endif
}
The second one commented out by the preprocessor block was suggested by Nick Clifton on the Binutils mailing list. The idea is GCC generates code using the ISA based on -march=XXX, so it does not matter if we increase capabilities to get past the assembler. We decided to go with Wang's answer because we did not want potential side effects from modifying the .cpu.
And the verification with GCC 4.8 and Binutils 2.24:
$ g++ -O1 test.cxx -c
$ objdump --disassemble test.o
test.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <main>:
0: 12001c01 and w1, w0, #0xff
4: 1ac14800 crc32w w0, w0, w1
8: d65f03c0 ret

for XCode, default C++ language dialect?

In Xcode6, what's the "compiler default" for C++ language dialect.
I am using a C++ new feature std:max(a,b,c)
if I use "Compiler Default", it failed to compile.
When I changed to "C++11 or GNUC++11", it compiles fine.
I am wondering if compiler default is C++98?
I ran below code to get - GNU C++ 98.
#include <iostream>
int main()
{
//gnu mode
#ifndef __STRICT_ANSI__
std::cout << "GNU - ";
#endif
// C++ iso standard
#if __cplusplus == 199711L
std::cout << "C++98" << std::endl;
#elif __cplusplus == 201103L
std::cout << "C++11" << std::endl;
#elif __cplusplus > 201103L
std::cout << "C++14" << std::endl;
#endif
}
Macros chosen
__cplusplus - From gcc online documentation
Depending on the language standard selected, the value of the macro is
199711L, as mandated by the 1998 C++ standard; 201103L, per the 2011
C++ standard; an unspecified value strictly larger than 201103L for
the experimental languages enabled by -std=c++1y and -std=gnu++1y.
__STRICT_ANSI__ - From clang user manual
Differences between all c* and gnu* modes => c* modes define
__STRICT_ANSI__
As a side note, __STRICT_ANSI__ for GNU standard differentiation could also be found from this SO answer
$ g++ -E -dM -std=c++11 -x c++ /dev/null >b
$ g++ -E -dM -std=gnu++11 -x c++ /dev/null >a
$ diff -u a b
--- a 2014-12-19 12:27:11.000000000 +0530
+++ b 2014-12-19 12:27:05.000000000 +0530
## -144,6 +144,7 ##
#define __STDC_UTF_16__ 1
#define __STDC_UTF_32__ 1
#define __STDC__ 1
+#define __STRICT_ANSI__ 1
#define __UINTMAX_TYPE__ long unsigned int
#define __USER_LABEL_PREFIX__ _
#define __VERSION__ "4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)"

GCC compilation yields "real.h:53: error: 'SIZEOF_LONG' undeclared here (not in a function)"

I'm trying to compile GCC 4.7.2 on a Buffalo LinkStation Pro Duo (after unlocking it) which runs Linux 2.6.31.8 armv5tel.
Unfortunately, make throws quite some errors, starting with
gcc -c -DIN_GCC_FRONTEND -g -fkeep-inline-functions -DIN_GCC -W -Wall -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-formIn file included from ../../gcc-4.7.2/gcc/tree.h:32,
from ../../gcc-4.7.2/gcc/c-lang.c:27:
../../gcc-4.7.2/gcc/real.h:53: error: 'SIZEOF_LONG' undeclared here (not in a function)
In file included from ../../gcc-4.7.2/gcc/tree.h:32,
from ../../gcc-4.7.2/gcc/c-lang.c:27:
../../gcc-4.7.2/gcc/real.h:87:5: error: division by zero in #if
../../gcc-4.7.2/gcc/real.h:87:5: error: division by zero in #if
../../gcc-4.7.2/gcc/real.h:90:6: error: division by zero in #if
Line 53 of real.h reads unsigned long sig[SIGSZ];, where SIGSZ is defined at line 40 as
#define SIGSZ (SIGNIFICAND_BITS / HOST_BITS_PER_LONG)
while line 87 is #if REAL_WIDTH == 1 with REAL_WIDTH defined starting at line 72 as
#define REAL_WIDTH \
(REAL_VALUE_TYPE_SIZE/HOST_BITS_PER_WIDE_INT \
+ (REAL_VALUE_TYPE_SIZE%HOST_BITS_PER_WIDE_INT ? 1 : 0)) /* round up */
This seems to boil down to the HOST_BITS_PER_* being zero. Do I have to define these manually with some configure parameter or how can this issue be resolved?
update
config.log contains the following errors:
conftest.c:10:19: error: ppl_c.h: No such file or directory
conftest.c: In function 'main':
conftest.c:16: error: 'choke' undeclared (first use in this function)
conftest.c:16: error: (Each undeclared identifier is reported only once
conftest.c:16: error: for each function it appears in.)
conftest.c:16: error: expected ';' before 'me'
configure:5708: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME ""
| #define PACKAGE_TARNAME ""
| #define PACKAGE_VERSION ""
| #define PACKAGE_STRING ""
| #define PACKAGE_BUGREPORT ""
| #define PACKAGE_URL ""
| #define LT_OBJDIR ".libs/"
| /* end confdefs.h. */
| #include "ppl_c.h"
| int
| main ()
| {
|.
| #if PPL_VERSION_MAJOR != 0 || PPL_VERSION_MINOR < 11
| choke me
| #endif
|.
| ;
| r
Following this post I seem to have forgotten to install ppl, which I'll try now
SIZEOF_LONG should be #defined by configure in the file auto-host.h. Your auto-host.h should contain something like:
/* The size of `long', as computed by sizeof. */
#ifndef USED_FOR_TARGET
#define SIZEOF_LONG 8
#endif
If the above is not present (and it looks like in your case it's indeed so), check config.log for errors. Search for errors around the string checking size of long.
Thanks to chill's answer I checked config.log to discover
conftest.c:10:19: error: ppl_c.h: No such file or directory
(which, curiously, did not prevent configure from creating a Makefile and returning a success error code). The first google hit on that was this post, showing I didn't provide the ppl dependency.
The ppl-1.0 compilation greeted me with
checked_float.inlines.hh:1012: error: 'frexpl' was not declared in this scope
which led me to this post suggesting I'd use the 1.1 snapshot instead, which worked
Now, gcc's make offered me another "helpful" error:
gcc/../libcpp/include/line-map.h:66: error: 'CHAR_BIT'
which turned out to be due to C_INCLUDE_PATH ending with a colon (I already experienced the checking LIBRARY_PATH variable... contains current directory error mentioned in that post, but didn't think about checking other variables for that as well)
Compilation is still running, so far no more errors...

GCC #pragma to stop compilation

Is there a GCC pragma directive that will stop, halt, or abort the compilation process?
I am using GCC 4.1, but I would want the pragma to be available in GCC 3.x versions also.
You probably want #error:
$ cd /tmp
$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
$ ./stopthis
Hello, world
$ g++ -Wall -o stopthis stopthis.cpp
stopthis.cpp:7:6: error: #error I had enough
File stopthis.cpp
#include <iostream>
int main(void) {
std::cout << "Hello, world\n";
#ifndef GoOn
#error I had enough
#endif
return 0;
}
I do not know about a #pragma, but #error should do what you want:
#error Failing compilation
It will terminate compilation with the error message "Failing compilation".
This works:
#include <stophere>
GCC stops when it can't find the include file. I wanted GCC to stop if C++14 was not supported.
#if __cplusplus<201300L
#error need g++14
#include <stophere>
#endif
While typically #error is sufficient (and portable), there are times when you want to use a pragma, namely, when you want to optionally cause an error within a macro.
Here is an example use which depends on C11's _Generic and _Pragma.
This example ensures var isn't an int * or a short *, but not a const int * at compile time.
Example:
#define MACRO(var) do { \
(void)_Generic(var, \
int *: 0, \
short *: 0, \
const int *: 0 _Pragma("GCC error \"const not allowed\"")); \
\
MACRO_BODY(var); \
} while (0)
#pragma GCC error "error message"
Ref: 7 Pragmas
You can use:
#pragma GCC error "my message"
But it is not standard.
An alternative is to use static_assert:
#if defined(_MSC_VER) && _MSC_VER < 1916
static_assert(false, "MSVC supported versions are 1916 and later");
#endif

Resources