Merge pull request #64743 from raulsntos/dotnet6-signal-analyzer

Improve C# signal analyzer errors
This commit is contained in:
Ignacio Roldán Etcheverry 2022-08-24 19:28:47 +02:00 committed by GitHub
commit e172b1aa91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 7 deletions

View File

@ -192,15 +192,15 @@ namespace Godot.SourceGenerators
location?.SourceTree?.FilePath));
}
public static void ReportSignalDelegateSignatureNotSupported(
public static void ReportSignalParameterTypeNotSupported(
GeneratorExecutionContext context,
INamedTypeSymbol delegateSymbol)
IParameterSymbol parameterSymbol)
{
var locations = delegateSymbol.Locations;
var locations = parameterSymbol.Locations;
var location = locations.FirstOrDefault(l => l.SourceTree != null) ?? locations.FirstOrDefault();
string message = "The delegate signature of the signal " +
$"is not supported: '{delegateSymbol.ToDisplayString()}'";
string message = "The parameter of the delegate signature of the signal " +
$"is not supported: '{parameterSymbol.ToDisplayString()}'";
string description = $"{message}. Use supported types only or remove the '[Signal]' attribute.";
@ -215,5 +215,29 @@ namespace Godot.SourceGenerators
location,
location?.SourceTree?.FilePath));
}
public static void ReportSignalDelegateSignatureMustReturnVoid(
GeneratorExecutionContext context,
INamedTypeSymbol delegateSymbol)
{
var locations = delegateSymbol.Locations;
var location = locations.FirstOrDefault(l => l.SourceTree != null) ?? locations.FirstOrDefault();
string message = "The delegate signature of the signal " +
$"must return void: '{delegateSymbol.ToDisplayString()}'";
string description = $"{message}. Return void or remove the '[Signal]' attribute.";
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(id: "GODOT-G0203",
title: message,
messageFormat: message,
category: "Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description),
location,
location?.SourceTree?.FilePath));
}
}
}

View File

@ -148,8 +148,29 @@ namespace Godot.SourceGenerators
if (invokeMethodData == null)
{
// TODO: Better error for incompatible signature. We should indicate incompatible argument types, as we do with exported properties.
Common.ReportSignalDelegateSignatureNotSupported(context, signalDelegateSymbol);
if (signalDelegateSymbol.DelegateInvokeMethod is IMethodSymbol methodSymbol)
{
foreach (var parameter in methodSymbol.Parameters)
{
if (parameter.RefKind != RefKind.None)
{
Common.ReportSignalParameterTypeNotSupported(context, parameter);
continue;
}
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(parameter.Type, typeCache);
if (marshalType == null)
{
Common.ReportSignalParameterTypeNotSupported(context, parameter);
}
}
if (!methodSymbol.ReturnsVoid)
{
Common.ReportSignalDelegateSignatureMustReturnVoid(context, signalDelegateSymbol);
}
}
continue;
}