Add additional diagnostics for invalid shader discovered types

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:33 +02:00
1 親 7318f5bd54
コミット d51c0f5bf0
9ファイル変更
+173 -32

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

+1
ファイルの表示
@@ -47,3 +47,4 @@ CMPSD2D0037 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0038 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0039 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+18 -2
ファイルの表示
@@ -555,16 +555,32 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a method invocation that is not valid from a shader.
/// <para>
/// Format: <c>"The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)"</c>.
/// Format: <c>"The method {0} cannot be used in a D2D1 shader (methods need to either be HLSL intrinsics or with source available for analysis)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidMethodCall = new DiagnosticDescriptor(
id: "CMPSD2D0040",
title: "Invalid method invocation from a D2D1 shader",
messageFormat: "The method {0} cannot be used in a shader (methods need to either be HLSL intrinsics or with source available for analysis)",
messageFormat: "The method {0} cannot be used in a D2D1 shader (methods need to either be HLSL intrinsics or with source available for analysis)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid discovered type.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} uses the invalid type {1}"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidDiscoveredType = new DiagnosticDescriptor(
id: "CMPSD2D0041",
title: "Invalid discovered type",
messageFormat: "The D2D1 shader of type {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "D2D1 shaders can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
@@ -56,7 +56,7 @@ partial class ID2D1ShaderGenerator
var semanticModelProvider = new SemanticModelProvider(compilation);
var valueFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
var declaredTypes = GetDeclaredTypes(discoveredTypes);
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -310,13 +310,20 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Gets the sequence of processed discovered custom types.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="types">The sequence of discovered custom types.</param>
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
private static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(IEnumerable<INamedTypeSymbol> types)
private static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(
ImmutableArray<Diagnostic>.Builder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
IEnumerable<INamedTypeSymbol> types)
{
ImmutableArray<(string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string)>();
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
foreach (var type in HlslKnownTypes.GetCustomTypes(types))
// Process the discovered types
foreach (var type in HlslKnownTypes.GetCustomTypes(types, out invalidTypes))
{
var structType = type.GetFullMetadataName().ToHlslIdentifierName();
var structDeclaration = StructDeclaration(structType);
@@ -349,6 +356,12 @@ partial class ID2D1ShaderGenerator
.ToFullString()));
}
// Process the invalid types
foreach (INamedTypeSymbol invalidType in invalidTypes)
{
diagnostics.Add(InvalidDiscoveredType, structDeclarationSymbol, structDeclarationSymbol, invalidType);
}
return builder.ToImmutable();
}
+25 -7
ファイルの表示
@@ -211,41 +211,58 @@ internal static partial class HlslKnownTypes
/// Gets the sequence of unique custom types from a collection of discovered types.
/// </summary>
/// <param name="discoveredTypes">The input collection of discovered types.</param>
/// <param name="invalidTypes">The collection of discovered invalid types, if any.</param>
/// <returns>The list of unique custom types.</returns>
public static IEnumerable<INamedTypeSymbol> GetCustomTypes(IEnumerable<INamedTypeSymbol> discoveredTypes)
public static IEnumerable<INamedTypeSymbol> GetCustomTypes(IEnumerable<INamedTypeSymbol> discoveredTypes, out IReadOnlyCollection<INamedTypeSymbol> invalidTypes)
{
// Local function to recursively gather nested types
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes)
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes, HashSet<INamedTypeSymbol> invalidTypes)
{
if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
// Check if the type is unsupported
if (!type.IsUnmanagedType ||
type.TypeKind is TypeKind.Enum ||
type.IsGenericType ||
type.IsRefLikeType ||
type.GetFullyQualifiedName().StartsWith("System."))
{
invalidTypes.Add(type);
return;
}
if (!customTypes.Add(type)) return;
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
{
if (field.IsStatic) continue;
ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
ExploreTypes((INamedTypeSymbol)field.Type, customTypes, invalidTypes);
}
}
HashSet<INamedTypeSymbol> customTypes = new(SymbolEqualityComparer.Default);
HashSet<INamedTypeSymbol> invalidTypes2 = new(SymbolEqualityComparer.Default);
// Explore all input types and their nested types
foreach (INamedTypeSymbol type in discoveredTypes)
{
ExploreTypes(type, customTypes);
ExploreTypes(type, customTypes, invalidTypes2);
}
return OrderByDependency(customTypes);
invalidTypes = invalidTypes2;
return OrderByDependency(customTypes, invalidTypes2);
}
/// <summary>
/// Orders the input sequence of types so that they can be declared in HLSL successfully.
/// </summary>
/// <param name="types">The input collection of types to declare.</param>
/// <param name="invalidTypes">The collection of discovered invalid types, if any.</param>
/// <returns>The same list as input, but in a valid HLSL declaration order.</returns>
private static IEnumerable<INamedTypeSymbol> OrderByDependency(IEnumerable<INamedTypeSymbol> types)
private static IEnumerable<INamedTypeSymbol> OrderByDependency(IEnumerable<INamedTypeSymbol> types, IReadOnlyCollection<INamedTypeSymbol> invalidTypes)
{
Queue<(INamedTypeSymbol Type, HashSet<INamedTypeSymbol> Fields)> queue = new();
@@ -267,7 +284,8 @@ internal static partial class HlslKnownTypes
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()) &&
!invalidTypes.Contains(fieldType))
{
_ = dependencies.Add(fieldType);
}
+1
ファイルの表示
@@ -56,3 +56,4 @@ CMPS0046 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0047 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+16
ファイルの表示
@@ -696,4 +696,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "Shaders can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid discovered type.
/// <para>
/// Format: <c>"The compute shader or method {0} uses the invalid type {1}"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidDiscoveredType = new DiagnosticDescriptor(
id: "CMPS0050",
title: "Invalid discovered type",
messageFormat: "The compute shader or method {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Shaders and shader methods can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+16 -3
ファイルの表示
@@ -67,7 +67,7 @@ partial class IShaderGenerator
var sharedBuffers = GetSharedBuffers(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader);
var implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
var declaredTypes = GetDeclaredTypes(discoveredTypes);
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -432,12 +432,19 @@ partial class IShaderGenerator
/// Gets the sequence of processed discovered custom types.
/// </summary>
/// <param name="types">The sequence of discovered custom types.</param>
/// <param name="sourceSymbol">The symbol for the current object being processed.</param>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(IEnumerable<INamedTypeSymbol> types)
internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(
ImmutableArray<Diagnostic>.Builder diagnostics,
ISymbol sourceSymbol,
IEnumerable<INamedTypeSymbol> types)
{
ImmutableArray<(string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string)>();
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
foreach (var type in HlslKnownTypes.GetCustomTypes(types))
// Process the discovered types
foreach (var type in HlslKnownTypes.GetCustomTypes(types, out invalidTypes))
{
var structType = type.GetFullMetadataName().ToHlslIdentifierName();
var structDeclaration = StructDeclaration(structType);
@@ -470,6 +477,12 @@ partial class IShaderGenerator
.ToFullString()));
}
// Process the invalid types
foreach (INamedTypeSymbol invalidType in invalidTypes)
{
diagnostics.Add(InvalidDiscoveredType, sourceSymbol, sourceSymbol, invalidType);
}
return builder.ToImmutable();
}
+10 -9
ファイルの表示
@@ -89,16 +89,20 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
IMethodSymbol methodSymbol,
out ImmutableArray<Diagnostic> diagnostics)
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var semanticModel = new SemanticModelProvider(compilation);
var (entryPoint, dependentMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, constantDefinitions, out diagnostics);
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(discoveredTypes);
var (entryPoint, dependentMethods) = GetProcessedMethods(builder, methodDeclaration, semanticModel, discoveredTypes, constantDefinitions);
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(builder, methodSymbol, discoveredTypes);
var definedConstants = IShaderGenerator.BuildHlslSource.GetDefinedConstants(constantDefinitions);
diagnostics = builder.ToImmutable();
return new(
methodSymbol.GetFullMetadataName(includeParameters: true),
entryPoint,
@@ -110,25 +114,22 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
/// <summary>
/// Gets a sequence of processed methods from a target method declaration.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> instance for the current method.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the method to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and all captured methods).</returns>
private static (string TargetMethod, ImmutableArray<(string Signature, string Definition)> DependentMethods) GetProcessedMethods(
ImmutableArray<Diagnostic>.Builder diagnostics,
MethodDeclarationSyntax methodDeclaration,
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
out ImmutableArray<Diagnostic> diagnostics)
IDictionary<IFieldSymbol, string> constantDefinitions)
{
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, builder);
diagnostics = builder.ToImmutable();
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, diagnostics);
// Rewrite the method syntax tree
var targetMethod = shaderSourceRewriter
+70 -8
ファイルの表示
@@ -283,7 +283,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0010", "CMPS0031", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0010", "CMPS0031", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -310,7 +310,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0010", "CMPS0031", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0010", "CMPS0031", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -337,7 +337,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0011", "CMPS0031", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0011", "CMPS0031", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -867,7 +867,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0028", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0028", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -894,7 +894,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0029", "CMPS0031", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0029", "CMPS0031", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -956,7 +956,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0030", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0030", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -983,7 +983,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0031", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0031", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -1194,7 +1194,7 @@ public class DiagnosticsTests
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0037", "CMPS0047");
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0037", "CMPS0047", "CMPS0050");
}
[TestMethod]
@@ -1602,6 +1602,68 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0049", "CMPS0047");
}
[TestMethod]
public void InvalidDiscoveredType_Primitive()
{
string source = @"
using System;
using ComputeSharp;
namespace ComputeSharp
{
public class ReadWriteBuffer<T> { }
}
namespace MyFancyApp.Sample
{
public struct MyShader : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
public void Execute()
{
buffer[0] = (byte)42;
}
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0047", "CMPS0050");
}
[TestMethod]
public void InvalidDiscoveredType_CustomType()
{
string source = @"
using System;
using ComputeSharp;
namespace ComputeSharp
{
public class ReadWriteBuffer<T> { }
}
namespace MyFancyApp.Sample
{
public struct Foo
{
public DateTime bar;
}
public struct MyShader : IComputeShader
{
public readonly ReadWriteBuffer<float> buffer;
public void Execute()
{
Foo foo = default;
buffer[0] = 42;
}
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0047", "CMPS0050");
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>