Tool/Parser for preprocessor #if statements?

2019-03-20 08:39发布

问题:

I am working on many C-sourcecode files which contain many preprocessor #if, #elseif and #else statements.

This statements often check for a #define, e.g.

#if(Switch_TestMode == Switch_TestModeON)
/* code 1 */
#else
/*code 2 */
#endif

Often this preprocessor statements are located within c-if statements which makes the sourcecode nearly unreadable for human beeings.

The #defines used for this preprocessor #if statements are defined within an extra file.

My idea now is to have a tool which checks this #defined switch settings and then only copies the lines of sourcecode which apply using the current #defines/switch settings.

For the above example I would like to get a new .c file which contains only

/*code 2 */

assumed the #define of Switch_TestMode is not equal to Switch_TestModeON.

Are there tools (freeware || low-cost) available which do this job? Or do I have to write my own preprocessor parser for this?

(It is impossible for me to run the compiler using a special parameter which does this job, because our company is creating the C-sourcecode, another company is compiling.)

Thanks for any hint!

Regards

Thomas

回答1:

unifdef is available from http://dotat.at/prog/unifdef/.



回答2:

Try Sunifdef

Edit: Which has now become Coan



回答3:

You can run the GNU compiler using command line option -E to do the preprocessing.

http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html



回答4:

You can use unifdef.