From d5ddf8bf1edab16343a2e247d9b4ede1ad2a4429 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 01:59:45 +0200 Subject: [PATCH 1/5] Share MissingAllowUnsafeBlocksCompilationOptionAnalyzer --- ...owUnsafeBlocksCompilationOptionAnalyzer.cs | 30 ++++-------- ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + ...safeBlocksCompilationOptionAnalyzerBase.cs | 47 +++++++++++++++++++ ...owUnsafeBlocksCompilationOptionAnalyzer.cs | 30 ++++-------- 4 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs index b2a162b5..54cf04c6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs @@ -1,33 +1,19 @@ -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Diagnostics; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; -/// -/// A diagnostic analyzer that generates an error if the AllowUnsafeBlocks compilation option is not set. -/// +/// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer +public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase { - /// - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption); - - /// - public override void Initialize(AnalysisContext context) + /// + /// Creates a new instance. + /// + public MissingAllowUnsafeBlocksCompilationOptionAnalyzer() + : base(MissingAllowUnsafeBlocksOption) { - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.EnableConcurrentExecution(); - - context.RegisterCompilationAction(static context => - { - // Check whether unsafe blocks are available, and emit an error if they are not - if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) - { - context.ReportDiagnostic(Diagnostic.Create(MissingAllowUnsafeBlocksOption, location: null)); - } - }); } } \ 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 5c009c45..5caeb67c 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/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs new file mode 100644 index 00000000..9f60a6f3 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs @@ -0,0 +1,47 @@ +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 the AllowUnsafeBlocks compilation option is not set. +/// +public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : DiagnosticAnalyzer +{ + /// + /// The instance to use. + /// + private readonly DiagnosticDescriptor diagnosticDescriptor; + + /// + /// Creates a new instance with the specified arguments. + /// + /// The instance to use. + protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) + { + this.diagnosticDescriptor = diagnosticDescriptor; + + 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.RegisterCompilationAction(context => + { + // Check whether unsafe blocks are available, and emit an error if they are not + if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) + { + context.ReportDiagnostic(Diagnostic.Create(this.diagnosticDescriptor, location: null)); + } + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs index 08e869b4..2473abff 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs @@ -1,33 +1,19 @@ -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Diagnostics; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.SourceGenerators; -/// -/// A diagnostic analyzer that generates an error if the AllowUnsafeBlocks compilation option is not set. -/// +/// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer +public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase { - /// - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption); - - /// - public override void Initialize(AnalysisContext context) + /// + /// Creates a new instance. + /// + public MissingAllowUnsafeBlocksCompilationOptionAnalyzer() + : base(MissingAllowUnsafeBlocksOption) { - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.EnableConcurrentExecution(); - - context.RegisterCompilationAction(static context => - { - // Check whether unsafe blocks are available, and emit an error if they are not - if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) - { - context.ReportDiagnostic(Diagnostic.Create(MissingAllowUnsafeBlocksOption, location: null)); - } - }); } } \ No newline at end of file From e85c8f870a15dcacb9901e57eee756d2f14129ea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 02:43:27 +0200 Subject: [PATCH 2/5] Add NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase --- .../MissingAttributeCodeFixer.cs | 2 +- .../AnalyzerReleases.Shipped.md | 1 + .../D2DPixelShaderDescriptorGenerator.cs | 6 ++ ...ShaderDescriptorAttributeTargetAnalyzer.cs | 19 +++++ .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++ ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + ...safeBlocksCompilationOptionAnalyzerBase.cs | 2 +- ...erDescriptorAttributeTargetAnalyzerBase.cs | 75 +++++++++++++++++++ .../Extensions/ISymbolExtensions.cs | 17 +++++ .../AnalyzerReleases.Shipped.md | 1 + .../ComputeShaderDescriptorGenerator.cs | 6 ++ ...ShaderDescriptorAttributeTargetAnalyzer.cs | 4 +- ...ShaderDescriptorAttributeTargetAnalyzer.cs | 19 +++++ .../Diagnostics/DiagnosticDescriptors.cs | 18 ++++- 14 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs create mode 100644 src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs create mode 100644 src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs diff --git a/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs b/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs index ade028d3..20a485e4 100644 --- a/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs +++ b/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs @@ -40,7 +40,7 @@ public abstract class MissingAttributeCodeFixer : CodeFixProvider /// The title to display for the code fixer. /// The fully qualified type name of the attribute to add. /// The fully qualified type names of leading attributes to skip. - protected MissingAttributeCodeFixer( + private protected MissingAttributeCodeFixer( string diagnosticId, string codeActionTitle, string attributeFullyQualifiedMetadataName, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 53d2153b..e98b7f21 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -73,3 +73,4 @@ CMPSD2D0063 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0064 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) 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) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 5ddf70ba..8ab8cc4f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -43,6 +43,12 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene return default; } + // Immediately bail if the target type doesn't have internal accessibility + if (!typeSymbol.IsAccessibleFromContainingAssembly(context.SemanticModel.Compilation)) + { + return default; + } + // Check that the shader implements the ID2D1PixelShader interface if (!typeSymbol.HasInterfaceWithType(context.SemanticModel.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader")!)) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..6252c770 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute, "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 e395e8a6..b167e000 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -992,4 +992,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DGeneratedPixelShaderDescriptor] attribute must be used on types that implement the ID2D1PixelShader interface.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DGeneratedPixelShaderDescriptor] attribute is being used on a type that is not accessible from its containing assembly. + /// + /// Format: "The [D2DGeneratedPixelShaderDescriptor] attribute requires target types to be accessible from their containing assembly (type {0} has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPSD2D0067", + title: "Invalid [D2DGeneratedPixelShaderDescriptor] attribute target", + messageFormat: "The [D2DGeneratedPixelShaderDescriptor] attribute requires target types to be accessible from their containing assembly (type {0} has less effective accessibility than internal)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DGeneratedPixelShaderDescriptor] attribute requires 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 5caeb67c..702fe210 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/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs index 9f60a6f3..1b719501 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs @@ -19,7 +19,7 @@ public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : Di /// Creates a new instance with the specified arguments. /// /// The instance to use. - protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) + private protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) { this.diagnosticDescriptor = diagnosticDescriptor; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs new file mode 100644 index 00000000..9e050c6c --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -0,0 +1,75 @@ +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 target shader type is not accessible from its containing assembly. +/// +public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase : 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 NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + 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; + } + + // Emit a diagnostic if the target type is using the attribute but is not accessible + if (typeSymbol.TryGetAttributeWithType(generatedShaderDescriptorAttributeSymbol, out AttributeData? attribute) && + !typeSymbol.IsAccessibleFromContainingAssembly(context.Compilation)) + { + context.ReportDiagnostic(Diagnostic.Create( + this.diagnosticDescriptor, + attribute.GetLocation(), + typeSymbol)); + } + }, 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 a0dd6fbe..558e8bbf 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -17,6 +17,23 @@ internal static class ISymbolExtensions genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers); + /// + /// Checks whether a given symbol is accessible from its containing assembly (including eg. through nested types). + /// + /// The input instance. + /// The instance currently in use. + /// Whether is accessible from its containing assembly. + public static bool IsAccessibleFromContainingAssembly(this ISymbol symbol, Compilation compilation) + { + // If the symbol is associated across multiple assemblies, it must be accessible + if (symbol.ContainingAssembly is not IAssemblySymbol assemblySymbol) + { + return true; + } + + return compilation.IsSymbolAccessibleWithin(symbol, assemblySymbol); + } + /// /// 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 5afa651d..731e8941 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -60,3 +60,4 @@ CMPS0051 | ComputeSharp | Error | [Documentation](https://github.com/Sergio0694/ CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) 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) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index c4a64b2c..d25f8e90 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -43,6 +43,12 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener return default; } + // Immediately bail if the target type doesn't have internal accessibility + if (!typeSymbol.IsAccessibleFromContainingAssembly(context.SemanticModel.Compilation)) + { + return default; + } + // Check whether type is a compute shader, and if so, if it's pixel shader like if (!TryGetIsPixelShaderLike(typeSymbol, context.SemanticModel.Compilation, out bool isPixelShaderLike)) { diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs index 988ccd78..28f94f81 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.SourceGenerators; public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : DiagnosticAnalyzer { /// - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidGeneratedPixelShaderDescriptorAttributeTarget); + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidGeneratedComputeShaderDescriptorAttributeTarget); /// public override void Initialize(AnalysisContext context) @@ -49,7 +49,7 @@ public sealed class InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyz if (!MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol)) { context.ReportDiagnostic(Diagnostic.Create( - InvalidGeneratedPixelShaderDescriptorAttributeTarget, + InvalidGeneratedComputeShaderDescriptorAttributeTarget, attribute.GetLocation(), typeSymbol)); } diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..cef801ce --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleTargetTypeForGeneratedComputeShaderDescriptorAttribute, "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 5002c477..60469ac0 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -759,7 +759,7 @@ partial class DiagnosticDescriptors /// Format: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader<TPixel> interface are valid)". /// /// - public static readonly DiagnosticDescriptor InvalidGeneratedPixelShaderDescriptorAttributeTarget = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor InvalidGeneratedComputeShaderDescriptorAttributeTarget = new DiagnosticDescriptor( id: "CMPS0054", title: "Invalid [GeneratedComputeShaderDescriptor] attribute target", messageFormat: "The type {0} is not a valid target for the [GeneratedComputeShaderDescriptor] attribute (only types implementing the IComputeShader or IComputeShader interface are valid)", @@ -768,4 +768,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [GeneratedComputeShaderDescriptor] attribute must be used on types that implement the IComputeShader or IComputeShader interfaces.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [GeneratedComputeShaderDescriptor] attribute is being used on a type that is not accessible from its containing assembly. + /// + /// Format: "The [GeneratedComputeShaderDescriptor] attribute requires target types to be accessible from their containing assembly (type {0} has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleTargetTypeForGeneratedComputeShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPS0055", + title: "Invalid [GeneratedComputeShaderDescriptor] attribute target", + messageFormat: "The [GeneratedComputeShaderDescriptor] attribute requires target types to be accessible from their containing assembly (type {0} has less effective accessibility than internal)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [GeneratedComputeShaderDescriptor] attribute requires target types to be accessible from their containing assembly.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file From cea408a4d4cbde468a85e818bc66f0fbce79cd26 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 03:00:45 +0200 Subject: [PATCH 3/5] Update tests and samples to use internal accessibility --- .../D2D1PixelShaderTests.cs | 8 ++--- .../D2D1EffectRegistrationDataTests.cs | 6 ++-- .../D2D1PixelShaderEffectTests.cs | 16 ++++----- .../D2D1PixelShaderTests.cs | 36 +++++++++---------- .../D2D1ReflectionServicesTests.cs | 4 +-- .../D2D1ResourceTextureManagerTests.cs | 14 ++++---- .../D2D1TransformMapperTests.cs | 4 +-- .../Effects/BokehBlurEffect.cs | 4 +-- .../Tests/CanvasEffectTests.cs | 2 +- .../Tests/PixelShaderEffectTests.cs | 10 +++--- .../DeviceDisposalTests.cs | 4 +-- 11 files changed, 54 insertions(+), 54 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs index e84fdad4..d202251b 100644 --- a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs @@ -50,7 +50,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(1)] [D2DInputSimple(2)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithNoCompileAttributes : ID2D1PixelShader + internal readonly partial struct ShaderWithNoCompileAttributes : ID2D1PixelShader { public float4 Execute() { @@ -96,7 +96,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(2)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithOverriddenProfile : ID2D1PixelShader + internal readonly partial struct ShaderWithOverriddenProfile : ID2D1PixelShader { public float4 Execute() { @@ -142,7 +142,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(2)] [D2DCompileOptions(D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithOverriddenOptions : ID2D1PixelShader + internal readonly partial struct ShaderWithOverriddenOptions : ID2D1PixelShader { public float4 Execute() { @@ -179,7 +179,7 @@ public partial class D2D1PixelShaderTests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DCompileOptions(D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithOverriddenProfileAndOptions : ID2D1PixelShader + internal readonly partial struct ShaderWithOverriddenProfileAndOptions : ID2D1PixelShader { public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs index c0cd430a..181e7a8f 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs @@ -53,7 +53,7 @@ public partial class D2D1EffectRegistrationDataTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private readonly partial struct TestRegistrationBlobShader : ID2D1PixelShader + internal readonly partial struct TestRegistrationBlobShader : ID2D1PixelShader { private readonly float a; @@ -99,7 +99,7 @@ public partial class D2D1EffectRegistrationDataTests [D2DEffectAuthor("Bob Ross")] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private readonly partial struct TestRegistrationBlobWithCustomMetadataShader : ID2D1PixelShader + internal readonly partial struct TestRegistrationBlobWithCustomMetadataShader : ID2D1PixelShader { private readonly float a; @@ -196,7 +196,7 @@ public partial class D2D1EffectRegistrationDataTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private readonly partial struct TestRegistrationBlobWithResourceTextures : ID2D1PixelShader + internal readonly partial struct TestRegistrationBlobWithResourceTextures : ID2D1PixelShader { [D2DResourceTextureIndex(0)] public readonly D2D1ResourceTexture1D t0; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index f968d244..9df99f1d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -122,7 +122,7 @@ public partial class D2D1PixelShaderEffectTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct NullConstantBufferShader : ID2D1PixelShader + internal partial struct NullConstantBufferShader : ID2D1PixelShader { private float dummy; @@ -154,7 +154,7 @@ public partial class D2D1PixelShaderEffectTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct ConstantBufferSizeTestShader : ID2D1PixelShader + internal partial struct ConstantBufferSizeTestShader : ID2D1PixelShader { private float a; private float b; @@ -188,7 +188,7 @@ public partial class D2D1PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithDefaultEffectId : ID2D1PixelShader + internal partial struct ShaderWithDefaultEffectId : ID2D1PixelShader { public float4 Execute() { @@ -198,7 +198,7 @@ public partial class D2D1PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithDefaultEffectId2 : ID2D1PixelShader + internal partial struct ShaderWithDefaultEffectId2 : ID2D1PixelShader { public float4 Execute() { @@ -222,7 +222,7 @@ public partial class D2D1PixelShaderEffectTests [D2DInputCount(0)] [D2DEffectId("8E1F7F49-EF0D-4242-8912-08ADA36AB4EC")] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithExplicitEffectId : ID2D1PixelShader + internal partial struct ShaderWithExplicitEffectId : ID2D1PixelShader { public float4 Execute() { @@ -241,7 +241,7 @@ public partial class D2D1PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithDefaultEffectDisplayName : ID2D1PixelShader + internal partial struct ShaderWithDefaultEffectDisplayName : ID2D1PixelShader { public float4 Execute() { @@ -261,7 +261,7 @@ public partial class D2D1PixelShaderEffectTests [D2DInputCount(0)] [D2DEffectDisplayName("Fancy blur")] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithExplicitEffectDisplayName1 : ID2D1PixelShader + internal partial struct ShaderWithExplicitEffectDisplayName1 : ID2D1PixelShader { public float4 Execute() { @@ -284,7 +284,7 @@ public partial class D2D1PixelShaderEffectTests [D2DEffectCategory("Test effects!")] [D2DEffectAuthor("Bob \r\nRoss")] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWithExplicitEffectDisplayName2 : ID2D1PixelShader + internal partial struct ShaderWithExplicitEffectDisplayName2 : ID2D1PixelShader { public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 1aef404c..15524477 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -67,7 +67,7 @@ public partial class D2D1PixelShaderTests [D2DInputComplex(5)] [D2DInputSimple(6)] [D2DGeneratedPixelShaderDescriptor] - partial struct ShaderWithMultipleInputs : ID2D1PixelShader + internal partial struct ShaderWithMultipleInputs : ID2D1PixelShader { public Float4 Execute() { @@ -95,7 +95,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - partial struct EmptyShader : ID2D1PixelShader + internal partial struct EmptyShader : ID2D1PixelShader { public Float4 Execute() { @@ -106,7 +106,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DOutputBuffer(D2D1BufferPrecision.UInt16Normalized)] [D2DGeneratedPixelShaderDescriptor] - partial struct OnlyBufferPrecisionShader : ID2D1PixelShader + internal partial struct OnlyBufferPrecisionShader : ID2D1PixelShader { public Float4 Execute() { @@ -117,7 +117,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DOutputBuffer(D2D1ChannelDepth.Four)] [D2DGeneratedPixelShaderDescriptor] - partial struct OnlyChannelDepthShader : ID2D1PixelShader + internal partial struct OnlyChannelDepthShader : ID2D1PixelShader { public Float4 Execute() { @@ -128,7 +128,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DOutputBuffer(D2D1BufferPrecision.UInt8NormalizedSrgb, D2D1ChannelDepth.One)] [D2DGeneratedPixelShaderDescriptor] - partial struct CustomBufferOutputShader : ID2D1PixelShader + internal partial struct CustomBufferOutputShader : ID2D1PixelShader { public Float4 Execute() { @@ -195,7 +195,7 @@ public partial class D2D1PixelShaderTests [D2DInputDescription(5, D2D1Filter.MinMagPointMipLinear)] [D2DInputDescription(6, D2D1Filter.MinPointMagMipLinear, LevelOfDetailCount = 3)] [D2DGeneratedPixelShaderDescriptor] - partial struct ShaderWithInputDescriptions : ID2D1PixelShader + internal partial struct ShaderWithInputDescriptions : ID2D1PixelShader { public Float4 Execute() { @@ -257,7 +257,7 @@ public partial class D2D1PixelShaderTests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - partial struct ShaderWithoutResourceTextures : ID2D1PixelShader + internal partial struct ShaderWithoutResourceTextures : ID2D1PixelShader { public Float4 Execute() { @@ -269,7 +269,7 @@ public partial class D2D1PixelShaderTests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader + internal partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader { [D2DResourceTextureIndex(0)] D2D1ResourceTexture1D myTexture; @@ -288,7 +288,7 @@ public partial class D2D1PixelShaderTests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - partial struct ShaderWithResourceTextures : ID2D1PixelShader + internal partial struct ShaderWithResourceTextures : ID2D1PixelShader { float number; @@ -332,7 +332,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithoutEmbeddedBytecode : ID2D1PixelShader + internal readonly partial struct ShaderWithoutEmbeddedBytecode : ID2D1PixelShader { public float4 Execute() { @@ -404,7 +404,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(2)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithEmbeddedBytecode : ID2D1PixelShader + internal readonly partial struct ShaderWithEmbeddedBytecode : ID2D1PixelShader { public float4 Execute() { @@ -454,7 +454,7 @@ public partial class D2D1PixelShaderTests [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithEmbeddedBytecodeAndCompileOptions : ID2D1PixelShader + internal readonly partial struct ShaderWithEmbeddedBytecodeAndCompileOptions : ID2D1PixelShader { public float4 Execute() { @@ -486,7 +486,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct SimpleShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader + internal readonly partial struct SimpleShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader { public float4 Execute() { @@ -498,7 +498,7 @@ public partial class D2D1PixelShaderTests [D2DInputComplex(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ComplexShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader + internal readonly partial struct ComplexShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader { public float4 Execute() { @@ -524,7 +524,7 @@ public partial class D2D1PixelShaderTests [D2DInputSimple(0)] [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithExplicitCompileOptionsAndNoShaderProfile : ID2D1PixelShader + internal readonly partial struct ShaderWithExplicitCompileOptionsAndNoShaderProfile : ID2D1PixelShader { public float4 Execute() { @@ -573,7 +573,7 @@ public partial class D2D1PixelShaderTests [D2DInputDescription(0, D2D1Filter.MinMagMipPoint)] [D2DPixelOptions(D2D1PixelOptions.TrivialSampling)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ShaderWithNoCapturedValues : ID2D1PixelShader + internal readonly partial struct ShaderWithNoCapturedValues : ID2D1PixelShader { public float4 Execute() { @@ -822,7 +822,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader + internal readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader { public readonly int x; public readonly int y; @@ -915,7 +915,7 @@ public partial class D2D1PixelShaderTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private readonly partial struct ShaderWithScalarVectorAndMatrixTypesInNestedStructs : ID2D1PixelShader + internal readonly partial struct ShaderWithScalarVectorAndMatrixTypesInNestedStructs : ID2D1PixelShader { public readonly int x; public readonly int y; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index 3e3947d2..384a9944 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -64,7 +64,7 @@ public partial class D2D1ReflectionServicesTests [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader41)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ReflectedShader : ID2D1PixelShader + internal readonly partial struct ReflectedShader : ID2D1PixelShader { private readonly float2 offset; @@ -127,7 +127,7 @@ public partial class D2D1ReflectionServicesTests [D2DInputCount(1)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - private readonly partial struct ReflectedShaderWithDoubleOperations : ID2D1PixelShader + internal readonly partial struct ReflectedShaderWithDoubleOperations : ID2D1PixelShader { private readonly double amount; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 81ee741a..91767537 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -171,7 +171,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - private partial struct DummyShaderWithResourceTexture : ID2D1PixelShader + internal partial struct DummyShaderWithResourceTexture : ID2D1PixelShader { [D2DResourceTextureIndex(0)] private D2D1ResourceTexture2D source; @@ -460,7 +460,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - private partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader + internal partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] private D2D1ResourceTexture2D source; @@ -536,7 +536,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader + internal partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader { private int height; @@ -624,7 +624,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader + internal partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader { private int width; @@ -794,7 +794,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader + internal partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader { private int width; private int height; @@ -901,7 +901,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader + internal partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader { private int width; private int height; @@ -946,7 +946,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - private partial struct NullResourceTextureShader : ID2D1PixelShader + internal partial struct NullResourceTextureShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] private D2D1ResourceTexture2D source; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs index f567f2b2..364892eb 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs @@ -195,7 +195,7 @@ public partial class D2D1TransformMapperTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct DummyShader : ID2D1PixelShader + internal partial struct DummyShader : ID2D1PixelShader { public float4 Execute() { @@ -286,7 +286,7 @@ public partial class D2D1TransformMapperTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct ShaderWithDispatchArea : ID2D1PixelShader + internal partial struct ShaderWithDispatchArea : ID2D1PixelShader { public int Width; public int Height; diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index d006faad..7dd4ea6e 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -482,7 +482,7 @@ public sealed partial class BokehBlurEffect /// /// The vertical convolution shader and transform. /// - private sealed partial class VerticalConvolution + internal sealed partial class VerticalConvolution { /// /// The for the shader. @@ -530,7 +530,7 @@ public sealed partial class BokehBlurEffect /// /// The horizontal convolutin and partial accumulation effect and transform. /// - private sealed partial class HorizontalConvolutionAndAccumulatePartials + internal sealed partial class HorizontalConvolutionAndAccumulatePartials { /// /// The for the shader. diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs index 8f549f15..ec58ee95 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs @@ -693,7 +693,7 @@ public partial class CanvasEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct ShaderWithNoInputs : ID2D1PixelShader + internal partial struct ShaderWithNoInputs : ID2D1PixelShader { public int Value; diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs index f44b0b74..24674079 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs @@ -456,7 +456,7 @@ public partial class PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct ShaderWithSomeProperties : ID2D1PixelShader + internal partial struct ShaderWithSomeProperties : ID2D1PixelShader { public float A; public int B; @@ -473,7 +473,7 @@ public partial class PixelShaderEffectTests [D2DInputCount(1)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - private partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader + internal partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader { public int Number; @@ -485,7 +485,7 @@ public partial class PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWith0Inputs : ID2D1PixelShader + internal partial struct ShaderWith0Inputs : ID2D1PixelShader { public float4 Execute() { @@ -496,7 +496,7 @@ public partial class PixelShaderEffectTests [D2DInputCount(1)] [D2DInputSimple(0)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWith1Input : ID2D1PixelShader + internal partial struct ShaderWith1Input : ID2D1PixelShader { public float4 Execute() { @@ -508,7 +508,7 @@ public partial class PixelShaderEffectTests [D2DInputSimple(0)] [D2DInputSimple(1)] [D2DGeneratedPixelShaderDescriptor] - private partial struct ShaderWith2Inputs : ID2D1PixelShader + internal partial struct ShaderWith2Inputs : ID2D1PixelShader { public float4 Execute() { diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs index 312509ea..a6287dda 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs @@ -146,7 +146,7 @@ public partial class DeviceDisposalTests [EmbeddedBytecode(DispatchAxis.X)] [GeneratedComputeShaderDescriptor] [AutoConstructor] - private readonly partial struct InitializeShader : IComputeShader + internal readonly partial struct InitializeShader : IComputeShader { private readonly ReadWriteBuffer buffer; @@ -179,7 +179,7 @@ public partial class DeviceDisposalTests [EmbeddedBytecode(DispatchAxis.XY)] [GeneratedComputeShaderDescriptor] - private partial struct HelloWorldShader : IComputeShader + internal partial struct HelloWorldShader : IComputeShader { public float4 Execute() { From d076ce5c054a2e6b9ab91fa76633c40380517d26 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 11:52:31 +0200 Subject: [PATCH 4/5] Add test for new accessibility analyzer --- .../AnalyzerTests.cs | 36 ++++++++++++ ...ComputeSharp.Tests.SourceGenerators.csproj | 5 ++ ...lyzerWithLanguageVersionTest{TAnalyzer}.cs | 55 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs create mode 100644 tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpAnalyzerWithLanguageVersionTest{TAnalyzer}.cs diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs new file mode 100644 index 00000000..6861d648 --- /dev/null +++ b/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs @@ -0,0 +1,36 @@ +using System.Threading.Tasks; +using ComputeSharp.SourceGenerators; +using ComputeSharp.Tests.SourceGenerators.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.SourceGenerators; + +[TestClass] +[TestCategory("Analyzers")] +public class AnalyzerTests +{ + [TestMethod] + public async Task InvalidShaderField() + { + const string source = """ + using ComputeSharp; + + namespace MyFancyApp.Sample; + + public partial class Foo + { + [{|CMPS0055:GeneratedComputeShaderDescriptor|}] + private partial struct MyShader : IComputeShader + { + public ReadWriteBuffer buffer; + + public void Execute() + { + } + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/ComputeSharp.Tests.SourceGenerators.csproj b/tests/ComputeSharp.Tests.SourceGenerators/ComputeSharp.Tests.SourceGenerators.csproj index 9b923a51..361b6d24 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/ComputeSharp.Tests.SourceGenerators.csproj +++ b/tests/ComputeSharp.Tests.SourceGenerators/ComputeSharp.Tests.SourceGenerators.csproj @@ -1,10 +1,15 @@ net7.0 + + https://api.nuget.org/v3/index.json; + https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json; + + diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpAnalyzerWithLanguageVersionTest{TAnalyzer}.cs b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpAnalyzerWithLanguageVersionTest{TAnalyzer}.cs new file mode 100644 index 00000000..7988be13 --- /dev/null +++ b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpAnalyzerWithLanguageVersionTest{TAnalyzer}.cs @@ -0,0 +1,55 @@ +extern alias Core; +extern alias D3D12; + +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CSharp.Testing; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing.Verifiers; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.Tests.SourceGenerators.Helpers; + +/// +/// A custom that uses a specific C# language version to parse code. +/// +/// The type of the analyzer to test. +internal sealed class CSharpAnalyzerWithLanguageVersionTest : CSharpAnalyzerTest + where TAnalyzer : DiagnosticAnalyzer, new() +{ + /// + /// The C# language version to use to parse code. + /// + private readonly LanguageVersion languageVersion; + + /// + /// Creates a new instance with the specified paramaters. + /// + /// The C# language version to use to parse code. + private CSharpAnalyzerWithLanguageVersionTest(LanguageVersion languageVersion) + { + this.languageVersion = languageVersion; + } + + /// + protected override ParseOptions CreateParseOptions() + { + return new CSharpParseOptions(this.languageVersion, DocumentationMode.Diagnose); + } + + /// + /// The source code to analyze. + /// The language version to use to run the test. + public static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion = LanguageVersion.CSharp11) + { + CSharpAnalyzerWithLanguageVersionTest test = new(languageVersion) { TestCode = source }; + + test.TestState.ReferenceAssemblies = ReferenceAssemblies.Net.Net70; + test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location)); + test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(D3D12::ComputeSharp.IComputeShader).Assembly.Location)); + + return test.RunAsync(CancellationToken.None); + } +} From 0a04db2a5bfb0d9a8d44c80e8351fae26a69bf23 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 11:54:46 +0200 Subject: [PATCH 5/5] Restore MissingPixelShaderDescriptorOnPixelShaderCodeFixer --- ...teSharp.D2D1.Tests.SourceGenerators.csproj | 10 ++++++--- ...lShaderDescriptorOnPixelShaderCodeFixer.cs | 22 +++++++------------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/ComputeSharp.D2D1.Tests.SourceGenerators.csproj b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/ComputeSharp.D2D1.Tests.SourceGenerators.csproj index b1e979a9..eaff9edc 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/ComputeSharp.D2D1.Tests.SourceGenerators.csproj +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/ComputeSharp.D2D1.Tests.SourceGenerators.csproj @@ -1,15 +1,19 @@ net7.0 + + https://api.nuget.org/v3/index.json; + https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json; + - - - + + + diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index 81b96c3e..461490bc 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -2,6 +2,7 @@ extern alias Core; extern alias D2D1; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Testing; using Microsoft.VisualStudio.TestTools.UnitTesting; using CSharpCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest< ComputeSharp.D2D1.SourceGenerators.MissingPixelShaderDescriptorOnPixelShaderAnalyzer, @@ -14,7 +15,6 @@ namespace ComputeSharp.D2D1.Tests.SourceGenerators; public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task FileContainsComputeSharpD2D1UsingDirective() { string original = """ @@ -40,7 +40,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -51,7 +51,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task FileContainsComputeSharpD2D1UsingDirective_MultipleOccurrences() { string original = """ @@ -102,7 +101,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -113,7 +112,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task FileDoesNotContainComputeSharpD2D1UsingDirective() { string original = """ @@ -137,7 +135,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -148,7 +146,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task FileDoesNotContainComputeSharpD2D1UsingDirective_MultipleOccurrences() { string original = """ @@ -196,7 +193,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -207,7 +204,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task InsertsAttributeAfterAllOtherD2DAttributes() { string original = """ @@ -237,7 +233,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -248,7 +244,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task InsertsAttributeAfterAllOtherD2DAttributes_WithOtherNonD2DAttribute() { string original = """ @@ -290,7 +285,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); @@ -301,7 +296,6 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer } [TestMethod] - [Ignore("Missing .NET 7 reference assemblies.")] public async Task InsertsAttributeAfterAllOtherD2DAttributes_WithFakeAttributes() { string original = """ @@ -355,7 +349,7 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer { TestCode = original, FixedCode = @fixed, - //ReferenceAssemblies = ReferenceAssemblies.Net.Net70 + ReferenceAssemblies = ReferenceAssemblies.Net.Net70 }; test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly);