Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force warning/error

I want to put some warning or error into my code. I am using visual studio 2010.

I used #error and #warning in Xcode, but visual studio doesn't know those directives.

like image 665
SerbanLupu Avatar asked Jun 22 '11 10:06

SerbanLupu


1 Answers

After some searching through different articles, I finally came to this solution which works in Visual Studio 2010:

#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define __MESSAGE(text) __pragma( message(__FILE__ "(" STRINGIZE(__LINE__) ")" text) ) 
#define WARNING(text) __MESSAGE( " : Warning: " #text )
#define ERROR(text) __MESSAGE( " : Error: " #text )
#define MESSAGE(text) __MESSAGE( ": " #text )
#define TODO(text) WARNING( TODO: text )

and you can use it as:

WARNING( This will be a compiler warning );
ERROR( This will be a compiler error );
MESSAGE( Well this is what I have to say about this code );
TODO( Still have to fix 3D rendering );

Note that TODO() will also generate a compiler warning; if you don't want to register your TODOs as warnings just use this instead:

#define TODO(text) MESSAGE( TODO: text )

If you want to display function name inside warnings/errors/TODOs, use this instead:

#define WARNING(text) __MESSAGE( " : Warning: (" __FUNCTION__ "): " #text )
#define ERROR(text) __MESSAGE( " : Error: (" __FUNCTION__ "): " #text )
#define MESSAGE(text) __MESSAGE( ": (" __FUNCTION__ "): " #text )
#define TODO(text) __MESSAGE( " : Warning: TODO: (" __FUNCTION__ ") " #text )
like image 114
vedranm Avatar answered Sep 30 '22 02:09

vedranm