Merge pull request #95 from Sergio0694/feature/generator-diagnostics

Source generator diagnostics
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2021-02-18 23:51:21 +01:00
コミット 1660982239
16ファイル変更
+2180 -42

検索条件に一致するファイルはありません

+2
ファイルの表示
@@ -0,0 +1,2 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
+43
ファイルの表示
@@ -0,0 +1,43 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0002 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0003 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0004 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0005 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0006 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0007 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0008 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0009 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0010 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0011 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0012 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0013 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0014 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0015 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0016 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0017 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0018 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0019 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0020 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0021 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0022 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0023 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0024 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0025 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0026 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0027 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
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)
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)
CMPS0036 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+1 -1
ファイルの表示
@@ -48,7 +48,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3-beta1.21109.6" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3-beta1.21117.2" PrivateAssets="all" Pack="false" />
</ItemGroup>
</Project>
+511
ファイルの表示
@@ -0,0 +1,511 @@
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGenerators.Diagnostics
{
/// <summary>
/// A container for all <see cref="DiagnosticDescriptor"/> instances for errors reported by analyzers in this project.
/// </summary>
internal static class DiagnosticDescriptors
{
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid shader field.
/// <para>
/// Format: <c>"The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidShaderField { get; } = new(
id: "CMPS0001",
title: "Invalid shader field",
messageFormat: "The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A type representing a compute shader contains a field of a type that is not supported in HLSL.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid group shared field type.
/// <para>
/// Format: <c>"The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidGroupSharedFieldType { get; } = new(
id: "CMPS0002",
title: "Invalid group shared field type",
messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2} (it must be an array)",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A group shared field must be of an array type.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid group shared field element type.
/// <para>
/// Format: <c>"The compute shader of type {0} contains a group shared field \"{1}\" of an invalid element type {2}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidGroupSharedFieldElementType { get; } = new(
id: "CMPS0003",
title: "Invalid group shared field element type",
messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2} (it must be a primitive or unmanaged type)",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A group shared field element must be of a primitive or unmanaged type.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid group shared field declaration.
/// <para>
/// Format: <c>"The compute shader of type {0} contains a group shared field \"{1}\" that is not static"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidGroupSharedFieldDeclaration { get; } = new(
id: "CMPS0004",
title: "Invalid group shared field declaration",
messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" that is not static",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A group shared field must be static.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader with no resources.
/// <para>
/// Format: <c>"The compute shader of type {0} contains no resources to work on"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor MissingShaderResources { get; } = new(
id: "CMPS0005",
title: "Missing shader resources",
messageFormat: "The compute shader of type {0} contains no resources to work on",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A compute shader must contain at least one resource.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid <see cref="ThreadIds"/> usage.
/// <para>
/// Format: <c>"The ThreadIds type is used in method {0} of type {1}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidThreadIdsUsage { get; } = new(
id: "CMPS0006",
title: "Invalid ThreadIds usage",
messageFormat: "The ThreadIds type can only be used within the main body of a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The ThreadIds type can only be used within the main body of a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid <see cref="GroupIds"/> usage.
/// <para>
/// Format: <c>"The GroupIds type is used in method {0} of type {1}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidGroupIdsUsage { get; } = new(
id: "CMPS0007",
title: "Invalid GroupIds usage",
messageFormat: "The GroupIds type can only be used within the main body of a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The GroupIds type can only be used within the main body of a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid <see cref="GroupSize"/> usage.
/// <para>
/// Format: <c>"The GroupSize type is used in method {0} of type {1}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidGroupSizeUsage { get; } = new(
id: "CMPS0008",
title: "Invalid GroupSize usage",
messageFormat: "The GroupSize type can only be used within the main body of a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The GroupSize type can only be used within the main body of a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid <see cref="WarpIds"/> usage.
/// <para>
/// Format: <c>"The WarpIds type is used in method {0} of type {1}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidWarpIdsUsage { get; } = new(
id: "CMPS0009",
title: "Invalid WarpIds usage",
messageFormat: "The WarpIds type can only be used within the main body of a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The WarpIds type can only be used within the main body of a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid object creation expression.
/// <para>
/// Format: <c>"The type {0} cannot be created in a compute shader"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidObjectCreationExpression { get; } = new(
id: "CMPS0010",
title: "Invalid object creation expression",
messageFormat: "The type {0} cannot be created in a compute shader (only unmanaged types are supported)",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Only unmanaged value type objects can be created in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an anonymous object creation expression.
/// </summary>
public static DiagnosticDescriptor AnonymousObjectCreationExpression { get; } = new(
id: "CMPS0011",
title: "Anonymous object creation expression",
messageFormat: "An anonymous object cannot be created in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "An anonymous object cannot be created in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an async modifier on a method or function.
/// </summary>
public static DiagnosticDescriptor AsyncModifierOnMethodOrFunction { get; } = new(
id: "CMPS0012",
title: "Async modifier on method or function",
messageFormat: "The async modifier cannot be used in methods or functions used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The async modifier cannot be used in methods or functions used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an await expression.
/// </summary>
public static DiagnosticDescriptor AwaitExpression { get; } = new(
id: "CMPS0013",
title: "Await expression",
messageFormat: "The await expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The await expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a checked expression.
/// </summary>
public static DiagnosticDescriptor CheckedExpression { get; } = new(
id: "CMPS0014",
title: "Checked expression",
messageFormat: "A checked expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A checked expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a checked statement.
/// </summary>
public static DiagnosticDescriptor CheckedStatement { get; } = new(
id: "CMPS0015",
title: "Checked statement",
messageFormat: "A checked statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A checked statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a fixed statement.
/// </summary>
public static DiagnosticDescriptor FixedStatement { get; } = new(
id: "CMPS0016",
title: "Fixed statement",
messageFormat: "A fixed statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A fixed statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a foreach statement.
/// </summary>
public static DiagnosticDescriptor ForEachStatement { get; } = new(
id: "CMPS0017",
title: "Foreach statement",
messageFormat: "A foreach statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A foreach statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a lock statement.
/// </summary>
public static DiagnosticDescriptor LockStatement { get; } = new(
id: "CMPS0018",
title: "Foreach statement",
messageFormat: "A lock statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A lock statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a query statement.
/// </summary>
public static DiagnosticDescriptor QueryExpression { get; } = new(
id: "CMPS0019",
title: "Foreach statement",
messageFormat: "A LINQ query expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A LINQ query expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a range expression.
/// </summary>
public static DiagnosticDescriptor RangeExpression { get; } = new(
id: "CMPS0020",
title: "Range expression",
messageFormat: "A range expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A range expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a recursive pattern.
/// </summary>
public static DiagnosticDescriptor RecursivePattern { get; } = new(
id: "CMPS0021",
title: "Recursive pattern",
messageFormat: "A recursive pattern cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A recursive pattern cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a ref type.
/// </summary>
public static DiagnosticDescriptor RefType { get; } = new(
id: "CMPS0022",
title: "Ref type",
messageFormat: "A compute shader cannot have a ref type declaration",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A compute shader cannot have a ref type declaration.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a relational pattern.
/// </summary>
public static DiagnosticDescriptor RelationalPattern { get; } = new(
id: "CMPS0023",
title: "Relational pattern",
messageFormat: "A relational pattern cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A relational pattern cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a sizeof expression.
/// </summary>
public static DiagnosticDescriptor SizeOfExpression { get; } = new(
id: "CMPS0024",
title: "Sizeof expression",
messageFormat: "A sizeof expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A sizeof expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a stackalloc expression.
/// </summary>
public static DiagnosticDescriptor StackAllocArrayCreationExpression { get; } = new(
id: "CMPS0025",
title: "Stackalloc expression",
messageFormat: "A stackalloc expression cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A stackalloc expression cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a throw expression or statement.
/// </summary>
public static DiagnosticDescriptor ThrowExpressionOrStatement { get; } = new(
id: "CMPS0026",
title: "Throw expression or statement",
messageFormat: "Throw expressions and statements cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Throw expressions and statements cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a try statement.
/// </summary>
public static DiagnosticDescriptor TryStatement { get; } = new(
id: "CMPS0027",
title: "Try statement",
messageFormat: "A try statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A try statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a tuple type.
/// </summary>
public static DiagnosticDescriptor TupleType { get; } = new(
id: "CMPS0028",
title: "Tuple type",
messageFormat: "A compute shader cannot have a tuple type declaration",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A compute shader cannot have a tuple type declaration.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a using statement or declaration.
/// </summary>
public static DiagnosticDescriptor UsingStatementOrDeclaration { get; } = new(
id: "CMPS0029",
title: "Using statement or declaration",
messageFormat: "Using statements and declarations cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Using statements and declarations cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a yield statement.
/// </summary>
public static DiagnosticDescriptor YieldStatement { get; } = new(
id: "CMPS0030",
title: "Yield statement",
messageFormat: "A yield statement cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A yield statement cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid object declaration.
/// <para>
/// Format: <c>"A variable of type {0} cannot be declared in a compute shader"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidObjectDeclaration { get; } = new(
id: "CMPS0031",
title: "Invalid object declaration",
messageFormat: "A variable of type {0} cannot be declared in a compute shader (only unmanaged types are supported)",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Only unmanaged value type objects can be declared in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a pointer type.
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a function pointer type.
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an unsafe statement.
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an unsafe modifier on a method or function.
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a string literal.
/// </summary>
public static DiagnosticDescriptor StringLiteralExpression { get; } = new(
id: "CMPS0036",
title: "String literal expression",
messageFormat: "String literal expressions cannot be used in a compute shader",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "String literal expressions cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
}
+43
ファイルの表示
@@ -0,0 +1,43 @@
using System.Linq;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGenerators.Diagnostics
{
/// <summary>
/// Extension methods for <see cref="GeneratorExecutionContext"/>, specifically for reporting diagnostics.
/// </summary>
internal static class DiagnosticsExtensions
{
/// <summary>
/// Adds a new diagnostics to the current compilation.
/// </summary>
/// <param name="context">The <see cref="GeneratorEditContext"/> instance currently in use.</param>
/// <param name="descriptor">The input <see cref="DiagnosticDescriptor"/> for the diagnostics to create.</param>
/// <param name="symbol">The source <see cref="ISymbol"/> to attach the diagnostics to.</param>
/// <param name="args">The optional arguments for the formatted message to include.</param>
public static void ReportDiagnostic(
this GeneratorExecutionContext context,
DiagnosticDescriptor descriptor,
ISymbol symbol,
params object[] args)
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args));
}
/// <summary>
/// Adds a new diagnostics to the current compilation.
/// </summary>
/// <param name="context">The <see cref="GeneratorEditContext"/> instance currently in use.</param>
/// <param name="descriptor">The input <see cref="DiagnosticDescriptor"/> for the diagnostics to create.</param>
/// <param name="node">The source <see cref="SyntaxNode"/> to attach the diagnostics to.</param>
/// <param name="args">The optional arguments for the formatted message to include.</param>
public static void ReportDiagnostic(
this GeneratorExecutionContext context,
DiagnosticDescriptor descriptor,
SyntaxNode node,
params object[] args)
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, node.GetLocation(), args));
}
}
}
+3 -3
ファイルの表示
@@ -31,12 +31,12 @@ namespace ComputeSharp.SourceGenerators.Extensions
.WithParameterOptions(SymbolDisplayParameterOptions.None);
/// <summary>
/// Gets the full metadata name for a given <see cref="INamedTypeSymbol"/> instance.
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this INamedTypeSymbol symbol)
public static string GetFullMetadataName(this ITypeSymbol symbol)
{
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
{
+14 -1
ファイルの表示
@@ -44,7 +44,20 @@ namespace ComputeSharp.SourceGenerators.Extensions
public static TRoot ReplaceAndTrackType<TRoot>(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel, ICollection<INamedTypeSymbol> 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);
+4 -2
ファイルの表示
@@ -141,9 +141,11 @@ namespace ComputeSharp.SourceGenerators
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
if (fieldSymbol.Type is not INamedTypeSymbol { IsStatic: false } typeSymbol)
{
continue;
}
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
string typeName = typeSymbol.GetFullMetadataName();
if (HlslKnownTypes.IsTypedResourceType(typeName))
+4 -5
ファイルの表示
@@ -120,11 +120,10 @@ namespace ComputeSharp.SourceGenerators
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
if (typeSymbol.TypeKind != TypeKind.Delegate) continue;
if (fieldSymbol.Type is not INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false } typeSymbol)
{
continue;
}
// hash += hash << 5;
yield return ExpressionStatement(ParseExpression($"hash += hash << 5"));
+94 -19
ファイルの表示
@@ -4,6 +4,7 @@ using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGenerators.Diagnostics;
using ComputeSharp.SourceGenerators.Extensions;
using ComputeSharp.SourceGenerators.Mappings;
using ComputeSharp.SourceGenerators.SyntaxRewriters;
@@ -13,6 +14,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static ComputeSharp.SourceGenerators.Helpers.SyntaxFactoryHelper;
using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors;
#pragma warning disable CS0618
@@ -49,9 +51,9 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedMembers = GetProcessedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
var processedConstants = GetProcessedConstants(constantDefinitions);
@@ -82,17 +84,59 @@ namespace ComputeSharp.SourceGenerators
/// <summary>
/// Gets a sequence of captured members and their mapped names.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="types">The collection of currently discovered types.</param>
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection<INamedTypeSymbol> types)
private static IEnumerable<IEnumerable<string>> GetProcessedMembers(
GeneratorExecutionContext context,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> types)
{
bool hlslResourceFound = false;
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) });
// Group shared fields must be static
if (attribute is not null)
{
context.ReportDiagnostic(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name);
}
// Captured fields must be named type symbols
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol)
{
context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
string metadataName = typeSymbol.GetFullMetadataName();
// Allowed fields must be either resources, unmanaged values or delegates
if (HlslKnownTypes.IsTypedResourceType(metadataName))
{
hlslResourceFound = true;
// Track the type of items in the current buffer
if (HlslKnownTypes.IsStructuredBufferType(metadataName))
{
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
}
}
else if (!typeSymbol.IsUnmanagedType &&
typeSymbol.TypeKind != TypeKind.Delegate)
{
// Shaders can only capture valid HLSL resource types or unmanaged types
context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, typeSymbol);
continue;
}
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
@@ -100,32 +144,50 @@ namespace ComputeSharp.SourceGenerators
// Yield back the current mapping for the name (if the name used a reserved keyword)
yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName };
}
// Track the type of items in the current buffer
if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName()) &&
typeSymbol.TypeArguments.Length == 1)
{
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
}
if (!hlslResourceFound)
{
context.ReportDiagnostic(MissingShaderResources, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>
/// Gets a sequence of captured members and their mapped names.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="types">The collection of currently discovered types.</param>
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection<INamedTypeSymbol> types)
private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers(
GeneratorExecutionContext context,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> types)
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (!fieldSymbol.IsStatic) continue;
AttributeData attribute = fieldSymbol.GetAttributes().First(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) });
AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) });
if (attribute is null) continue;
if (fieldSymbol.Type is not IArrayTypeSymbol typeSymbol)
{
context.ReportDiagnostic(InvalidGroupSharedFieldType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
if (!typeSymbol.ElementType.IsUnmanagedType)
{
context.ReportDiagnostic(InvalidGroupSharedFieldElementType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
int? bufferSize = (int?)attribute.ConstructorArguments.FirstOrDefault().Value;
IArrayTypeSymbol typeSymbol = (IArrayTypeSymbol)fieldSymbol.Type;
string typeName = HlslKnownTypes.GetMappedElementName(typeSymbol);
@@ -140,6 +202,7 @@ namespace ComputeSharp.SourceGenerators
/// <summary>
/// Gets a sequence of processed methods declared within a given type.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the type to process.</param>
@@ -149,6 +212,7 @@ namespace ComputeSharp.SourceGenerators
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
[Pure]
private static (string EntryPoint, IEnumerable<SyntaxNode> Methods) GetProcessedMethods(
GeneratorExecutionContext context,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
SemanticModel semanticModel,
@@ -168,16 +232,27 @@ namespace ComputeSharp.SourceGenerators
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
ShaderSourceRewriter shaderSourceRewriter = new(structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
bool isShaderEntryPoint =
methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
methodDeclarationSymbol.ReturnsVoid &&
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0;
// Create the source rewriter for the current method
ShaderSourceRewriter shaderSourceRewriter = new(
structDeclarationSymbol,
semanticModel,
discoveredTypes,
staticMethods,
constantDefinitions,
context,
isShaderEntryPoint);
// Rewrite the method syntax tree
MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
// If the method is the shader entry point, do additional processing
if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
methodDeclarationSymbol.ReturnsVoid &&
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0)
if (isShaderEntryPoint)
{
processedMethod = new ExecuteMethodRewriter(shaderSourceRewriter).Visit(processedMethod)!;
+22 -1
ファイルの表示
@@ -66,6 +66,23 @@ namespace ComputeSharp.SourceGenerators.Mappings
typeof(WarpIds)
};
/// <summary>
/// Checks whether or not a given type name matches a structured buffer type.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a structured buffer type.</returns>
public static bool IsStructuredBufferType(string typeName)
{
switch (typeName)
{
case "ComputeSharp.ConstantBuffer`1":
case "ComputeSharp.ReadOnlyBuffer`1":
case "ComputeSharp.ReadWriteBuffer`1":
return true;
default: return false;
};
}
/// <summary>
/// Checks whether or not a given type name matches a typed resource type.
/// </summary>
@@ -195,10 +212,12 @@ namespace ComputeSharp.SourceGenerators.Mappings
{
if (KnownTypes.ContainsKey(type.GetFullMetadataName())) return;
customTypes.Add(type);
if (!customTypes.Add(type)) return;
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
{
if (field.IsStatic) continue;
ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
}
}
@@ -238,6 +257,8 @@ namespace ComputeSharp.SourceGenerators.Mappings
// Only add other custom types as dependencies, and ignore HLSL types
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
{
if (field.IsStatic) continue;
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
if (!KnownTypes.ContainsKey(fieldType.GetFullMetadataName()))
+4 -2
ファイルの表示
@@ -48,7 +48,7 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var (invokeMethod, processedMethods) = GetProcessedMethods(context, methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
var processedConstants = IComputeShaderSourceGenerator.GetProcessedConstants(constantDefinitions);
@@ -75,6 +75,7 @@ namespace ComputeSharp.SourceGenerators
/// <summary>
/// Gets a sequence of processed methods from a target method declaration.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> instance for the current method.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the method to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
@@ -83,13 +84,14 @@ namespace ComputeSharp.SourceGenerators
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/>.</returns>
[Pure]
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
GeneratorExecutionContext context,
MethodDeclarationSyntax methodDeclaration,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
IDictionary<IFieldSymbol, string> constantDefinitions)
{
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions);
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter
+300 -4
ファイルの表示
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using ComputeSharp.SourceGenerators.Diagnostics;
using ComputeSharp.SourceGenerators.Extensions;
using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
@@ -9,6 +10,7 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
@@ -52,11 +54,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
private readonly List<VariableDeclarationSyntax> implicitVariables;
/// <summary>
/// The current generator context in use.
/// </summary>
private readonly GeneratorExecutionContext context;
/// <summary>
/// Whether or not the current instance is processing a shader entry point.
/// </summary>
private readonly bool isEntryPoint;
/// <summary>
/// The current <see cref="MethodDeclarationSyntax"/> tree being visited.
/// </summary>
private MethodDeclarationSyntax? currentMethod;
/// <summary>
/// The current depth inside local declarations.
/// </summary>
private int localFunctionDepth;
/// <summary>
/// Creates a new <see cref="ShaderSourceRewriter"/> instance with the specified parameters.
/// </summary>
@@ -65,12 +82,16 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// <param name="discoveredTypes">The set of discovered custom types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="context">The current generator context in use.</param>
/// <param name="isEntryPoint">Whether or not the current instance is processing a shader entry point.</param>
public ShaderSourceRewriter(
INamedTypeSymbol shaderType,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
IDictionary<IFieldSymbol, string> constantDefinitions)
IDictionary<IFieldSymbol, string> constantDefinitions,
GeneratorExecutionContext context,
bool isEntryPoint)
{
this.shaderType = shaderType;
this.semanticModel = semanticModel;
@@ -79,6 +100,8 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
this.constantDefinitions = constantDefinitions;
this.localFunctions = new();
this.implicitVariables = new();
this.context = context;
this.isEntryPoint = isEntryPoint;
}
/// <summary>
@@ -88,11 +111,13 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// <param name="discoveredTypes">The set of discovered custom types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="context">The current generator context in use.</param>
public ShaderSourceRewriter(
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
IDictionary<IFieldSymbol, string> constantDefinitions)
IDictionary<IFieldSymbol, string> constantDefinitions,
GeneratorExecutionContext context)
{
this.semanticModel = semanticModel;
this.discoveredTypes = discoveredTypes;
@@ -100,6 +125,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
this.constantDefinitions = constantDefinitions;
this.implicitVariables = new();
this.localFunctions = new();
this.context = context;
}
/// <summary>
@@ -129,6 +155,16 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
var updatedNode = (MethodDeclarationSyntax?)base.Visit(node);
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());
@@ -176,6 +212,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = ((LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node)!);
if (this.semanticModel.GetOperation(node) is IOperation { Kind: OperationKind.UsingDeclaration })
{
this.context.ReportDiagnostic(UsingStatementOrDeclaration, node);
}
return updatedNode.ReplaceAndTrackType(updatedNode.Declaration.Type, node.Declaration.Type, this.semanticModel, this.discoveredTypes);
}
@@ -184,6 +225,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!;
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType: false } type)
{
this.context.ReportDiagnostic(InvalidObjectCreationExpression, node, type);
}
updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, this.semanticModel, this.discoveredTypes);
// New objects use the default HLSL cast syntax, eg. (float4)0
@@ -195,11 +241,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList);
}
/// <inheritdoc/>
public override SyntaxNode? VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax node)
{
var updatedNode = (AnonymousObjectCreationExpressionSyntax)base.VisitAnonymousObjectCreationExpression(node)!;
this.context.ReportDiagnostic(DiagnosticDescriptors.AnonymousObjectCreationExpression, node);
return updatedNode;
}
/// <inheritdoc/>
public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
{
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
@@ -255,6 +316,10 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d));
}
}
else if (updatedNode.IsKind(SyntaxKind.StringLiteralExpression))
{
this.context.ReportDiagnostic(StringLiteralExpression, node);
}
return updatedNode;
}
@@ -285,12 +350,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// <inheritdoc/>
public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
{
this.localFunctionDepth++;
var updatedNode =
((LocalFunctionStatementSyntax)base.VisitLocalFunctionStatement(node)!)
.WithBlockBody()
.WithAttributeLists(List<AttributeListSyntax>())
.WithIdentifier(Identifier($"__{this.currentMethod!.Identifier.Text}__{node.Identifier.Text}"));
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
// them from the current syntax tree completely. These will be added to the shader source
// as external static method with a special name to avoid conflicts with other methods.
@@ -331,6 +410,24 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (mapping == $"__{nameof(GroupIds)}__get_Index") IsGroupIdsIndexUsed = true;
else if (typeName == typeof(GroupIds).FullName) IsGroupIdsUsed = true;
else if (typeName == typeof(WarpIds).FullName) IsWarpIdsUsed = true;
// Check that the dispatch info types are only used from the main shader body
if (!this.isEntryPoint || this.localFunctionDepth > 0)
{
DiagnosticDescriptor? descriptor = typeName switch
{
_ when typeName == typeof(ThreadIds).FullName => DiagnosticDescriptors.InvalidThreadIdsUsage,
_ when typeName == typeof(GroupIds).FullName => DiagnosticDescriptors.InvalidGroupIdsUsage,
_ when typeName == typeof(GroupSize).FullName => DiagnosticDescriptors.InvalidGroupSizeUsage,
_ when typeName == typeof(WarpIds).FullName => DiagnosticDescriptors.InvalidWarpIdsUsage,
_ => null
};
if (descriptor is not null)
{
this.context.ReportDiagnostic(descriptor, node);
}
}
}
// Static and instance members are handled differently, with static members being
@@ -436,7 +533,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (!this.staticMethods.ContainsKey(method))
{
ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods, this.constantDefinitions);
ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods, this.constantDefinitions, this.context);
MethodDeclarationSyntax
methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax(),
processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace().WithoutTrivia();
@@ -478,7 +575,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// Track and rewrite the discarded declaration
if (this.semanticModel.GetOperation(node.Expression) is IDiscardOperation operation)
{
TypeSyntax typeSyntax = operation.Type.TrackType(this.discoveredTypes);
TypeSyntax typeSyntax = operation.Type!.TrackType(this.discoveredTypes);
string identifier = $"__implicit{this.implicitVariables.Count}";
// Add the variable to the list of implicit declarations
@@ -508,6 +605,205 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode;
}
/// <inheritdoc/>
public override SyntaxNode? VisitAwaitExpression(AwaitExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.AwaitExpression, node);
return base.VisitAwaitExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitCheckedExpression(CheckedExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.CheckedExpression, node);
return base.VisitCheckedExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitCheckedStatement(CheckedStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.CheckedStatement, node);
return base.VisitCheckedStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitFixedStatement(FixedStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.FixedStatement, node);
return base.VisitFixedStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitForEachStatement(ForEachStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.ForEachStatement, node);
return base.VisitForEachStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitForEachVariableStatement(ForEachVariableStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.ForEachStatement, node);
return base.VisitForEachVariableStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitLockStatement(LockStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.LockStatement, node);
return base.VisitLockStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitQueryExpression(QueryExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.QueryExpression, node);
return base.VisitQueryExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitRangeExpression(RangeExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.RangeExpression, node);
return base.VisitRangeExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitRecursivePattern(RecursivePatternSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.RecursivePattern, node);
return base.VisitRecursivePattern(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitRefType(RefTypeSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.RefType, node);
return base.VisitRefType(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitRelationalPattern(RelationalPatternSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.RelationalPattern, node);
return base.VisitRelationalPattern(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitSizeOfExpression(SizeOfExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.SizeOfExpression, node);
return base.VisitSizeOfExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.StackAllocArrayCreationExpression, node);
return base.VisitStackAllocArrayCreationExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitThrowExpression(ThrowExpressionSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.ThrowExpressionOrStatement, node);
return base.VisitThrowExpression(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitThrowStatement(ThrowStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.ThrowExpressionOrStatement, node);
return base.VisitThrowStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitTryStatement(TryStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.TryStatement, node);
return base.VisitTryStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitTupleType(TupleTypeSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.TupleType, node);
return base.VisitTupleType(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitUsingStatement(UsingStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.UsingStatementOrDeclaration, node);
return base.VisitUsingStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
var updatedNode = (VariableDeclaratorSyntax)base.VisitVariableDeclarator(node)!;
if (node.Initializer is null &&
node.Parent is VariableDeclarationSyntax declaration &&
this.semanticModel.GetTypeInfo(declaration.Type).Type is ITypeSymbol { IsUnmanagedType: false } type)
{
this.context.ReportDiagnostic(InvalidObjectDeclaration, node, type);
}
return updatedNode;
}
/// <inheritdoc/>
public override SyntaxNode? VisitYieldStatement(YieldStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.YieldStatement, node);
return base.VisitYieldStatement(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitFunctionPointerType(FunctionPointerTypeSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.FunctionPointer, node);
return base.VisitFunctionPointerType(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitPointerType(PointerTypeSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.PointerType, node);
return base.VisitPointerType(node);
}
/// <inheritdoc/>
public override SyntaxNode? VisitUnsafeStatement(UnsafeStatementSyntax node)
{
this.context.ReportDiagnostic(DiagnosticDescriptors.UnsafeStatement, node);
return base.VisitUnsafeStatement(node);
}
/// <inheritdoc/>
public override SyntaxToken VisitToken(SyntaxToken token)
{
@@ -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;
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
ファイル差分が大きすぎるため表示されません。 差分を読み込み