Visual Studio: Custom warnings for TODOs, Code Reviews etc.

Visual Studio can sometime surprise you!

The number of TODOs I’ve seen strewn throughout the code-base — sometimes as well-thought out comments, sometimes less so, or a code review request that I forgot to get done (and the reviewer forgot to get me to get it done) — was bugging me for the last few months. So, I was out on a stroll down the Google alley. And I landed, with some trouble, on a MSDN KB article that just answered my quest!

Those who find it tedious to punch in `#pragma` directives every-time they need to put a bookmark in the code: You can save a few keystrokes by whipping up a macro to do this for you! While in general, you cannot have a `#pragma` directive within macros, MS C/C++ compilers do support a special vendor-specific extension called the `__pragma` which can be used with macros. See Pragma Directives and the __Pragma Keyword.

I use something akin to the following on a daily basis:

#define STR2(x) #x
#define STR1(x) STR2(x)
#define LOC __FILE__ “(“STR1(__LINE__)”) : Warning Msg: ”
#define WARNING_BUILDER(x) __FILE__ “(“STR1(__LINE__)”) : Warning Msg: ” __FUNCTION__ ” requires ” #x
#define WUT WARNING_BUILDER(unit-testing)

#define MARK_FOR_REVIEW() do { \
__pragma(message( WREVIEW )) \
} while (0)

#define MARK_FOR_UNIT_TEST() do { \
__pragma(message( WUT )) \
} while (0)

// uncomment/set in build-environment to enable special warnings
// uncomment/set in build-environment if you want only code review warnings
// uncomment/set in build-environment if you want only unit-test warnings

int main()

You can easily extend it to suit your needs and add more warnings. The good part of having such a system is that you can selectively turn-on say, only code-review items and not have to worry about anything else by setting the appropriate macro in the build settings.


About this entry