| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 6 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Analyzers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Analyzer that detects reflection-based Needlr API usage in AOT-enabled projects. |
| | | 12 | | /// </summary> |
| | | 13 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 14 | | public sealed class ReflectionInAotProjectAnalyzer : DiagnosticAnalyzer |
| | | 15 | | { |
| | | 16 | | // Reflection-based method names that should trigger the diagnostic |
| | 1 | 17 | | private static readonly ImmutableHashSet<string> ReflectionMethodNames = ImmutableHashSet.Create( |
| | 1 | 18 | | "UsingReflectionTypeRegistrar", |
| | 1 | 19 | | "UsingReflectionTypeFilterer", |
| | 1 | 20 | | "UsingReflectionPluginFactory", |
| | 1 | 21 | | "UsingReflectionAssemblyLoader", |
| | 1 | 22 | | "UsingReflectionAssemblyProvider"); |
| | | 23 | | |
| | | 24 | | // Reflection-based type names that should trigger the diagnostic |
| | 1 | 25 | | private static readonly ImmutableHashSet<string> ReflectionTypeNames = ImmutableHashSet.Create( |
| | 1 | 26 | | "ReflectionPluginFactory", |
| | 1 | 27 | | "ReflectionTypeRegistrar", |
| | 1 | 28 | | "ReflectionTypeFilterer", |
| | 1 | 29 | | "ReflectionAssemblyLoader", |
| | 1 | 30 | | "ReflectionAssemblyProvider", |
| | 1 | 31 | | "ReflectionServiceProviderBuilder"); |
| | | 32 | | |
| | | 33 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 242 | 34 | | ImmutableArray.Create(DiagnosticDescriptors.ReflectionInAotProject); |
| | | 35 | | |
| | | 36 | | public override void Initialize(AnalysisContext context) |
| | | 37 | | { |
| | 14 | 38 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 14 | 39 | | context.EnableConcurrentExecution(); |
| | | 40 | | |
| | 14 | 41 | | context.RegisterCompilationStartAction(compilationContext => |
| | 14 | 42 | | { |
| | 14 | 43 | | // Check if project has AOT/trimming enabled |
| | 9 | 44 | | if (!IsAotEnabledProject(compilationContext.Options)) |
| | 14 | 45 | | { |
| | 1 | 46 | | return; |
| | 14 | 47 | | } |
| | 14 | 48 | | |
| | 14 | 49 | | // Register syntax node analysis for method invocations |
| | 8 | 50 | | compilationContext.RegisterSyntaxNodeAction( |
| | 8 | 51 | | AnalyzeInvocation, |
| | 8 | 52 | | SyntaxKind.InvocationExpression); |
| | 14 | 53 | | |
| | 14 | 54 | | // Register syntax node analysis for object creation |
| | 8 | 55 | | compilationContext.RegisterSyntaxNodeAction( |
| | 8 | 56 | | AnalyzeObjectCreation, |
| | 8 | 57 | | SyntaxKind.ObjectCreationExpression); |
| | 22 | 58 | | }); |
| | 14 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static bool IsAotEnabledProject(AnalyzerOptions options) |
| | | 62 | | { |
| | | 63 | | // Check for PublishAot or IsAotCompatible in analyzer config |
| | 9 | 64 | | var globalOptions = options.AnalyzerConfigOptionsProvider.GlobalOptions; |
| | | 65 | | |
| | 9 | 66 | | if (globalOptions.TryGetValue("build_property.PublishAot", out var publishAot) && |
| | 9 | 67 | | string.Equals(publishAot, "true", System.StringComparison.OrdinalIgnoreCase)) |
| | | 68 | | { |
| | 6 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | |
| | 3 | 72 | | if (globalOptions.TryGetValue("build_property.IsAotCompatible", out var isAotCompatible) && |
| | 3 | 73 | | string.Equals(isAotCompatible, "true", System.StringComparison.OrdinalIgnoreCase)) |
| | | 74 | | { |
| | 2 | 75 | | return true; |
| | | 76 | | } |
| | | 77 | | |
| | 1 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context) |
| | | 82 | | { |
| | 6 | 83 | | var invocation = (InvocationExpressionSyntax)context.Node; |
| | | 84 | | |
| | | 85 | | // Get the method name being invoked |
| | 6 | 86 | | string? methodName = null; |
| | 6 | 87 | | Location? location = null; |
| | | 88 | | |
| | 6 | 89 | | if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) |
| | | 90 | | { |
| | 6 | 91 | | methodName = memberAccess.Name.Identifier.Text; |
| | 6 | 92 | | location = memberAccess.Name.GetLocation(); |
| | | 93 | | } |
| | 0 | 94 | | else if (invocation.Expression is IdentifierNameSyntax identifier) |
| | | 95 | | { |
| | 0 | 96 | | methodName = identifier.Identifier.Text; |
| | 0 | 97 | | location = identifier.GetLocation(); |
| | | 98 | | } |
| | | 99 | | |
| | 6 | 100 | | if (methodName != null && location != null && ReflectionMethodNames.Contains(methodName)) |
| | | 101 | | { |
| | 6 | 102 | | var diagnostic = Diagnostic.Create( |
| | 6 | 103 | | DiagnosticDescriptors.ReflectionInAotProject, |
| | 6 | 104 | | location, |
| | 6 | 105 | | methodName); |
| | | 106 | | |
| | 6 | 107 | | context.ReportDiagnostic(diagnostic); |
| | | 108 | | } |
| | 6 | 109 | | } |
| | | 110 | | |
| | | 111 | | private static void AnalyzeObjectCreation(SyntaxNodeAnalysisContext context) |
| | | 112 | | { |
| | 8 | 113 | | var objectCreation = (ObjectCreationExpressionSyntax)context.Node; |
| | | 114 | | |
| | | 115 | | // Get the type name being instantiated |
| | 8 | 116 | | string? typeName = null; |
| | 8 | 117 | | Location? location = null; |
| | | 118 | | |
| | 8 | 119 | | if (objectCreation.Type is IdentifierNameSyntax identifier) |
| | | 120 | | { |
| | 8 | 121 | | typeName = identifier.Identifier.Text; |
| | 8 | 122 | | location = identifier.GetLocation(); |
| | | 123 | | } |
| | 0 | 124 | | else if (objectCreation.Type is QualifiedNameSyntax qualifiedName) |
| | | 125 | | { |
| | 0 | 126 | | typeName = qualifiedName.Right.Identifier.Text; |
| | 0 | 127 | | location = qualifiedName.Right.GetLocation(); |
| | | 128 | | } |
| | | 129 | | |
| | 8 | 130 | | if (typeName != null && location != null && ReflectionTypeNames.Contains(typeName)) |
| | | 131 | | { |
| | 4 | 132 | | var diagnostic = Diagnostic.Create( |
| | 4 | 133 | | DiagnosticDescriptors.ReflectionInAotProject, |
| | 4 | 134 | | location, |
| | 4 | 135 | | typeName); |
| | | 136 | | |
| | 4 | 137 | | context.ReportDiagnostic(diagnostic); |
| | | 138 | | } |
| | 8 | 139 | | } |
| | | 140 | | } |