A consulting client of mine has asked me to make a cpp patch that
causes GCC to optionally expand preprocessor macros in #pragmas.  He
is trying to migrate a huge codebase from Microsoft C to GCC.  The
code has a lot of pragmas like

   #pragma FOO

where FOO is a symbol created by a #define statement elsewhere in the program.

C99 specifies certain standard pragmas and further specifies that
macros should NOT be expanded for those pragmas.  What happens with
other pragmas is up to the compiler.  GCC handles it by not expanding
macros for any pragmas.  It would still be C99 conformant if it didn't
expand the C99-standard pragmas but expanded all other pragmas.
Microsoft C apparently does expand those pragmas, and the client's
program relies on the expansion taking place.

I've made a patch that adds a command line option ("--expand-pragmas")
that, when activated, disables the inhibition of pragma macro
expansion in libcpp/directives.c:do_pragma().  This of course means
ALL pragmas get expanded, including the C99 standard pragmas, so the
patch is not C99-conformant.  I'd been planning to add some additional
logic to distinguish between the C99 pragmas and other pragmas, and
expand only the C99 pragmas.  But I see there's an internal table of
pragmas, which don't get expanded (IIRC there's a flag in the table
entry; I don't have the code in front of me right now).  So I'm
thinking of just adding the C99 pragmas to that table.  Is that a
reasonable approach?

Also, the client is hoping that the GCC team will accept the patch
once it's finished, assuming it's clean and documented (I think I can
do that).  I think the patch should be accepted since it implements a
Microsoft C feature that some users rely on, and having the patch
makes it easier to port Win32 legacy code.  Of course for new code,
the C99 "Pragma" keyword, or GCC function attributes, are preferred.
Please let me know your thoughts on this.

Very minor bug: the comment at the top of the (automatically
generated) options.c file says it's generated by opts.sh rather than
by those awk scripts.

Semi-minor doc bug: the comment at the top of gcc/gcc.c (guide to
adding a command line option) is incomplete; it should say how to add
the option to the appropriate place in c.opt and rebuild the compiler,
and then describe how you then get an appropriate options.c entry and
OPT_whatever variable.  I couldn't find docs about that anywhere in
the distro, and it took me a while to puzzle that out by tracing the
code.

Thanks

--Paul Rubin
(original author of cccp.c if that matters)

Reply via email to