diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
index a1d77353..c5fc51d1 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
@@ -35,4 +35,8 @@ CMPS0027 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator,
CMPS0028 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0029 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0030 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
-CMPS0031 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
\ No newline at end of file
+CMPS0031 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0032 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0033 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0034 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0035 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 1a60fbce..119f197f 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -442,5 +442,57 @@ namespace ComputeSharp.SourceGenerators.Diagnostics
isEnabledByDefault: true,
description: "Only unmanaged value type objects can be declared in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for a pointer type.
+ ///
+ public static DiagnosticDescriptor PointerType { get; } = new(
+ id: "CMPS0032",
+ title: "Pointer type",
+ messageFormat: "A compute shader cannot have a pointer type declaration",
+ category: typeof(IComputeShader).FullName,
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A compute shader cannot have a pointer type declaration.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for a function pointer type.
+ ///
+ public static DiagnosticDescriptor FunctionPointer { get; } = new(
+ id: "CMPS0033",
+ title: "Function pointer type",
+ messageFormat: "A compute shader cannot have a function pointer type declaration",
+ category: typeof(IComputeShader).FullName,
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A compute shader cannot have a function pointer type declaration.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for an unsafe statement.
+ ///
+ public static DiagnosticDescriptor UnsafeStatement { get; } = new(
+ id: "CMPS0034",
+ title: "Unsafe statement",
+ messageFormat: "A compute shader cannot have an unsafe statement",
+ category: typeof(IComputeShader).FullName,
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A compute shader cannot have an unsafe statement.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for an unsafe modifier on a method or function.
+ ///
+ public static DiagnosticDescriptor UnsafeModifierOnMethodOrFunction { get; } = new(
+ id: "CMPS0035",
+ title: "Unsafe modifier on method or function",
+ messageFormat: "The unsafe modifier cannot be used in methods or functions used in a compute shader",
+ category: typeof(IComputeShader).FullName,
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "The unsafe modifier cannot be used in methods or functions used in a compute shader.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
}
diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
index 77831764..86418169 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
@@ -44,7 +44,20 @@ namespace ComputeSharp.SourceGenerators.Extensions
public static TRoot ReplaceAndTrackType(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel, ICollection discoveredTypes)
where TRoot : SyntaxNode
{
- ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(sourceType).Type!;
+ // Skip immediately for function pointers
+ if (sourceType is FunctionPointerTypeSyntax)
+ {
+ return node.ReplaceNode(targetType, ParseTypeName("void*"));
+ }
+
+ // Handle the various possible type kinds
+ ITypeSymbol typeSymbol = sourceType switch
+ {
+ RefTypeSyntax refType => semanticModel.GetTypeInfo(refType.Type).Type!,
+ PointerTypeSyntax pointerType => semanticModel.GetTypeInfo(pointerType.ElementType).Type!,
+ _ => semanticModel.GetTypeInfo(sourceType).Type!
+ };
+
string typeName = typeSymbol.ToDisplayString(ISymbolExtensions.FullyQualifiedWithoutGlobalFormat);
discoveredTypes.Add((INamedTypeSymbol)typeSymbol);
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 4b5c1337..c7e4f57f 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -106,8 +106,6 @@ namespace ComputeSharp.SourceGenerators
if (attribute is not null)
{
context.ReportDiagnostic(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name);
-
- continue;
}
// Captured fields must be named type symbols
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 8815b3ac..75870fd3 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -155,12 +155,16 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
var updatedNode = (MethodDeclarationSyntax?)base.Visit(node);
-
- if (node!.Modifiers.Contains(Token(SyntaxKind.AsyncKeyword)))
+ if (node!.Modifiers.Any(m => m.IsKind(SyntaxKind.AsyncKeyword)))
{
this.context.ReportDiagnostic(AsyncModifierOnMethodOrFunction, node);
}
+ if (node!.Modifiers.Any(m => m.IsKind(SyntaxKind.UnsafeKeyword)))
+ {
+ this.context.ReportDiagnostic(UnsafeModifierOnMethodOrFunction, node);
+ }
+
if (updatedNode is not null)
{
var implicitBlock = Block(this.implicitVariables.Select(static v => LocalDeclarationStatement(v)).ToArray());
@@ -252,6 +256,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (ImplicitObjectCreationExpressionSyntax)base.VisitImplicitObjectCreationExpression(node)!;
+ if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType: false } type)
+ {
+ this.context.ReportDiagnostic(InvalidObjectCreationExpression, node, type);
+ }
+
TypeSyntax explicitType = IdentifierName("").ReplaceAndTrackType(node, this.semanticModel, this.discoveredTypes);
// Mutate the syntax like with explicit object creation expressions
@@ -345,11 +354,16 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
.WithAttributeLists(List())
.WithIdentifier(Identifier($"__{this.currentMethod!.Identifier.Text}__{node.Identifier.Text}"));
- if (node.Modifiers.Contains(Token(SyntaxKind.AsyncKeyword)))
+ if (node.Modifiers.Any(m => m.IsKind(SyntaxKind.AsyncKeyword)))
{
this.context.ReportDiagnostic(AsyncModifierOnMethodOrFunction, node);
}
+ if (node.Modifiers.Any(m => m.IsKind(SyntaxKind.UnsafeKeyword)))
+ {
+ this.context.ReportDiagnostic(UnsafeModifierOnMethodOrFunction, node);
+ }
+
this.localFunctionDepth--;
// HLSL doesn't support local functions, so we first process them as usual and then remove
@@ -762,6 +776,30 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return base.VisitYieldStatement(node);
}
+ ///
+ public override SyntaxNode? VisitFunctionPointerType(FunctionPointerTypeSyntax node)
+ {
+ this.context.ReportDiagnostic(DiagnosticDescriptors.FunctionPointer, node);
+
+ return base.VisitFunctionPointerType(node);
+ }
+
+ ///
+ public override SyntaxNode? VisitPointerType(PointerTypeSyntax node)
+ {
+ this.context.ReportDiagnostic(DiagnosticDescriptors.PointerType, node);
+
+ return base.VisitPointerType(node);
+ }
+
+ ///
+ public override SyntaxNode? VisitUnsafeStatement(UnsafeStatementSyntax node)
+ {
+ this.context.ReportDiagnostic(DiagnosticDescriptors.UnsafeStatement, node);
+
+ return base.VisitUnsafeStatement(node);
+ }
+
///
public override SyntaxToken VisitToken(SyntaxToken token)
{
diff --git a/tests/ComputeSharp.Tests.Internals.SourceGenerators/SourceGeneratorTests.cs b/tests/ComputeSharp.Tests.Internals.SourceGenerators/AutoConstructorGeneratorTests.cs
similarity index 96%
rename from tests/ComputeSharp.Tests.Internals.SourceGenerators/SourceGeneratorTests.cs
rename to tests/ComputeSharp.Tests.Internals.SourceGenerators/AutoConstructorGeneratorTests.cs
index 82a2a0a1..487b41c1 100644
--- a/tests/ComputeSharp.Tests.Internals.SourceGenerators/SourceGeneratorTests.cs
+++ b/tests/ComputeSharp.Tests.Internals.SourceGenerators/AutoConstructorGeneratorTests.cs
@@ -10,11 +10,11 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Internals
{
[TestClass]
- [TestCategory("SourceGenerator")]
- public class SourceGeneratorTests
+ [TestCategory("AutoConstructorGenerator")]
+ public class AutoConstructorGeneratorTests
{
[TestMethod]
- public void SequentialEnum()
+ public void GenerateConstructor()
{
string source = @"
using System.Numerics;
diff --git a/tests/ComputeSharp.Tests.Internals.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.Internals.SourceGenerators/DiagnosticsTests.cs
new file mode 100644
index 00000000..0004739d
--- /dev/null
+++ b/tests/ComputeSharp.Tests.Internals.SourceGenerators/DiagnosticsTests.cs
@@ -0,0 +1,1104 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using System.Linq;
+using ComputeSharp.SourceGenerators;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.Tests.Internals
+{
+ [TestClass]
+ [TestCategory("Diagnostics")]
+ public class DiagnosticsTests
+ {
+ [TestMethod]
+ public void InvalidShaderField()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+ public string text;
+
+ public void Execute() { }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0001");
+ }
+
+ [TestMethod]
+ public void InvalidGroupSharedFieldType()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ public class GroupSharedAttribute : Attribute { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ [GroupShared]
+ public static string text;
+
+ public void Execute() { }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0002");
+ }
+
+ [TestMethod]
+ public void InvalidGroupSharedFieldElementType()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ public class GroupSharedAttribute : Attribute { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ [GroupShared]
+ public static string[] text;
+
+ public void Execute() { }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0003");
+ }
+
+ [TestMethod]
+ public void InvalidGroupSharedFieldDeclaration()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ public class GroupSharedAttribute : Attribute { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ [GroupShared]
+ public ReadWriteBuffer buffer;
+
+ public void Execute() { }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0004");
+ }
+
+ [TestMethod]
+ public void MissingShaderResources()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public void Execute() { }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0005");
+ }
+
+ [TestMethod]
+ [DataRow(nameof(ThreadIds), "CMPS0006")]
+ [DataRow(nameof(GroupIds), "CMPS0007")]
+ [DataRow(nameof(GroupSize), "CMPS0008")]
+ [DataRow(nameof(WarpIds), "CMPS0009")]
+ public void InvalidDispatchInfoUsage_LocalFunction(string typeName, string diagnosticsId)
+ {
+ string source = $@"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {{
+ public class ReadWriteBuffer {{ }}
+ public static class {typeName} {{ public static int X => 0; }}
+ }}
+
+ namespace MyFancyApp.Sample
+ {{
+ public struct MyShader : IComputeShader
+ {{
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {{
+ static int Fail() => {typeName}.X;
+
+ Fail();
+ }}
+ }}
+ }}";
+
+ VerifyGeneratedDiagnostics(source, diagnosticsId);
+ }
+
+ [TestMethod]
+ [DataRow(nameof(ThreadIds), "CMPS0006")]
+ [DataRow(nameof(GroupIds), "CMPS0007")]
+ [DataRow(nameof(GroupSize), "CMPS0008")]
+ [DataRow(nameof(WarpIds), "CMPS0009")]
+ public void InvalidDispatchInfoUsage_StaticMethod(string typeName, string diagnosticsId)
+ {
+ string source = $@"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {{
+ public class ReadWriteBuffer {{ }}
+ public static class {typeName} {{ public static int X => 0; }}
+ }}
+
+ namespace MyFancyApp.Sample
+ {{
+ public struct MyShader : IComputeShader
+ {{
+ public ReadWriteBuffer buffer;
+
+ public static int Fail() => {typeName}.X;
+
+ public void Execute() => Fail();
+ }}
+ }}";
+
+ VerifyGeneratedDiagnostics(source, diagnosticsId);
+ }
+
+ [TestMethod]
+ public void InvalidObjectCreationExpression_Explicit()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ object o = new object();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0010");
+ }
+
+ [TestMethod]
+ public void InvalidObjectCreationExpression_Implicit()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ object o = new();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0010");
+ }
+
+ [TestMethod]
+ public void AnonymousObjectCreationExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ var o = new { Age = 12 };
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0011");
+ }
+
+ [TestMethod]
+ public void AsyncModifierOnMethodOrFunction_LocalFunction()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ static async void Fail() { }
+
+ Fail();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0012");
+ }
+
+ [TestMethod]
+ public void AsyncModifierOnMethodOrFunction_StaticMethod()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public static async void Fail() { }
+
+ public void Execute() => Fail();
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0012");
+ }
+
+ [TestMethod]
+ public void AwaitExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public async void Execute()
+ {
+ await null;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0012", "CMPS0013");
+ }
+
+ [TestMethod]
+ [DataRow("checked")]
+ [DataRow("unchecked")]
+ public void CheckedExpression(string text)
+ {
+ string source = $@"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {{
+ public class ReadWriteBuffer {{ }}
+ }}
+
+ namespace MyFancyApp.Sample
+ {{
+ public struct MyShader : IComputeShader
+ {{
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {{
+ int a = {text}(1 + 1);
+ }}
+ }}
+ }}";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0014");
+ }
+
+ [TestMethod]
+ [DataRow("checked")]
+ [DataRow("unchecked")]
+ public void CheckedStatement(string text)
+ {
+ string source = $@"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {{
+ public class ReadWriteBuffer {{ }}
+ }}
+
+ namespace MyFancyApp.Sample
+ {{
+ public struct MyShader : IComputeShader
+ {{
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {{
+ {text}
+ {{
+ int a = 1 + 1;
+ }}
+ }}
+ }}
+ }}";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0015");
+ }
+
+ [TestMethod]
+ public void FixedStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public int[] Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ fixed (void* p = buffer.Data)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0016", "CMPS0032");
+ }
+
+ [TestMethod]
+ public void ForEachStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public int[] Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ foreach (int i in buffer.Data)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0017");
+ }
+
+ [TestMethod]
+ public void LockStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ lock (buffer)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0018");
+ }
+
+ [TestMethod]
+ public void QueryExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public int[] Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ _ = from x in buffer.Data select x;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0019");
+ }
+
+ [TestMethod]
+ public void RangeExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public int[] Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ _ = buffer.Data[1..];
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0020");
+ }
+
+ [TestMethod]
+ public void RecursivePattern()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public object Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ if (buffer.Data is int[] { Length: 50 } foo)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0021");
+ }
+
+ [TestMethod]
+ public void RefType()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public ref T this[int x] => throw null;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ ref float x = ref buffer[0];
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0022");
+ }
+
+ [TestMethod]
+ public void RelationalPattern()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer
+ {
+ public int Data;
+ }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ if (buffer.Data is > 0 and < 10)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0023");
+ }
+
+ [TestMethod]
+ public void SizeOfExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ _ = sizeof(float);
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0024");
+ }
+
+ [TestMethod]
+ public void StackAllocArrayCreationExpression()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ int* p = stackalloc int[10];
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0032", "CMPS0025");
+ }
+
+ [TestMethod]
+ public void ThrowExpressionOrStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ throw null;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0026");
+ }
+
+ [TestMethod]
+ public void TryStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ try
+ {
+ }
+ catch
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0027");
+ }
+
+ [TestMethod]
+ public void TupleType()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ (int, float) foo;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0028");
+ }
+
+ [TestMethod]
+ public void UsingDeclaration()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ using IDisposable foo = null
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0029");
+ }
+
+ [TestMethod]
+ public void UsingStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ using (IDisposable foo = null)
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0029");
+ }
+
+ [TestMethod]
+ public void YieldStatement()
+ {
+ string source = @"
+ using System.Collections.Generic;
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public static IEnumerable Fail()
+ {
+ yield return 3.14f;
+ }
+
+ public void Execute()
+ {
+ Fail();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0030");
+ }
+
+ [TestMethod]
+ public void InvalidObjectDeclaration()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ object bar;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0031");
+ }
+
+ [TestMethod]
+ public void PointerType()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ int* p;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0032");
+ }
+
+ [TestMethod]
+ public void FunctionPointer()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ delegate* f;
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0033");
+ }
+
+ [TestMethod]
+ public void UnsafeStatement()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ unsafe
+ {
+ }
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0034");
+ }
+
+ [TestMethod]
+ public void UnsafeModifier_LocalFunction()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ static unsafe void Fail() { }
+
+ Fail();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0035");
+ }
+
+ [TestMethod]
+ public void UnsafeModifier_StaticMethod()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public static unsafe void Fail() { }
+
+ public ReadWriteBuffer buffer;
+
+ public void Execute()
+ {
+ Fail();
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0035");
+ }
+
+ ///
+ /// Verifies the output of a source generator.
+ ///
+ /// The generator type to use.
+ /// The input source to process.
+ /// The target index to check in the resulting output.
+ /// The expected body to compare with the generated code.
+ private void VerifyGeneratedDiagnostics(string source, params string[] diagnosticsIds)
+ where TGenerator : class, ISourceGenerator, new()
+ {
+ SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
+
+ IEnumerable references =
+ from assembly in AppDomain.CurrentDomain.GetAssemblies()
+ where !assembly.IsDynamic
+ let reference = MetadataReference.CreateFromFile(assembly.Location)
+ select reference;
+
+ CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
+
+ ISourceGenerator generator = new TGenerator();
+
+ CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(generator);
+
+ driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics);
+
+ HashSet resultingIds = diagnostics.Select(diagnostic => diagnostic.Id).ToHashSet();
+
+ Assert.IsTrue(resultingIds.SetEquals(diagnosticsIds));
+ }
+ }
+}
\ No newline at end of file