From 5c32501fd574fe649490bbb2c589e9bd4bf37a04 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 22 Jul 2021 23:34:57 +0200 Subject: [PATCH] Added [SkipLocalsInit] to generated methods, if possible --- .../IShaderGenerator.cs | 13 ++++++++----- tests/ComputeSharp.Tests/ShadersTests.cs | 3 --- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 2035fd0e..9dd8a4c5 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using ComputeSharp.__Internals; using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; @@ -12,8 +11,6 @@ using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; -#pragma warning disable CS0618 - namespace ComputeSharp.SourceGenerators { /// @@ -75,7 +72,7 @@ namespace ComputeSharp.SourceGenerators }; // Method attributes - AttributeListSyntax[] attributes = new[] + List attributes = new() { AttributeList(SingletonSeparatedList( Attribute(IdentifierName("GeneratedCode")).AddArgumentListArguments( @@ -93,6 +90,12 @@ namespace ComputeSharp.SourceGenerators Literal("This method is not intended to be used directly by user code")))))) }; + // Add [SkipLocalsInit] if the target project allows it + if (context.Compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }) + { + attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("SkipLocalsInit"))))); + } + string namespaceName = structDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)); string structName = structDeclaration.Identifier.Text; SyntaxTokenList structModifiers = structDeclaration.Modifiers; @@ -107,7 +110,7 @@ namespace ComputeSharp.SourceGenerators var structDeclarationSyntax = StructDeclaration(structName) .WithModifiers(structModifiers) - .AddMembers(methods.Select(m => m.AddAttributeLists(attributes)).ToArray()); + .AddMembers(methods.Select(m => m.AddAttributeLists(attributes.ToArray())).ToArray()); TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax; diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index e5333572..7300fd6f 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -2,7 +2,6 @@ using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; using ComputeSharp.SwapChain.Shaders; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -11,8 +10,6 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests { [TestClass]