Add diagnostic for invalid return type

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-18 14:26:09 +02:00
1 親 7576cc3045
コミット 140aa10a12
7ファイル変更
+113 -27

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -63,3 +63,4 @@ CMPSD2D0053 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0054 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0055 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+55 -26
ファイルの表示
@@ -24,31 +24,68 @@ partial class D2DPixelShaderSourceGenerator
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the target method.</returns>
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeMethodInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
{
// If there is no precompiled bytecode, just emit a span with a 0 value
string bytecodeLiterals = bytecodeInfo.Bytecode.IsDefaultOrEmpty
? "0"
: ID2D1ShaderGenerator.LoadBytecode.BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
// Get the TypeSyntax for the return type (either ReadOnlySpan<byte>, or an invalid one).
// This is done so that if the user picks an invalid type, the code is still generated,
// just returning default, so that proper diagnostic can be generated, without also
// having hard to understand compiler errors due to the mismatched return type here.
TypeSyntax returnType = bytecodeInfo.InvalidReturnType is string invalidReturnType
? IdentifierName(invalidReturnType)
: GenericName(Identifier("global::System.ReadOnlySpan"))
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)));
// Prepare the fixup function
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
BlockSyntax methodBlock;
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
{
// This code generates the following block:
//
// {
// return default;
// }
methodBlock =
Block(ReturnStatement(
LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword))));
fixup = tree => tree.GetText(Encoding.UTF8);
}
else
{
// This code generates the following block:
//
// {
// return new byte[] { __EMBEDDED_SHADER_BYTECODE };
// }
//
// As in the ID2D1Shader generator, the __EMBEDDED_SHADER_BYTECODE identifier will be replaced with the bytecode
// after the full string for the syntax tree has been generated, to avoid every literal going through a token.
methodBlock =
Block(
ReturnStatement(
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(
InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE"))))));
string bytecodeLiterals = ID2D1ShaderGenerator.LoadBytecode.BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
// Prepare the fixup function
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
}
// This code produces a branch as follows:
//
// /// <inheritdoc/>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <MODIFIERS> global::System.ReadOnlySpan<byte> <METHOD_NAME>()
// {
// return new byte[] { __EMBEDDED_SHADER_BYTECODE };
// }
//
// As in the ID2D1Shader generator, the __EMBEDDED_SHADER_BYTECODE identifier will be replaced with the bytecode
// after the full string for the syntax tree has been generated, to avoid every literal going through a token.
// <MODIFIERS> <RETURN_TYPE> <METHOD_NAME>()
// <METHOD_BODY>
return
MethodDeclaration(
GenericName(Identifier("global::System.ReadOnlySpan"))
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
Identifier(bytecodeInfo.MethodName))
MethodDeclaration(returnType, Identifier(bytecodeInfo.MethodName))
.AddModifiers(bytecodeInfo.Modifiers.Select(Token).ToArray())
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
@@ -61,15 +98,7 @@ partial class D2DPixelShaderSourceGenerator
SyntaxKind.OpenBracketToken,
TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.WithBody(Block(
ReturnStatement(
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(
InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))));
.WithBody(methodBlock);
}
}
}
+29
ファイルの表示
@@ -23,6 +23,35 @@ partial class D2DPixelShaderSourceGenerator
/// </summary>
private static partial class Execute
{
/// <summary>
/// Validates that the return type of the annotated method is valid and returns the type name.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <returns>The HLSL source to compile, if present.</returns>
public static string? GetInvalidReturnType(ImmutableArray<Diagnostic>.Builder diagnostics, IMethodSymbol methodSymbol)
{
if (!(methodSymbol.ReturnType is INamedTypeSymbol
{
Name: "ReadOnlySpan",
ContainingNamespace.Name: "System",
IsGenericType: true,
TypeParameters.Length: 1
} returnType && returnType.TypeArguments[0].SpecialType == SpecialType.System_Byte))
{
diagnostics.Add(
InvalidD2DPixelShaderSourceMethodReturnType,
methodSymbol,
methodSymbol.Name,
methodSymbol.ContainingType,
methodSymbol.ReturnType);
return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
return null;
}
/// <summary>
/// Extracts the HLSL source from a method with the <see cref="D2DPixelShaderSourceAttribute"/> annotation.
/// </summary>
+3
ファイルの表示
@@ -41,6 +41,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
// Get all necessary info for the current shader
ImmutableArray<SyntaxKind> modifiers = item.Syntax.Modifiers.Select(token => token.Kind()).ToImmutableArray();
string methodName = item.Symbol.Name;
string? invalidReturnType = Execute.GetInvalidReturnType(diagnostics, item.Symbol);
string hlslSource = Execute.GetHlslSource(diagnostics, item.Symbol);
D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, item.Symbol);
D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, item.Symbol);
@@ -48,6 +49,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
HlslShaderMethodSourceInfo sourceInfo = new(
modifiers,
methodName,
invalidReturnType,
hlslSource,
shaderProfile,
compileOptions,
@@ -82,6 +84,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
EmbeddedBytecodeMethodInfo bytecodeInfo = new(
item.Source.Modifiers,
item.Source.MethodName,
item.Source.InvalidReturnType,
item.Source.HlslSource,
bytecode);
+16
ファイルの表示
@@ -824,4 +824,20 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "Precompiled shaders using [D2DPixelShaderSource] must explicitly indicate the compile options.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when a method with [D2DPixelShaderSource] has an invalid return type.
/// <para>
/// Format: <c>"The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has an invalid return type {2} (it must return a ReadOnlySpan&lt;byte&gt;)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DPixelShaderSourceMethodReturnType = new DiagnosticDescriptor(
id: "CMPSD2D0057",
title: "Missing compile options for D2D pixel shader source",
messageFormat: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has an invalid return type {2} (it must return a ReadOnlySpan<byte>)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Methods using using [D2DPixelShaderSource] must use ReadOnlySpan<byte> as the return type.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+5 -1
ファイルの表示
@@ -11,13 +11,15 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// A model representing info on a shader method that has requested to be precompiled at build time.
/// </summary>
///<param name="Modifiers">The modifiers for the annotated method.</param>
/// <param name="Modifiers">The modifiers for the annotated method.</param>
/// <param name="MethodName">The name of the annotated method.</param>
/// <param name="InvalidReturnType">The fully qualified name of the return type, if invalid.</param>
/// <param name="HlslSource">The HLSL source.</param>
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
internal sealed record EmbeddedBytecodeMethodInfo(
ImmutableArray<SyntaxKind> Modifiers,
string MethodName,
string? InvalidReturnType,
string HlslSource,
ImmutableArray<byte> Bytecode)
{
@@ -31,6 +33,7 @@ internal sealed record EmbeddedBytecodeMethodInfo(
{
hashCode.AddRange(obj.Modifiers);
hashCode.Add(obj.MethodName);
hashCode.Add(obj.InvalidReturnType);
hashCode.Add(obj.HlslSource);
hashCode.AddBytes(obj.Bytecode.AsSpan());
}
@@ -41,6 +44,7 @@ internal sealed record EmbeddedBytecodeMethodInfo(
return
MemoryMarshal.Cast<SyntaxKind, ushort>(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast<SyntaxKind, ushort>(y.Modifiers.AsSpan())) &&
x.MethodName == y.MethodName &&
x.InvalidReturnType == y.InvalidReturnType &&
x.HlslSource == y.HlslSource &&
x.Bytecode.SequenceEqual(y.Bytecode);
}
+4
ファイルの表示
@@ -12,6 +12,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// </summary>
/// <param name="Modifiers">The modifiers for the annotated method.</param>
/// <param name="MethodName">The name of the annotated method.</param>
/// <param name="InvalidReturnType">The fully qualified name of the return type, if invalid.</param>
/// <param name="HlslSource">The HLSL source.</param>
/// <param name="ShaderProfile">The shader profile to use to compile the shader.</param>
/// <param name="CompileOptions">The compile options to use to compile the shader.</param>
@@ -19,6 +20,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
internal sealed record HlslShaderMethodSourceInfo(
ImmutableArray<SyntaxKind> Modifiers,
string MethodName,
string? InvalidReturnType,
string HlslSource,
D2D1ShaderProfile ShaderProfile,
D2D1CompileOptions CompileOptions,
@@ -34,6 +36,7 @@ internal sealed record HlslShaderMethodSourceInfo(
{
hashCode.AddRange(obj.Modifiers);
hashCode.Add(obj.MethodName);
hashCode.Add(obj.InvalidReturnType);
hashCode.Add(obj.HlslSource);
hashCode.Add(obj.ShaderProfile);
hashCode.Add(obj.CompileOptions);
@@ -46,6 +49,7 @@ internal sealed record HlslShaderMethodSourceInfo(
return
MemoryMarshal.Cast<SyntaxKind, ushort>(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast<SyntaxKind, ushort>(y.Modifiers.AsSpan())) &&
x.MethodName == y.MethodName &&
x.InvalidReturnType == y.InvalidReturnType &&
x.HlslSource == y.HlslSource &&
x.ShaderProfile == y.ShaderProfile &&
x.CompileOptions == y.CompileOptions &&