Debian Codegraph @

modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs

raw symbols SCIP
1 using System.Collections.Immutable;
2 using System.Linq;
3 using Microsoft.CodeAnalysis;
4 using Microsoft.CodeAnalysis.CSharp;
5 using Microsoft.CodeAnalysis.CSharp.Syntax;
6 using Microsoft.CodeAnalysis.Diagnostics;
7
8 namespace Godot.SourceGenerators
9 {
10 [DiagnosticAnalyzer(LanguageNames.CSharp)]
11 public sealed class MustBeVariantAnalyzer : DiagnosticAnalyzer
12 {
13 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
14 => ImmutableArray.Create(
15 Common.GenericTypeArgumentMustBeVariantRule,
16 Common.GenericTypeParameterMustBeVariantAnnotatedRule,
17 Common.TypeArgumentParentSymbolUnhandledRule);
18
19 public override void Initialize(AnalysisContext context)
20 {
21 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
22 context.EnableConcurrentExecution();
23 context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.TypeArgumentList);
24 }
25
26 private void AnalyzeNode(SyntaxNodeAnalysisContext context)
27 {
28 // Ignore syntax inside comments
29 if (IsInsideDocumentation(context.Node))
30 return;
31
32 var typeArgListSyntax = (TypeArgumentListSyntax)context.Node;
33
34 // Method invocation or variable declaration that contained the type arguments
35 var parentSyntax = context.Node.Parent;
36 Helper.ThrowIfNull(parentSyntax);
37
38 var sm = context.SemanticModel;
39
40 var typeCache = new MarshalUtils.TypeCache(context.Compilation);
41
42 for (int i = 0; i < typeArgListSyntax.Arguments.Count; i++)
43 {
44 var typeSyntax = typeArgListSyntax.Arguments[i];
45
46 // Ignore omitted type arguments, e.g.: List<>, Dictionary<,>, etc
47 if (typeSyntax is OmittedTypeArgumentSyntax)
48 continue;
49
50 var typeSymbol = sm.GetSymbolInfo(typeSyntax).Symbol as ITypeSymbol;
51 Helper.ThrowIfNull(typeSymbol);
52
53 var parentSymbolInfo = sm.GetSymbolInfo(parentSyntax);
54 var parentSymbol = parentSymbolInfo.Symbol;
55 if (parentSymbol == null)
56 {
57 if (parentSymbolInfo.CandidateReason == CandidateReason.LateBound)
58 {
59 // Invocations on dynamic are late bound so we can't retrieve the symbol.
60 continue;
61 }
62
63 Helper.ThrowIfNull(parentSymbol);
64 }
65
66 if (!ShouldCheckTypeArgument(context, parentSyntax, parentSymbol, typeSyntax, typeSymbol, i))
67 {
68 return;
69 }
70
71 if (typeSymbol is ITypeParameterSymbol typeParamSymbol)
72 {
73 if (!typeParamSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false))
74 {
75 context.ReportDiagnostic(Diagnostic.Create(
76 Common.GenericTypeParameterMustBeVariantAnnotatedRule,
77 typeSyntax.GetLocation(),
78 typeSymbol.ToDisplayString()
79 ));
80 }
81 continue;
82 }
83
84 var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(typeSymbol, typeCache);
85
86 if (marshalType is null)
87 {
88 context.ReportDiagnostic(Diagnostic.Create(
89 Common.GenericTypeArgumentMustBeVariantRule,
90 typeSyntax.GetLocation(),
91 typeSymbol.ToDisplayString()
92 ));
93 }
94 }
95 }
96
97 /// <summary>
98 /// Check if the syntax node is inside a documentation syntax.
99 /// </summary>
100 /// <param name="syntax">Syntax node to check.</param>
101 /// <returns><see langword="true"/> if the syntax node is inside a documentation syntax.</returns>
102 private bool IsInsideDocumentation(SyntaxNode? syntax)
103 {
104 while (syntax != null)
105 {
106 if (syntax is DocumentationCommentTriviaSyntax)
107 {
108 return true;
109 }
110
111 syntax = syntax.Parent;
112 }
113
114 return false;
115 }
116
117 /// <summary>
118 /// Check if the given type argument is being used in a type parameter that contains
119 /// the <c>MustBeVariantAttribute</c>; otherwise, we ignore the attribute.
120 /// </summary>
121 /// <param name="context">Context for a syntax node action.</param>
122 /// <param name="parentSyntax">The parent node syntax that contains the type node syntax.</param>
123 /// <param name="parentSymbol">The symbol retrieved for the parent node syntax.</param>
124 /// <param name="typeArgumentSyntax">The type node syntax of the argument type to check.</param>
125 /// <param name="typeArgumentSymbol">The symbol retrieved for the type node syntax.</param>
126 /// <param name="typeArgumentIndex"></param>
127 /// <returns><see langword="true"/> if the type must be variant and must be analyzed.</returns>
128 private bool ShouldCheckTypeArgument(
129 SyntaxNodeAnalysisContext context,
130 SyntaxNode parentSyntax,
131 ISymbol parentSymbol,
132 TypeSyntax typeArgumentSyntax,
133 ITypeSymbol typeArgumentSymbol,
134 int typeArgumentIndex)
135 {
136 ITypeParameterSymbol? typeParamSymbol = parentSymbol switch
137 {
138 IMethodSymbol methodSymbol when parentSyntax.Ancestors().Any(s => s is AttributeSyntax) &&
139 methodSymbol.ContainingType.TypeParameters.Length > 0
140 => methodSymbol.ContainingType.TypeParameters[typeArgumentIndex],
141
142 IMethodSymbol { TypeParameters.Length: > 0 } methodSymbol
143 => methodSymbol.TypeParameters[typeArgumentIndex],
144
145 INamedTypeSymbol { TypeParameters.Length: > 0 } typeSymbol
146 => typeSymbol.TypeParameters[typeArgumentIndex],
147
148 _
149 => null
150 };
151
152 if (typeParamSymbol != null)
153 {
154 return typeParamSymbol.GetAttributes()
155 .Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false);
156 }
157
158 context.ReportDiagnostic(Diagnostic.Create(
159 Common.TypeArgumentParentSymbolUnhandledRule,
160 typeArgumentSyntax.GetLocation(),
161 parentSymbol.ToDisplayString()
162 ));
163
164 return false;
165 }
166 }
167 }