diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index 57915d4d..b3e8a176 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -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)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs
new file mode 100644
index 00000000..ec1cbabb
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs
@@ -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;
+
+///
+/// A diagnostic analyzer that generates an error whenever [D2DEffectName] is used with an invalid value.
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue);
+
+ ///
+ 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);
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectIdValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectIdValueAnalyzer.cs
index e7b07246..f2f0e57f 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectIdValueAnalyzer.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectIdValueAnalyzer.cs
@@ -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;
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 8b673569..3f1debd2 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -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");
+
+ ///
+ /// Gets a for when the [D2DEffectDisplayName] attribute is being used with an invalid value.
+ ///
+ /// Format: "The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)".
+ ///
+ ///
+ 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");
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs
new file mode 100644
index 00000000..79148ef2
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs
@@ -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;
+
+///
+/// A helper class to parse D2D metadata values.
+///
+internal static class D2D1EffectMetadataParser
+{
+ ///
+ /// A instance to find all newlines.
+ ///
+ private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
+
+ ///
+ /// Checks whether a given value contains a valid effect display name.
+ ///
+ /// The input object to inspect.
+ /// Whether contains a valid effect display name.
+ 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;
+ }
+
+ ///
+ /// Tries to get the defined effect display name for a given attribute.
+ ///
+ /// The input object to inspect.
+ /// The resulting defined effect display name, if found.
+ /// Whether or not a defined effect display name could be found.
+ 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;
+ }
+}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs
index 953abbd6..6fa0b3ad 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs
@@ -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
///
private static partial class EffectDisplayName
{
- ///
- /// A instance to find all newlines.
- ///
- private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
-
///
/// Extracts the effect display name info for the current shader.
///
@@ -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);
}
}