|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | +using Microsoft.CodeAnalysis.Operations; |
| 6 | + |
| 7 | +namespace DotVVM.Analyzers.ApiUsage |
| 8 | +{ |
| 9 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 10 | + public sealed class UnsupportedCallSiteAttributeAnalyzer : DiagnosticAnalyzer |
| 11 | + { |
| 12 | + private static readonly LocalizableResourceString unsupportedCallSiteTitle = new(nameof(Resources.ApiUsage_UnsupportedCallSite_Title), Resources.ResourceManager, typeof(Resources)); |
| 13 | + private static readonly LocalizableResourceString unsupportedCallSiteMessage = new(nameof(Resources.ApiUsage_UnsupportedCallSite_Message), Resources.ResourceManager, typeof(Resources)); |
| 14 | + private static readonly LocalizableResourceString unsupportedCallSiteDescription = new(nameof(Resources.ApiUsage_UnsupportedCallSite_Description), Resources.ResourceManager, typeof(Resources)); |
| 15 | + private const string unsupportedCallSiteAttributeMetadataName = "DotVVM.Framework.CodeAnalysis.UnsupportedCallSiteAttribute"; |
| 16 | + private const int callSiteTypeServerUnderlyingValue = 0; |
| 17 | + |
| 18 | + public static DiagnosticDescriptor DoNotInvokeMethodFromUnsupportedCallSite = new( |
| 19 | + DotvvmDiagnosticIds.DoNotInvokeMethodFromUnsupportedCallSiteRuleId, |
| 20 | + unsupportedCallSiteTitle, |
| 21 | + unsupportedCallSiteMessage, |
| 22 | + DiagnosticCategory.ApiUsage, |
| 23 | + DiagnosticSeverity.Warning, |
| 24 | + isEnabledByDefault: true, |
| 25 | + unsupportedCallSiteDescription); |
| 26 | + |
| 27 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 28 | + => ImmutableArray.Create(DoNotInvokeMethodFromUnsupportedCallSite); |
| 29 | + |
| 30 | + public override void Initialize(AnalysisContext context) |
| 31 | + { |
| 32 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 33 | + context.EnableConcurrentExecution(); |
| 34 | + |
| 35 | + context.RegisterOperationAction(context => |
| 36 | + { |
| 37 | + var unsupportedCallSiteAttribute = context.Compilation.GetTypeByMetadataName(unsupportedCallSiteAttributeMetadataName); |
| 38 | + if (unsupportedCallSiteAttribute is null) |
| 39 | + return; |
| 40 | + |
| 41 | + if (context.Operation is IInvocationOperation invocation) |
| 42 | + { |
| 43 | + var method = invocation.TargetMethod; |
| 44 | + var attribute = method.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, unsupportedCallSiteAttribute)); |
| 45 | + if (attribute is null || !attribute.ConstructorArguments.Any()) |
| 46 | + return; |
| 47 | + |
| 48 | + if (attribute.ConstructorArguments.First().Value is not int callSiteType || callSiteTypeServerUnderlyingValue != callSiteType) |
| 49 | + return; |
| 50 | + |
| 51 | + var reason = (string?)attribute.ConstructorArguments.Skip(1).First().Value; |
| 52 | + context.ReportDiagnostic( |
| 53 | + Diagnostic.Create( |
| 54 | + DoNotInvokeMethodFromUnsupportedCallSite, |
| 55 | + invocation.Syntax.GetLocation(), |
| 56 | + invocation.TargetMethod.Name, |
| 57 | + (reason != null) ? $"due to: \"{reason}\"" : string.Empty)); |
| 58 | + } |
| 59 | + }, OperationKind.Invocation); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments