views:

688

answers:

6

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.

+9  A: 
lothar
Nice linux theme! What is that one called?
Lucas McCoy
I'm running on KDE (Kubuntu)
lothar
I liked Kubuntu, but had to uninstall it because my PC sucks and only has 60 GB of HDD space (that's not alot when you count Vista, 2 GB of Music, 3 IDE's, and lots of other crap I should probably uninstall).
Lucas McCoy
@Aardvark I know it's off topic, but how about installing it on an external HD?
lothar
@lothar: I don't have one. I've really been meaning to buy one but I don't have the money (I'm still in high school, which explains the sucky PC). When I can I do plan on buying one (well that and a Mac ;-).
Lucas McCoy
i would pick this one but i don't want it to highlight code but remove dead code.
ojblass
+1 on CDT. You can open your code, place the cursor over a macro and a window will popup with the real definition of the macro with your defined preprocessor tokens (just like if the preprocessor was run just on that macro).
David Rodríguez - dribeas
@ojblass What do you mean with remove? Just don't see it? CDT allows to fold the inactive code away (collapses to 1 line). If you want real removal, then using the C preprocessor is the best option.
lothar
+3  A: 

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.)

Josh Kelley
+1 for awesome command line syntax!
Lucas McCoy
+1  A: 

If you're a Linux user and you also use GNOME then I would have to recommend GEdit. I really loved it after I installed some plugins for Intellisense, file browsing, etc... If you're not running GNOME but are still using Linux (KDE) you might be able to use Kate. I didn't play with it for too long so I don't have much to say about that. GEdit will work in KDE but it won't look right.

If you're using Windows and have a really good PC then try Netbeans (yeah it's for C++ also, not just Java). If not there's always Visual C++ 2008 (still pretty RAM dependent though).

I'm not sure about Mac as I can't afford one, but Netbeans works on it also (as well as Linux).

Lucas McCoy
+2  A: 

Consider looking at "Son of Unifdef", cited in the answer to Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?. This isn't a visual editor - it would, however, take C code (or code with C preprocessor directives in it) and generate a modified file which you could then compare with the original.

I'm curious about the mention of Informix 4GL (I4GL). The C code it generates is not, AFAIK, laced with #ifdef constructs. At least, the output was not allowed to contain any when I was in charge of it. (Which version of I4GL are you using?) There are lots of #line entries; those permit you to chase the generated code back to the corresponding I4GL source. I have a script that converts those into simple C comments so that I can use a debugger (gdb or dbx or ... perish the thought, sdb or adb) on the compiled code. And I have also seen preprocessors (both cpp and m4) used to generate I4GL source code for submission to the I4GL compiler.

Jonathan Leffler
We are using version version 2.95 and version 3.5 of the informix client SDK; however, it is highly customized for our environment.
ojblass
The preprocessed code is unattractive for many other reasons (even with the line numbers). Looking at my small universe of code is much easier to digest. As I look more closely all of it seems to come from our own crappy header files.
ojblass
Some of them enable or disable features depending on versions of the compiler since we have to support a non uniform environment (Informix 7 engine here, Informix 9 engine here, Informix 10 engine there. Please kill me.
ojblass
The good news is you should be encouraging everyone to get off IDS 7.31 and IDS 9.40 since both of those are about to go out of support from IBM (9.40 in April, 7.31 in September). CSDK 3.50 is current; I don't think there was a 2.95, but it doesn't very much matter. They're all rather similar.
Jonathan Leffler
And I agree that what I see looks more like code from your headers than stuff from I4GL per se. I4GL generated C code - like most generated code - is not really designed for human consumption, just for compilers.
Jonathan Leffler
+1  A: 

Emacs has hide-ifdef-mode (I've never had to use it myself, so I can't vouch for its quality)

Arkadiy
A: 

You can also try unifdef which is rather simpler than sunifdef.

Tony Finch