| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 8 | | |
| | | 9 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 10 | | public sealed class TypeNotSupportedAnalyzer : DiagnosticAnalyzer |
| | | 11 | | { |
| | 0 | 12 | | private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = ImmutableArray.Create(Diagnostic |
| | 0 | 13 | | private static readonly ImmutableArray<SyntaxKind> supportedSyntaxKinds = ImmutableArray.Create(SyntaxKind.Attribute |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| | | 17 | | { |
| | 0 | 18 | | get { return supportedDiagnostics; } |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public override void Initialize(AnalysisContext context) |
| | | 23 | | { |
| | 0 | 24 | | context.EnableConcurrentExecution(); |
| | 0 | 25 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | | 26 | | |
| | 0 | 27 | | context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | private static void Action(SyntaxNodeAnalysisContext context) |
| | | 31 | | { |
| | 0 | 32 | | if (context.Node is not AttributeSyntax attribute) |
| | | 33 | | { |
| | 0 | 34 | | return; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | // Get symbol |
| | 0 | 38 | | INamedTypeSymbol? attributeSymbol = attribute.GetAttributeSymbol(context.SemanticModel, context.CancellationToke |
| | 0 | 39 | | if (attributeSymbol == null || !attributeSymbol.StringEquals(Types.GLOBAL_MARKER_ATTRIBUTE)) |
| | | 40 | | { |
| | 0 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | TypeDeclarationSyntax? containingType = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>(); |
| | 0 | 45 | | if (containingType == null) |
| | | 46 | | { |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | if (context.SemanticModel.GetDeclaredSymbol(containingType) is not ITypeSymbol parentTypeSymbol) |
| | | 51 | | { |
| | 0 | 52 | | return; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | if (ScriptableValueHelper.DoesTypeSupportCallbacks(in parentTypeSymbol, out NotSupportedReason notSupportedReaso |
| | | 56 | | { |
| | | 57 | | // This type is supported, stop here. |
| | 0 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | string type; |
| | | 62 | | |
| | | 63 | | switch (notSupportedReason) |
| | | 64 | | { |
| | | 65 | | case NotSupportedReason.ReadOnlyStruct: |
| | 0 | 66 | | type = "'readonly struct'"; |
| | 0 | 67 | | break; |
| | | 68 | | case NotSupportedReason.StaticClass: |
| | 0 | 69 | | type = "'static class'"; |
| | 0 | 70 | | break; |
| | | 71 | | default: |
| | 0 | 72 | | type = "ERROR"; |
| | | 73 | | break; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.TypeNotSupported, attribute.GetLocation(), type)); |
| | 0 | 77 | | } |
| | | 78 | | } |