Special C Preprocessing Tool Needed for Pruning of #ifdefs in code

J

jvankay

Does any C guru out there know of a C pre-processing tool that will allow
limited pruning of C source based on a single #define variable.

That is, in the code fragment below:

XXX
#ifdef UndesirableTag
YYY
#ifdef TagB
ZZZ
#endif
AAA
#endif
BBB
#ifdef TagC
CCC.
#endif
DDD

If "UndesirableTag" and all of its code is to be removed from a file (with
other #defines in it), then the outer #ifdef and the code all the way to the
2nd #endif inclusive should be removed, but nothing else touched.

Cpp is not suitable because it will remove all the defines and inline the
#includes. Ideally the tool will handle ifdef, ifndef, else, elif, endif,
if Defined, if expr, and nested ifs. We need this for a project with many
source files and many defines in the project.

Anyone know of a tool that will do this, or something close to this?

Any help would be appreciated. Please email to (e-mail address removed).

Thanks and best regards.
 
C

Chris Croughton

Does any C guru out there know of a C pre-processing tool that will allow
limited pruning of C source based on a single #define variable.

That is, in the code fragment below:

XXX
#ifdef UndesirableTag
YYY
#ifdef TagB
ZZZ
#endif
AAA
#endif
BBB
#ifdef TagC
CCC.
#endif
DDD

If "UndesirableTag" and all of its code is to be removed from a file (with
other #defines in it), then the outer #ifdef and the code all the way to the
2nd #endif inclusive should be removed, but nothing else touched.

Yes. You can look for things called rmif and scpp, if you can get them
to compile and run on your system. Or I have one which I wrote
recently:

http://www.keristor.co.uk/stuff/xcpp-0.4.0.tar.gz

Mine is ISO C89 (other versions are pre-ISO, as I recall). It's coped
with everything I've thrown at it, I'd be interested if you find any
bugs in it or any problems compiling it on some platform, I'd like to
fix them if found. It also handles things like limits.h, float.h and
autoconf type config.h files which use #define and #undef, which the
others don't, as well as -D and -U to defined and undefine macros. It
doesn't hadle macros with varargs but I doubt you want that...
Cpp is not suitable because it will remove all the defines and inline the
#includes. Ideally the tool will handle ifdef, ifndef, else, elif, endif,
if Defined, if expr, and nested ifs. We need this for a project with many
source files and many defines in the project.

Yup, that's what mine is designed for. It's released as Free Software
(Zlib type licence).
Anyone know of a tool that will do this, or something close to this?

Any help would be appreciated. Please email to (e-mail address removed).

Emailed as well as posted.

Chris C
 
I

Ira Baxter

jvankay said:
Does any C guru out there know of a C pre-processing tool that will allow
limited pruning of C source based on a single #define variable.

That is, in the code fragment below:

XXX
#ifdef UndesirableTag
YYY
#ifdef TagB
ZZZ
#endif
AAA
#endif
BBB
#ifdef TagC
CCC.
#endif
DDD

If "UndesirableTag" and all of its code is to be removed from a file (with
other #defines in it), then the outer #ifdef and the code all the way to the
2nd #endif inclusive should be removed, but nothing else touched.

Cpp is not suitable because it will remove all the defines and inline the
#includes. Ideally the tool will handle ifdef, ifndef, else, elif, endif,
if Defined, if expr, and nested ifs. We need this for a project with many
source files and many defines in the project.

Anyone know of a tool that will do this, or something close to this?

See http://www.semanticdesigns.com/Products/Tools.html

-- IDB
 
C

CBFalconer

Chris said:
Oops, that should be

http://www.keristor.co.uk/stuff/xcpp-0.5.0.tar.gz

I fixed some bugs and uploaded it to the website but didn't
regenerate my web page to indicate the latest version.

You may or may not be interested to know that it compiled out of
the box under DJGPP. However note the warnings below. I haven't
attempted to do anything about them. Offhand it looks like a well
done piece.

It would also be pleasant to have the man page in info format.

c:\dnld\scratch\junk>make
gcc -g -Wall -W -pedantic -ansi -c -o xcpp.o xcpp.c
xcpp.c: In function `tracemem':
xcpp.c:2712: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2713: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2714: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2716: warning: int format, long unsigned int arg (arg 3)
xcpp.c: In function `main':
xcpp.c:2811: warning: int format, long unsigned int arg (arg 3)
gcc -g -Wall -W -pedantic -ansi -c -o xcppopt.o xcppopt.c
gcc -g -Wall -W -pedantic -ansi -c -o xcppmem.o xcppmem.c
gcc -g -Wall -W -pedantic -ansi -c -o xcppmath.o xcppmath.c
gcc -g -Wall -W -pedantic -ansi -o xcpp xcpp.o xcppopt.o xcppmem.o
xcppmath.o

You might also want to look on my page (URL below), download
section, for id2id-20.zip. It contains a simple technique for
automatic help when stdin on filters has not been redirected.
Steal it.
 
C

Chris Croughton

You may or may not be interested to know that it compiled out of
the box under DJGPP. However note the warnings below. I haven't
attempted to do anything about them. Offhand it looks like a well
done piece.

Thanks. DJGPP is a version of gcc, so it should have compiled, but it
probably has different memory options from the Linux and Cygwin
versions. Borland C 5.6 gives a load of warnings about unused variables
which I need to check sometime (and I may include a Borland makefile
with the next version).
It would also be pleasant to have the man page in info format.

I'll have to learn the texinfo stuff sometime. Or there might be a tool
which does the conversion.
c:\dnld\scratch\junk>make
gcc -g -Wall -W -pedantic -ansi -c -o xcpp.o xcpp.c
xcpp.c: In function `tracemem':
xcpp.c:2712: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2713: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2714: warning: int format, long unsigned int arg (arg 3)
xcpp.c:2716: warning: int format, long unsigned int arg (arg 3)
xcpp.c: In function `main':
xcpp.c:2811: warning: int format, long unsigned int arg (arg 3)

Ah yes. I wonder why gcc didn't report them for me. Which version of
gcc are you using (which base version, that is)? Neither 2.95.4 (Debian
GNU/Linux) nor 3.3.3 (Cygwin) produce any of those warnings with those
options. Which is odd, since I know I've seen gcc complain about printf
mismatches before. Perhaps they don't care because long and int are the
same size on those platforms?
gcc -g -Wall -W -pedantic -ansi -c -o xcppopt.o xcppopt.c
gcc -g -Wall -W -pedantic -ansi -c -o xcppmem.o xcppmem.c
gcc -g -Wall -W -pedantic -ansi -c -o xcppmath.o xcppmath.c
gcc -g -Wall -W -pedantic -ansi -o xcpp xcpp.o xcppopt.o xcppmem.o
xcppmath.o

You might also want to look on my page (URL below), download
section, for id2id-20.zip. It contains a simple technique for
automatic help when stdin on filters has not been redirected.
Steal it.

It seems to use non-C POSIX functions fileno() and isatty() to determine
whether stdin has been redirected, which makes it not standard C (it's
not often I get to tease /you/ about being off-topic <g>). AS far as I
know there is no portable way to do it. I might just disable the
automatic filter mode since an argument of just '-' means "use stdin"
anyway (indeed, when it finds there are no arguments it just calls the
function which does the work with "-").

Chris C
 
C

CBFalconer

Chris said:
.... snip ...
Steal it.

It seems to use non-C POSIX functions fileno() and isatty() to
determine whether stdin has been redirected, which makes it not
standard C (it's not often I get to tease /you/ about being
off-topic <g>). AS far as I know there is no portable way to
do it. I might just disable the automatic filter mode since an
argument of just '-' means "use stdin" anyway (indeed, when it
finds there are no arguments it just calls the function which
does the work with "-").

The routine is labelled as non-standard, but organized to co-exist
with the -pedantic option. If it can't be used on a system all you
have to do is return 0 from a replacement routine, and you are back
to the existing situation. At any rate, most of the time our
systems are running on something that has Posix functions
available, and this takes advantage of that without insisting on
it.

The other way you have to put in code to handle the '-' and
remember to use that argument in any chained commands, etc.

"something | operate thing"

is easier to remember or create than

"something | operate thing -"

especially when you are used to using operate with redirection.
 
K

Keith Thompson

Chris Croughton said:

I haven't tried it, but I did download it, and the packaging is a bit
odd. Generally a tarball named "xcpp-0.5.0.tar.gz" should contain a
directory called "xcpp-0.5.0"; yours contains all the files directly.
Also, one of the files is called "xcpp-0.5.0.tar.gz", which causes
problems when I unpack it into the current directory.
 
C

CBFalconer

Chris said:
.... snip ...

I've found and fixed a bug with nested #else when the code was being
compiled out, removed the warnings some compilers gave, and the tar
now extracts to a subdirectory (as Keith pointed out) and doesn't
contain the previous targzball.

http://www.keristor.co.uk/stuff/xcpp-0.5.2.tar.gz

Grabbed it but haven't looked inside. For some reason it arrives
here with an extra .tar appended to the filename. The other thing
is why is it so much smaller than the previous?

1-11-05 17:46 70,443 xcpp-0.5.0.tar.gz
1-17-05 21:57 33,307 xcpp-0.5.2.tar.gz

I ran your older one with --version (nothing happened) and with -h,
which told me something, but not the version. Neither did -V. :-(
 
K

Keith Thompson

CBFalconer said:
Grabbed it but haven't looked inside. For some reason it arrives
here with an extra .tar appended to the filename.

It works for me. (I used "wget" to download it; maybe your browser is
doing something funny.)
The other thing
is why is it so much smaller than the previous?

1-11-05 17:46 70,443 xcpp-0.5.0.tar.gz
1-17-05 21:57 33,307 xcpp-0.5.2.tar.gz

The previous one had an extraneous copy of xcpp-0.5.0.tar.gz within
it. (Presumably not an identical copy, but I didn't check.)
 
C

Chris Croughton

Grabbed it but haven't looked inside. For some reason it arrives
here with an extra .tar appended to the filename.

What are you using to fetch it? I've used both wget and Firefox, both
get it with the correct name.
The other thing is why is it so much smaller than the previous?

Because the previous one contained the previous targzball (34k
uncompressable).
1-11-05 17:46 70,443 xcpp-0.5.0.tar.gz
1-17-05 21:57 33,307 xcpp-0.5.2.tar.gz

I ran your older one with --version (nothing happened) and with -h,
which told me something, but not the version. Neither did -V. :-(

True, --version/-V is one of the bits not implemented yet <g>. Along
with decent help (all it does at present is list the options with no
other information; my C++ help system can handle option types (whether
they accept values etc.), do word wrapping on text for each option,
etc., but I haven't rewritten it in C).

Chris C
 
C

Chris Croughton

It's not there.

Hmm, I don't know why it didn't get there, it got to my other servers
and I didn't see an error from sitecpoy. I've just forced an upload and
verified that I can fetch it from there now...

Chris C
 
C

CBFalconer

Chris said:
Hmm, I don't know why it didn't get there, it got to my other servers
and I didn't see an error from sitecpoy. I've just forced an upload and
verified that I can fetch it from there now...

0.5.4 isn't there, but 0.5.3 is.
 
C

Chris Croughton

R

Richard Bos

Chris Croughton said:
Chris said:
[...]
http://www.keristor.co.uk/stuff/xcpp-0.5.4.tar.gz

It's not there.

Hmm, I don't know why it didn't get there, it got to my other servers
and I didn't see an error from sitecpoy. I've just forced an upload and
verified that I can fetch it from there now...

0.5.4 isn't there, but 0.5.3 is.

I just fetched 0.5.4 using the URL above from my work machine using
Firefox, it's there...

Just FTR, it exists for me as well, using Opera. This means that it's
not just visible from within keristor.whatever.

Richard
 
C

CBFalconer

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top