I have a pretty big codebase and I wanted to clean it out by removing and applying some ifdef's scattered around it. For example, I have lot's of these:
test.c
#ifdef MYCHECK
// do other sutff
#else
// do stuff
#endif
Is there a tool that allows me to run through the entire codebase and remove all that code, leaving only the code inside my variable condition? For example:
nicetool -D MYCHECK *.c
Would result in:
test.c
// do other stuff
It looks like unifdef is what you want, it is also used in the Linux kernel. This is the description of the tool from the linked site (emphasis mine):
The unifdef utility selectively processes conditional C preprocessor #if and #ifdef directives. It removes from a file both the directives and the additional text that they delimit, while otherwise leaving the file alone.
It is useful for avoiding distractions when studying code that uses #ifdef heavily for portability: my original motivation was to understand xterm's pty handling code. It can be used as a lightweight preprocessor; for example the Linux kernel uses unifdef to strip out #ifdef KERNEL sections from the headers it exports to userland. You can use unifdef with languages other than C; for example UIT, a publisher in Cambridge where I live, uses unifdef with LaTeX.
If you check out the manual there are some exceptions listed in the BUGS section:
Handling one line at a time means preprocessor directives split across more than one physical line (because of comments or backslash-newline) cannot be handled in every situation.
Trigraphs are not recognized.
There is no support for macros with different definitions at different points in the source file.
The text-mode and ignore functionality does not correspond to modern cpp(1) behaviour.
Other options include Sunifdef whose main site no longer is available and has not been updated since 2008 and Coan: The C Preprocessor Chainsaw which describes itself as:
Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration. Dead code removal is an application of this sort.
Coan is most useful to developers of constantly evolving products with large code bases, where preprocessor definitions and #if-directives are used differentiate progressive releases or parallel variants of the product. In these settings the upkeep of the product's configuration tree can become difficult and the incidence of configuration-related defects can become costly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With