From bfe1a93023c8a05226cc2aff13b946354e741415 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Fri, 6 Oct 2023 18:12:16 +0200 Subject: [PATCH] C#: Report diagnostic for Node exports in a type that doesn't derive from Node --- .../Godot.SourceGenerators/Common.cs | 25 +++++++++++++++++++ .../ExtensionMethods.cs | 2 +- .../ScriptPropertyDefValGenerator.cs | 18 +++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs index 72614dd7e079..325c0dd8e3c3 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs @@ -220,6 +220,31 @@ ISymbol exportedMemberSymbol location?.SourceTree?.FilePath)); } + public static void ReportOnlyNodesShouldExportNodes( + GeneratorExecutionContext context, + ISymbol exportedMemberSymbol + ) + { + var locations = exportedMemberSymbol.Locations; + var location = locations.FirstOrDefault(l => l.SourceTree != null) ?? locations.FirstOrDefault(); + bool isField = exportedMemberSymbol is IFieldSymbol; + + string message = $"Types not derived from Node should not export Node {(isField ? "fields" : "properties")}"; + + string description = $"{message}. Node export is only supported in Node-derived classes."; + + context.ReportDiagnostic(Diagnostic.Create( + new DiagnosticDescriptor(id: "GD0107", + title: message, + messageFormat: message, + category: "Usage", + DiagnosticSeverity.Error, + isEnabledByDefault: true, + description), + location, + location?.SourceTree?.FilePath)); + } + public static void ReportSignalDelegateMissingSuffix( GeneratorExecutionContext context, INamedTypeSymbol delegateSymbol) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index 5866db5144f9..c7fd45238db0 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -32,7 +32,7 @@ public static bool IsGodotSourceGeneratorDisabled(this GeneratorExecutionContext disabledGenerators != null && disabledGenerators.Split(';').Contains(generatorName)); - public static bool InheritsFrom(this INamedTypeSymbol? symbol, string assemblyName, string typeFullName) + public static bool InheritsFrom(this ITypeSymbol? symbol, string assemblyName, string typeFullName) { while (symbol != null) { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs index 4df16d05f07a..8accfcfe3f71 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs @@ -166,6 +166,15 @@ INamedTypeSymbol symbol continue; } + if (marshalType == MarshalType.GodotObjectOrDerived) + { + if (!symbol.InheritsFrom("GodotSharp", "Godot.Node") && + propertyType.InheritsFrom("GodotSharp", "Godot.Node")) + { + Common.ReportOnlyNodesShouldExportNodes(context, property); + } + } + var propertyDeclarationSyntax = property.DeclaringSyntaxReferences .Select(r => r.GetSyntax() as PropertyDeclarationSyntax).FirstOrDefault(); @@ -261,6 +270,15 @@ INamedTypeSymbol symbol continue; } + if (marshalType == MarshalType.GodotObjectOrDerived) + { + if (!symbol.InheritsFrom("GodotSharp", "Godot.Node") && + fieldType.InheritsFrom("GodotSharp", "Godot.Node")) + { + Common.ReportOnlyNodesShouldExportNodes(context, field); + } + } + EqualsValueClauseSyntax? initializer = field.DeclaringSyntaxReferences .Select(r => r.GetSyntax()) .OfType()