views:

292

answers:

3

I have turned on "Treat warnings as errors" for my VS project which mean that I get errors for missing documentation (nice reminder for this particular project).

However, part of the code is generated by a custom tool which does not insert xml documentation so I'm looking for away to ignore the missing xml documentation for the generated code only, not for the entire project. I have no influence on the actual file generated and cannot really insert anything in the file (as it is regenerated frequently by the tool) so I looking for something existing outside the generated file (the classes that are generated are partial, if that helps)

A: 

The best you will be able to do in this is suppress the particular warning in the project that contains the generated code. You can do this in the Project Properties on the Build tab. It's not ideal, but short of putting the pragmas in the generated code, it is the best you can do.

Tom Cabanski
+1  A: 

As Tom says, you can add an "ignore" to the whole project (Build / Suppress Warnings - enter 1591 as the warning number) - but then you can restore the warning yourself at the top of each of your non-generated files:

#pragma warning restore 1591

It's pretty ugly, but it works (I've just tested it).

Jon Skeet
Excellent work-around. I love it!
Tom Cabanski
@TomTom: In what way? You can fairly easily automate a check that you've got that pragma at the start of each non-generated file. (The OP may well have something already to check for copyright statements etc - that could very easily be modified to include that pragma.) The end result is what the OP wants: errors for hand-written files that don't have XML comments, and no such errors for the generated files.
Jon Skeet
Accepting this as the answer as all others fail to solve the problem of not touching the generated file...nice one, Jon
soren.enemaerke
+1  A: 

The best way to avoid having warnings or Code Analysis error on generated code is to decorate your generated classes with the GeneratedCodeAttribute and make the code file ends with the *.generated.cs pattern.

If your code files also have a file header, you should had these tags:

//----------------------
// <auto-generated>
//     Tool description
// </auto-generated>
//----------------------

This is not mandatory, but if you have code file header it is a good practice.

This way, FxCop and other tools like StyleCop will not analyse your code anymore.

What is abnormal is that your code generation tool is not decortating your code elements with the attribute mentioned above. Try to look if there is an option to enable in your tool settings or contact the developing team.


EDIT: Does the generated classes are partial classes and do the actual classes name and number changes often? Because if the generated code content is not moving a lot, what you can do is simply create another code file and just declare the generated partial class to decorate them with the GeneratedCodeAttribute. One time it saved my life (and my time!).

Ucodia
Grats, first post that is from soenone not talking crap. Gratulations and +1.
TomTom
"I have no influence on the actual file generated" - doesn't that rule out this answer?
Jon Skeet
I understand what the problem is, I had exactly the same problem with a DSL coming from Codeplex. The fact is that if it is often regenerated, you do not want to manually modify it everytime. What I done is that I contacted the Codeplex developer that fixed the problem. There is actually two solutions: looking for an setting to enable in the tool or contact the developing team.
Ucodia
@TomTom(and @TheRHCP): as Jon points out, I can't control the generated code so no go on adding attributes. Also, partial classes may solve missing comments on class, but won't help my on properties/methods...
soren.enemaerke
I'm having a similar issue with the code generated by WCF RIA. All of the *.g.i.cs generated files have <auto-generated> in their file headers, but the VS 2010 code analysis tool is still tripping over them. Any suggestions?
David Moye