473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2399
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
29
by: Richard Lionheart | last post by:
Hi All, I've taken the advice of a few people and managed to cobble together an HTML 4.01 Strict-compliant document (according to the W3C Validation Service), but the way I try to pass a...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
6
by: ludovicd | last post by:
Hi to all, There is my question, Let's say I got a fonction which takes a numeric argument long enough so that there has to be spaces in it to represent it correctly. (Ex. 0xf63a002c5ff0338ec...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.