From 0340bbd1499a4e3edc668b33ab4e71d3777bc96b Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 21 Dec 2015 11:11:37 -0600 Subject: [PATCH 1/3] Work around dotnet/roslyn#7446 --- .../Roslyn7446WorkaroundAnalyzer.cs | 59 +++++++++++++++++++ .../StyleCop.Analyzers.csproj | 1 + 2 files changed, 60 insertions(+) create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs new file mode 100644 index 000000000..6b5ac8b53 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs @@ -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 +{ + using System; + using System.Collections.Immutable; + using System.Reflection; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.Diagnostics; + + [DiagnosticAnalyzer(LanguageNames.CSharp)] + internal sealed class Roslyn7446WorkaroundAnalyzer : DiagnosticAnalyzer + { + /// + /// The ID for diagnostics produced by the analyzer. + /// + public const string DiagnosticId = "Roslyn7446WorkaroundAnalyzer"; + private const string Title = "Roslyn Bug 7446 Workaround"; + private const string MessageFormat = "Roslyn Bug 7446 Workaround"; + private const string Description = "Roslyn Bug 7446 Workaround"; + private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Roslyn7446WorkaroundAnalyzer.md"; + + private static readonly DiagnosticDescriptor Descriptor = + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink, WellKnownDiagnosticTags.NotConfigurable); + + private static readonly Action CompilationStartAction = HandleCompilationStart; + + private static readonly bool CallGetDeclarationDiagnostics; + + static Roslyn7446WorkaroundAnalyzer() + { + // dotnet/roslyn#7446 was fixed for Roslyn 1.2 + CallGetDeclarationDiagnostics = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0); + } + + /// + public override ImmutableArray SupportedDiagnostics { get; } = + ImmutableArray.Create(Descriptor); + + /// + 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); + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj b/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj index c58ebe09b..ff48abf2b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj +++ b/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj @@ -268,6 +268,7 @@ + From 24bff5340a7eac17efe7020253357a469a847d09 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 23 Dec 2015 08:06:53 -0600 Subject: [PATCH 2/3] Usability improvements for the workaround * Rename Roslyn7446WorkaroundAnalyzer to SA0000Roslyn7446Workaround * Allow users to configure the behavior of SA0000Roslyn7446Workaround * Improve the title and description of SA0000Roslyn7446Workaround * Disable the rule by default for improved performance --- .../StyleCop.Analyzers/AnalyzerCategory.cs | 5 + .../SA0000Roslyn7446Workaround.cs} | 20 +-- .../SpecialRules/SpecialResources.Designer.cs | 93 ++++++++++++ .../SpecialRules/SpecialResources.resx | 136 ++++++++++++++++++ .../StyleCop.Analyzers.csproj | 11 +- 5 files changed, 254 insertions(+), 11 deletions(-) rename StyleCop.Analyzers/StyleCop.Analyzers/{Roslyn7446WorkaroundAnalyzer.cs => SpecialRules/SA0000Roslyn7446Workaround.cs} (60%) create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.Designer.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.resx diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerCategory.cs b/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerCategory.cs index ec4d9013c..f1363fc18 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerCategory.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerCategory.cs @@ -42,5 +42,10 @@ internal static class AnalyzerCategory /// Category definition for spacing rules. /// internal const string SpacingRules = "StyleCop.CSharp.SpacingRules"; + + /// + /// Category definition for special purpose rules. + /// + internal const string SpecialRules = "StyleCop.CSharp.SpecialRules"; } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SA0000Roslyn7446Workaround.cs similarity index 60% rename from StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs rename to StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SA0000Roslyn7446Workaround.cs index 6b5ac8b53..0639729fe 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Roslyn7446WorkaroundAnalyzer.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SA0000Roslyn7446Workaround.cs @@ -1,7 +1,7 @@ // 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 +namespace StyleCop.Analyzers.SpecialRules { using System; using System.Collections.Immutable; @@ -10,25 +10,25 @@ namespace StyleCop.Analyzers using Microsoft.CodeAnalysis.Diagnostics; [DiagnosticAnalyzer(LanguageNames.CSharp)] - internal sealed class Roslyn7446WorkaroundAnalyzer : DiagnosticAnalyzer + internal sealed class SA0000Roslyn7446Workaround : DiagnosticAnalyzer { /// - /// The ID for diagnostics produced by the analyzer. + /// The ID for diagnostics produced by the analyzer. /// - public const string DiagnosticId = "Roslyn7446WorkaroundAnalyzer"; - private const string Title = "Roslyn Bug 7446 Workaround"; - private const string MessageFormat = "Roslyn Bug 7446 Workaround"; - private const string Description = "Roslyn Bug 7446 Workaround"; - private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Roslyn7446WorkaroundAnalyzer.md"; + 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.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink, WellKnownDiagnosticTags.NotConfigurable); + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Info, AnalyzerConstants.DisabledByDefault, Description, HelpLink); private static readonly Action CompilationStartAction = HandleCompilationStart; private static readonly bool CallGetDeclarationDiagnostics; - static Roslyn7446WorkaroundAnalyzer() + static SA0000Roslyn7446Workaround() { // dotnet/roslyn#7446 was fixed for Roslyn 1.2 CallGetDeclarationDiagnostics = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.Designer.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.Designer.cs new file mode 100644 index 000000000..5208b8da4 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.Designer.cs @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace StyleCop.Analyzers.SpecialRules { + using System; + using System.Reflection; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class SpecialResources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal SpecialResources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("StyleCop.Analyzers.SpecialRules.SpecialResources", typeof(SpecialResources).GetTypeInfo().Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to 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 no [rest of string was truncated]";. + /// + internal static string SA0000Description { + get { + return ResourceManager.GetString("SA0000Description", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to n/a. + /// + internal static string SA0000MessageFormat { + get { + return ResourceManager.GetString("SA0000MessageFormat", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Workaround incomplete diagnostics in Visual Studio 2015 Update 1. + /// + internal static string SA0000Title { + get { + return ResourceManager.GetString("SA0000Title", resourceCulture); + } + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.resx b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.resx new file mode 100644 index 000000000..de1056c5e --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpecialRules/SpecialResources.resx @@ -0,0 +1,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 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. + + + n/a + + + Workaround incomplete diagnostics in Visual Studio 2015 Update 1 + + \ No newline at end of file diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj b/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj index ff48abf2b..8b0c1d8ba 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj +++ b/StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj @@ -268,7 +268,6 @@ - @@ -323,6 +322,12 @@ SpacingResources.resx + + + True + True + SpecialResources.resx + @@ -424,6 +429,10 @@ ResXFileCodeGenerator SpacingResources.Designer.cs + + ResXFileCodeGenerator + SpecialResources.Designer.cs + From 988017f04f26ededff43762b5f5d4fa7850b093c Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 27 Dec 2015 12:50:50 -0600 Subject: [PATCH 3/3] Add proper documentation page for SA0000Roslyn7446Workaround --- StyleCopAnalyzers.sln | 1 + documentation/SA0000Roslyn7446Workaround.md | 44 +++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 documentation/SA0000Roslyn7446Workaround.md diff --git a/StyleCopAnalyzers.sln b/StyleCopAnalyzers.sln index ba0b30c9f..e44366a7f 100644 --- a/StyleCopAnalyzers.sln +++ b/StyleCopAnalyzers.sln @@ -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 diff --git a/documentation/SA0000Roslyn7446Workaround.md b/documentation/SA0000Roslyn7446Workaround.md new file mode 100644 index 000000000..e4c920eb9 --- /dev/null +++ b/documentation/SA0000Roslyn7446Workaround.md @@ -0,0 +1,44 @@ +## SA0000Roslyn7446Workaround + + + + + + + + + + + + + + +
TypeNameSA0000Roslyn7446Workaround
CheckIdSA0000
CategorySpecial Rules
+ +## 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.