Skip to content

Commit

Permalink
Merge pull request #82918 from raulsntos/dotnet/only-node-can-export-…
Browse files Browse the repository at this point in the history
…node

C#: Report diagnostic for Node exports in a type that doesn't derive from Node
  • Loading branch information
akien-mga committed Oct 27, 2023
2 parents d88ef22 + bfe1a93 commit bf41c6b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
25 changes: 25 additions & 0 deletions modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
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();

Expand Down Expand Up @@ -265,6 +274,15 @@ void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType)
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<VariableDeclaratorSyntax>()
Expand Down

0 comments on commit bf41c6b

Please sign in to comment.