diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
index 03539010..497432fa 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -1,4 +1,4 @@
-; Shipped analyzer releases
+; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
## Release 1.0
@@ -58,3 +58,4 @@ CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0051 | ComputeSharp | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzer.cs
new file mode 100644
index 00000000..08e869b4
--- /dev/null
+++ b/src/ComputeSharp.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.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.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 27588984..e761b882 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -729,4 +729,21 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The ComputeContext type cannot be copied (and values should rather be passed via ref readonly or in instead) and cannot be used as a field of value types (as it could be indirectly copied).",
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: "CMPS0052",
+ 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.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.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index e32d8da4..280a1d1e 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -34,6 +34,12 @@ public sealed partial class IShaderGenerator : 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/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
index b7d90664..18c30552 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
@@ -1804,7 +1804,11 @@ public class DiagnosticsTests
let reference = MetadataReference.CreateFromFile(assembly.Location)
select reference;
- CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
+ CSharpCompilation compilation = CSharpCompilation.Create(
+ "original",
+ new SyntaxTree[] { syntaxTree },
+ references,
+ new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
IIncrementalGenerator generator = new TGenerator();