Skip to content

Commit

Permalink
Disable D2D1 generator if unsafe blocks are not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 24, 2023
1 parent d4f4b31 commit 2fbb84e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
static (node, _) => node.IsTypeDeclarationWithOrPotentiallyWithBaseTypes<StructDeclarationSyntax>(),
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace ComputeSharp.SourceGeneration.Extensions;

Expand All @@ -11,6 +12,16 @@ namespace ComputeSharp.SourceGeneration.Extensions;
/// </summary>
internal static class CompilationExtensions
{
/// <summary>
/// Checks whether the <c>AllowUnsafeBlocks</c> option is set for a given compilation.
/// </summary>
/// <param name="compilation">The <see cref="Compilation"/> object to check.</param>
/// <returns>Whether the <c>AllowUnsafeBlocks</c> option is set for <paramref name="compilation"/>.</returns>
public static bool IsAllowUnsafeBlocksEnabled(this Compilation compilation)
{
return compilation.Options is CSharpCompilationOptions { AllowUnsafe: true };
}

/// <summary>
/// <para>
/// Checks whether or not a type with a specified metadata name is accessible from a given <see cref="Compilation"/> instance.
Expand Down

0 comments on commit 2fbb84e

Please sign in to comment.