|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 3 | +using System; |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace Riverside.CompilerPlatform.Analyzers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Base class for analyzers that focus on a specific attribute type. |
| 11 | +/// </summary> |
| 12 | +/// <typeparam name="TAttribute">The attribute type to analyze.</typeparam> |
| 13 | +public abstract class AttributeAnalyzer<TAttribute> : DiagnosticAnalyzerEx |
| 14 | + where TAttribute : Attribute |
| 15 | +{ |
| 16 | + private readonly string _attributeTypeName = typeof(TAttribute).FullName; |
| 17 | + private readonly string _attributeShortName = typeof(TAttribute).Name; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets or sets whether to also analyze subclasses of the target attribute. |
| 21 | + /// </summary> |
| 22 | + protected virtual bool IncludeAttributeSubclasses => false; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets the set of syntax kinds to register for. |
| 26 | + /// By default, registers for attributes, but can be overridden. |
| 27 | + /// </summary> |
| 28 | + protected override ImmutableArray<SyntaxKind> SyntaxKindsOfInterest => |
| 29 | + ImmutableArray.Create(SyntaxKind.Attribute); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Handles syntax node analysis by filtering for attributes of the target type. |
| 33 | + /// </summary> |
| 34 | + /// <param name="context">The syntax node analysis context.</param> |
| 35 | + protected sealed override void AnalyzeNode(SyntaxNodeAnalysisContext context) |
| 36 | + { |
| 37 | + // Skip generated code |
| 38 | + if (context.IsGeneratedCode) |
| 39 | + return; |
| 40 | + |
| 41 | + if (context.Node is AttributeSyntax attributeSyntax) |
| 42 | + { |
| 43 | + var semanticModel = context.SemanticModel; |
| 44 | + var attributeType = semanticModel.GetTypeInfo(attributeSyntax).Type; |
| 45 | + |
| 46 | + // Check if the attribute is of the target type or a subclass |
| 47 | + bool isTargetAttribute = false; |
| 48 | + |
| 49 | + if (attributeType != null) |
| 50 | + { |
| 51 | + string typeName = attributeType.ToDisplayString(); |
| 52 | + |
| 53 | + isTargetAttribute = typeName == _attributeTypeName || |
| 54 | + (IncludeAttributeSubclasses && IsSubclassOfAttribute(attributeType)); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + // Try to match by name if type info is not available |
| 59 | + var name = attributeSyntax.Name.ToString(); |
| 60 | + isTargetAttribute = name.EndsWith(_attributeShortName) || |
| 61 | + name.EndsWith(_attributeShortName + "Attribute"); |
| 62 | + } |
| 63 | + |
| 64 | + if (isTargetAttribute) |
| 65 | + { |
| 66 | + AnalyzeAttribute(context, attributeSyntax); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Checks if a type is a subclass of the target attribute. |
| 73 | + /// </summary> |
| 74 | + /// <param name="type">The type to check.</param> |
| 75 | + /// <returns>True if the type is a subclass of the target attribute.</returns> |
| 76 | + private bool IsSubclassOfAttribute(ITypeSymbol type) |
| 77 | + { |
| 78 | + var currentType = type; |
| 79 | + while (currentType != null) |
| 80 | + { |
| 81 | + if (currentType.ToDisplayString() == _attributeTypeName) |
| 82 | + return true; |
| 83 | + |
| 84 | + currentType = currentType.BaseType; |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Analyzes an attribute of the target type. |
| 92 | + /// </summary> |
| 93 | + /// <param name="context">The syntax node analysis context.</param> |
| 94 | + /// <param name="attributeSyntax">The attribute syntax node.</param> |
| 95 | + protected abstract void AnalyzeAttribute( |
| 96 | + SyntaxNodeAnalysisContext context, |
| 97 | + AttributeSyntax attributeSyntax); |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets the attribute target symbol (the element the attribute is applied to). |
| 101 | + /// </summary> |
| 102 | + /// <param name="context">The syntax node analysis context.</param> |
| 103 | + /// <param name="attributeSyntax">The attribute syntax.</param> |
| 104 | + /// <returns>The symbol that the attribute is applied to, or null if not found.</returns> |
| 105 | + protected ISymbol? GetAttributeTarget(SyntaxNodeAnalysisContext context, AttributeSyntax attributeSyntax) |
| 106 | + { |
| 107 | + // Navigate to the element that this attribute is applied to |
| 108 | + var attributeList = attributeSyntax.Parent as AttributeListSyntax; |
| 109 | + if (attributeList == null) |
| 110 | + return null; |
| 111 | + |
| 112 | + var target = attributeList.Parent; |
| 113 | + if (target == null) |
| 114 | + return null; |
| 115 | + |
| 116 | + return context.SemanticModel.GetDeclaredSymbol(target); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Gets the attribute data for this attribute instance. |
| 121 | + /// </summary> |
| 122 | + /// <param name="context">The syntax node analysis context.</param> |
| 123 | + /// <param name="attributeSyntax">The attribute syntax.</param> |
| 124 | + /// <returns>The attribute data, or null if not found.</returns> |
| 125 | + protected AttributeData? GetAttributeData(SyntaxNodeAnalysisContext context, AttributeSyntax attributeSyntax) |
| 126 | + { |
| 127 | + var target = GetAttributeTarget(context, attributeSyntax); |
| 128 | + if (target == null) |
| 129 | + return null; |
| 130 | + |
| 131 | + // Find the attribute data that matches this attribute syntax |
| 132 | + return target.GetAttributes() |
| 133 | + .FirstOrDefault(attr => |
| 134 | + attr.ApplicationSyntaxReference?.GetSyntax() == attributeSyntax); |
| 135 | + } |
| 136 | +} |
0 commit comments