patch advice needed: macro expansion for #pragmas

Paul Rubin phr-2006@nightsong.com
Wed Feb 8 02:50:00 GMT 2006


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().  That 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.

Thanks.

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



More information about the Gcc-patches mailing list