diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 82bfaea4..6ca0fc7d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -81,72 +81,31 @@ partial class ID2D1ShaderGenerator } /// - /// Gets an instance producing -s for invalid assembly-level [D2D1CompileOptions] attributes. + /// Gets a -s for invalid assembly-level [D2D1CompileOptions] attribute, if one is present. /// - /// The input instance to use to produce the diagnostics. + /// The input instance to process. + /// The for the operation. /// The diagnostic for the attribute, if invalid. - public static IncrementalValuesProvider GetAssemblyLevelCompileOptionsDiagnostics(SyntaxValueProvider syntaxProvider) + public static Diagnostic? GetAssemblyLevelCompileOptionsDiagnostics(IAssemblySymbol assemblySymbol, CancellationToken cancellationToken) { // In order to emit diagnostics for [D2D1CompileOptions] attributes at the assembly level, the following is needed: - // - The type symbol for the attribute, to be able to check it's actually [D2D1CompileOptions]. - // - The syntax node representing the attribute targeting the assembly, to get a location. - // - The input D2D1CompileOptions value, which can be retrieved as a constant argument to the attribute. - // - // The first step to do this is to find all AttributeListSyntax values and filter those targeting the assembly. - static bool IsAssemblyTargetingAttributeNode(SyntaxNode node, CancellationToken token) + // - The type symbol for the assembly, to get the AttributeData object for the [D2D1CompileOptions] attribute, if used. + // - The syntax node representing the attribute targeting the assembly, to get a location (this is retrieved from the AttributeData). + // - The input D2D1CompileOptions value, which can be retrieved from the constructor arguments of the AttributeData object. + if (assemblySymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { - return node is AttributeListSyntax attributeList && attributeList.Target?.Identifier.IsKind(SyntaxKind.AssemblyKeyword) == true; - } + D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; - // Helper that detects invalid [D2D1CompileOptions] uses and produces a diagnostic - static Diagnostic? TryGetDiagnosticForAssemblyLevelCompileOptions(GeneratorSyntaxContext context, CancellationToken token) - { - Location? location = null; - D2D1CompileOptions? options = null; - - // The input is an AttributeListSyntax object, so first traverse all its containing attributes - foreach (AttributeSyntax attributeSyntax in ((AttributeListSyntax)context.Node).Attributes) + if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) { - // Match the symbol (retrieved from the constructor symbol) to check it's a [D2D1CompileOptions] attribute - if (context.SemanticModel.GetSymbolInfo(attributeSyntax, token).Symbol is IMethodSymbol attributeConstructorSymbol && - attributeConstructorSymbol.ContainingType.HasFullyQualifiedName("ComputeSharp.D2D1.D2DCompileOptionsAttribute")) - { - // Get the value from the expression (get the argument expression and the constant value from there) - if (attributeSyntax.ArgumentList?.Arguments.FirstOrDefault() is AttributeArgumentSyntax argumentSyntax && - context.SemanticModel.GetConstantValue(argumentSyntax.Expression, token) is { HasValue: true, Value: int rawOptions }) - { - options = (D2D1CompileOptions)rawOptions; - } - - // Also get the location from the attribute syntax node - location = attributeSyntax.GetLocation(); - - break; - } - } - - // Check that some options were actually found, and that they were incorrect. - // The attribute can't be repeated, so checking for multiple values isn't needed. - if (options is not null && - (options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) - { - IAssemblySymbol assemblySymbol = context.SemanticModel.Compilation.Assembly; - - // Emit the diagnostic targeting the assembly (instead of a shader type) return Diagnostic.Create( InvalidPackMatrixColumnMajorOption, - location, + attributeData.ApplicationSyntaxReference?.GetSyntax(cancellationToken).GetLocation(), assemblySymbol); } - - return null; } - // Create the incremental collection and only retrieve non null items - return - syntaxProvider - .CreateSyntaxProvider(IsAssemblyTargetingAttributeNode, TryGetDiagnosticForAssemblyLevelCompileOptions) - .Where(static diagnostic => diagnostic is not null)!; + return null; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 1ea0d17d..c9202f24 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -117,7 +117,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics)); // Get all errors for invalid [D2D1CompileOptions] attributes added to the assembly level - IncrementalValuesProvider d2DCompileOptionsAtAssemblyLevelErrors = LoadBytecode.GetAssemblyLevelCompileOptionsDiagnostics(context.SyntaxProvider); + IncrementalValuesProvider d2DCompileOptionsAtAssemblyLevelErrors = + context.CompilationProvider + .SelectMany(static (compilation, token) => ImmutableArray.Create(LoadBytecode.GetAssemblyLevelCompileOptionsDiagnostics(compilation.Assembly, token))) + .Where(static item => item is not null)!; // Output the diagnostics context.ReportDiagnostics(d2DCompileOptionsAtAssemblyLevelErrors.Select(static (item, token) => item));