| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.Analyzers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Analyzer that detects Lazy<T> dependencies where T is not discovered |
| | | 15 | | /// by source generation. Only active when [assembly: GenerateTypeRegistry] is present. |
| | | 16 | | /// </summary> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class LazyResolutionAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string GenerateTypeRegistryAttributeName = "NexusLabs.Needlr.Generators.GenerateTypeRegistryAttribute" |
| | | 21 | | |
| | | 22 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 118 | 23 | | ImmutableArray.Create(DiagnosticDescriptors.LazyResolutionUnknown); |
| | | 24 | | |
| | | 25 | | public override void Initialize(AnalysisContext context) |
| | | 26 | | { |
| | 14 | 27 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 14 | 28 | | context.EnableConcurrentExecution(); |
| | | 29 | | |
| | 14 | 30 | | context.RegisterCompilationStartAction(compilationContext => |
| | 14 | 31 | | { |
| | 8 | 32 | | if (!HasGenerateTypeRegistryAttribute(compilationContext.Compilation)) |
| | 1 | 33 | | return; |
| | 14 | 34 | | |
| | 7 | 35 | | var lazyType = compilationContext.Compilation.GetTypeByMetadataName("System.Lazy`1"); |
| | 7 | 36 | | if (lazyType == null) |
| | 0 | 37 | | return; |
| | 14 | 38 | | |
| | 14 | 39 | | // Collect all discovered types and interfaces during compilation |
| | 7 | 40 | | var discoveredTypes = new ConcurrentDictionary<INamedTypeSymbol, byte>(SymbolEqualityComparer.Default); |
| | 7 | 41 | | var discoveredInterfaces = new ConcurrentDictionary<INamedTypeSymbol, byte>(SymbolEqualityComparer.Default); |
| | 14 | 42 | | |
| | 14 | 43 | | // Collect pending diagnostics to verify at compilation end |
| | 7 | 44 | | var pendingDiagnostics = new ConcurrentBag<(Location Location, INamedTypeSymbol InnerType)>(); |
| | 14 | 45 | | |
| | 14 | 46 | | // First pass: collect all discoverable types and their interfaces |
| | 7 | 47 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 7 | 48 | | { |
| | 65 | 49 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 7 | 50 | | |
| | 65 | 51 | | if (!IsDiscoverableClass(typeSymbol)) |
| | 56 | 52 | | return; |
| | 7 | 53 | | |
| | 9 | 54 | | discoveredTypes.TryAdd(typeSymbol, 0); |
| | 20 | 55 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | 7 | 56 | | { |
| | 1 | 57 | | discoveredInterfaces.TryAdd(iface, 0); |
| | 7 | 58 | | } |
| | 16 | 59 | | }, SymbolKind.NamedType); |
| | 14 | 60 | | |
| | 14 | 61 | | // Second pass: collect Lazy<T> parameters for later verification |
| | 7 | 62 | | compilationContext.RegisterSyntaxNodeAction( |
| | 14 | 63 | | ctx => CollectLazyParameter(ctx, lazyType, pendingDiagnostics), |
| | 7 | 64 | | SyntaxKind.Parameter); |
| | 14 | 65 | | |
| | 14 | 66 | | // At compilation end, verify and report diagnostics |
| | 7 | 67 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 7 | 68 | | { |
| | 28 | 69 | | foreach (var (location, innerType) in pendingDiagnostics) |
| | 7 | 70 | | { |
| | 7 | 71 | | // Framework types are typically registered by the framework |
| | 7 | 72 | | var ns = innerType.ContainingNamespace?.ToDisplayString() ?? ""; |
| | 7 | 73 | | if (ns.StartsWith("Microsoft.Extensions.") || |
| | 7 | 74 | | ns.StartsWith("Microsoft.AspNetCore.") || |
| | 7 | 75 | | ns == "System" || |
| | 7 | 76 | | ns.StartsWith("System.")) |
| | 7 | 77 | | { |
| | 7 | 78 | | continue; |
| | 7 | 79 | | } |
| | 7 | 80 | | |
| | 7 | 81 | | // Check if the type or its interface is discovered |
| | 6 | 82 | | if (innerType.TypeKind == TypeKind.Interface) |
| | 7 | 83 | | { |
| | 5 | 84 | | if (discoveredInterfaces.ContainsKey(innerType)) |
| | 1 | 85 | | continue; |
| | 7 | 86 | | } |
| | 7 | 87 | | else |
| | 7 | 88 | | { |
| | 1 | 89 | | if (discoveredTypes.ContainsKey(innerType)) |
| | 7 | 90 | | continue; |
| | 7 | 91 | | } |
| | 7 | 92 | | |
| | 4 | 93 | | var diagnostic = Diagnostic.Create( |
| | 4 | 94 | | DiagnosticDescriptors.LazyResolutionUnknown, |
| | 4 | 95 | | location, |
| | 4 | 96 | | innerType.Name); |
| | 7 | 97 | | |
| | 4 | 98 | | endContext.ReportDiagnostic(diagnostic); |
| | 7 | 99 | | } |
| | 14 | 100 | | }); |
| | 21 | 101 | | }); |
| | 14 | 102 | | } |
| | | 103 | | |
| | | 104 | | private static bool HasGenerateTypeRegistryAttribute(Compilation compilation) |
| | | 105 | | { |
| | 23 | 106 | | foreach (var attribute in compilation.Assembly.GetAttributes()) |
| | | 107 | | { |
| | 7 | 108 | | var fullName = attribute.AttributeClass?.ToDisplayString(); |
| | 7 | 109 | | if (fullName == GenerateTypeRegistryAttributeName) |
| | 7 | 110 | | return true; |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private static void CollectLazyParameter( |
| | | 117 | | SyntaxNodeAnalysisContext context, |
| | | 118 | | INamedTypeSymbol lazyType, |
| | | 119 | | ConcurrentBag<(Location, INamedTypeSymbol)> pendingDiagnostics) |
| | | 120 | | { |
| | 14 | 121 | | var parameter = (ParameterSyntax)context.Node; |
| | 14 | 122 | | if (parameter.Type == null) |
| | 0 | 123 | | return; |
| | | 124 | | |
| | 14 | 125 | | var typeInfo = context.SemanticModel.GetTypeInfo(parameter.Type); |
| | 14 | 126 | | if (typeInfo.Type is not INamedTypeSymbol parameterType) |
| | 0 | 127 | | return; |
| | | 128 | | |
| | 14 | 129 | | if (!SymbolEqualityComparer.Default.Equals(parameterType.OriginalDefinition, lazyType)) |
| | 7 | 130 | | return; |
| | | 131 | | |
| | 7 | 132 | | if (parameterType.TypeArguments.Length != 1) |
| | 0 | 133 | | return; |
| | | 134 | | |
| | 7 | 135 | | var innerType = parameterType.TypeArguments[0] as INamedTypeSymbol; |
| | 7 | 136 | | if (innerType == null) |
| | 0 | 137 | | return; |
| | | 138 | | |
| | 7 | 139 | | pendingDiagnostics.Add((parameter.Type.GetLocation(), innerType)); |
| | 7 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Uses the shared TypeDiscoveryHelper to determine if a type is discoverable. |
| | | 144 | | /// This ensures consistency with the source generator's logic. |
| | | 145 | | /// </summary> |
| | | 146 | | private static bool IsDiscoverableClass(INamedTypeSymbol typeSymbol) |
| | 65 | 147 | | => TypeDiscoveryHelper.IsInjectableType(typeSymbol, isCurrentAssembly: true); |
| | | 148 | | } |