Add diagnostics for [D2DCompileOptions] at assembly level

このコミットが含まれているのは:
Sergio Pedri committed 2022-05-24 22:10:15 +02:00
1 親 f5ea4b7911
コミット 7411c1fe0d
3ファイル変更
+79 -3

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

@@ -11,6 +11,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -79,6 +80,75 @@ partial class ID2D1ShaderGenerator
return null;
}
/// <summary>
/// Gets an <see cref="IncrementalValuesProvider{TValues}"/> instance producing <see cref="Diagnostic"/>-s for invalid assembly-level <c>[D2D1CompileOptions]</c> attributes.
/// </summary>
/// <param name="syntaxProvider">The input <see cref="SyntaxValueProvider"/> instance to use to produce the diagnostics.</param>
/// <returns>The diagnostic for the attribute, if invalid.</returns>
public static IncrementalValuesProvider<Diagnostic> GetAssemblyLevelCompileOptionsDiagnostics(SyntaxValueProvider syntaxProvider)
{
// 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)
{
return node is AttributeListSyntax attributeList && attributeList.Target?.Identifier.IsKind(SyntaxKind.AssemblyKeyword) == true;
}
// 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)
{
// 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,
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)!;
}
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>