diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs new file mode 100644 index 00000000..602437a9 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates an error whenever invalid options are used in an assembly-level [D2D1CompileOptions] attribute. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidAssemblyLevelCompileOptionsAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidPackMatrixColumnMajorOption); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationAction(static context => + { + IAssemblySymbol assemblySymbol = context.Compilation.Assembly; + + // In order to emit diagnostics for [D2D1CompileOptions] attributes at the assembly level, the following is needed: + // - 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)) + { + D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + + if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + Diagnostic diagnostic = Diagnostic.Create( + InvalidPackMatrixColumnMajorOption, + attributeData.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation(), + assemblySymbol); + + context.ReportDiagnostic(diagnostic); + } + } + }); + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index a8dd4db6..8b7be511 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -78,34 +78,6 @@ partial class ID2D1ShaderGenerator return null; } - /// - /// Gets a -s for invalid assembly-level [D2D1CompileOptions] attribute, if one is present. - /// - /// The input instance to process. - /// The for the operation. - /// The diagnostic for the attribute, if invalid. - 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 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)) - { - D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; - - if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) - { - return Diagnostic.Create( - InvalidPackMatrixColumnMajorOption, - attributeData.ApplicationSyntaxReference?.GetSyntax(cancellationToken).GetLocation(), - assemblySymbol); - } - } - - return null; - } - /// /// Extracts the metadata definition for the current shader. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index e070c17b..e372204e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -128,15 +128,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics)); - // Get all errors for invalid [D2D1CompileOptions] attributes added to the assembly level - 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)); - // Filter all items to enable caching at a coarse level, and remove diagnostics IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors