<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David DeWinter &#187; Code Analysis</title>
	<atom:link href="http://davedewinter.com/category/code-analysis/feed/" rel="self" type="application/rss+xml" />
	<link>http://davedewinter.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 09 Aug 2011 21:05:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>PostSharp RC2 Released (And Code Analysis&#8230;)</title>
		<link>http://davedewinter.com/2008/02/24/postsharp-rc2-released-and-code-analysis/</link>
		<comments>http://davedewinter.com/2008/02/24/postsharp-rc2-released-and-code-analysis/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 16:29:21 +0000</pubDate>
		<dc:creator>David DeWinter</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[MSBuild]]></category>
		<category><![CDATA[PostSharp]]></category>
		<category><![CDATA[Visual Studio Team Suite]]></category>

		<guid isPermaLink="false">http://blogs.rev-net.com/ddewinter/?p=50</guid>
		<description><![CDATA[Gael Fraiteur has released another release candidate of PostSharp, an Aspect-Oriented Programming solution for .NET. In my previous post on PostSharp, I discussed a way to work around some pesky Code Analysis errors that PostSharp incurs as a result of &#8230; <a href="http://davedewinter.com/2008/02/24/postsharp-rc2-released-and-code-analysis/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Gael Fraiteur has released another <a href="http://www.postsharp.org/blog/2008/02/announcing-postsharp-10-rc-2.html">release candidate</a> of <a href="http://www.postsharp.org/">PostSharp</a>, an Aspect-Oriented Programming solution for .NET. In my previous <a href="http://blogs.rev-net.com/ddewinter/?p=30">post</a> on PostSharp, I discussed a way to work around some pesky Code Analysis errors that PostSharp incurs as a result of its post-compilation processing.</p>
<p>Unfortunately, I don&#8217;t believe adding the CompilerGeneratedAttribute (CGA), which was done before RC2&#8242;s release, to PostSharp-specific code fixed all of the Code Analysis warnings that our team received. After upgrading to RC2, Code Analysis warned us specifically about initializing our static fields inline in many of our classes and removing the explicit static constructors, as well as the cyclomatic complexity of some of our methods. It was the fact that we <em>did</em> initialize our static fields inline that tipped us off about the same problems that caused me to write my first <a href="http://blogs.rev-net.com/ddewinter/?p=30">PostSharp and Code Analysis</a> post. The problem is that adding the CGA is not comprehensive enough to suppress all code analysis violations. For example, PostSharp effectively injects code at certain points in a program execution (join points in a method). You wouldn&#8217;t want the CompilerGeneratedAttribute applied to that code, because it could suppress other rule violations that come from <em>your code</em>, not PostSharp&#8217;s code. Fortunately PostSharp does <em>not</em> apply the CGA in those circumstances, but there needs to be a middle ground such that PostSharp code is totally ignored by code analysis.</p>
<p><span id="more-84"></span></p>
<p>Looking ahead, I&#8217;m not sure what the best solution for the PostSharp team is on this. I know it&#8217;s low priority compared to other functionality, but as an end user it is frustrating to filter rule violations based on what PostSharp generates. The easiest approach, in my opinion, would be for PostSharp to preserve the assembly the compiler produces. That way, Visual Studio Team Edition for Developers (or Visual Studio Team Suite) can perform static code analysis on those assemblies, and FxCop can have an assembly on disk to analyze.</p>
<p>Since there were some changes in the targets file that PostSharp uses, I had to make a new version of the targets file I showed last time. This time, however, I took the opportunity to fix a bug that the old targets file introduced, which ran code analysis on PostSharp-affected code in certain circumstances.</p>
<p>Last time after using the new targets file, if you ran code analysis (using, for example, the context menu commands &#8220;Run Code Analysis&#8221; or &#8220;Rebuild&#8221; if code analysis was enabled) on a project, then everything would run as expected. Code analysis would show only warnings from the assembly before PostSharp &#8220;touched&#8221; it. The problem was that Code Analysis (CA) would run, and then PostSharp would overwrite the assembly that it analyzed. If the project were built again without any modifications to any file in the assembly, then CA would see that the assembly changed (because PostSharp overwrote it during the last build), and thus would run again.</p>
<p>The new solution takes advantage of the fact that PostSharp also stores the assembly that contains AOP-processed code in the obj$(Configuration)PostSharp directory. This is new process:</p>
<ol>
<li>Compiler runs and places assembly and PDB in obj$(Configuration) folder.</li>
<li>Code Analysis runs on obj$(Configuration) DLL.</li>
<li>PostSharp disassembles and reassembles DLL in the obj$(Configuration) folder, placing result DLL and PDB in the obj$(Configuration)PostSharp folder. Note that PostSharp <em>does not</em> ovewrite the obj$(Configuration) DLL and PDB.</li>
<li>CopyFilesToOutputDirectory copies files from obj$(Configuration) to bin$(Configuration), including the DLL and PDB. Obviously, we want the PostSharp code, not the code created straight from the compiler.</li>
<li>CopyPostSharpFilesToOutputDirectory (a new MSBuild target) copies the DLL and PDB from obj$(Configuration)PostSharp to the bin$(Configuration) folder.</li>
</ol>
<p>This process leaves the obj$(Configuration) DLL and PDB as the one that the compiler created. This gives CA a chance to run on an &#8220;untouched&#8221; assembly, both inside and outside the build process.</p>
<p>The latest targets file is available here: (Remember to rename the extension to .targets instead of .targets.xml)</p>
<p><a href="http://dev.rev-net.com/blog/ddewinter/wp-content/uploads/2008/02/postsharp-10targets.xml" title="PostSharp-1.0 Targets (RC2)">PostSharp-1.0 Targets (RC2)</a> &#8211; Copy to your %PROGRAMFILES%MSBuildPostSharp directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://davedewinter.com/2008/02/24/postsharp-rc2-released-and-code-analysis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PostSharp + Code Analysis = Abomination</title>
		<link>http://davedewinter.com/2008/01/15/postsharp-code-analysis-abomination/</link>
		<comments>http://davedewinter.com/2008/01/15/postsharp-code-analysis-abomination/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 01:25:33 +0000</pubDate>
		<dc:creator>David DeWinter</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[MSBuild]]></category>
		<category><![CDATA[PostSharp]]></category>
		<category><![CDATA[Visual Studio Team Suite]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[Orcas]]></category>
		<category><![CDATA[Targets]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://dev.rev-net.com/blog/ddewinter/?p=30</guid>
		<description><![CDATA[(If you just want the targets files, click here.) Are you using PostSharp for your .NET development? Do you get frustrated when code analysis within VSTS reports warnings like these: Now PostSharp is a superb AOP tool for .NET, but &#8230; <a href="http://davedewinter.com/2008/01/15/postsharp-code-analysis-abomination/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>(If you just want the targets files, click <a href="http://blogs.rev-net.com/ddewinter/?p=30#targets">here</a>.)</p>
<p>Are you using <a title="PostSharp - Bringing AOP to .NET" href="http://www.postsharp.org/">PostSharp</a> for your .NET development? Do you get frustrated when code analysis within VSTS reports warnings like these:</p>
<p style="text-align: center" align="left"><img alt="Code Analysis Warnings" src="http://blogs.rev-net.com/ddewinter/wp-content/uploads/2008/01/postsharp_ca_warnings.png" /></p>
<p>Now PostSharp is a superb AOP tool for .NET, but it is annoying to see these issues in code analysis again and again; what would be great is if we can edit the build process to make Code Analysis (CA) run <em>before</em> PostSharp processes the assembly. That way, code analysis errors on PostSharp-generated code is ignored, and everyone is happy. <img class="wp-smiley" alt=":)" src="http://blogs.rev-net.com/ddewinter/wp-includes/images/smilies/icon_smile.gif" /></p>
<p> <span id="more-80"></span><span id="more-30"></span>
</p>
<p>It can take a while to parse through all the targets in Microsoft.Common.targets, Microsoft.CodeAnalysis.targets, and PostSharp-1.0.targets, but here&#8217;s the general order of what&#8217;s happening with a fresh PostSharp installation.</p>
<ul>
<li>&#8230; </li>
<li>Compile
<ul>
<li>&#8230; </li>
<li>CoreCompile (Delegated to a specific language&#8217;s targets file; for example, C# runs csc.exe at this point.) </li>
<li>&#8230; </li>
<li>PostSharpInspectConstants </li>
<li>PostSharpInspectReferences </li>
<li>PostSharp (Where the actual post-compilation is done) </li>
</ul>
</li>
<li>&#8230; </li>
<li>PrepareForRun (After build, but before running the application)
<ul>
<li>CopyFilesToOutputDirectory </li>
<li>RunCodeAnalysis </li>
</ul>
</li>
<li>&#8230; </li>
</ul>
<p>So there are obviously a couple of steps between the PostSharp processing and CA. When building the project, the CoreCompile target will place the newly built assembly in the obj$(Configuration) folder. Afterwards, when PostSharp runs, it compiles a new assembly with PDBs in the obj$(Configuration)PostSharp folder. Finally, that is copied to the obj$(Configuration) folder, which overwrites what the CoreCompile task created.</p>
<p>CA waits until the files are copied to the $(OutDir) directory (which is by default bin$(Configuration)) before executing. Thus it would be great if we could now change the order of things to do the following when PostSharp is enabled:</p>
<ul>
<li>&#8230; </li>
<li>Compile
<ul>
<li>&#8230; </li>
<li>CoreCompile (Delegated to a specific language&#8217;s targets file; for example, C# runs csc.exe at this point.) </li>
<li>&#8230; </li>
<li><em>RunCodeAnalysis</em> </li>
<li>PostSharpInspectConstants </li>
<li>PostSharpInspectReferences </li>
<li>PostSharp </li>
</ul>
</li>
<li>&#8230; </li>
<li>PrepareForRun
<ul>
<li>CopyFilesToOutputDirectory </li>
</ul>
</li>
<li>&#8230; </li>
</ul>
<p>So the next question is how do we do that?</p>
<p>The first thing is we need to find the targets files for both CA and PostSharp. The former is located at %PROGRAMFILES%MSBuildMicrosoftVisualStudiov9.0CodeAnalysisMicrosoft.CodeAnalysis.targets, and the latter is located at %PROGRAMFILES%MSBuildPostSharpPostSharp-1.0.targets. At the end of the PostSharp targets file, you can see that it inserts itself into the chain of compilation targets I described above.</p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(CompileDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectConstants;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectReferences;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharp</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(BuildDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpVerify</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p>Now perhaps we can trick code analysis into running before PostSharp just by re-ordering the dependencies, like this:</p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(CompileDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>RunCodeAnalysis</strong>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectConstants;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectReferences;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharp</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(BuildDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpVerify</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p>So we&#8217;ve said to run code analysis before the PostSharp processing. Unfortunately, this doesn&#8217;t work, as MSBuild reports the following error:</p>
<p style="text-align: center"><img alt="MSBuild Circular Dependency" src="http://blogs.rev-net.com/ddewinter/wp-content/uploads/2008/01/circular_dependency.png" /></p>
<p>If we navigate to the Microsoft.CodeAnalysis.targets file, we can see that the RunCodeAnalysis target depends on the Compile target. But because we also overrode the CompileDependsOn property to include RunCodeAnalysis, we have two targets that are dependent on each other to run.</p>
<p>The next trick is to change what CodeAnalysis is dependent on. To do this, we need to change its semantics; instead of performing analysis on the $(OutDir)$(TargetName)$(TargetExt)&#160; file (<em>e.g.</em> binDebugTarget.dll), we need to perform analysis on the obj$(Configuration)$(TargetName)$(TargetExt) file (<em>e.g.</em> objDebugTarget.dll). So instead of being dependent on Compile, the target is dependent on the step that generates the objDebugTarget.dll file: CoreCompile.</p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">Target</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; </span><span style="color: red">Name</span><span style="color: blue">=</span>&#8220;<span style="color: blue">RunCodeAnalysis</span>&#8220;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(RunCodeAnalysis)&#8217;==&#8217;true&#8217; or &#8216;$(RunCodeAnalysisOnce)&#8217;==&#8217;true&#8217;</span>&#8220;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; </span><span style="color: red">Inputs</span><span style="color: blue">=</span>&#8220;<span style="color: blue">$(CodeAnalysisInputAssemblyForTask)</span>&#8220;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; </span><span style="color: red">Outputs</span><span style="color: blue">=</span>&#8220;<span style="color: blue">$(CodeAnalysisLogFileForTask);$(CodeAnalysisSucceededFile)</span>&#8220;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; </span><span style="color: red">DependsOnTargets</span><span style="color: blue">=</span>&#8220;<span style="color: blue">CoreCompile;SetCodeAnalysisProperties</span>&#8220;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &gt;</span></p>
<p>We&#8217;re almost done. Unfortunately, now CodeAnalysis runs at the correct point, but it doesn&#8217;t run on the correct file. Remember we want it to perform analysis on the obj$(Configuration) assembly before PostSharp has overwritten it.&#160; So how does Code Analysis know where to retrieve the assembly? Well, if we open the Microsoft.CodeAnalysis.targets file again and have a look at the properties, we can see:</p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(CodeAnalysisInputAssembly)&#8217;!=&#8221;</span>&#8220;<span style="color: blue">&gt;</span>$(CodeAnalysisInputAssembly)<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(CodeAnalysisInputAssembly)&#8217;==&#8221;</span>&#8220;<span style="color: blue">&gt;</span>$(OutDir)$(TargetName)$(TargetExt)<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(CodeAnalysisLogFile)&#8217;!=&#8221;</span>&#8220;<span style="color: blue">&gt;</span>$(CodeAnalysisLogFile)<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(CodeAnalysisLogFile)&#8217;==&#8221;</span>&#8220;<span style="color: blue">&gt;</span>$(CodeAnalysisInputAssemblyForTask).CodeAnalysisLog.xml<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span></p>
<p>So what this tells us is that the CodeAnalysisInputAssemblyForTask and CodeAnalysisLogFileForTask files can be overridden by a project file or another targets file by specifying either property in a &lt;PropertyGroup&gt; tag, like this:</p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span>obj$(Configuration)$(TargetName)$(TargetExt)<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span>$(CodeAnalysisInputAssemblyForTask).CodeAnalysisLog.xml<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p>When placed in a project file, this will instruct Code Analysis to analyze the assembly that we want and to produce the log file in the correct directory. This is a bit of a hassle when you have to change every single project, and there may be some cases where you want to keep the default behavior of Code Analysis (i.e. point at the binDebug folder when PostSharp is not run). There is one last tweak we can make to automate this change across all projects. Open up the PostSharp targets file and scroll down to the PropertyGroup that defines the CompileDependsOn property. Overwrite that single property group with the following:</p>
<p style="margin: 0px"><span style="color: blue">&lt;!&#8211;</span><span style="color: green"> Introduces PostSharp in the chain of compilation targets </span><span style="color: blue">&#8211;&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(RunCodeAnalysis)&#8217;==&#8217;true&#8217; or &#8216;$(RunCodeAnalysisOnce)&#8217;==&#8217;true&#8217;</span>&#8220;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(CompileDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; RunCodeAnalysis;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectConstants;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectReferences;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharp</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span>obj$(Configuration)$(TargetName)$(TargetExt)<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisInputAssemblyForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span>$(CodeAnalysisInputAssemblyForTask).CodeAnalysisLog.xml<span style="color: blue">&lt;/</span><span style="color: #a31515">CodeAnalysisLogFileForTask</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue"> </span><span style="color: red">Condition</span><span style="color: blue">=</span>&#8220;<span style="color: blue">&#8216;$(RunCodeAnalysis)&#8217;!=&#8217;true&#8217; and &#8216;$(RunCodeAnalysisOnce)&#8217;!=&#8217;true&#8217;</span>&#8220;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(CompileDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectConstants;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpInspectReferences;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharp</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">CompileDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; $(BuildDependsOn);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; PostSharpVerify</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">BuildDependsOn</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&lt;/</span><span style="color: #a31515">PropertyGroup</span><span style="color: blue">&gt;</span></p>
<p><a id="targets"></a></p>
<p>This final edit makes the targets a little more robust in that we (1) schedule Code Analysis to run before PostSharp only when CA is enabled and we (2) edit the CA Input Assembly property to target the desired location.</p>
<p>Here are the final targets files (Be sure to rename them to .targets instead of .xml):</p>
<p><a title="PostSharp-1.0 Targets" href="http://blogs.rev-net.com/ddewinter/wp-content/uploads/2008/01/PostSharp-1.0.targets.xml">PostSharp-1.0 Targets</a> &#8211; %PROGRAMFILES%MSBuildPostSharp</p>
<p><a title="Code Analysis VSTS 9.0 Targets" href="http://blogs.rev-net.com/ddewinter/wp-content/uploads/2008/01/Microsoft.CodeAnalysis.Targets.xml">Code Analysis VSTS 9.0 Targets</a> &#8211; %PROGRAMFILES%MSBuildMicrosoftVisualStudiov9.0CodeAnalysis</p>
]]></content:encoded>
			<wfw:commentRss>http://davedewinter.com/2008/01/15/postsharp-code-analysis-abomination/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CA2104: DoNotDeclareReadOnlyMutableReferenceTypes</title>
		<link>http://davedewinter.com/2008/01/05/ca2104-donotdeclarereadonlyreferencetypes/</link>
		<comments>http://davedewinter.com/2008/01/05/ca2104-donotdeclarereadonlyreferencetypes/#comments</comments>
		<pubDate>Sat, 05 Jan 2008 20:26:32 +0000</pubDate>
		<dc:creator>David DeWinter</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Visual Studio Team Suite]]></category>

		<guid isPermaLink="false">http://dev.rev-net.com/blog/ddewinter/?p=28</guid>
		<description><![CDATA[If you&#8217;re not familiar with the code analysis rule mentioned in the title of this post, I recommend you check out David Kean&#8217;s FAQ post here for some background. In summary, a violation of this rule may be fired even &#8230; <a href="http://davedewinter.com/2008/01/05/ca2104-donotdeclarereadonlyreferencetypes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re not familiar with the code analysis rule mentioned in the title of this post, I recommend you check out David Kean&#8217;s FAQ post <a href="http://blogs.msdn.com/fxcop/archive/2006/04/04/faq-how-do-i-indicate-to-donotdeclarereadonlymutablereferencetypes-that-a-type-is-immutable-david-kean.aspx">here</a> for some background.</p>
<p>In summary, a violation of this rule may be fired even if you declare a read-only variable of a type that&#8217;s immutable, because the code analysis engine won&#8217;t know that that type <em>is</em> immutable. To get around this, you can follow David&#8217;s suggestion of putting an ImmutableTypes.txt file in the same directory as your FxCop project file.</p>
<p>But if you&#8217;re using Visual Studio Team System to execute your Code Analysis, it&#8217;s a different story because you don&#8217;t have an FxCop project file. Fortunately, this ImmutableTypes.txt file can be placed in the C:Program FilesMicrosoft Visual Studio 9Team ToolsStatic Analysis ToolsFxCopImmutableTypes.txt location to be picked up.</p>
<p>What would be great is if there was an MSBuild task that allowed developers on a project to make an ImmutableTypes.txt file directly in a VS project and have that merged with the existing ImmutableTypes.txt at the target location. That way, individual projects can build their ImmutableTypes without worrying about CA2104 violations.  In fact&#8230;I may just build that task. <img src='http://davedewinter.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Right now I have a centralized project for gathering Immutable Types and I do this as part of my build:</p>
<p style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<p style="margin: 0px"><span style="color: blue">&lt;</span><span style="color: #a31515">Copy</span><span style="color: blue"> </span><span style="color: red">SourceFiles</span><span style="color: blue">=</span>&#8220;<span style="color: blue">ImmutableTypes.txt</span>&#8220;<span style="color: blue"> </span><span style="color: red">DestinationFolder</span><span style="color: blue">=</span>&#8220;<span style="color: blue">$(VS90COMNTOOLS)&#8230;.Team ToolsStatic Analysis ToolsFxCop</span>&#8220;<span style="color: blue"> /&gt;</span></p>
</p>
<p>That will copy the ImmutableTypes.txt file from my project to the  C:Program FilesMicrosoft Visual Studio 9Team ToolsStatic Analysis ToolsFxCopImmutableTypes.txt location. Problem (tentatively) solved. <img src='http://davedewinter.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davedewinter.com/2008/01/05/ca2104-donotdeclarereadonlyreferencetypes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

