Re: Complete macro expansion with cpp

"Rici Lake" <RLake@oxfam.org.uk>
13 Feb 2005 22:45:49 -0500

          From comp.compilers

Related articles
Complete macro expansion with cpp peter.mathes@gmx.de (2005-02-12)
Re: Complete macro expansion with cpp RLake@oxfam.org.uk (Rici Lake) (2005-02-13)
Re: Complete macro expansion with cpp skandgoe@gwdg.de (Skandinavisches Seminar) (2005-02-13)
Re: Complete macro expansion with cpp codeworker@free.fr (2005-02-16)
Re: Complete macro expansion with cpp codeworker@free.fr (2005-02-16)
Re: Complete macro expansion with cpp peter.mathes@gmx.de (peter.mathes@gmx.de) (2005-02-16)
| List of all articles for this month |

From: "Rici Lake" <RLake@oxfam.org.uk>
Newsgroups: comp.compilers
Date: 13 Feb 2005 22:45:49 -0500
Organization: Compilers Central
Keywords: C, macros

> When I pre-process the following lines


> #define MACRO1 (1)
> #define MACRO2 (MACRO1)


> using cpp with the option '-dM' it outputs the same lines:


> #define MACRO1 (1)
> #define MACRO2 (MACRO1)


> But I need an output with the macros expanded completely. Something
> like this:


> #define MACRO1 (1)
> #define MACRO2 ((1))


But that is changing the semantics of cpp macros. MACRO2 really
does mean (MACRO1):


rlake@freeb:~$ less cpptest.c
#define MACRO1 (1)
#define MACRO2 (MACRO1)
MACRO2
#undef MACRO1
#define MACRO1 (2)
MACRO2
#undef MACRO1
MACRO2


rlake@freeb:~$ cpp cpptest.c
# 1 "cpptest.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "cpptest.c"




((1))




((2))


(MACRO1)


-----


If that doesn't bother you, you could simply lex the C input, which is
reasonably straightforward, unlike parsing C, and do macro
substitutions using a simple hash table. However, that will miss out
on a number of curious but occasionally useful (and used)
"features". (Note, in particular, the non expansion of MACRO in the
definition of SHOW):


rlake@freeb:~$ less cpptest2.c
#define MACRO1 one
#define MACRO2 two
#define X(a,b) a##b
#define MACRO unexpanded
#define SHOW(i) Macro i is X(MACRO, i)
SHOW(1)
SHOW(2)


rlake@freeb:~$ cpp cpptest2.c
# 1 "cpptest2.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "cpptest2.c"






Macro 1 is one
Macro 2 is two



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.