views:

168

answers:

5

We have a large amount of C/C++ code that's compiled for multiple targets, separated by #ifdefs. One of the targets is very different from the others and it's often important to know if the code you're editing is compiled for that target. Unfortunately the #ifdefs can be very spread out, so it's not always obvious which code is compiled for which targets.

Visual Studio's #ifdef highlighting can be helpful for visually identifying which code is compiled for which target, but changing the highlighting apparently requires modifications to the project file.

I'm interested in finding a tool or method that can help coders quickly recognize which targets are using each line of code. Even if it requires some sort of manual in-source annotation I think it could still be helpful. Best case it's automated, not tied to a specific editor or IDE, and it could be configured to warn in certain conditions (eg "you modified some code on Target X, be sure to test your code on that platform!").

+5  A: 

If your code is getting that big that you can't tell what #ifdef your in then it's time to refactor your code. I would recommend that you refactor it into seperate cpp files per platform.

I noramlly only use #idef when the code is only one or two lines long, any longer and I normally refactor into it's only function or class into there own cpp file. That makes it simple to figure out where you are.

Shane Powell
it depends on what is platform-dependent, it's fine to do it this way if you have a clear cut with whole functions that vary...but otherwise, that could quickly become a nightmare to refactor (all the more since you've no tool and potentially a lot of legacy code..)
LB
I do agree that it can be a nightmare to refactor, but I do think it's worth it in the end. Legacy code is the killer... My approach with legacy code is to leave it along until I need to touch it. Once I need to touch it is when I do the refactoring.
Shane Powell
A: 

I don't know if there is a tool for this already, but I'd guess would be fairly easy to roll your own by using the precompiler. Precompile your file with a set of specific #defines and the output is what the compiler sees for that platform. I reckon this is not the same as highlighting the current file, but it can be automated and integrated into your IDE, push a button get a temp file with te current edited one under specific #define. Didn't try it myself, is just an idea.

PS. Yes, I had to read couple times your post to searching for where exactly is 'code coverage' involved lol.

Remus Rusanu
In this case, the problem is to obtain the good set of #defines...this is not really easy to trigger only the part you want...
LB
If there are editors already capable of doing this really no point reinventing the... spare tire
Remus Rusanu
+1  A: 

Check out Visual SlickEdit. The "Selective Display" option might be what you are looking for. I can't find any on-line documentation on it, but it will allow you to essentially apply a set of macro definitions to the code. So you can tell it to show you the code as the compiler will see it with a set of macros defined. This is a lot more than preprocessor output since it literally hides blocks of code that would be excluded based on the macro definitions.

This doesn't give you the ability to answer the question "Under what preprocessor conditions is this line of code included in compilation" though. The nice thing is that it applies the selective display filter to searches and printing.

D.Shawley
how long does the free trial last ? I wasn't able to find it...
LB
SlickEdit trial versions last 30 days - realizing that you cannot live without it takes considerably shorter time. :)
Rune Braathen
@Rune: no doubt there... I can't remember life without SlickEdit and I cringe every time that Visual Studio launches instead.
D.Shawley
A: 

Check XRefactory and Cscout.

LB
+1  A: 

I know for a fact that eclipse cdt does it. It has other nice features and some not-so-nice features for an IDE. Now, I code with vi, so I might be biased.

David Rodríguez - dribeas