473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2421
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 SOMEOTHERCONSTA NT
/* code */
#endif /* !defined(SOMEOT HERCONSTANT) */
#endif /* defined(SOEMCON STANT) */

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.es cape.de> wrote in message
news:m2******** ****@janus.isno gud.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******@semde signs.com> writes:
"Urs Thuermann" <ur*@isnogud.es cape.de> wrote in message
news:m2******** ****@janus.isno gud.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_Keit h) 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.or g> wrote:
"Ira Baxter" <id******@semde signs.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.x s4all.nl...
Keith Thompson <ks***@mib.or g> wrote:
"Ira Baxter" <id******@semde signs.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
10469
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
40102
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 looking to write something along the lines of:
13
2108
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 defined = 2;
9
3646
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. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor...
29
1748
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 TextArea object to a script function doesn't work. What's the least amount of change I can make to this to remain compliant but also correct for...
32
2749
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 the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger...
6
1610
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 becoming -0xf63a 002c 5ff0 338ec) is there a way to tell the preprocessor to make: fct(0xf63a 002c 5ff0 338ec) into === fct(0xf63a) f(002c)...
31
2887
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of...
8
2424
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
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.