diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index e98b7f21..82766b2e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -74,3 +74,4 @@ CMPSD2D0064 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0065 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0066 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0067 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0068 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs similarity index 58% rename from src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs rename to src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs index 6252c770..00dfe59a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs @@ -7,12 +7,12 @@ namespace ComputeSharp.D2D1.SourceGenerators; /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase +public sealed class NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase { /// - /// Creates a new instance. + /// Creates a new instance. /// - public NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer() + public NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer() : base(NotAccessibleTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute, "ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") { } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..d01b4a97 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer : NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleFieldTypeInTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute, "ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") + { + } +} \ 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 b167e000..9b4bda1c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1008,4 +1008,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DGeneratedPixelShaderDescriptor] attribute requires target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a field in a type using the [D2DGeneratedPixelShaderDescriptor] attribute has a type that is not accessible from its containing assembly. + /// + /// Format: "The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleFieldTypeInTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPSD2D0068", + title: "Invalid [D2DGeneratedPixelShaderDescriptor] attribute target", + messageFormat: """The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)""", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 702fe210..86c8f648 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -9,6 +9,7 @@ ComputeSharp.SourceGeneration.Hlsl + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs new file mode 100644 index 00000000..b0165b0f --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -0,0 +1,91 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ComputeSharp.SourceGeneration.Diagnostics; + +/// +/// A diagnostic analyzer that generates an error if a field in a target shader type is of a type that is not accessible from its containing assembly. +/// +public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +{ + /// + /// The instance to use. + /// + private readonly DiagnosticDescriptor diagnosticDescriptor; + + /// + /// The fully qualified type name of the target attribute. + /// + private readonly string generatedShaderDescriptorFullyQualifiedTypeName; + + /// + /// Creates a new instance with the specified arguments. + /// + /// The instance to use. + /// The fully qualified type name of the target attribute. + private protected NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) + { + this.diagnosticDescriptor = diagnosticDescriptor; + this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; + + SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); + } + + /// + public sealed override ImmutableArray SupportedDiagnostics { get; } + + /// + public sealed override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(context => + { + // Get the symbol for the target attribute type + if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // If the target type is not using the attribute, there's nothing left to do + if (!typeSymbol.TryGetAttributeWithType(generatedShaderDescriptorAttributeSymbol, out AttributeData? attribute)) + { + return; + } + + foreach (ISymbol memberSymbol in typeSymbol.GetMembers()) + { + // Only select allowed fields (ie. skip constants, static fields, etc.) + if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol fieldTypeSymbol, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }) + { + continue; + } + + // Emit a diagnostic if the field type is not accessible + if (!fieldTypeSymbol.IsAccessibleFromCompilationAssembly(context.Compilation)) + { + context.ReportDiagnostic(Diagnostic.Create( + this.diagnosticDescriptor, + attribute.GetLocation(), + typeSymbol, + memberSymbol.Name, + fieldTypeSymbol)); + } + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 558e8bbf..52063552 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -34,6 +34,17 @@ internal static class ISymbolExtensions return compilation.IsSymbolAccessibleWithin(symbol, assemblySymbol); } + /// + /// Checks whether a given symbol is accessible from the assembly of a given compilation (including eg. through nested types). + /// + /// The input instance. + /// The instance currently in use. + /// Whether is accessible from the assembly for . + public static bool IsAccessibleFromCompilationAssembly(this ISymbol symbol, Compilation compilation) + { + return compilation.IsSymbolAccessibleWithin(symbol, compilation.Assembly); + } + /// /// Gets the fully qualified name for a given symbol. /// diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 731e8941..d89faabf 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -61,3 +61,4 @@ CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser CMPS0053 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0055 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..dd0672aa --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer : NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleFieldTypeInTargetTypeForGeneratedComputeShaderDescriptorAttribute, "ComputeSharp.GeneratedComputeShaderDescriptorAttribute") + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 60469ac0..e6536130 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -784,4 +784,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [GeneratedComputeShaderDescriptor] attribute requires target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a field in a type using the [GeneratedComputeShaderDescriptor] attribute has a type that is not accessible from its containing assembly. + /// + /// Format: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleFieldTypeInTargetTypeForGeneratedComputeShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPS0056", + title: "Invalid [GeneratedComputeShaderDescriptor] attribute target", + messageFormat: """The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)""", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file