Add InvalidEffectDisplayNameValueAnalyzer
このコミットが含まれているのは:
1 親
8cbb3c958e
コミット
9d5e412f7d
6ファイル変更
+155
-28
検索条件に一致するファイルはありません
@@ -66,3 +66,4 @@ CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
|
||||
CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0058 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0059 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0060 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
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 [D2DEffectName] is used with an invalid value.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// Get the [D2DEffectDisplayName] symbol
|
||||
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute") is not { } d2DEffectIdAttributeSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.RegisterSymbolAction(context =>
|
||||
{
|
||||
// Only struct types are possible targets
|
||||
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
|
||||
{
|
||||
// Look for the [D2DEffectDisplayName] use
|
||||
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate the effect display name
|
||||
if (!D2D1EffectMetadataParser.IsValidEffectDisplayName(attributeData))
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidD2DEffectDisplayNameAttributeValue,
|
||||
attributeData.GetLocation()));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}, SymbolKind.NamedType);
|
||||
});
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -38,21 +38,21 @@ public sealed class InvalidEffectIdValueAnalyzer : DiagnosticAnalyzer
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (AttributeData attributeDate in typeSymbol.GetAttributes())
|
||||
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
|
||||
{
|
||||
// Look for the [D2DEffectId] use
|
||||
if (!SymbolEqualityComparer.Default.Equals(attributeDate.AttributeClass, d2DEffectIdAttributeSymbol))
|
||||
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate the GUID text and emit a diagnostic if needed
|
||||
if (attributeDate.ConstructorArguments is not [{ Value: string value }] ||
|
||||
if (attributeData.ConstructorArguments is not [{ Value: string value }] ||
|
||||
!Guid.TryParse(value, out _))
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidD2DEffectIdAttributeValue,
|
||||
attributeDate.GetLocation()));
|
||||
attributeData.GetLocation()));
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
@@ -873,4 +873,20 @@ partial class DiagnosticDescriptors
|
||||
isEnabledByDefault: true,
|
||||
description: "The [D2DEffectId] attribute must use a valid GUID expression as value.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDisplayName]</c> attribute is being used with an invalid value.
|
||||
/// <para>
|
||||
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidD2DEffectDisplayNameAttributeValue = new DiagnosticDescriptor(
|
||||
id: "CMPSD2D0060",
|
||||
title: "Invalid [D2DEffectDisplayName] attribute value",
|
||||
messageFormat: "The [D2DEffectDisplayName] attribute is being used with an invalid value",
|
||||
category: "ComputeSharp.D2D1.Shaders",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "The [D2DEffectDisplayName] attribute must contain valid text.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A helper class to parse D2D metadata values.
|
||||
/// </summary>
|
||||
internal static class D2D1EffectMetadataParser
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> instance to find all newlines.
|
||||
/// </summary>
|
||||
private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="AttributeData"/> value contains a valid effect display name.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <returns>Whether <paramref name="attributeData"/> contains a valid effect display name.</returns>
|
||||
public static bool IsValidEffectDisplayName(AttributeData attributeData)
|
||||
{
|
||||
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
{
|
||||
// Remove new lines (the values cannot span multiple lines in XML)
|
||||
string singleLineValue = NewLinesRegex.Replace(value, string.Empty);
|
||||
|
||||
// Trim directly and check that (no need to escape, as that wouldn't change things)
|
||||
return singleLineValue.AsSpan().Trim().Length > 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect display name for a given attribute.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <param name="effectDisplayName">The resulting defined effect display name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect display name could be found.</returns>
|
||||
public static bool TryGetEffectDisplayName(AttributeData attributeData, [NotNullWhen(true)] out string? effectDisplayName)
|
||||
{
|
||||
// Check that the attribute is [D2DEffectDisplayName] and with a valid parameter
|
||||
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
{
|
||||
// Remove new lines (the values cannot span multiple lines in XML)
|
||||
string singleLineValue = NewLinesRegex.Replace(value, string.Empty);
|
||||
|
||||
// Make sure to escape any invalid XML characters
|
||||
string escapedValue = SecurityElement.Escape(singleLineValue);
|
||||
|
||||
// Trim the display name as well
|
||||
if (escapedValue.AsSpan().Trim() is { Length: > 0 } trimmedValue)
|
||||
{
|
||||
effectDisplayName = trimmedValue.ToString();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
effectDisplayName = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+4
-24
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
@@ -17,11 +15,6 @@ partial class ID2D1ShaderGenerator
|
||||
/// </summary>
|
||||
private static partial class EffectDisplayName
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> instance to find all newlines.
|
||||
/// </summary>
|
||||
private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the effect display name info for the current shader.
|
||||
/// </summary>
|
||||
@@ -55,23 +48,10 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
|
||||
{
|
||||
// Check that the attribute is [D2DEffectDisplayName] and with a valid parameter
|
||||
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, effectIdAttributeSymbol) &&
|
||||
attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
// Check that the attribute is [D2DEffectDisplayName]
|
||||
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, effectIdAttributeSymbol))
|
||||
{
|
||||
// Remove new lines (the values cannot span multiple lines in XML)
|
||||
string singleLineValue = NewLinesRegex.Replace(value, string.Empty);
|
||||
|
||||
// Make sure to escape any invalid XML characters
|
||||
string escapedValue = SecurityElement.Escape(singleLineValue);
|
||||
|
||||
// Trim the display name as well
|
||||
if (escapedValue.AsSpan().Trim() is { Length: > 0 } trimmedValue)
|
||||
{
|
||||
effectDisplayName = trimmedValue.ToString();
|
||||
|
||||
return true;
|
||||
}
|
||||
return D2D1EffectMetadataParser.TryGetEffectDisplayName(attributeData, out effectDisplayName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする