|
| 1 | +// Copyright (c) Tocsoft and contributors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
| 11 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 12 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 13 | +using Microsoft.CodeAnalysis.Operations; |
| 14 | + |
| 15 | +namespace Tocsoft.DateTimeAbstractions.Analyzer |
| 16 | +{ |
| 17 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 18 | + public class ClockTimerUsageInvocationAnalyzer : DiagnosticAnalyzer |
| 19 | + { |
| 20 | + public const string DiagnosticId = "TOCSOFT0003"; |
| 21 | + |
| 22 | + // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat. |
| 23 | + // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Localizing%20Analyzers.md for more on localization |
| 24 | + private static readonly LocalizableString Title = new LocalizableResourceString( |
| 25 | + nameof(Resources.ClockTimerTitle), |
| 26 | + Resources.ResourceManager, |
| 27 | + typeof(Resources)); |
| 28 | + |
| 29 | + private static readonly LocalizableString InvocationMessageFormat = new LocalizableResourceString( |
| 30 | + nameof(Resources.ClockTimerAnalyzer_InvocationMessageFormat), |
| 31 | + Resources.ResourceManager, |
| 32 | + typeof(Resources)); |
| 33 | + |
| 34 | + private static readonly LocalizableString InvocationMessageFormatDirected = new LocalizableResourceString( |
| 35 | + nameof(Resources.ClockTimerAnalyzer_InvocationMessageFormat_DirectToClockTimer), |
| 36 | + Resources.ResourceManager, |
| 37 | + typeof(Resources)); |
| 38 | + |
| 39 | + private static readonly LocalizableString Description = new LocalizableResourceString( |
| 40 | + nameof(Resources.ClockTimerAnalyzerDescription), |
| 41 | + Resources.ResourceManager, |
| 42 | + typeof(Resources)); |
| 43 | + |
| 44 | + private const string Category = "Testability"; |
| 45 | + |
| 46 | + private static DiagnosticDescriptor invocationRule = new DiagnosticDescriptor( |
| 47 | + DiagnosticId, |
| 48 | + Title, |
| 49 | + InvocationMessageFormat, |
| 50 | + Category, |
| 51 | + DiagnosticSeverity.Warning, |
| 52 | + isEnabledByDefault: true, |
| 53 | + description: Description); |
| 54 | + |
| 55 | + private static DiagnosticDescriptor invocationRuleDirected = new DiagnosticDescriptor( |
| 56 | + DiagnosticId, |
| 57 | + Title, |
| 58 | + InvocationMessageFormatDirected, |
| 59 | + Category, |
| 60 | + DiagnosticSeverity.Warning, |
| 61 | + isEnabledByDefault: true, |
| 62 | + description: Description); |
| 63 | + |
| 64 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 65 | + { |
| 66 | + get { return ImmutableArray.Create(invocationRule, invocationRuleDirected); } |
| 67 | + } |
| 68 | + |
| 69 | + private static readonly ImmutableArray<string> StopwatchMethods = new[] |
| 70 | + { |
| 71 | + "StartNew" |
| 72 | + }.ToImmutableArray(); |
| 73 | + |
| 74 | + public override void Initialize(AnalysisContext context) |
| 75 | + { |
| 76 | + context.EnableConcurrentExecution(); |
| 77 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 78 | + |
| 79 | + context.RegisterCompilationStartAction(compilationStartContext => |
| 80 | + { |
| 81 | + Compilation compilation = compilationStartContext.Compilation; |
| 82 | + INamedTypeSymbol stopwatchType = compilation.GetTypeByMetadataName("System.Diagnostics.Stopwatch"); |
| 83 | + INamedTypeSymbol clockTimerType = compilation.GetTypeByMetadataName("Tocsoft.DateTimeAbstractions.ClockTimer"); |
| 84 | + |
| 85 | + compilationStartContext.RegisterOperationAction( |
| 86 | + operationContext => |
| 87 | + { |
| 88 | + IInvocationOperation invocation = (IInvocationOperation)operationContext.Operation; |
| 89 | + |
| 90 | + if (invocation.Type == null || invocation.Type != stopwatchType) |
| 91 | + { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (!StopwatchMethods.Contains(invocation.TargetMethod?.Name)) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + operationContext.ReportDiagnostic(Diagnostic.Create( |
| 101 | + clockTimerType == null ? invocationRule : invocationRuleDirected, |
| 102 | + invocation.Syntax.GetLocation(), |
| 103 | + stopwatchType.Name, |
| 104 | + invocation.TargetMethod.Name, |
| 105 | + clockTimerType?.Name)); |
| 106 | + }, OperationKind.Invocation); |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments