|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Immutable; |
| 8 | + using System.Reflection; |
| 9 | + using Microsoft.CodeAnalysis; |
| 10 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | + |
| 12 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 13 | + internal sealed class Roslyn7446WorkaroundAnalyzer : DiagnosticAnalyzer |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// The ID for diagnostics produced by the <see cref="Roslyn7446WorkaroundAnalyzer"/> analyzer. |
| 17 | + /// </summary> |
| 18 | + public const string DiagnosticId = "Roslyn7446WorkaroundAnalyzer"; |
| 19 | + private const string Title = "Roslyn Bug 7446 Workaround"; |
| 20 | + private const string MessageFormat = "Roslyn Bug 7446 Workaround"; |
| 21 | + private const string Description = "Roslyn Bug 7446 Workaround"; |
| 22 | + private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Roslyn7446WorkaroundAnalyzer.md"; |
| 23 | + |
| 24 | + private static readonly DiagnosticDescriptor Descriptor = |
| 25 | + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink, WellKnownDiagnosticTags.NotConfigurable); |
| 26 | + |
| 27 | + private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart; |
| 28 | + |
| 29 | + private static readonly bool CallGetDeclarationDiagnostics; |
| 30 | + |
| 31 | + static Roslyn7446WorkaroundAnalyzer() |
| 32 | + { |
| 33 | + // dotnet/roslyn#7446 was fixed for Roslyn 1.2 |
| 34 | + CallGetDeclarationDiagnostics = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0); |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc/> |
| 38 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = |
| 39 | + ImmutableArray.Create(Descriptor); |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + public override void Initialize(AnalysisContext context) |
| 43 | + { |
| 44 | + if (!CallGetDeclarationDiagnostics) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + context.RegisterCompilationStartAction(CompilationStartAction); |
| 50 | + } |
| 51 | + |
| 52 | +#pragma warning disable RS1012 // Start action has no registered actions. |
| 53 | + private static void HandleCompilationStart(CompilationStartAnalysisContext context) |
| 54 | +#pragma warning restore RS1012 // Start action has no registered actions. |
| 55 | + { |
| 56 | + context.Compilation.GetDeclarationDiagnostics(context.CancellationToken); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments