Search code examples
gccc-preprocessorpragma

_Pragma("GCC error") is processed differently inside and outside a #if directive


I'm experiencing a behavior that I find weird with the preprocessing of _Pragma("GCC error").

I get the same results with avr-gcc (GCC) 4.9.2 and with gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4).

The source code:

#define E               _Pragma("GCC error \"This is an error\"")

#define _IS_VOID_       , 1

/*  Return "1" if <c> is "", return "0" otherwise.
 */
#define ISVOID(c)         _ISVOID2(c)
#define _ISVOID2(c)       _ISVOID3(_IS_VOID_##c,0,)
#define _ISVOID3(...)     _ISVOID4(__VA_ARGS__)
#define _ISVOID4(v,x,...) x


"ISVOID(something):" ISVOID(something)
"ISVOID():" ISVOID()
"ISVOID(E):" ISVOID(E)


#if ISVOID(something) == 1
"ISVOID(something)==1: true"
#else
"ISVOID(something)==1: false"
#endif

#if ISVOID() == 1
"ISVOID()==1: true"
#else
"ISVOID()==1: false"
#endif

#if ISVOID(E) == 1
"ISVOID(E)==1: true"
#else
"ISVOID(E)==1: false"
#endif

I process this with gcc -E -std=c1x -Wall -Wextra -Wpedantic main.c >output.

I get this on the command line (almost normal):

main.c:16:11: error: This is an error
 "ISVOID(E):" ISVOID(E)
           ^

and this in the output:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "main.c"
# 14 "main.c"
"ISVOID(something):" 0
"ISVOID():" 1
"ISVOID(E):"
# 16 "main.c"

# 16 "main.c"
 1





"ISVOID(something)==1: false"



"ISVOID()==1: true"







"ISVOID(E)==1: false"

Then, ISVOID(E) expands to 1 outside the #if directive and it expands to 0 inside, meaning that _Pragma("GCC error") is processed differently inside and outside a #if directive.

Moreover, there is no emission of a "This is an error" related to the #if ISVOID(E) == 1.

Is that behavior normal?


Solution

  • GCC Bugzilla has confirmed that this is a bug (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=79948).