suppress warning for generated c# code
Asked Answered
S

3

23

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)

Selfassured answered 23/4, 2010 at 20:15 Comment(1)
@TomTom: Well, I don't see this as a valid solution (and calling him stupid is a bit over the top, the author has both of us beat by 100k points here on SO...)Selfassured
M
10

EDIT: See comments indicating that this doesn't work with C# 4. I'm not clear whether it worked in earlier versions of the compiler. The C# 4 spec makes this pretty clear though. Section 2.5.8.1 states:

A #pragma warning restore directive restores all or the given set of warnings to the state that was in effect at the beginning of the compilation unit. Note that if a particular warning was disabled externally, a #pragma warning restore (whether for all or the specified warning) will not re-enable that warning.

Jeff has a workaround in a blog post - basically to reprocess autogenerated code as part of the build.


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).

Muir answered 23/4, 2010 at 20:26 Comment(5)
@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.Muir
Accepting this as the answer as all others fail to solve the problem of not touching the generated file...nice one, JonSelfassured
@Jon: This doesn't work for C# 4. According to the spec, if the warning is globally disabled (i.e. by the project), restore will restore to that global state (i.e. disabled). I should add, I tried it (I didn't just read the spec).Racquelracquet
@Jeff: Eek. Weird. I wonder whether it genuinely worked before... have edited my answer :(Muir
@Jon: See this blog entry for a workable solution with MSBuild 4. This works for my Windows Workflow auto-generated files (though I made it only disable 1591 rather than all). lvquoc.blogspot.com/2010/11/…Racquelracquet
C
13

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!).

Cule answered 23/4, 2010 at 20:36 Comment(4)
"I have no influence on the actual file generated" - doesn't that rule out this answer?Muir
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.Cule
@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...Selfassured
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?Advocaat
M
10

EDIT: See comments indicating that this doesn't work with C# 4. I'm not clear whether it worked in earlier versions of the compiler. The C# 4 spec makes this pretty clear though. Section 2.5.8.1 states:

A #pragma warning restore directive restores all or the given set of warnings to the state that was in effect at the beginning of the compilation unit. Note that if a particular warning was disabled externally, a #pragma warning restore (whether for all or the specified warning) will not re-enable that warning.

Jeff has a workaround in a blog post - basically to reprocess autogenerated code as part of the build.


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).

Muir answered 23/4, 2010 at 20:26 Comment(5)
@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.Muir
Accepting this as the answer as all others fail to solve the problem of not touching the generated file...nice one, JonSelfassured
@Jon: This doesn't work for C# 4. According to the spec, if the warning is globally disabled (i.e. by the project), restore will restore to that global state (i.e. disabled). I should add, I tried it (I didn't just read the spec).Racquelracquet
@Jeff: Eek. Weird. I wonder whether it genuinely worked before... have edited my answer :(Muir
@Jon: See this blog entry for a workable solution with MSBuild 4. This works for my Windows Workflow auto-generated files (though I made it only disable 1591 rather than all). lvquoc.blogspot.com/2010/11/…Racquelracquet
V
2

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.

Vaginitis answered 23/4, 2010 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.