diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index b3e8a176..60e42424 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -67,3 +67,6 @@ CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github 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) +CMPSD2D0061 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0062 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0063 | 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 deleted file mode 100644 index 9fc3bd75..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs +++ /dev/null @@ -1,62 +0,0 @@ -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.IsValidEffectMetadataName(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/InvalidEffectMetadataValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs new file mode 100644 index 00000000..085603ed --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +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 any shader metadata value attributes are used with an invalid value. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer +{ + /// + /// The set of type names for all metadata attributes. + /// + private static readonly ImmutableArray MetadataAttributeTypeNames = ImmutableArray.Create( + "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + "ComputeSharp.D2D1.D2DEffectAuthorAttribute"); + + /// + /// The mapping of diagnostics to their attribute type names. + /// + private static readonly ImmutableDictionary AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(new[] + { + new KeyValuePair("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue), + new KeyValuePair("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue), + new KeyValuePair("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue), + new KeyValuePair("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue) + }); + + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create( + InvalidD2DEffectDisplayNameAttributeValue, + InvalidD2DEffectDescriptionAttributeValue, + InvalidD2DEffectCategoryAttributeValue, + InvalidD2DEffectAuthorAttributeValue); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the metadata attribute symbols + if (!context.Compilation.TryBuildNamedTypeSymbolSet(MetadataAttributeTypeNames, out ImmutableHashSet? typeSymbols)) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types and assemblies are possible targets + if (context.Symbol is not (INamedTypeSymbol { TypeKind: TypeKind.Struct } or IAssemblySymbol)) + { + return; + } + + foreach (AttributeData attributeData in context.Symbol.GetAttributes()) + { + // Only check attributes we care about + if (attributeData.AttributeClass is not INamedTypeSymbol attributeSymbol || + !typeSymbols.Contains(attributeSymbol)) + { + continue; + } + + // Validate the effect display name + if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData)) + { + context.ReportDiagnostic(Diagnostic.Create( + AttributeTypeNameToDiagnosticMapping[attributeSymbol.MetadataName], + attributeData.GetLocation())); + } + + return; + } + }, SymbolKind.NamedType | SymbolKind.Assembly); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 3f1debd2..0cb58f8e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -877,7 +877,7 @@ partial class DiagnosticDescriptors /// /// 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)". + /// Format: "The [D2DEffectDisplayName] attribute is being used with an invalid value". /// /// public static readonly DiagnosticDescriptor InvalidD2DEffectDisplayNameAttributeValue = new DiagnosticDescriptor( @@ -889,4 +889,52 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DEffectDisplayName] attribute must contain valid text.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectDescription] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectDescription] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectDescriptionAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0061", + title: "Invalid [D2DEffectDescription] attribute value", + messageFormat: "The [D2DEffectDescription] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectDescription] attribute must contain valid text.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectCategory] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectDescription] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectCategoryAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0062", + title: "Invalid [D2DEffectCategory] attribute value", + messageFormat: "The [D2DEffectCategory] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectCategory] attribute must contain valid text.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectAuthor] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectAuthor] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectAuthorAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0063", + title: "Invalid [D2DEffectAuthor] attribute value", + messageFormat: "The [D2DEffectAuthor] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectAuthor] 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 index fcb4cba9..018abee1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs @@ -2,6 +2,7 @@ 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; @@ -41,23 +42,29 @@ internal static class D2D1EffectMetadataParser /// 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)!; - foreach (AttributeData attributeData in typeSymbol.GetAttributes()) + // Always check the attributes on the type first + if (typeSymbol.TryGetAttributeWithType(metadataAttributeSymbol, out AttributeData? attributeData)) { - // Check that the attribute is the one we're looking for - if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, metadataAttributeSymbol)) - { - return TryGetEffectMetadataName(attributeData, out effectMetadataName); - } + 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; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs index 9e574e31..d2158878 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs @@ -23,6 +23,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + isAssemblyLevelAttributeSupported: false, out string? effectDisplayName)) { return effectDisplayName; @@ -43,6 +44,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + isAssemblyLevelAttributeSupported: false, out string? effectDescription)) { return effectDescription; @@ -63,6 +65,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + isAssemblyLevelAttributeSupported: true, out string? effectCategory)) { return effectCategory; @@ -83,6 +86,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectAuthorAttribute", + isAssemblyLevelAttributeSupported: true, out string? effectAuthor)) { return effectAuthor; diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs index 2b0def4a..8e589eea 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1; /// /// This only applies to effects created from . /// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] public sealed class D2DEffectAuthorAttribute : Attribute { /// diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs index bb77860d..53708f6c 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1; /// /// This only applies to effects created from . /// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] public sealed class D2DEffectCategoryAttribute : Attribute { /// diff --git a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs index 01f78575..b937a26b 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs @@ -2,6 +2,9 @@ // 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.Generic; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; @@ -11,6 +14,37 @@ namespace ComputeSharp.SourceGeneration.Extensions; /// internal static class CompilationExtensions { + /// + /// Tries to build a set of instances form the input mapping of names. + /// + /// The to consider for analysis. + /// The input fully qualified type names. + /// The resulting mapping of resolved instances. + /// Whether all requested instances could be resolved. + public static bool TryBuildNamedTypeSymbolSet( + this Compilation compilation, + IEnumerable fullyQualifiedTypeNames, + [NotNullWhen(true)] out ImmutableHashSet? typeSymbols) + { + ImmutableHashSet.Builder builder = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); + + foreach (string fullyQualifiedTypeName in fullyQualifiedTypeNames) + { + if (compilation.GetTypeByMetadataName(fullyQualifiedTypeName) is not INamedTypeSymbol typeSymbol) + { + typeSymbols = null; + + return false; + } + + _ = builder.Add(typeSymbol); + } + + typeSymbols = builder.ToImmutable(); + + return true; + } + /// /// /// Checks whether or not a type with a specified metadata name is accessible from a given instance. diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index fa276e2b..a69c739e 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -27,6 +27,30 @@ internal static class ISymbolExtensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } + /// + /// Tries to get an attribute with the specified type. + /// + /// The input instance to check. + /// The instance for the attribute type to look for. + /// The resulting attribute, if it was found. + /// Whether or not has an attribute with the specified name. + public static bool TryGetAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol, [NotNullWhen(true)] out AttributeData? attributeData) + { + foreach (AttributeData attribute in symbol.GetAttributes()) + { + if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, typeSymbol)) + { + attributeData = attribute; + + return true; + } + } + + attributeData = null; + + return false; + } + /// /// Tries to get an attribute with the specified fully qualified metadata name. ///