From 7a630204efc159cc0d890bedee84402778407586 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 22:58:13 +0100 Subject: [PATCH 1/2] Add MultipleComputeShaderInterfacesOnShaderTypeAnalyzer --- ...uteShaderInterfacesOnShaderTypeAnalyzer.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs new file mode 100644 index 00000000..de85a567 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MultipleComputeShaderInterfacesOnShaderTypeAnalyzer.cs @@ -0,0 +1,72 @@ +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +/// A diagnostic analyzer that generates an error when a shader type using [GeneratedComputeShaderDescriptor] is implementing multiple shader interfaces. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class MultipleComputeShaderInterfacesOnShaderTypeAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MultipleShaderTypesImplemented); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the IComputeShader, IComputeShader, [GeneratedComputeShaderDescriptor] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.GeneratedComputeShaderDescriptorAttribute") is not { } generatedComputeShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // We only want to check types using [GeneratedComputeShaderDescriptor] + if (!typeSymbol.HasAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol)) + { + return; + } + + int shaderInferfaceCount = 0; + + // Count how many shader interfaces are implemented + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol) || + SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) + { + shaderInferfaceCount++; + } + } + + // Emit a diagnostic if there's more than one + if (shaderInferfaceCount > 1) + { + context.ReportDiagnostic(Diagnostic.Create( + MultipleShaderTypesImplemented, + typeSymbol.Locations.FirstOrDefault(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file From 86bc68e4dddf0441a8968f6534599dceb4cb950e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 22:58:22 +0100 Subject: [PATCH 2/2] Move MultipleShaderTypesImplemented to analyzer --- ...uteShaderDescriptorGenerator.HlslSource.cs | 6 ----- .../DiagnosticsTests.cs | 20 ---------------- .../Test_Analyzers.cs | 23 +++++++++++++++++++ .../Test_D2DPixelShaderSourceGenerator.cs | 1 + 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index c9a1fc0b..25ee4f1c 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -58,12 +58,6 @@ partial class ComputeShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); - // A given type can only represent a single shader type - if (structDeclarationSymbol.AllInterfaces.Count(static interfaceSymbol => interfaceSymbol is { Name: nameof(IComputeShader) } or { IsGenericType: true, Name: nameof(IComputeShader) }) > 1) - { - diagnostics.Add(MultipleShaderTypesImplemented, structDeclarationSymbol, structDeclarationSymbol); - } - // Explore the syntax tree and extract the processed info SemanticModelProvider semanticModelProvider = new(compilation); INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IComputeShader) }); diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index b4d99408..e42ca2c2 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1116,26 +1116,6 @@ public class DiagnosticsTests VerifyGeneratedDiagnostics(source, "CMPS0041", "CMPS0047"); } - [TestMethod] - public void MultipleShaderTypes() - { - const string source = """ - using ComputeSharp; - - namespace MyFancyApp.Sample; - - [GeneratedComputeShaderDescriptor] - public partial struct MyShader : IComputeShader, IComputeShader - { - public void Execute() - { - } - } - """; - - VerifyGeneratedDiagnostics(source, "CMPS0042", "CMPS0047"); - } - [TestMethod] [DataRow(-1, 1, 1)] [DataRow(1, -1, 1)] diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs index 343c521b..a26475ab 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs @@ -391,4 +391,27 @@ public class Test_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task MultipleComputeShaderInterfacesOnShader_TwoInterfaces() + { + const string source = """ + using ComputeSharp; + + [GeneratedComputeShaderDescriptor] + internal partial struct {|CMPS0042:MyType|} : IComputeShader, IComputeShader + { + public void Execute() + { + } + + Float4 IComputeShader.Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index 5e4d6743..5446cee7 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -899,6 +899,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);