Pragmas and #defines

I’m generating many identical functions with small differences using C #defines. Inside these I use pragmas. This doesn’t work with old C #pragma directive, but should work using standard C99 _Pragma. The following example, for example, works with GCC:

#define MKFUN(X, OP) \
   void X(double *dst, double *src, size_t n) \
   { \
       _Pragma("omp parallel for") \
       for(size_t i=0; i<n i++){ \
           dst[i]=OP(src[i]); \
       } \

MKFUN(multiexp,  exp)

It fails with PGCC, which complains

PGC-S-0036-Syntax error: Recovery attempted by inserting ‘;’ before ‘{’

Looking at the output of the preprocessor (pgcc -E), it seems to replace _Pragma with #pragma during the preprocessing, which makes the compiler fail, because the whole expansion MKFUN(multiexp, exp) is on a single line.

I think is a bug in PGCC, not conforming to C99. In the meanwhile, does anyone know a simple workaround, which does not involve a custom preprocessor hack for function generation?

Hi tttttttttt,

In looking that the generated post-processed files, the only difference between the PGI pre-processed file and the CPP/GCC pre-processed file is new line after the generated “#pragma”. In reading Section 6.10.9 of the C99 standard, “Pragma operator”, both compilers are correctly generating the “#pragma” but the standard is unclear if a new line should be generated. I’ll need to talk with our compiler architect to better understand what the correct behaviour should be.

  • Mat
% gcc -E test2.c
# 1 "test2.c"
# 1 "<built>"
# 1 "<command>"
# 1 "test2.c"
# 10 "test2.c"
void multiexp(double *dst, double *src, size_t n) {
# 10 "test2.c"
#pragma omp parallel for
# 10 "test2.c"
 for(size_t i=0; i<n i++){ dst[i]=exp(src[i]); } }
% pgcc -E test2.c
# 1 "test2.c"
# 10 "test2.c"
void multiexp ( double * dst , double * src , size_t n ) { 
# 10 "test2.c"
#pragma omp parallel for for ( size_t i = 0 ; i < n i ++ ) { dst [ i ] = exp ( src [ i ] ) ; } }