Tool to remove/Apply ifdef's/else's from codebase

六眼飞鱼酱① 提交于 2019-12-01 18:17:32

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!