Preprocessor for removing conditional code only?

Preprocessor for removing conditional code only?

Post by Mark Summerfiel » Sat, 06 Mar 1999 04:00:00



Does anyone know of a program (or a way of using the standard C
preprocessor) which can remove unneeded conditionally compiled code from
source files?

I.e. suppose I have some large number of source files with sections
of code surrounded by '#if defined(DEFINEx) ... #endif', for some
number of "defines", I would like to preprocess the code so that
all conditional compilation is eliminated.  I need to be able to
define some conditions, and not define others so that:

1) for defined conditions, the "#if ..." and "#endif" lines are removed,
   but the conditional code in between is left

2) for undefined conditions, the entire block of conditional code,
   including the #if and #endif lines, is removed.

The preprocessor should not read in include files (all required
definitions are currently provided with -D command-line switches),
perform macro substitution, remove comments, or perform any other
reformatting.  It does not need to understand any preprocessor
directives other than the simple #if format shown above (and I use
"#if defined(...)" exclusively, never #ifdef).  But it does need to
be able to handle nested #if's.  It's safe to assume that the real
C preprocessor and compiler have already successfully parsed the
code.

I have considered writing a PERL script to do the job -- with recursion,
even handling the nested #if's is probably straightforward.  But I
thought maybe I'm not the first person to want to do this.

Thanks in advance for any tips.

Mark

 
 
 

Preprocessor for removing conditional code only?

Post by Felix Schroet » Tue, 16 Mar 1999 04:00:00


Hello!



Quote:>Does anyone know of a program (or a way of using the standard C
>preprocessor) which can remove unneeded conditionally compiled code from
>source files?

unifdef(1).

Quote:>[...]

Regards, Felix.

 
 
 

Preprocessor for removing conditional code only?

Post by Mark Summerfiel » Wed, 17 Mar 1999 04:00:00



> unifdef(1).

Wow!!  Thanks, Felix -- I never knew this existed.  Unfortunately it
can't handle the "#if defined()" syntax, but a quick grep through my
code shows that in 99% of cases awk can handle the conversion to #ifdef's.
I'll convert the small number of boolean expressions into nested #if's
by hand.

Mark

 
 
 

1. Sys V/BSD "ps" newbie g++ preprocessor conditional compilation?

I'm writing a poriton of a program which needs to know what flavor of
unix is running so that it can properly format a "ps" command to be
used through "popen" depending on Sys V or BSD style PS.

In g++, is there a predefined preprocessor conditional I can test for
and use to conditionally compile the proper "ps" command?

If not, is there any reliable way to programmatically test for the
operating system and use the results to format the proper ps command?

If not, then, is there a reasonable way to approach this problem?

Thanks in advance!

remove "-" to "-" if replying via e-mail.

2. 3590 boot tape

3. Assembler and Conditional Compilation (C Preprocessor)

4. Lyrix-1.05

5. Preprocessor and C++, and commented out debugging code.

6. IBM JDKs, Sun JDK

7. Conditional access to code

8. best way to search for multiple strings in ps -elf output (Solaris 2.6)

9. remove kernel 2.0 and 2.2 code from drivers/net/wan/lmc/*

10. Removing source codes

11. remove kernel 2.0 compatibility code from i91uscsi.c

12. remove #if'd kernel 2.0 code from drivers/media/video/*

13. HFS cleanup #1 - remove partition code