| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.Analyzers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Analyzer that detects [DeferToContainer] attributes placed in generated code. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// Source generators run in isolation and cannot see output from other generators. |
| | | 18 | | /// If another generator adds [DeferToContainer] to a partial class, Needlr's |
| | | 19 | | /// TypeRegistryGenerator will not see it and will generate incorrect factory code. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// This analyzer runs after all generators complete and can detect this scenario, |
| | | 23 | | /// warning users to move the attribute to their original source file. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 27 | | public sealed class DeferToContainerInGeneratedCodeAnalyzer : DiagnosticAnalyzer |
| | | 28 | | { |
| | | 29 | | private const string DeferToContainerAttributeName = "DeferToContainerAttribute"; |
| | | 30 | | private const string DeferToContainerAttributeShortName = "DeferToContainer"; |
| | | 31 | | private const string DeferToContainerAttributeFullName = "NexusLabs.Needlr.DeferToContainerAttribute"; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 314 | 35 | | ImmutableArray.Create(DiagnosticDescriptors.DeferToContainerInGeneratedCode); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public override void Initialize(AnalysisContext context) |
| | | 39 | | { |
| | | 40 | | // Important: We WANT to analyze generated code - that's the whole point! |
| | 26 | 41 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDia |
| | 26 | 42 | | context.EnableConcurrentExecution(); |
| | | 43 | | |
| | 26 | 44 | | context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration); |
| | 26 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) |
| | | 48 | | { |
| | 32 | 49 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | | 50 | | |
| | | 51 | | // Check if this class has [DeferToContainer] attribute |
| | 32 | 52 | | var deferToContainerAttribute = FindDeferToContainerAttribute(classDeclaration); |
| | 32 | 53 | | if (deferToContainerAttribute == null) |
| | | 54 | | { |
| | 18 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Check if the attribute is in generated code |
| | 14 | 59 | | if (!IsInGeneratedCode(deferToContainerAttribute, context)) |
| | | 60 | | { |
| | 2 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // Report diagnostic on the attribute |
| | 12 | 65 | | var diagnostic = Diagnostic.Create( |
| | 12 | 66 | | DiagnosticDescriptors.DeferToContainerInGeneratedCode, |
| | 12 | 67 | | deferToContainerAttribute.GetLocation(), |
| | 12 | 68 | | classDeclaration.Identifier.Text); |
| | | 69 | | |
| | 12 | 70 | | context.ReportDiagnostic(diagnostic); |
| | 12 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static AttributeSyntax? FindDeferToContainerAttribute(ClassDeclarationSyntax classDeclaration) |
| | | 74 | | { |
| | 112 | 75 | | foreach (var attributeList in classDeclaration.AttributeLists) |
| | | 76 | | { |
| | 110 | 77 | | foreach (var attribute in attributeList.Attributes) |
| | | 78 | | { |
| | 31 | 79 | | var name = GetAttributeName(attribute); |
| | 31 | 80 | | if (name == DeferToContainerAttributeName || |
| | 31 | 81 | | name == DeferToContainerAttributeShortName) |
| | | 82 | | { |
| | 14 | 83 | | return attribute; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | 18 | 88 | | return null; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private static string? GetAttributeName(AttributeSyntax attribute) |
| | | 92 | | { |
| | 31 | 93 | | return attribute.Name switch |
| | 31 | 94 | | { |
| | 11 | 95 | | IdentifierNameSyntax identifier => identifier.Identifier.Text, |
| | 20 | 96 | | QualifiedNameSyntax qualified => qualified.Right.Identifier.Text, |
| | 0 | 97 | | _ => null |
| | 31 | 98 | | }; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private static bool IsInGeneratedCode(SyntaxNode node, SyntaxNodeAnalysisContext context) |
| | | 102 | | { |
| | 14 | 103 | | var syntaxTree = node.SyntaxTree; |
| | | 104 | | |
| | | 105 | | // Check file path for common generated code patterns |
| | 14 | 106 | | var filePath = syntaxTree.FilePath; |
| | 14 | 107 | | if (!string.IsNullOrEmpty(filePath)) |
| | | 108 | | { |
| | | 109 | | // Common generated file patterns |
| | 14 | 110 | | if (filePath.EndsWith(".g.cs") || |
| | 14 | 111 | | filePath.EndsWith(".generated.cs") || |
| | 14 | 112 | | filePath.EndsWith(".designer.cs")) |
| | | 113 | | { |
| | 10 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Check for obj/generated folder (common for source generators) |
| | 4 | 118 | | var normalizedPath = filePath.Replace('\\', '/').ToLowerInvariant(); |
| | 4 | 119 | | if (normalizedPath.Contains("/obj/") && normalizedPath.Contains("/generated/")) |
| | | 120 | | { |
| | 2 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | // Check for [GeneratedCode] attribute on the class |
| | 2 | 126 | | var classDeclaration = node.FirstAncestorOrSelf<ClassDeclarationSyntax>(); |
| | 2 | 127 | | if (classDeclaration != null) |
| | | 128 | | { |
| | 2 | 129 | | var symbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 2 | 130 | | if (symbol != null && HasGeneratedCodeAttribute(symbol)) |
| | | 131 | | { |
| | 0 | 132 | | return true; |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // Check if the syntax tree is marked as generated |
| | 2 | 137 | | if (context.SemanticModel.Compilation.Options.SyntaxTreeOptionsProvider != null) |
| | | 138 | | { |
| | | 139 | | // The tree options provider can indicate generated code status |
| | | 140 | | // but we've already covered the main cases above |
| | | 141 | | } |
| | | 142 | | |
| | 2 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private static bool HasGeneratedCodeAttribute(ISymbol symbol) |
| | | 147 | | { |
| | 8 | 148 | | foreach (var attribute in symbol.GetAttributes()) |
| | | 149 | | { |
| | 2 | 150 | | var attrClass = attribute.AttributeClass; |
| | 2 | 151 | | if (attrClass == null) |
| | | 152 | | { |
| | | 153 | | continue; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | // Check for System.CodeDom.Compiler.GeneratedCodeAttribute |
| | 2 | 157 | | if (attrClass.Name == "GeneratedCodeAttribute" && |
| | 2 | 158 | | attrClass.ContainingNamespace?.ToDisplayString() == "System.CodeDom.Compiler") |
| | | 159 | | { |
| | 0 | 160 | | return true; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // Also check for CompilerGeneratedAttribute |
| | 2 | 164 | | if (attrClass.Name == "CompilerGeneratedAttribute" && |
| | 2 | 165 | | attrClass.ContainingNamespace?.ToDisplayString() == "System.Runtime.CompilerServices") |
| | | 166 | | { |
| | 0 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | 2 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | } |