diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 60e42424..7a211015 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -70,3 +70,4 @@ CMPSD2D0060 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github 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) +CMPSD2D0064 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs new file mode 100644 index 00000000..b2a162b5 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs @@ -0,0 +1,33 @@ +using System.Collections.Immutable; +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 if the AllowUnsafeBlocks compilation option is not set. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption); + + /// + public override void Initialize(AnalysisContext context) + { + 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.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 0cb58f8e..a5110fe2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -937,4 +937,21 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DEffectAuthor] attribute must contain valid text.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the AllowUnsafeBlocks option is not set. + /// + /// Format: "Unsafe blocks must be enabled for the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)". + /// + /// + public static readonly DiagnosticDescriptor MissingAllowUnsafeBlocksOption = new DiagnosticDescriptor( + id: "CMPSD2D0064", + title: "Missing 'AllowUnsafeBlocks' compilation option", + messageFormat: "Unsafe blocks must be enabled for the source generators to emit valid code (add true to your .csproj/.props file)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Unsafe blocks must be enabled for the source generators to emit valid code (the true option must be set in the .csproj/.props file).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp", + customTags: WellKnownDiagnosticTags.CompilationEnd); } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index d9f603b0..33cb52ea 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -36,6 +36,12 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator static (node, _) => node.IsTypeDeclarationWithOrPotentiallyWithBaseTypes(), static (context, token) => { + // The source generator requires unsafe blocks to be enabled (eg. for pointers, [SkipLocalsInit], etc.) + if (!context.SemanticModel.Compilation.IsAllowUnsafeBlocksEnabled()) + { + return default; + } + StructDeclarationSyntax typeDeclaration = (StructDeclarationSyntax)context.Node; // If the type symbol doesn't have at least one interface, it can't possibly be a shader type diff --git a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs index 10714715..0c10fb39 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; namespace ComputeSharp.SourceGeneration.Extensions; @@ -45,6 +46,16 @@ internal static class CompilationExtensions return true; } + /// + /// Checks whether the AllowUnsafeBlocks option is set for a given compilation. + /// + /// The object to check. + /// Whether the AllowUnsafeBlocks option is set for . + public static bool IsAllowUnsafeBlocksEnabled(this Compilation compilation) + { + return compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }; + } + /// /// /// Checks whether or not a type with a specified metadata name is accessible from a given instance.