Skip to content

Commit

Permalink
Merge pull request #1979 from sharwell/roslyn7446-workaround
Browse files Browse the repository at this point in the history
Work around dotnet/roslyn#7446
  • Loading branch information
sharwell committed Dec 27, 2015
2 parents 7e27340 + 988017f commit b2686ff
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 0 deletions.
5 changes: 5 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ internal static class AnalyzerCategory
/// Category definition for spacing rules.
/// </summary>
internal const string SpacingRules = "StyleCop.CSharp.SpacingRules";

/// <summary>
/// Category definition for special purpose rules.
/// </summary>
internal const string SpecialRules = "StyleCop.CSharp.SpecialRules";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.SpecialRules
{
using System;
using System.Collections.Immutable;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class SA0000Roslyn7446Workaround : DiagnosticAnalyzer
{
/// <summary>
/// The ID for diagnostics produced by the <see cref="SA0000Roslyn7446Workaround"/> analyzer.
/// </summary>
public const string DiagnosticId = "SA0000";
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(SpecialResources.SA0000Title), SpecialResources.ResourceManager, typeof(SpecialResources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(SpecialResources.SA0000MessageFormat), SpecialResources.ResourceManager, typeof(SpecialResources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(SpecialResources.SA0000Description), SpecialResources.ResourceManager, typeof(SpecialResources));
private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA0000Roslyn7446Workaround.md";

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Info, AnalyzerConstants.DisabledByDefault, Description, HelpLink);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;

private static readonly bool CallGetDeclarationDiagnostics;

static SA0000Roslyn7446Workaround()
{
// dotnet/roslyn#7446 was fixed for Roslyn 1.2
CallGetDeclarationDiagnostics = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0);
}

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(Descriptor);

/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
if (!CallGetDeclarationDiagnostics)
{
return;
}

context.RegisterCompilationStartAction(CompilationStartAction);
}

#pragma warning disable RS1012 // Start action has no registered actions.
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
#pragma warning restore RS1012 // Start action has no registered actions.
{
context.Compilation.GetDeclarationDiagnostics(context.CancellationToken);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="SA0000Description" xml:space="preserve">
<value>Visual Studio 2015 Update 1 contains a bug which can cause diagnostics to occasionally not display in the Errors window. When this occurs, it is impossible to use the code fixes to address style violations reported during a build. This analyzer works around the bug (dotnet/roslyn#7446).

When this analyzer is enabled, all diagnostics will eventually be reported in the Error window, but the performance of the analyzers is reduced. The rule is disabled for maximum performance, but can be enabled if users notice errors appearing during a build but not while editing, and they wish to use the code fixes to correct them.

Note that some situations are not affected by the bug:

* When building a project, all relevant warnings are reported even if this rule is disabled.
* The various Fix All operations work properly for the selected scope, even if only a subset of the violations are appearing in the Errors window.</value>
</data>
<data name="SA0000MessageFormat" xml:space="preserve">
<value>n/a</value>
</data>
<data name="SA0000Title" xml:space="preserve">
<value>Workaround incomplete diagnostics in Visual Studio 2015 Update 1</value>
</data>
</root>
10 changes: 10 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@
<DependentUpon>SpacingResources.resx</DependentUpon>
</Compile>
<Compile Include="SpacingRules\TokenSpacingProperties.cs" />
<Compile Include="SpecialRules\SA0000Roslyn7446Workaround.cs" />
<Compile Include="SpecialRules\SpecialResources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>SpecialResources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\build\keys\StyleCopAnalyzers.dev.snk">
Expand Down Expand Up @@ -423,6 +429,10 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>SpacingResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="SpecialRules\SpecialResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>SpecialResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
1 change: 1 addition & 0 deletions StyleCopAnalyzers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "documentation", "documentat
documentation\Configuration.md = documentation\Configuration.md
documentation\EnableConfiguration.md = documentation\EnableConfiguration.md
documentation\KnownChanges.md = documentation\KnownChanges.md
documentation\SA0000Roslyn7446Workaround.md = documentation\SA0000Roslyn7446Workaround.md
documentation\SA1000.md = documentation\SA1000.md
documentation\SA1001.md = documentation\SA1001.md
documentation\SA1002.md = documentation\SA1002.md
Expand Down
44 changes: 44 additions & 0 deletions documentation/SA0000Roslyn7446Workaround.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## SA0000Roslyn7446Workaround

<table>
<tr>
<td>TypeName</td>
<td>SA0000Roslyn7446Workaround</td>
</tr>
<tr>
<td>CheckId</td>
<td>SA0000</td>
</tr>
<tr>
<td>Category</td>
<td>Special Rules</td>
</tr>
</table>

## Cause

Workaround incomplete diagnostics in Visual Studio 2015 Update 1.

## Rule description

Visual Studio 2015 Update 1 contains a bug which can cause diagnostics to occasionally not display in the Errors window.
When this occurs, it is impossible to use the code fixes to address style violations reported during a build. This
analyzer works around the bug [dotnet/roslyn#7446](https://github.com/dotnet/roslyn/issues/7446).

When this analyzer is enabled, all diagnostics will eventually be reported in the Error window, but the performance of
the analyzers is reduced. The rule is disabled for maximum performance, but can be enabled if users notice errors
appearing during a build but not while editing, and they wish to use the code fixes to correct them.

Note that some situations are not affected by the bug:

* When building a project, all relevant warnings are reported even if this rule is disabled.
* The various Fix All operations work properly for the selected scope, even if only a subset of the violations are
appearing in the Errors window.

## How to fix violations

This analyzer does not report any diagnostics.

## How to suppress violations

This analyzer does not report any diagnostics.

0 comments on commit b2686ff

Please sign in to comment.