using System; using System.Diagnostics.CodeAnalysis; using System.Security; using System.Text.RegularExpressions; using ComputeSharp.SourceGeneration.Extensions; 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 metadata name. /// /// The input object to inspect. /// Whether contains a valid effect metadata name. public static bool IsValidEffectMetadataName(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 metadata name for a given shader type. /// /// The input object currently in use. /// The input instance. /// The fully qualified metadata name of the target attribute. /// Whether the attribute can also be on the assembly. /// The resulting defined effect metadata name, if found. /// Whether or not a defined effect metadata name could be found. public static bool TryGetEffectMetadataName( Compilation compilation, INamedTypeSymbol typeSymbol, string attributeFullyQualifiedMetadataName, bool isAssemblyLevelAttributeSupported, [NotNullWhen(true)] out string? effectMetadataName) { INamedTypeSymbol metadataAttributeSymbol = compilation.GetTypeByMetadataName(attributeFullyQualifiedMetadataName)!; // Always check the attributes on the type first if (typeSymbol.TryGetAttributeWithType(metadataAttributeSymbol, out AttributeData? attributeData)) { return TryGetEffectMetadataName(attributeData, out effectMetadataName); } // Check assembly-level attributes next, if needed if (isAssemblyLevelAttributeSupported && typeSymbol.ContainingAssembly!.TryGetAttributeWithType(metadataAttributeSymbol, out attributeData)) { return TryGetEffectMetadataName(attributeData, out effectMetadataName); } effectMetadataName = default; return false; } /// /// Tries to get the defined effect metadata name for a given attribute. /// /// The input object to inspect. /// The resulting defined effect metadata name, if found. /// Whether or not a defined effect metadata name could be found. public static bool TryGetEffectMetadataName(AttributeData attributeData, [NotNullWhen(true)] out string? effectMetadataName) { // Check that the attribute has 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) { effectMetadataName = trimmedValue.ToString(); return true; } } effectMetadataName = default; return false; } }