469,927 Members | 1,910 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

applying C preprocessor partially

Does a tool exist to apply C preprocessor expansion to a C source only
partially, i.e. replace only some directives? I want to replace some
#if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all
other #ifdef OTHER_SYMBOL and all #include directives.

urs
May 29 '06 #1
6 2144
Urs Thuermann said:
Does a tool exist to apply C preprocessor expansion to a C source only
partially, i.e. replace only some directives? I want to replace some
#if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all
other #ifdef OTHER_SYMBOL and all #include directives.


I know of no such tool, but it shouldn't be too hard to write one. See
Exercise 6-6 of K&R2, on page 145.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 29 '06 #2
Urs Thuermann wrote:
Does a tool exist to apply C preprocessor expansion to a C source only
partially, i.e. replace only some directives? I want to replace some
#if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all
other #ifdef OTHER_SYMBOL and all #include directives.

urs


<OT>
If what you are wanting to do is simply remove some of this
conditionally compiled code, you can write a quick perl script that will
do this. This is a rather trivial task and if you don't need the speed
of C (e.g., if you are only doing this to 20 or so files) then it would
be the easiest method.
</OT>

Another method would be to open the file for reading, see if you have
your #ifdef SOMECONSTANT and if so discard every line until you
encounter an #endif. This is a trivial task to do and if you adhere to
keeping all preprocessor directives at column 0 then it should work
without a problem. Note that this method does not take into
consideration embedded conditionals. E.g.,

#ifdef SOMECONSTANT
#ifndef SOMEOTHERCONSTANT
/* code */
#endif /* !defined(SOMEOTHERCONSTANT) */
#endif /* defined(SOEMCONSTANT) */

The above method will not work on the above code (unless you want to
remove the conditional embedded in the SOMECONSTANT conditional).

Hope that helps,

Joe
May 29 '06 #3
"Urs Thuermann" <ur*@isnogud.escape.de> wrote in message
news:m2************@janus.isnogud.escape.de...
Does a tool exist to apply C preprocessor expansion to a C source only
partially, i.e. replace only some directives? I want to replace some
#if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all
other #ifdef OTHER_SYMBOL and all #include directives.


We've used the DMS Software Reengineering Toolkit to do exactly this.
The basic idea is to pick up the conditional directives, symbolically
substitute the status of each #define symbol
(e.g., 0 --> "I'll never use this define again",
1 --> "Assume this is always true",
or the symbol name --> "Leave this conditional"),
simplify the resulting boolean formula symbolically,
and resubstitute the resulting formula. A last optimization
handles the cases of #if 0 or #if 1,
#elseif 0, #elseif 1, #else, etc. to strip away
the dead branches of the conditionals.

There's a paper on how to do this with DMS that
gives more details:
Baxter, I. and Mehlich, M.
Preprocessor Conditional Removal by Simple Partial Evaluation,
2001 Workshop on Analysis, Slicing and Transformation, 2001 ,
available at our website:
http://www.semanticdesigns.com/Company/Publications/

and we have done this multi-million lines systems.

So yes, such a tool exists.

--
Ira Baxter, CTO
www.semanticdesigns.com
May 29 '06 #4
"Ira Baxter" <id******@semdesigns.com> writes:
"Urs Thuermann" <ur*@isnogud.escape.de> wrote in message
news:m2************@janus.isnogud.escape.de...
Does a tool exist to apply C preprocessor expansion to a C source only
partially, i.e. replace only some directives? I want to replace some
#if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all
other #ifdef OTHER_SYMBOL and all #include directives.


We've used the DMS Software Reengineering Toolkit to do exactly this.
The basic idea is to pick up the conditional directives, symbolically
substitute the status of each #define symbol
(e.g., 0 --> "I'll never use this define again",
1 --> "Assume this is always true",
or the symbol name --> "Leave this conditional"),
simplify the resulting boolean formula symbolically,
and resubstitute the resulting formula. A last optimization
handles the cases of #if 0 or #if 1,
#elseif 0, #elseif 1, #else, etc. to strip away
the dead branches of the conditionals.


<QUIBBLE>
#elif, not #elseif
</QUIBBLE>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 29 '06 #5
Keith Thompson <ks***@mib.org> wrote:
"Ira Baxter" <id******@semdesigns.com> writes:
and resubstitute the resulting formula. A last optimization
handles the cases of #if 0 or #if 1,
#elseif 0, #elseif 1, #else, etc. to strip away
the dead branches of the conditionals.


<QUIBBLE>
#elif, not #elseif
</QUIBBLE>


*Shrug* What do you expect from an itinerant spammer?

Richard
May 30 '06 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:44****************@news.xs4all.nl...
Keith Thompson <ks***@mib.org> wrote:
"Ira Baxter" <id******@semdesigns.com> writes:
and resubstitute the resulting formula. A last optimization
handles the cases of #if 0 or #if 1,
#elseif 0, #elseif 1, #else, etc. to strip away
the dead branches of the conditionals.


<QUIBBLE>
#elif, not #elseif
</QUIBBLE>


*Shrug* What do you expect from an itinerant spammer?


I'm happy with Keith's quibble. Our tools work with a wide variety
of languages, and I don't code a lot in C directly, so I sometimes
make minor technical errors in writing down language constructs.

My response was to a question posed by original poster,
and it answered his question directly.
You have problem with that?

--
Ira Baxter, CTO
www.semanticdesigns.com
Jun 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

205 posts views Thread by Jeremy Siek | last post: by
24 posts views Thread by Nudge | last post: by
13 posts views Thread by Chris Croughton | last post: by
9 posts views Thread by Walter Roberson | last post: by
29 posts views Thread by Richard Lionheart | last post: by
6 posts views Thread by ludovicd | last post: by
31 posts views Thread by Sam of California | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.