views:

202

answers:

5

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 missing 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) :(

A: 

You can set properties of individual source files, including compiler options for individual source files. Those will overrule properties inherited from the project level.

Windows programmer
Really? How????
erikkallen
I did comment on the above but yes how is this achevied, could you give an example please.
krystan honour
Sorry, I'm not sure if this can be done for C#. For C++, in the Solution Explorer window, you can right-click the filename of an individual file and set its properties. Sometimes I need to compile one source file with different options from others, and this works at least in C++.
Windows programmer
yes that is not possible with c# i'm afraid. (not in an obvious right clicky way)
krystan honour
+4  A: 

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.

egrunin
Interesting, you say as does (Windows programmer) below that you can alter the properties on a per source file basis in the .csproj properties, I cannot find how this is acheived. is this done in the Suppress warnings: text box?, to be honest I pretty much want to ignore generated code.
krystan honour
I'm afraid you'll have to use the C# command-line compiler (csc.exe).
egrunin
The idea is that once you've added the #pragma warning disable, you're set. You just have to update the code generator once. Do you have to option of changing the code generator?
Pieter
A: 

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

MartinStettner
yes thats rather the issue Its a maintainance nightmare having to do it every time, again it looks useful but I would want that to be put in every time if that were possible.
krystan honour
A: 

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.

Steve Haigh
I'm afraid this does not solve the issue, visual studio when generating the documentation appears to ignore the fact these headers exist.
krystan honour
OK, added anither thought to my list:)
Steve Haigh
I fail to see why this attracted down vote, could the downvoter at least have the courtesy to leave a comment saying what was wrong with this response?
Steve Haigh