diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md new file mode 100644 index 00000000..054a5960 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -0,0 +1,2 @@ +; Shipped analyzer releases +; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md new file mode 100644 index 00000000..2b28783f --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -0,0 +1,8 @@ +; Unshipped analyzer release +; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md + +### New Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index b044bc0e..94eb7ab6 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -47,8 +47,8 @@ - - + + diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs new file mode 100644 index 00000000..81a35cae --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -0,0 +1,26 @@ +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGenerators.Diagnostics +{ + /// + /// A container for all instances for errors reported by analyzers in this project. + /// + internal static class DiagnosticDescriptors + { + /// + /// Gets a for an invalid shader field. + /// + /// Format: "The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}". + /// + /// + public static DiagnosticDescriptor InvalidShaderField { get; } = new( + id: "CMPS0001", + title: "Invalid shader field", + messageFormat: "The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A type representing a compute shader contains a field of a type that is not supported in HLSL.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + } +} diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index e08f19e8..e84e89ad 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -31,12 +31,12 @@ namespace ComputeSharp.SourceGenerators.Extensions .WithParameterOptions(SymbolDisplayParameterOptions.None); /// - /// Gets the full metadata name for a given instance. + /// Gets the full metadata name for a given instance. /// - /// The input instance. + /// The input instance. /// The full metadata name for . [Pure] - public static string GetFullMetadataName(this INamedTypeSymbol symbol) + public static string GetFullMetadataName(this ITypeSymbol symbol) { static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder) { diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 028d7ed4..e7bc5c2d 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -141,9 +141,11 @@ namespace ComputeSharp.SourceGenerators foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { - if (fieldSymbol.IsStatic) continue; + if (fieldSymbol.Type is not INamedTypeSymbol { IsStatic: false } typeSymbol) + { + continue; + } - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; string typeName = typeSymbol.GetFullMetadataName(); if (HlslKnownTypes.IsTypedResourceType(typeName)) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index 127677c7..b5dad985 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -120,11 +120,10 @@ namespace ComputeSharp.SourceGenerators { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { - if (fieldSymbol.IsStatic) continue; - - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; - - if (typeSymbol.TypeKind != TypeKind.Delegate) continue; + if (fieldSymbol.Type is not INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false } typeSymbol) + { + continue; + } // hash += hash << 5; yield return ExpressionStatement(ParseExpression($"hash += hash << 5")); diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index 05ec04c3..1a799b82 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Text; using ComputeSharp.__Internals; +using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.SyntaxRewriters; @@ -49,7 +50,7 @@ namespace ComputeSharp.SourceGenerators Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); // Explore the syntax tree and extract the processed info - var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); + var processedMembers = GetProcessedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray(); var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); @@ -82,17 +83,30 @@ namespace ComputeSharp.SourceGenerators /// /// Gets a sequence of captured members and their mapped names. /// + /// The current generator context in use. /// The input instance to process. /// The collection of currently discovered types. /// A sequence of captured members in . [Pure] - private static IEnumerable> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) + private static IEnumerable> GetProcessedMembers( + GeneratorExecutionContext context, + INamedTypeSymbol structDeclarationSymbol, + ICollection types) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { - if (fieldSymbol.IsStatic) continue; + // Captured fields must be named type symbols + if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidShaderField, + fieldSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type)); - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; + continue; + } + + if (fieldSymbol.IsStatic) continue; string typeName = HlslKnownTypes.GetMappedName(typeSymbol); @@ -107,6 +121,14 @@ namespace ComputeSharp.SourceGenerators { types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); } + else if (!typeSymbol.IsUnmanagedType) + { + // Shaders can only capture valid HLSL resource types or unmanaged types + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidShaderField, + fieldSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol, fieldSymbol.Name, typeSymbol)); + } } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 5c40b29d..c5d8de24 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -478,7 +478,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters // Track and rewrite the discarded declaration if (this.semanticModel.GetOperation(node.Expression) is IDiscardOperation operation) { - TypeSyntax typeSyntax = operation.Type.TrackType(this.discoveredTypes); + TypeSyntax typeSyntax = operation.Type!.TrackType(this.discoveredTypes); string identifier = $"__implicit{this.implicitVariables.Count}"; // Add the variable to the list of implicit declarations