Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any cure for the preprocessor blues?

I know that I can kick the the preprocessor to spit out output with the -E option in my particular circumstance. For generated code this preprocessor output is murderous. For example I have a 4gl application and Informix converts this into C which in turn gets spit out to a horrible ugly mess.

What I want is an editor that will allow me to specify what preprocessor values are in effect and show me only the relevant code. I have something very basic working in Vim matching #ifdef and #endif, but the code is riddled with more advanced constructs such is #ifndef, #if, and #else. To make matters worse, the constructs are logically more complex, and I don't think my Vim scripting skills are adequate for me to get what I want out of it. For example:

#if DLEVEL > 5
    #define SIGNAL  1
    #if STACKUSE == 1
        #define STACK   200
    #else
        #define STACK   100
    #endif
#else
    #define SIGNAL  0
    #if STACKUSE == 1
        #define STACK   100
    #else
        #define STACK   50
    #endif
#endif
#if DLEVEL == 0
    #define STACK 0
#elif DLEVEL == 1
    #define STACK 100
#elif DLEVEL > 5
    display( debugptr );
#else
    #define STACK 200
#endif

Includes defining an expression evaluator if I want to tackle it. This has to be a solved problem! If you have Vim suggestions or other ones please let me know.

like image 701
ojblass Avatar asked Apr 11 '09 00:04

ojblass


2 Answers

The Eclipse CDT editor does a pretty good job highlighting code based on the macros you declare to be active. You may want to check it out.alt text

like image 189
lothar Avatar answered Nov 08 '22 23:11

lothar


For an editor, Eclipse CDT works quite well. It shows which code is active and which code is #ifdef'ed out, it provides syntax highlighting within code that's #ifdef'ed out so you can still easily read it, and it can step through macro expansions one at a time if you mouse over a macro.

From the command line, cpp -dM filename.c processes a file and shows only the #defines that are in effect. For example, in your code, it might spit out

#define DLEVEL 5
#define SIGNAL 1
#define STACK 200
#define STACKUSE 1

without cluttering the listing with other preprocessor directives or with C/C++ code.

(On a related note, cpp -dM /dev/null is a handy way to find predefined macros for your platform.)

like image 37
Josh Kelley Avatar answered Nov 08 '22 23:11

Josh Kelley