-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79007 from 398utubzyt/dotnet/globalclass-analyzer
C#: Add a Roslyn analyzer for global classes
- Loading branch information
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GlobalClassAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Godot.SourceGenerators | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class GlobalClassAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics | ||
=> ImmutableArray.Create( | ||
Common.GlobalClassMustDeriveFromGodotObjectRule, | ||
Common.GlobalClassMustNotBeGenericRule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
private void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
{ | ||
var typeClassDecl = (ClassDeclarationSyntax)context.Node; | ||
|
||
// Return if not a type symbol or the type is not a global class. | ||
if (context.ContainingSymbol is not INamedTypeSymbol typeSymbol || | ||
!typeSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotGlobalClassAttribute() ?? false)) | ||
return; | ||
|
||
if (typeSymbol.IsGenericType) | ||
Common.ReportGlobalClassMustNotBeGeneric(context, typeClassDecl, typeSymbol); | ||
|
||
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject)) | ||
Common.ReportGlobalClassMustDeriveFromGodotObject(context, typeClassDecl, typeSymbol); | ||
} | ||
} | ||
} |