开发者

Ignoring generated files when using "Treat warnings as errors"

开发者 https://www.devze.com 2022-12-28 12:44 出处:网络
We have started a new project but also have this problem for an existing project. The problem is that when we compile with a warning level of 4 we also want to switch on

We have started a new project but also have this problem for an existing project. The problem is that when we compile with a warning level of 4 we also want to switch on

'Treat all warnings as errors'

We are unable to do this at the moment because generated files (in particular reference.cs files) are missi开发者_StackOverflowng things like XML comments and this generates a warning, we do not want to suppress the xml comment warnings totally out of all files just for specific types of files (namely generated code).

I have thought of a way this could be achieved but am not sure if these are the best way to do this or indeed where to start :) My thinking is that we need to do something with T4 templates for the code that is generated such that it does fill in XML documentation for generated code.

Does anyone have any ideas, currently I'm at well over 2k warnings (its a big project) :(


You can selectively disable warnings with a pragma:

// Disable warning messages 4507 and 4034.
#pragma warning( disable : 4507 34 )

If you can emit such warnings (or an #include) in the generated code files, you're done.

Alternatively, you can disable them globally on the command-line for the compiler:

/wd4326 disables compiler warning C4326.

Then re-enable them (via a header file) in the files you want them for:

// Report warning 4326 as an error.
#pragma warning( error : 326 )

Finally, you can set different compile options for each source file by altering the Properties in the project file. Personally I find that a maintenance nightmare, but there are times you have no choice.

Edit: I see that your source files are C#, not C++.

Using the C# command-line:

to suppress CS0028, you could specify /nowarn:28.

Unfortunately, /warnaserror makes all warnings errors.


I've written a PowerShell script that calls svcutil and then wraps the auto-generated code with the #pragma directives to ignore the missing xml, but still allows me to regenerate as needed.

$outFile = 'generatedCode_fromSVCUTIL.cs'
svcutil '..\XML Schema\myXsd.xsd' /dataContractOnly /n:'*,MyNamespace.GeneratedCode' /language:C#  /importxmltypes /out:$outFile

# ----------------------------------------------------- 
# Exempt this file from XML documentation requirements

Write-Host 'Wrapping ', $outFile, ' in #pragma 1591 flags' 
$a = Get-Content $outFile 

# Set up pragma lines for enabling and disabling the XML doc warning
$disableWarning = '#pragma warning disable 1591'
$restoreWarning = '#pragma warning restore 1591'

# wrap the generated code in the pragma tags
Set-Content $outFile –value $disableWarning, $a, $restoreWarning 
Write-Host 'Done.' 


In VS 2010 you can right-click on the service reference, select 'Configure Service Reference...' and change the access modifier from Public to Internal.
This may of course not be appropriate for your particular solution but the warnings are not applicable to Internal methods and you can still re-generate the service reference.


For C# you can simply place a

#pragma warning disable 1591

at the beginning of the reference.cs file. Then the warning concerning missing XML documentation will not be issued.

But you have to do this every time, the file is regenerated (i.e. when your service definition changes). I'm not aware of any way to influence the code generation (I'm not sure if they use T4 templates or where these might be located ...)


A couple of thoughts.

1) Do you have the autogenerated tag in your file header (comments at the top of the file), like this:

// <auto-generated>

// This file is auto-generated...

// </auto-generated>

This tag is important (the contents are not), as some tools will skip such files (e.g. StyleCop can be configured to ignore these files).

2) If you are autogenerating code why not autogenerate at least some XML comments? I can understand that you don't want to spend a lot of time documenting code that probably won't ever be read, but when debugging code I often find myself dropping in to some autogenerated proxy and even a simple comment can be helpful, even if it just says "autogenerated code" :)

Edit

3) You can also suppress warnings by adding the pragmas to the build options (right click on the project, choose properties, choose the Build tab). This is more convenient than adding to code. Try adding 1591;1574;1587 to the Suppress Warnings box.

4) You could go in to the Code Analysis tab in the Project Properties and uncheck "Treat Warning as Error" for specific warnings that are causing you problems.

Obviously both these are global settings, they don't just pick on the autogenerated files.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号