| 1 |
using System.Collections.Immutable; |
| 2 |
using System.Linq; |
| 3 |
using Microsoft.CodeAnalysis; |
| 4 |
using Microsoft.CodeAnalysis.CSharp; |
| 5 |
using Microsoft.CodeAnalysis.Diagnostics; |
| 6 |
|
| 7 |
namespace Godot.SourceGenerators |
| 8 |
{ |
| 9 |
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 10 |
public sealed class GlobalClassAnalyzer : DiagnosticAnalyzer |
| 11 |
{ |
| 12 |
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 13 |
=> ImmutableArray.Create( |
| 14 |
Common.GlobalClassMustDeriveFromGodotObjectRule, |
| 15 |
Common.GlobalClassMustNotBeGenericRule); |
| 16 |
|
| 17 |
public override void Initialize(AnalysisContext context) |
| 18 |
{ |
| 19 |
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 20 |
context.EnableConcurrentExecution(); |
| 21 |
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); |
| 22 |
} |
| 23 |
|
| 24 |
private static void AnalyzeNode(SyntaxNodeAnalysisContext context) |
| 25 |
{ |
| 26 |
|
| 27 |
if (context.ContainingSymbol is not INamedTypeSymbol typeSymbol || |
| 28 |
!typeSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotGlobalClassAttribute() ?? false)) |
| 29 |
return; |
| 30 |
|
| 31 |
if (typeSymbol.IsGenericType) |
| 32 |
{ |
| 33 |
context.ReportDiagnostic(Diagnostic.Create( |
| 34 |
Common.GlobalClassMustNotBeGenericRule, |
| 35 |
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 36 |
typeSymbol.ToDisplayString() |
| 37 |
)); |
| 38 |
} |
| 39 |
|
| 40 |
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject)) |
| 41 |
{ |
| 42 |
context.ReportDiagnostic(Diagnostic.Create( |
| 43 |
Common.GlobalClassMustDeriveFromGodotObjectRule, |
| 44 |
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 45 |
typeSymbol.ToDisplayString() |
| 46 |
)); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |