[comp.std.c] pragmas vs preprocessors

nydick@psc.edu (Dan Nydick) (01/10/91)

I have an application where I'd like to have a macro expand into
a #pragma.  I tried the following:

----
#define THISISAPRAGMA #pragma something

main()
{
   /* blah blah */
THISISAPRAGMA
   /* blah blah */
}
----

I could imagine several interpretations for this...
	1) insert and recognize the pragma in "main".
	2) complain about the # in the macro definition.
	3) insert "#pragma something" in main, but don't
		recognize it as a pragma.
	4) don't expand THISISAPRAGMA at all.
A quick scan of the C standard says that the # is illegal in
a "functionlike" definition, but didn't rule it out in an
"objectlike" definition.

Anyway, various compilers I've tried have resulted in 2, 3,
and 4.  I haven't found a compiler which does option 1.
I am able to run the code through the preprocessor and then
feed the output back into the compiler to get the effect of
option 1 with one of my compilers.

Should this work?  Which option is correct?  How would one
define a macro which expands into a pragma?  Or even, how
could one write a conditional expression so the pragma
only appears if certain symbols are defined (plain ifdefs
around the pragma are not good enough since some non-ansi
compilers will complain about #pragma even inside a "false"
condition)?  Is a #pragma supposed to be significant to the
compiler or to the pre-processor?

My current workaround is to conditionally include a file
which contains the #pragma line.

Thanks in advance.

Dan Nydick, Pittsburgh Supercomputing Center
nydick@psc.edu

henry@zoo.toronto.edu (Henry Spencer) (01/12/91)

In article <1991Jan10.153117.29024@fs7.ece.cmu.edu> nydick@psc.edu (Dan Nydick) writes:
>I have an application where I'd like to have a macro expand into
>a #pragma...

Can't be done.  The wording of the standard could use a bit of cleanup,
but 3.8.3 refers you to 3.8.3.4 for rescanning of both kinds of macros,
and 3.8.3.4 is quite explicit:  "The resulting completely macro-replaced
preprocessing token sequence is not processed as a preprocessing directive
even if it resembles one."

>... Or even, how
>could one write a conditional expression so the pragma
>only appears if certain symbols are defined (plain ifdefs
>around the pragma are not good enough since some non-ansi
>compilers will complain about #pragma even inside a "false"
>condition)?

Try indenting the #pragma; most pre-ANSI compilers will not "see" a
directive unless the `#' is the first character on the line:

	#ifdef foobar
	   #pragma pre-ANSI compilers probably will not see this
	#endif

>Is a #pragma supposed to be significant to the
>compiler or to the pre-processor?

Yes. :-)  The standard does not require the separation between compiler
and preprocessor; indeed, there are many implementations which integrate
the preprocessor into the compiler's scanner.
-- 
If the Space Shuttle was the answer,   | Henry Spencer at U of Toronto Zoology
what was the question?                 |  henry@zoo.toronto.edu   utzoo!henry

gwyn@smoke.brl.mil (Doug Gwyn) (01/12/91)

In article <1991Jan10.153117.29024@fs7.ece.cmu.edu> nydick@psc.edu (Dan Nydick) writes:
>#define THISISAPRAGMA #pragma something

The result of macro expansion is never rescanned to identify
preprocessor directives.  Find another method.