applying C preprocessor partially

U

Urs Thuermann

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
 
R

Richard Heathfield

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.
 
J

Joe Estock

Urs 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.

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
 
I

Ira Baxter

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.

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.
 
K

Keith Thompson

Ira Baxter said:
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>
 
I

Ira Baxter

Richard Bos said:
*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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top