| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Logging.Analyzers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Validates usage of <c>[NeedlrLoggerMessage]</c> so misconfigurations surface as build diagnostics |
| | | 13 | | /// rather than confusing generated-code errors. |
| | | 14 | | /// </summary> |
| | | 15 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 16 | | public sealed class NeedlrLoggerMessageAnalyzer : DiagnosticAnalyzer |
| | | 17 | | { |
| | | 18 | | private const string AttributeFullName = "NexusLabs.Needlr.Logging.NeedlrLoggerMessageAttribute"; |
| | | 19 | | private const string ILoggerFullName = "Microsoft.Extensions.Logging.ILogger"; |
| | | 20 | | private const string ExceptionFullName = "System.Exception"; |
| | | 21 | | private const int MaxDefineParameters = 6; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 333 | 25 | | ImmutableArray.Create( |
| | 333 | 26 | | DiagnosticDescriptors.MustBePartial, |
| | 333 | 27 | | DiagnosticDescriptors.MustReturnVoid, |
| | 333 | 28 | | DiagnosticDescriptors.MustNotBeGeneric, |
| | 333 | 29 | | DiagnosticDescriptors.ContainingTypeMustBePartial, |
| | 333 | 30 | | DiagnosticDescriptors.LoggerNotFound, |
| | 333 | 31 | | DiagnosticDescriptors.TooManyParameters); |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public override void Initialize(AnalysisContext context) |
| | | 35 | | { |
| | 20 | 36 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 20 | 37 | | context.EnableConcurrentExecution(); |
| | | 38 | | |
| | 20 | 39 | | context.RegisterCompilationStartAction(static compilationStart => |
| | 20 | 40 | | { |
| | 13 | 41 | | var attributeSymbol = compilationStart.Compilation.GetTypeByMetadataName(AttributeFullName); |
| | 13 | 42 | | if (attributeSymbol is null) |
| | 20 | 43 | | { |
| | 0 | 44 | | return; |
| | 20 | 45 | | } |
| | 20 | 46 | | |
| | 13 | 47 | | var loggerSymbol = compilationStart.Compilation.GetTypeByMetadataName(ILoggerFullName); |
| | 13 | 48 | | var exceptionSymbol = compilationStart.Compilation.GetTypeByMetadataName(ExceptionFullName); |
| | 20 | 49 | | |
| | 13 | 50 | | compilationStart.RegisterSymbolAction( |
| | 15 | 51 | | symbolContext => AnalyzeMethod(symbolContext, attributeSymbol, loggerSymbol, exceptionSymbol), |
| | 13 | 52 | | SymbolKind.Method); |
| | 33 | 53 | | }); |
| | 20 | 54 | | } |
| | | 55 | | |
| | | 56 | | private static void AnalyzeMethod( |
| | | 57 | | SymbolAnalysisContext context, |
| | | 58 | | INamedTypeSymbol attributeSymbol, |
| | | 59 | | INamedTypeSymbol? loggerSymbol, |
| | | 60 | | INamedTypeSymbol? exceptionSymbol) |
| | | 61 | | { |
| | 15 | 62 | | var method = (IMethodSymbol)context.Symbol; |
| | 15 | 63 | | if (!HasAttribute(method, attributeSymbol)) |
| | | 64 | | { |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // For a partial method with both parts, analyze only the definition to avoid double-reporting. |
| | 15 | 69 | | if (method.PartialDefinitionPart is not null) |
| | | 70 | | { |
| | 2 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 13 | 74 | | var location = method.Locations.Length > 0 ? method.Locations[0] : Location.None; |
| | | 75 | | |
| | 13 | 76 | | if (!method.IsPartialDefinition && method.PartialDefinitionPart is null) |
| | | 77 | | { |
| | 4 | 78 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustBePartial, location, method.Name)); |
| | | 79 | | } |
| | | 80 | | |
| | 13 | 81 | | if (!method.ReturnsVoid) |
| | | 82 | | { |
| | 2 | 83 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustReturnVoid, location, method.Name)); |
| | | 84 | | } |
| | | 85 | | |
| | 13 | 86 | | if (method.IsGenericMethod) |
| | | 87 | | { |
| | 2 | 88 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustNotBeGeneric, location, method.Name)); |
| | | 89 | | } |
| | | 90 | | |
| | 13 | 91 | | if (!IsContainingTypePartial(method)) |
| | | 92 | | { |
| | 2 | 93 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 94 | | DiagnosticDescriptors.ContainingTypeMustBePartial, |
| | 2 | 95 | | location, |
| | 2 | 96 | | method.ContainingType?.Name ?? method.Name)); |
| | | 97 | | } |
| | | 98 | | |
| | 13 | 99 | | if (loggerSymbol is not null && !HasAccessibleLogger(method, loggerSymbol)) |
| | | 100 | | { |
| | 2 | 101 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.LoggerNotFound, location, method.Name)); |
| | | 102 | | } |
| | | 103 | | |
| | 13 | 104 | | var messageParameterCount = CountMessageParameters(method, loggerSymbol, exceptionSymbol); |
| | 13 | 105 | | if (messageParameterCount > MaxDefineParameters) |
| | | 106 | | { |
| | 2 | 107 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 108 | | DiagnosticDescriptors.TooManyParameters, |
| | 2 | 109 | | location, |
| | 2 | 110 | | method.Name, |
| | 2 | 111 | | messageParameterCount)); |
| | | 112 | | } |
| | 13 | 113 | | } |
| | | 114 | | |
| | | 115 | | private static bool HasAttribute(IMethodSymbol method, INamedTypeSymbol attributeSymbol) |
| | | 116 | | { |
| | 45 | 117 | | foreach (var attribute in method.GetAttributes()) |
| | | 118 | | { |
| | 15 | 119 | | if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeSymbol)) |
| | | 120 | | { |
| | 15 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private static bool IsContainingTypePartial(IMethodSymbol method) |
| | | 129 | | { |
| | 13 | 130 | | var containingType = method.ContainingType; |
| | 13 | 131 | | if (containingType is null) |
| | | 132 | | { |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | 41 | 136 | | foreach (var reference in containingType.DeclaringSyntaxReferences) |
| | | 137 | | { |
| | 13 | 138 | | if (reference.GetSyntax() is TypeDeclarationSyntax typeDeclaration && |
| | 13 | 139 | | typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword)) |
| | | 140 | | { |
| | 11 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 2 | 145 | | return false; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private static bool HasAccessibleLogger(IMethodSymbol method, INamedTypeSymbol loggerSymbol) |
| | | 149 | | { |
| | 13 | 150 | | if (method.IsStatic) |
| | | 151 | | { |
| | 0 | 152 | | return method.Parameters.Any(parameter => IsLogger(parameter.Type, loggerSymbol)); |
| | | 153 | | } |
| | | 154 | | |
| | 34 | 155 | | for (var type = method.ContainingType; type is not null; type = type.BaseType) |
| | | 156 | | { |
| | 85 | 157 | | foreach (var member in type.GetMembers()) |
| | | 158 | | { |
| | 33 | 159 | | if (member.IsStatic) |
| | | 160 | | { |
| | | 161 | | continue; |
| | | 162 | | } |
| | | 163 | | |
| | 29 | 164 | | if (member is IFieldSymbol field && IsLogger(field.Type, loggerSymbol)) |
| | | 165 | | { |
| | 11 | 166 | | return true; |
| | | 167 | | } |
| | | 168 | | |
| | 18 | 169 | | if (member is IPropertySymbol { GetMethod: not null } property && IsLogger(property.Type, loggerSymbol)) |
| | | 170 | | { |
| | 0 | 171 | | return true; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | 2 | 176 | | return false; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static int CountMessageParameters( |
| | | 180 | | IMethodSymbol method, |
| | | 181 | | INamedTypeSymbol? loggerSymbol, |
| | | 182 | | INamedTypeSymbol? exceptionSymbol) |
| | | 183 | | { |
| | 13 | 184 | | var count = 0; |
| | 13 | 185 | | var loggerConsumed = !method.IsStatic; |
| | 13 | 186 | | var exceptionConsumed = false; |
| | | 187 | | |
| | 82 | 188 | | foreach (var parameter in method.Parameters) |
| | | 189 | | { |
| | 28 | 190 | | if (!loggerConsumed && loggerSymbol is not null && IsLogger(parameter.Type, loggerSymbol)) |
| | | 191 | | { |
| | 0 | 192 | | loggerConsumed = true; |
| | 0 | 193 | | continue; |
| | | 194 | | } |
| | | 195 | | |
| | 28 | 196 | | if (!exceptionConsumed && exceptionSymbol is not null && IsException(parameter.Type, exceptionSymbol)) |
| | | 197 | | { |
| | 9 | 198 | | exceptionConsumed = true; |
| | 9 | 199 | | continue; |
| | | 200 | | } |
| | | 201 | | |
| | 19 | 202 | | count++; |
| | | 203 | | } |
| | | 204 | | |
| | 13 | 205 | | return count; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private static bool IsLogger(ITypeSymbol type, INamedTypeSymbol loggerSymbol) |
| | | 209 | | { |
| | 11 | 210 | | if (SymbolEqualityComparer.Default.Equals(type, loggerSymbol)) |
| | | 211 | | { |
| | 11 | 212 | | return true; |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | return type.AllInterfaces.Any(iface => SymbolEqualityComparer.Default.Equals(iface, loggerSymbol)); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | private static bool IsException(ITypeSymbol type, INamedTypeSymbol exceptionSymbol) |
| | | 219 | | { |
| | 162 | 220 | | for (var current = type; current is not null; current = current.BaseType) |
| | | 221 | | { |
| | 62 | 222 | | if (SymbolEqualityComparer.Default.Equals(current, exceptionSymbol)) |
| | | 223 | | { |
| | 9 | 224 | | return true; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | 19 | 228 | | return false; |
| | | 229 | | } |
| | | 230 | | } |