2022-08-15 11:57:52 +08:00
|
|
|
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)]
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
public sealed class MustBeVariantAnalyzer : DiagnosticAnalyzer
|
2022-08-15 11:57:52 +08:00
|
|
|
{
|
|
|
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
|
|
|
|
=> ImmutableArray.Create(
|
|
|
|
Common.GenericTypeArgumentMustBeVariantRule,
|
|
|
|
Common.GenericTypeParameterMustBeVariantAnnotatedRule,
|
|
|
|
Common.TypeArgumentParentSymbolUnhandledRule);
|
|
|
|
|
|
|
|
public override void Initialize(AnalysisContext context)
|
|
|
|
{
|
|
|
|
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
|
|
|
context.EnableConcurrentExecution();
|
|
|
|
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.TypeArgumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AnalyzeNode(SyntaxNodeAnalysisContext context)
|
|
|
|
{
|
2022-12-23 09:47:34 +08:00
|
|
|
// Ignore syntax inside comments
|
|
|
|
if (IsInsideDocumentation(context.Node))
|
|
|
|
return;
|
|
|
|
|
2022-08-15 11:57:52 +08:00
|
|
|
var typeArgListSyntax = (TypeArgumentListSyntax)context.Node;
|
|
|
|
|
|
|
|
// Method invocation or variable declaration that contained the type arguments
|
|
|
|
var parentSyntax = context.Node.Parent;
|
2024-01-24 07:49:34 +08:00
|
|
|
Helper.ThrowIfNull(parentSyntax);
|
2022-08-15 11:57:52 +08:00
|
|
|
|
|
|
|
var sm = context.SemanticModel;
|
|
|
|
|
|
|
|
var typeCache = new MarshalUtils.TypeCache(context.Compilation);
|
|
|
|
|
|
|
|
for (int i = 0; i < typeArgListSyntax.Arguments.Count; i++)
|
|
|
|
{
|
|
|
|
var typeSyntax = typeArgListSyntax.Arguments[i];
|
2022-09-07 00:51:08 +08:00
|
|
|
|
|
|
|
// Ignore omitted type arguments, e.g.: List<>, Dictionary<,>, etc
|
|
|
|
if (typeSyntax is OmittedTypeArgumentSyntax)
|
|
|
|
continue;
|
|
|
|
|
2022-08-15 11:57:52 +08:00
|
|
|
var typeSymbol = sm.GetSymbolInfo(typeSyntax).Symbol as ITypeSymbol;
|
2024-01-24 07:49:34 +08:00
|
|
|
Helper.ThrowIfNull(typeSymbol);
|
2022-08-15 11:57:52 +08:00
|
|
|
|
2024-05-01 01:36:43 +08:00
|
|
|
var parentSymbolInfo = sm.GetSymbolInfo(parentSyntax);
|
|
|
|
var parentSymbol = parentSymbolInfo.Symbol;
|
|
|
|
if (parentSymbol == null)
|
|
|
|
{
|
|
|
|
if (parentSymbolInfo.CandidateReason == CandidateReason.LateBound)
|
|
|
|
{
|
|
|
|
// Invocations on dynamic are late bound so we can't retrieve the symbol.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Helper.ThrowIfNull(parentSymbol);
|
|
|
|
}
|
2022-08-15 11:57:52 +08:00
|
|
|
|
|
|
|
if (!ShouldCheckTypeArgument(context, parentSyntax, parentSymbol, typeSyntax, typeSymbol, i))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeSymbol is ITypeParameterSymbol typeParamSymbol)
|
|
|
|
{
|
|
|
|
if (!typeParamSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false))
|
|
|
|
{
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.GenericTypeParameterMustBeVariantAnnotatedRule,
|
|
|
|
typeSyntax.GetLocation(),
|
|
|
|
typeSymbol.ToDisplayString()
|
|
|
|
));
|
2022-08-15 11:57:52 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(typeSymbol, typeCache);
|
|
|
|
|
2024-01-24 07:49:34 +08:00
|
|
|
if (marshalType is null)
|
2022-08-15 11:57:52 +08:00
|
|
|
{
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.GenericTypeArgumentMustBeVariantRule,
|
|
|
|
typeSyntax.GetLocation(),
|
|
|
|
typeSymbol.ToDisplayString()
|
|
|
|
));
|
2022-08-15 11:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 09:47:34 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Check if the syntax node is inside a documentation syntax.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="syntax">Syntax node to check.</param>
|
|
|
|
/// <returns><see langword="true"/> if the syntax node is inside a documentation syntax.</returns>
|
|
|
|
private bool IsInsideDocumentation(SyntaxNode? syntax)
|
|
|
|
{
|
|
|
|
while (syntax != null)
|
|
|
|
{
|
|
|
|
if (syntax is DocumentationCommentTriviaSyntax)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
syntax = syntax.Parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:57:52 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Check if the given type argument is being used in a type parameter that contains
|
|
|
|
/// the <c>MustBeVariantAttribute</c>; otherwise, we ignore the attribute.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">Context for a syntax node action.</param>
|
|
|
|
/// <param name="parentSyntax">The parent node syntax that contains the type node syntax.</param>
|
|
|
|
/// <param name="parentSymbol">The symbol retrieved for the parent node syntax.</param>
|
|
|
|
/// <param name="typeArgumentSyntax">The type node syntax of the argument type to check.</param>
|
|
|
|
/// <param name="typeArgumentSymbol">The symbol retrieved for the type node syntax.</param>
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
/// <param name="typeArgumentIndex"></param>
|
2022-08-15 11:57:52 +08:00
|
|
|
/// <returns><see langword="true"/> if the type must be variant and must be analyzed.</returns>
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
private bool ShouldCheckTypeArgument(
|
|
|
|
SyntaxNodeAnalysisContext context,
|
|
|
|
SyntaxNode parentSyntax,
|
|
|
|
ISymbol parentSymbol,
|
|
|
|
TypeSyntax typeArgumentSyntax,
|
|
|
|
ITypeSymbol typeArgumentSymbol,
|
|
|
|
int typeArgumentIndex)
|
2022-08-15 11:57:52 +08:00
|
|
|
{
|
2023-12-08 08:17:09 +08:00
|
|
|
ITypeParameterSymbol? typeParamSymbol = parentSymbol switch
|
2022-08-15 11:57:52 +08:00
|
|
|
{
|
2023-12-08 08:17:09 +08:00
|
|
|
IMethodSymbol methodSymbol when parentSyntax.Parent is AttributeSyntax &&
|
|
|
|
methodSymbol.ContainingType.TypeParameters.Length > 0
|
|
|
|
=> methodSymbol.ContainingType.TypeParameters[typeArgumentIndex],
|
|
|
|
|
|
|
|
IMethodSymbol { TypeParameters.Length: > 0 } methodSymbol
|
|
|
|
=> methodSymbol.TypeParameters[typeArgumentIndex],
|
|
|
|
|
|
|
|
INamedTypeSymbol { TypeParameters.Length: > 0 } typeSymbol
|
|
|
|
=> typeSymbol.TypeParameters[typeArgumentIndex],
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
|
2023-12-08 08:17:09 +08:00
|
|
|
_
|
|
|
|
=> null
|
2022-08-15 11:57:52 +08:00
|
|
|
};
|
|
|
|
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
if (typeParamSymbol != null)
|
2022-08-15 11:57:52 +08:00
|
|
|
{
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
return typeParamSymbol.GetAttributes()
|
|
|
|
.Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false);
|
2022-08-15 11:57:52 +08:00
|
|
|
}
|
|
|
|
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-18 04:12:06 +08:00
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.TypeArgumentParentSymbolUnhandledRule,
|
|
|
|
typeArgumentSyntax.GetLocation(),
|
|
|
|
parentSymbol.ToDisplayString()
|
|
|
|
));
|
|
|
|
|
|
|
|
return false;
|
2022-08-15 11:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|