#pragma macro

Henry Spencer henry at utzoo.uucp
Thu Jun 28 02:45:37 AEST 1990


In article <1990Jun26.145937.27988 at csrd.uiuc.edu> pommerel at sp14.csrd.uiuc.edu (Claude Pommerell) writes:
>I am writing portable ANSI C code, but at some points I need to insert
>pragmas to use vectorization facilities of the compiler effectively.
>
>I would like to define a macro
>	#define _DIR_(p1,p2) #pragma thisdir(p1,p2)

There is inherently no fully portable way to do this.  Section 3.8.3.4 of
the Holy Writ sayeth (at the end of the discussion of macro substitution):
"The resulting completely macro-replaced preprocessing token sequence is
not processed as a preprocessing directive even if it resembles one."
Given that section 3.8 says "The preprocessing tokens within a preprocessing
directive are not subject to macro expansion unless otherwise stated", and
3.8.6 on #pragma does not otherwise state, there is no fix.  You can't get
a #pragma as the result of a macro, and you can't rely on macro substitution
inside #pragma.

The C preprocessor is not a general-purpose macro processor.

>7. I use the same idea as in variant 5, but run the preprocessing phase
>   twice, defining
>	   #define _DIR_(p1,p2) _HASH_ pragma thisdir(p1,p2)
>   and calling
>	   _DIR_(arg1,arg2)
>   and compiling
>	   cc -D_HASH_=# -I. -P FILE.c
>	   cc -c FILE.i
>   
>   At last, this seems to work.

This is fairly clever, although it relies on the ability to preprocess
twice, which is not guaranteed.  (ANSI C does not require the ability
to separate preprocessing from the rest of compilation at all.)
-- 
"Either NFS must be scrapped or NFS    | Henry Spencer at U of Toronto Zoology
must be changed."      -John Osterhout |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.std.c mailing list