473,396 Members | 2,016 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,396 software developers and data experts.

Preprocessor Directives Management Tool

All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).

Does such a tool exist? A Visual Studio plugin would be even better.

FYI: I have requirements to rid my code of all conditionally compiled
code.

Thanks,
Dan
Oct 22 '08 #1
14 2554
lagman wrote:
All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).
Most compilers have an options to output the pre-processes code. -E is
often used for this.

--
Ian Collins
Oct 22 '08 #2
Ian Collins wrote:
lagman wrote:
>All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).
Most compilers have an options to output the pre-processes code. -E is
often used for this.
... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.

The FAQ has pointers to some tools that might address the
problem better. (Can't vouch for them myself, but the pointers
are probably worth a look.)

--
Er*********@sun.com
Oct 22 '08 #3
Eric Sosman wrote:
Ian Collins wrote:
>lagman wrote:
>>All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).
Most compilers have an options to output the pre-processes code. -E is
often used for this.

... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.
Isn't #include a preprocessor directive?

The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.

--
Ian Collins
Oct 22 '08 #4
Ian Collins <ia******@hotmail.comwrites:
Eric Sosman wrote:
>Ian Collins wrote:
>>lagman wrote:
All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).

Most compilers have an options to output the pre-processes code. -E is
often used for this.

... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.
Isn't #include a preprocessor directive?
Yes, but...
The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.
They clarified the requirement:

| FYI: I have requirements to rid my code of all conditionally compiled
| code.

--
Ben.
Oct 22 '08 #5
Ben Bacarisse wrote:
Ian Collins <ia******@hotmail.comwrites:
>Eric Sosman wrote:
>>Ian Collins wrote:
lagman wrote:
All,
>
I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).
>
Most compilers have an options to output the pre-processes code. -E is
often used for this.
... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.
Isn't #include a preprocessor directive?

Yes, but...
>The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.

They clarified the requirement:

| FYI: I have requirements to rid my code of all conditionally compiled
| code.
Which -E would do.

--
Ian Collins
Oct 22 '08 #6
Ian Collins <ia******@hotmail.comwrites:
Ben Bacarisse wrote:
>Ian Collins <ia******@hotmail.comwrites:
>>Eric Sosman wrote:
Ian Collins wrote:
lagman wrote:
>All,
>>
>I'm looking for a tool that is able to take my code base (which is
>full of preprocessor directives) and spit out source code that is
>"clean" of any preprocessor directives based on the options I choose
>(possibly via some kind of user input screen).
>>
Most compilers have an options to output the pre-processes code. -E is
often used for this.
... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.

Isn't #include a preprocessor directive?

Yes, but...
>>The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.

They clarified the requirement:

| FYI: I have requirements to rid my code of all conditionally compiled
| code.
Which -E would do.
I never said otherwise! A tool that follows #includes (so it can find
#defines and so on) but does not replace them is more useful than -E.
Once you have expanded #includes you may not even have C anymore! You
certainly won't have portable C. The result might not even compile
using different compiler flags.

Eric was saying that the OP's *real* requirement might be better
served with a tool that is less crude than actually running the
pre-processor.

--
Ben.
Oct 22 '08 #7
Ian Collins wrote:
Eric Sosman wrote:
>Ian Collins wrote:
>>lagman wrote:
All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).

Most compilers have an options to output the pre-processes code. -E is
often used for this.
... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.
Isn't #include a preprocessor directive?

The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.
You're right. Sorry; I'd imagined for a moment that
the O.P. was asking for something useful.

O.P.: Follow Ian's suggestion to get source code that
is completely free from preprocessor directives (perhaps
after stripping out some #line directives), and almost
completely useless. You will never be able to move the
resulting source to another machine, or even to another
compiler. You may not even be able to move it to your
current compiler if you decide to change the compilation
options or apply the latest patches. Have a nice life!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 23 '08 #8
On Oct 23, 2:46*am, lagman <dan_hoff...@hailmail.netwrote:
All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).

Does such a tool exist? *A Visual Studio plugin would be even better.

FYI: *I have requirements to rid my code of all conditionally compiled
code.
Do you wish that your code should be 'compilable' after you get rid of
all preprocessor directives ? Or you just want to run some static code
analysis tool on it ?

- Pranav
http://pranavsbrain.peshwe.com
Oct 23 '08 #9
On Oct 22, 8:05*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Ian Collins wrote:
Eric Sosman wrote:
Ian Collins wrote:
lagman wrote:
All,
>>I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).
>Most compilers have an options to output the pre-processes code. *-E is
often used for this.
* * ... but that's not a good tool for the task, because it will
expand all the macros and replace #include directives with the
included source.
Isn't #include a preprocessor directive?
The OP asked for something that would 'spit out source code that is
"clean" of any preprocessor directives'.

* * *You're right. *Sorry; I'd imagined for a moment that
the O.P. was asking for something useful.

* * *O.P.: Follow Ian's suggestion to get source code that
is completely free from preprocessor directives (perhaps
after stripping out some #line directives), and almost
completely useless. *You will never be able to move the
resulting source to another machine, or even to another
compiler. *You may not even be able to move it to your
current compiler if you decide to change the compilation
options or apply the latest patches. *Have a nice life!

--
Eric Sosman
esos...@ieee-dot-org.invalid- Hide quoted text -

- Show quoted text -
Sorry.. I'll clarify.. Not all preprocessor directives, just
conditionally compiled code (i.e. #ifdef/#ifndef)

Thanks,
Dan
Oct 23 '08 #10
lagman wrote:
All,

I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the options I choose
(possibly via some kind of user input screen).

Does such a tool exist? A Visual Studio plugin would be even better.

FYI: I have requirements to rid my code of all conditionally compiled
code.

Thanks,
Dan
The lcc-win compiler will tell you which lines are active.
The utility browsegen from that system will show you which lines are
inactive.

For instance:

#ifdef FOO
int a;
#else
double a;
#endif

browsegen -showifdeflines -DFOO foo.c
3
4

browsegen -showifdeflines foo.c
1
2

Using the list of lines it is very easy to wrfite a simple utility that
will delete all the lines from a given file...

Obviously I can tailor the system to fit your needs, make it run under
linux/AIX/Solaris/ etc.

Just send me mail for price etc.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 23 '08 #11
ja*********@verizon.net writes:
lagman wrote:
[...]
>Sorry.. I'll clarify.. Not all preprocessor directives, just
conditionally compiled code (i.e. #ifdef/#ifndef)

Do you want it to remove only the #ifdef and #ifndef statements? Do
you also want #if's removed? Do you want it to also remove the
corresponding #else, #elif, and #endif statements? Do you want it to
also remove all the code between each #ifdef/#ifndef direction and the
matching #endif directive? Those are all pretty trivial options.
They'll produce code that might not even compile, and if it does,
might not do what the original code did. But if that's what you want,
it's quite feasible.

I think it's most likely that you want it to remove all of the
conditional compilation directives, and the branches that would not
have been selected, while keeping the branches that would have been
selected. On some systems, preprocessing is performed by a completely
separate program; even when that's not the case, many compilers
provide an option (often -E) that has precisely that effect. However,
you've indicated that the conditional compilation directives are the
only ones you want removed, and that makes it much more complicated.
[...]

Here's an idea that won't quite work, but it might be the basis for
something that would.

Do it in three passes.

In pass 1, comment out all #include and #define directives. Do this
in a way that's reversible; each comment includes some unique string
that can be recognized in pass 3.

In pass 2, run the output of pass 1 through your preprocessor. (A
compiler isn't required to provide a preprocessor as a separate
program, but most do.)

In pass 3, uncomment the #include and #define directives that you
commented out in pass 1. Remember that if you had this line:

/* #include "foo.h" */

in your original source, it must remain as a comment in the final
result; this is why you need the unique string.

Why doesn't this actually work? Because some of your #if, #ifdef, and
#ifndef directives are likely to depend on macros that you'll have
commented out in pass 1.

My assumption is that, given this input:

#define FOO 42
#ifdef FOO
printf("FOO is defined as %d\n", FOO);
#else
puts("FOO is not defined");
#endif

you want this output:

#define FOO 42
printf("FOO is defined as %d\n", FOO);

If so, that means you need to pay attention macro definitions for
purposes of #if, #ifdef, and #ifndef, but not for purposes of
expansion. That makes it tricky.

Most Unix-like systems have a tool, originally from BSD, called
"unifdef" that might do much of what you want. If you have access to
it, read the documentation carefully; if you have questions about the
unifdef tool itself, comp.unix.programmer is probably the best place
to ask.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 23 '08 #12
lagman <da*********@hailmail.netwrites:

<snip>
Sorry.. I'll clarify.. Not all preprocessor directives, just
conditionally compiled code (i.e. #ifdef/#ifndef)
Some C pre-processors can give you a leg up, so to speak. For
example, the GNU one has -fdirectives-only which gets you about 90% of
the way there.

It does process #includes but since it leaves a note of the files (and
line numbers) as it is reading the source, I suspect a small bit of
scripting could cut out the expansion of the includes and put the
original directives back.

--
Ben.
Oct 23 '08 #13
On Oct 23, 11:00*pm, Keith Thompson <ks...@mib.orgwrote:
jameskuy...@verizon.net writes:
lagman wrote:
[...]
Sorry.. I'll clarify.. Not all preprocessor directives, just
conditionally compiled code (i.e. #ifdef/#ifndef)
Do you want it to remove only the #ifdef and #ifndef statements? Do
you also want #if's removed? Do you want it to also remove the
corresponding #else, #elif, and #endif statements? Do you want it to
also remove all the code between each #ifdef/#ifndef direction and the
matching #endif directive? Those are all pretty trivial options.
They'll produce code that might not even compile, and if it does,
might not do what the original code did. But if that's what you want,
it's quite feasible.
I think it's most likely that you want it to remove all of the
conditional compilation directives, and the branches that would not
have been selected, while keeping the branches that would have been
selected. On some systems, preprocessing is performed by a completely
separate program; even when that's not the case, *many compilers
provide an option (often -E) that has precisely that effect. However,
you've indicated that the conditional compilation directives are the
only ones you want removed, and that makes it much more complicated.

[...]

Here's an idea that won't quite work, but it might be the basis for
something that would.

Do it in three passes.

In pass 1, comment out all #include and #define directives. *Do this
in a way that's reversible; each comment includes some unique string
that can be recognized in pass 3.

In pass 2, run the output of pass 1 through your preprocessor. *(A
compiler isn't required to provide a preprocessor as a separate
program, but most do.)

In pass 3, uncomment the #include and #define directives that you
commented out in pass 1. *Remember that if you had this line:

/* #include "foo.h" */

in your original source, it must remain as a comment in the final
result; this is why you need the unique string.

Why doesn't this actually work? *Because some of your #if, #ifdef, and
#ifndef directives are likely to depend on macros that you'll have
commented out in pass 1.
But, why to comment out the '#define's in the first place ? Without
doing that, your idea would probably work well.
Kindly CMIIW...

Pranav
http://pranavsbrain.peshwe.com
Oct 24 '08 #14
Pranav Peshwe <pr**********@gmail.comwrites:
On Oct 23, 11:00*pm, Keith Thompson <ks...@mib.orgwrote:
[...]
>Here's an idea that won't quite work, but it might be the basis for
something that would.

Do it in three passes.

In pass 1, comment out all #include and #define directives. *Do this
in a way that's reversible; each comment includes some unique string
that can be recognized in pass 3.

In pass 2, run the output of pass 1 through your preprocessor. *(A
compiler isn't required to provide a preprocessor as a separate
program, but most do.)

In pass 3, uncomment the #include and #define directives that you
commented out in pass 1. *Remember that if you had this line:

/* #include "foo.h" */

in your original source, it must remain as a comment in the final
result; this is why you need the unique string.

Why doesn't this actually work? *Because some of your #if, #ifdef, and
#ifndef directives are likely to depend on macros that you'll have
commented out in pass 1.

But, why to comment out the '#define's in the first place ? Without
doing that, your idea would probably work well.
Kindly CMIIW...
If you don't comment out the #defines, then this:

#define MAX_SIZE 1000
unsigned char buf[MAX_SIZE];

becomes this:

unsigned char buf[1000];

which I presume you don't want.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 24 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. ...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
6
by: max(01)* | last post by:
hi. i want to examine preprocessed source which only has certain macros expanded, for example i would like to have: #include <stdio.h> #include "other.c" int main() { ... }
2
by: Christopher Ireland | last post by:
Hello, I'm looking for a C# Preprocessor (shareware with source, if possible) which has the functionality of preprocessors which already exist in other languages, e.g. ...
8
by: claus.tondering | last post by:
I need to write a macro that inserts someStruct m_someStruct; into another struct declaration. The problem is that if the programmer specifies one particluar struct (called alpha), nothing...
6
by: Urs Thuermann | last post by:
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,...
21
by: Bogdan | last post by:
Can anyone recommend a program for indentation of C preprocessor directives. My file looks like this: #ifdef a #define b #else #define c #endif int main() {
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.