Add new InvalidAssemblyLevelCompileOptionsAnalyzer analyzer

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 13:07:21 +02:00
1 親 65ee05bf10
コミット 5204402c49
3ファイル変更
+52 -37

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

@@ -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;
/// <summary>
/// A diagnostic analyzer that generates an error whenever invalid options are used in an assembly-level [D2D1CompileOptions] attribute.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidAssemblyLevelCompileOptionsAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidPackMatrixColumnMajorOption);
/// <inheritdoc/>
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);
}
}
});
}
}
@@ -78,34 +78,6 @@ partial class ID2D1ShaderGenerator
return null;
}
/// <summary>
/// Gets a <see cref="Diagnostic"/>-s for invalid assembly-level <c>[D2D1CompileOptions]</c> attribute, if one is present.
/// </summary>
/// <param name="assemblySymbol">The input <see cref="IAssemblySymbol"/> instance to process.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>The diagnostic for the attribute, if invalid.</returns>
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;
}
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>
-9
ファイルの表示
@@ -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<Diagnostic> 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