|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Linq; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | +using Microsoft.CodeAnalysis.Operations; |
| 9 | +using static Microsoft.DurableTask.Analyzers.Orchestration.LoggerOrchestrationAnalyzer; |
| 10 | + |
| 11 | +namespace Microsoft.DurableTask.Analyzers.Orchestration; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Analyzer that reports a warning when a non-contextual ILogger is used in an orchestration method. |
| 15 | +/// </summary> |
| 16 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 17 | +public sealed class LoggerOrchestrationAnalyzer : OrchestrationAnalyzer<LoggerOrchestrationVisitor> |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Diagnostic ID supported for the analyzer. |
| 21 | + /// </summary> |
| 22 | + public const string DiagnosticId = "DURABLE0010"; |
| 23 | + |
| 24 | + static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LoggerOrchestrationAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); |
| 25 | + static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LoggerOrchestrationAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 26 | + |
| 27 | + static readonly DiagnosticDescriptor Rule = new( |
| 28 | + DiagnosticId, |
| 29 | + Title, |
| 30 | + MessageFormat, |
| 31 | + AnalyzersCategories.Orchestration, |
| 32 | + DiagnosticSeverity.Warning, |
| 33 | + isEnabledByDefault: true); |
| 34 | + |
| 35 | + /// <inheritdoc/> |
| 36 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule]; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Visitor that inspects the method body for ILogger usage. |
| 40 | + /// </summary> |
| 41 | + public sealed class LoggerOrchestrationVisitor : MethodProbeOrchestrationVisitor |
| 42 | + { |
| 43 | + INamedTypeSymbol? iLoggerSymbol; |
| 44 | + |
| 45 | + /// <inheritdoc/> |
| 46 | + public override bool Initialize() |
| 47 | + { |
| 48 | + this.iLoggerSymbol = this.KnownTypeSymbols.ILogger; |
| 49 | + if (this.iLoggerSymbol == null) |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc/> |
| 58 | + protected override void VisitMethod(SemanticModel semanticModel, SyntaxNode methodSyntax, IMethodSymbol methodSymbol, string orchestrationName, Action<Diagnostic> reportDiagnostic) |
| 59 | + { |
| 60 | + IOperation? methodOperation = semanticModel.GetOperation(methodSyntax); |
| 61 | + if (methodOperation is null) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Track which parameters we've already reported on to avoid duplicates |
| 67 | + HashSet<IParameterSymbol> reportedParameters = new(SymbolEqualityComparer.Default); |
| 68 | + |
| 69 | + // Check for ILogger parameters in the method signature |
| 70 | + foreach (IParameterSymbol parameter in methodSymbol.Parameters.Where( |
| 71 | + parameter => this.IsILoggerType(parameter.Type) && |
| 72 | + parameter.DeclaringSyntaxReferences.Length > 0)) |
| 73 | + { |
| 74 | + // Found an ILogger parameter - report diagnostic at the parameter location |
| 75 | + SyntaxNode parameterSyntax = parameter.DeclaringSyntaxReferences[0].GetSyntax(); |
| 76 | + reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, parameterSyntax, methodSymbol.Name, orchestrationName)); |
| 77 | + reportedParameters.Add(parameter); |
| 78 | + } |
| 79 | + |
| 80 | + // Check for ILogger field or property references (but not parameter references, as those were already reported) |
| 81 | + foreach (IOperation descendant in methodOperation.Descendants()) |
| 82 | + { |
| 83 | + ITypeSymbol? typeToCheck = null; |
| 84 | + SyntaxNode? syntaxNode = null; |
| 85 | + |
| 86 | + switch (descendant) |
| 87 | + { |
| 88 | + case IFieldReferenceOperation fieldRef: |
| 89 | + typeToCheck = fieldRef.Field.Type; |
| 90 | + syntaxNode = fieldRef.Syntax; |
| 91 | + break; |
| 92 | + case IPropertyReferenceOperation propRef: |
| 93 | + typeToCheck = propRef.Property.Type; |
| 94 | + syntaxNode = propRef.Syntax; |
| 95 | + break; |
| 96 | + case IParameterReferenceOperation paramRef: |
| 97 | + // Skip parameter references that we already reported on in the parameter list |
| 98 | + if (reportedParameters.Contains(paramRef.Parameter)) |
| 99 | + { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + typeToCheck = paramRef.Parameter.Type; |
| 104 | + syntaxNode = paramRef.Syntax; |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + if (typeToCheck != null && syntaxNode != null && this.IsILoggerType(typeToCheck)) |
| 109 | + { |
| 110 | + reportDiagnostic(RoslynExtensions.BuildDiagnostic(Rule, syntaxNode, methodSymbol.Name, orchestrationName)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + bool IsILoggerType(ITypeSymbol type) |
| 116 | + { |
| 117 | + if (this.iLoggerSymbol == null) |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + // First check for exact match with ILogger |
| 123 | + if (SymbolEqualityComparer.Default.Equals(type, this.iLoggerSymbol)) |
| 124 | + { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + // Check if the type implements ILogger interface (covers ILogger<T> case) |
| 129 | + if (type is INamedTypeSymbol namedType) |
| 130 | + { |
| 131 | + return namedType.AllInterfaces.Any(interfaceType => |
| 132 | + SymbolEqualityComparer.Default.Equals(interfaceType, this.iLoggerSymbol)); |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments