Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a message in the build output (that is not a warning)

At build time, I'd like to be able to print a message to the build output window without having it be classified as a warning. I.e. In the Visual Studio Error List, I would want it to display only if you had the "Messages" filter turned on.

So, I want identical functionality to #warning Warning Message But I don't want it to be classified as a warning.

Context:

I have a lot of projects that currently generate a lot of warnings. What I'd like to do first is prevent new warnings from being introduced. So, I've:

  1. Turned on "Warnings as Errors"
  2. Gone through and used #pragma warning disable/restore to eliminate existing warnings.

However, for the warnings I disabled, I'd like to print out a message indicating that there is a warning here that needs to be investigated (since I haven't yet investigated what needs to be done for these warnings). For example, many of the warnings are "obsolete" type warnings, where we do need to go and do some work at some point. So, I don't want these warnings to disappear.

Ideally, I'd do something like this:

#pragma warning disable 0618
#message Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618

I'm open to other approaches of dealing with the issue.

like image 514
Matt Smith Avatar asked Dec 14 '12 21:12

Matt Smith


3 Answers

According to MSDN you cannot extend #pragma: http://msdn.microsoft.com/en-us/library/x74w198a.aspx

But if ErrorList isn't strict requirement, you can work around this: you may use comments to highlight things important to you. If you add special token at the begining of your comment, you'll be able to track it in TaskList window.
So, your code will look like:

#pragma warning disable 0618
//TODO: Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618

And after that, if you'll open View -> Task List -> select Comments in dropdown, you'll see your comment there.
There are 3 predefined tokens: TODO, HACK and UNDONE - and you can add your own like MESSAGE and change it's priority to make your code look similar to what you initially expect:

#pragma warning disable 0618
//MESSAGE: Existing Warning: 0618: IObsoleteInterface is obsolete.
class MyClass : IObsoleteInterface
#pragma warning restore 0618

More information about tokens you will find here: http://msdn.microsoft.com/en-us/library/zce12xx2(v=vs.100).aspx

Worth to mention that if you use Resharper, it has another tool for TODO's. I'll find it here: ReSharper -> Tools -> To-do Items

like image 94
Dima Avatar answered Nov 02 '22 07:11

Dima


You should have a look at the csc task's WarningsNotAsErrors parameter. When the list of the "acceptable" warnings are specified to this parameter with TreatWarningsAsErrors=true you should get your existing warnings logged while new ones will show up as errors.

Ofcourse this is not exactly existing versus new warnings but a way to work with exact warning numbers that you are comfortable with leaving in.

But my first suggestion would be to just fix these. Once hidden outside of build they rarely get addressed.

like image 3
allen Avatar answered Nov 02 '22 07:11

allen


If you are using "Premium" or "Ultimate" edition of Visual Studio, it has a feature of code analysis using Rule Set.

For any project when you right click & go to properties, select Code Analysis Tab, it has various out of box settings like treating warning as error, disabling some of the warning.

You can configure warning to be ignored or treated as error. You can use out of box Microsoft rule sets or alternatively you can build a custom ruleset as per your requirements (which is not very difficult & worked for my requirement).

To enable code analysis for managed code:

  1. Select a project in Solution Explorer.
  2. On the Project menu, click Properties.
  3. Click Code Analysis.
  4. Select Enable Code Analysis on Build (defines CODE_ANALYSIS constant).

For details on configuring Code Analysis, please visit: http://seesharper.wordpress.com/2010/04/02/code-analysis-in-team-build-2010/

For building Custom Rulesets:

http://msdn.microsoft.com/en-us/library/dd264974.aspx

http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx

For Learning about Ruleset & code analysis:

http://msdn.microsoft.com/en-us/library/dd264996.aspx

like image 2
Pranav Singh Avatar answered Nov 02 '22 09:11

Pranav Singh