Add new InvalidD2DResourceTextureIndexUseAnalyzer analyzer
このコミットが含まれているのは:
1 親
5204402c49
コミット
f27e98f2fa
3ファイル変更
+52
-38
検索条件に一致するファイルはありません
+52
@@ -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 System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Mappings;
|
||||
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 [D2DResourceTextureIndex] is used incorrectly to annotate an invalid field.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidD2DResourceTextureIndexUseAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DResourceTextureIndexAttributeUse);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterSymbolAction(static context =>
|
||||
{
|
||||
IFieldSymbol fieldSymbol = (IFieldSymbol)context.Symbol;
|
||||
|
||||
if (fieldSymbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute"))
|
||||
{
|
||||
string metadataName = fieldSymbol.Type.GetFullMetadataName();
|
||||
|
||||
if (!HlslKnownTypes.IsResourceTextureType(metadataName))
|
||||
{
|
||||
Diagnostic diagnostic = Diagnostic.Create(
|
||||
InvalidD2DResourceTextureIndexAttributeUse,
|
||||
fieldSymbol.Locations.FirstOrDefault(),
|
||||
fieldSymbol.Name,
|
||||
fieldSymbol.ContainingType,
|
||||
fieldSymbol.Type);
|
||||
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
}
|
||||
}
|
||||
}, SymbolKind.Field);
|
||||
}
|
||||
}
|
||||
-25
@@ -106,30 +106,5 @@ partial class ID2D1ShaderGenerator
|
||||
diagnostics.Add(RepeatedD2DResourceTextureIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the diagnostics for a field with invalid <c>[D2DResourceTextureIndex]</c> attribute use, if that is the case.
|
||||
/// </summary>
|
||||
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
|
||||
/// <returns>The resulting <see cref="Diagnostic"/> instance for <paramref name="fieldSymbol"/>, if needed.</returns>
|
||||
public static Diagnostic? GetDiagnosticForFieldWithInvalidD2DResourceTextureIndexAttribute(IFieldSymbol fieldSymbol)
|
||||
{
|
||||
if (fieldSymbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute"))
|
||||
{
|
||||
string metadataName = fieldSymbol.Type.GetFullMetadataName();
|
||||
|
||||
if (!HlslKnownTypes.IsResourceTextureType(metadataName))
|
||||
{
|
||||
return Diagnostic.Create(
|
||||
InvalidD2DResourceTextureIndexAttributeUse,
|
||||
fieldSymbol.Locations.FirstOrDefault(),
|
||||
fieldSymbol.Name,
|
||||
fieldSymbol.ContainingType,
|
||||
fieldSymbol.Type);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -366,18 +366,5 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetPixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Get all diagnostics for invalid [D2DResourceTextureIndex] uses
|
||||
IncrementalValuesProvider<Diagnostic> invalidD2DResourceTextureIndexUseErrors =
|
||||
context.SyntaxProvider
|
||||
.CreateSyntaxProvider(
|
||||
static (node, _) => node is FieldDeclarationSyntax { AttributeLists.Count: > 0 },
|
||||
static (context, _) => ((FieldDeclarationSyntax)context.Node).Declaration.Variables.Select(v => (IFieldSymbol)context.SemanticModel.GetDeclaredSymbol(v)!))
|
||||
.SelectMany(static (item, _) => item)
|
||||
.Select(static (item, token) => LoadResourceTextureDescriptions.GetDiagnosticForFieldWithInvalidD2DResourceTextureIndexAttribute(item))
|
||||
.Where(static diagnostic => diagnostic is not null)!;
|
||||
|
||||
// Output the diagnostics
|
||||
context.ReportDiagnostics(invalidD2DResourceTextureIndexUseErrors);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする