Apr 182011
 

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!!");

  7 Responses to “Quickie: Add clickable messages to your Visual Studio Error List Window”

  1. Hello Jason, thanks for sharing this content.

    However, how can I use it? I’m trying to add the above code block to my class and then use the C__ functions, but VS does not recognizes the __FILE__ funcion.

    Can you also, please, share a little example of the use?

    Thanks in advance.

  2. Hi Paulo,

    The intent of the code in the article is that you would put it in its own header file and #include it when you need it. Alternatively, you could just #include it from a common header or a PCH.

    Then, whenever you want to flag an important situation or even leave yourself an important note, you can get the compiler to nag you about it. It also let’s you click on the message to go right to the problem area. Obviously, it would be bad to use this all over the place, but uses to come up from time to time.

    For example, you might have this in a file that adds some debugging functions:

    #ifdef RELEASE_BUILD
    CWARNING(“Blergh! We’re compiling diagnostic code into a release build. Fix this before we ship!!”);
    #endif

    …or perhaps you’ve seen an occasional bug that you can’t fix right now, but you don’t want to forget about it.

    void function()
    {
    CERROR(“Hey Julie, remember to fix the edge case in here. See bug report #123″);
    /* lots of important code with a subtle bug */
    }

    __FILE__ and __LINE__ are standard preprocessor macros in C and C++. They should just work without the need for additional includes.

  3. Please send me full details of this! Still i dont no how to use this code, I am using C#.net. using C# how i can add clickable messages to your Visual Studio Error List Window. please help me out of soon.

    • Hi Nasir,

      This code is C/C++. I’m not really a C# guy, so I don’t know what the analogue is for that language.

      Sorry.

  4. This is really nifty. Thanks for sharing.

    Do you know how to make info messages show up in the messages filter of the Error List? Currently they only show up for me in the output window, which for large projects means they are effectively hidden.

  5. Hi Phil,

    Yeah, that’s something I always intended to get back to as the CINFO case is definitely broken. The Error and Warning cases are obviously just a regex matches for “FileName(Line) : error/warning : MessageText“. The Messages case might be similar, but I don’t know the nuance of it. On the other hand, the message case might be special and only accessible with calls through the Visual Studio extension API. For example http://blog.mastykarz.nl/showing-messages-extending-visual-studio-sharepoint-development-tools-tip-6/

    -Jason

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>