I had this kicking around on my hard drive, so I thought I ‘d throw it out there. It’s not exactly a big secret, but it certainly amused me when I wrote it. Use CWARNING and CERROR to add compile-time items to the Error List window in Visual Studio.
This is Visual Studio specific, but can be adapted to work with other compilers and environments. The use of __pragma appears to be Microsoft-specific, but the C99 and C++11 standards introduce the _Pragma keyword.
#ifndef _COMPILEWARNINGS_H_
#define _COMPILEWARNINGS_H_
#define __NUMTOSTRINGHELPER2(x) #x
#define __NUMTOSTRINGHELPER(x) __NUMTOSTRINGHELPER2(x)
#define __FILE_AND_LINE__ __FILE__"(" __NUMTOSTRINGHELPER(__LINE__) ")"
#define __ADDWARNING__(msg) __FILE_AND_LINE__" : warning : " msg
#define __ADDERROR__(msg) __FILE_AND_LINE__" : error : " msg
// edit 2014-24-02 - CINFO is not correct...still looking for the answer here
#define __ADDINFO__(msg) __FILE_AND_LINE__" : info : " msg
#define CWARNING(msg) __pragma(message(__ADDWARNING__(msg)))
#define CERROR(msg) __pragma(message(__ADDERROR__(msg)))
#define CINFO(msg) __pragma(message(__ADDINFO__(msg)))
#endif // _COMPILEWARNINGS_H_
CWARNING("This is just a warning");
CERROR("If you get this error, call for help and hide under your desk!!");

Leave a Reply