S h o r t S t o r i e s

// Tales from software development

Suppressing analysis of generated code

leave a comment »

As a follow-up to the posting Using the XSD Inference and XSD Object Code Generator Tools, how do you suppress analysis of the generated code ?

As the generated code often doesn’t follow Microsoft’s own guidelines, tools such as StyleCop, FxCop, and the code analysis built in to Visual Studio (which is a variant of FxCop but is not identical to the standalone version of FxCop).

StyleCop analysis may be suppressed using the <auto-generated> element.

Visual Studio’s Code Analysis may be suppressed using the disable warnings pragma: #pragma warning disable.

FxCop analysis suppression requires the use of the GeneratedCodeAttribute at type or method level. Generated code files often contain multiple types making it difficult to suppress FxCop analysis by simply placing a directive at the beginning of the file. I’d recommend using the FxCop GUI to disable analysis of generated code by simply unchecking the code that you don’t want to analyze.

So, all you need to do to suppress analysis of generated code is automate a mechanism to insert the following two lines at the beginning of each file:

<auto-generated />
#pragma warning disable

I’ve implemented this as a step in the build that renames the generated source file, copies a file containing the two lines and the renamed file to a new copy of the source, and deletes the original source file. Three DOS commands are used:

REN "%FILENAME%.cs" *.temp
COPY /Y /B "generated_code_prolog.txt" + "%FILENAME%.temp" "%filename%.cs"
DEL "%~dp0..\%FILENAME%.temp"

Obviously, you’ll need to set the FILENAME environment variable before these three lines are executed and create a file called generated_code_prolog.txt containing the two lines that will suppress code analysis.


Written by Sea Monkey

January 8, 2009 at 8:00 pm

Posted in Development

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: