Wickoo
Wickoo

Reputation: 7345

How to apply a C preprocessor only to certain (#if/#endif) directives?

I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a C++ source file and only process the conditional directives #if #endif etc. I would like other directives to stay intact in the output file.

I'm doing some analysis on C# code and there is no C# pre-processor. My idea is to run a C preprocessor on C# file and process only conditionals. This way for example, the #region directive, will stay in the file, but cpp appears to remove #region.

Upvotes: 1

Views: 873

Answers (4)

gaDZella
gaDZella

Reputation: 71

Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one: https://github.com/gaDZella/undefine.

The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.

Upvotes: 1

Jim Balter
Jim Balter

Reputation: 16406

The linux unifdef command does what you want:

http://linux.die.net/man/1/unifdef

Even if you're not on linux, there is source available on the web.

BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?

Upvotes: 1

rici
rici

Reputation: 241671

You might be looking for a tool like coan:

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.

It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.

Upvotes: 2

radar
radar

Reputation: 13425

You can use g++ -E option to stop after preprocessing stage

-E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output

Upvotes: 0

Related Questions