diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs
new file mode 100644
index 00000000..e9509100
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs
@@ -0,0 +1,43 @@
+using System.Linq;
+using Microsoft.CodeAnalysis;
+
+namespace ComputeSharp.SourceGenerators.Diagnostics
+{
+ ///
+ /// Extension methods for , specifically for reporting diagnostics.
+ ///
+ internal static class DiagnosticsExtensions
+ {
+ ///
+ /// Adds a new diagnostics to the current compilation.
+ ///
+ /// The instance currently in use.
+ /// The input for the diagnostics to create.
+ /// The source to attach the diagnostics to.
+ /// The optional arguments for the formatted message to include.
+ public static void ReportDiagnostic(
+ this GeneratorExecutionContext context,
+ DiagnosticDescriptor descriptor,
+ ISymbol symbol,
+ params object[] args)
+ {
+ context.ReportDiagnostic(Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args));
+ }
+
+ ///
+ /// Adds a new diagnostics to the current compilation.
+ ///
+ /// The instance currently in use.
+ /// The input for the diagnostics to create.
+ /// The source to attach the diagnostics to.
+ /// The optional arguments for the formatted message to include.
+ public static void ReportDiagnostic(
+ this GeneratorExecutionContext context,
+ DiagnosticDescriptor descriptor,
+ SyntaxNode node,
+ params object[] args)
+ {
+ context.ReportDiagnostic(Diagnostic.Create(descriptor, node.GetLocation(), args));
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 59eca030..4b5c1337 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -14,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
@@ -104,10 +105,7 @@ namespace ComputeSharp.SourceGenerators
// Group shared fields must be static
if (attribute is not null)
{
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidGroupSharedFieldDeclaration,
- fieldSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol, fieldSymbol.Name));
+ context.ReportDiagnostic(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name);
continue;
}
@@ -115,10 +113,7 @@ namespace ComputeSharp.SourceGenerators
// Captured fields must be named type symbols
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol)
{
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidShaderField,
- fieldSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type));
+ context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
@@ -140,10 +135,7 @@ namespace ComputeSharp.SourceGenerators
typeSymbol.TypeKind != TypeKind.Delegate)
{
// Shaders can only capture valid HLSL resource types or unmanaged types
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidShaderField,
- fieldSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol, fieldSymbol.Name, typeSymbol));
+ context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, typeSymbol);
continue;
}
@@ -158,10 +150,7 @@ namespace ComputeSharp.SourceGenerators
if (!hlslResourceFound)
{
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.MissingShaderResources,
- structDeclarationSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol));
+ context.ReportDiagnostic(MissingShaderResources, structDeclarationSymbol, structDeclarationSymbol);
}
}
@@ -188,20 +177,14 @@ namespace ComputeSharp.SourceGenerators
if (fieldSymbol.Type is not IArrayTypeSymbol typeSymbol)
{
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidGroupSharedFieldType,
- fieldSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type));
+ context.ReportDiagnostic(InvalidGroupSharedFieldType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
if (!typeSymbol.ElementType.IsUnmanagedType)
{
- context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidGroupSharedFieldElementType,
- fieldSymbol.Locations.FirstOrDefault(),
- structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type));
+ context.ReportDiagnostic(InvalidGroupSharedFieldElementType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 06db18f5..8815b3ac 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -10,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
{
@@ -157,9 +158,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (node!.Modifiers.Contains(Token(SyntaxKind.AsyncKeyword)))
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.AsyncModifierOnMethodOrFunction,
- node.GetLocation()));
+ this.context.ReportDiagnostic(AsyncModifierOnMethodOrFunction, node);
}
if (updatedNode is not null)
@@ -211,9 +210,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (this.semanticModel.GetOperation(node) is IOperation { Kind: OperationKind.UsingDeclaration })
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.UsingStatementOrDeclaration,
- node.GetLocation()));
+ this.context.ReportDiagnostic(UsingStatementOrDeclaration, node);
}
return updatedNode.ReplaceAndTrackType(updatedNode.Declaration.Type, node.Declaration.Type, this.semanticModel, this.discoveredTypes);
@@ -226,10 +223,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType: false } type)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidObjectCreationExpression,
- node.GetLocation(),
- type));
+ this.context.ReportDiagnostic(InvalidObjectCreationExpression, node, type);
}
updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, this.semanticModel, this.discoveredTypes);
@@ -248,9 +242,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (AnonymousObjectCreationExpressionSyntax)base.VisitAnonymousObjectCreationExpression(node)!;
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.AnonymousObjectCreationExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.AnonymousObjectCreationExpression, node);
return updatedNode;
}
@@ -355,9 +347,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (node.Modifiers.Contains(Token(SyntaxKind.AsyncKeyword)))
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.AsyncModifierOnMethodOrFunction,
- node.GetLocation()));
+ this.context.ReportDiagnostic(AsyncModifierOnMethodOrFunction, node);
}
this.localFunctionDepth--;
@@ -417,7 +407,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
if (descriptor is not null)
{
- this.context.ReportDiagnostic(Diagnostic.Create(descriptor, node.GetLocation()));
+ this.context.ReportDiagnostic(descriptor, node);
}
}
}
@@ -600,9 +590,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitAwaitExpression(AwaitExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.AwaitExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.AwaitExpression, node);
return base.VisitAwaitExpression(node);
}
@@ -610,9 +598,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitCheckedExpression(CheckedExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.CheckedExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.CheckedExpression, node);
return base.VisitCheckedExpression(node);
}
@@ -620,9 +606,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitCheckedStatement(CheckedStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.CheckedStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.CheckedStatement, node);
return base.VisitCheckedStatement(node);
}
@@ -630,9 +614,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitFixedStatement(FixedStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.FixedStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.FixedStatement, node);
return base.VisitFixedStatement(node);
}
@@ -640,9 +622,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitForEachStatement(ForEachStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.ForEachStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.ForEachStatement, node);
return base.VisitForEachStatement(node);
}
@@ -650,9 +630,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitForEachVariableStatement(ForEachVariableStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.ForEachStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.ForEachStatement, node);
return base.VisitForEachVariableStatement(node);
}
@@ -660,9 +638,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitLockStatement(LockStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.LockStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.LockStatement, node);
return base.VisitLockStatement(node);
}
@@ -670,9 +646,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitQueryExpression(QueryExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.QueryExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.QueryExpression, node);
return base.VisitQueryExpression(node);
}
@@ -680,9 +654,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitRangeExpression(RangeExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.RangeExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.RangeExpression, node);
return base.VisitRangeExpression(node);
}
@@ -690,9 +662,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitRecursivePattern(RecursivePatternSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.RecursivePattern,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.RecursivePattern, node);
return base.VisitRecursivePattern(node);
}
@@ -700,9 +670,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitRefType(RefTypeSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.RefType,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.RefType, node);
return base.VisitRefType(node);
}
@@ -710,9 +678,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitRelationalPattern(RelationalPatternSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.RelationalPattern,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.RelationalPattern, node);
return base.VisitRelationalPattern(node);
}
@@ -720,9 +686,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitSizeOfExpression(SizeOfExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.SizeOfExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.SizeOfExpression, node);
return base.VisitSizeOfExpression(node);
}
@@ -730,9 +694,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.StackAllocArrayCreationExpression,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.StackAllocArrayCreationExpression, node);
return base.VisitStackAllocArrayCreationExpression(node);
}
@@ -740,9 +702,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitThrowExpression(ThrowExpressionSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.ThrowExpressionOrStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.ThrowExpressionOrStatement, node);
return base.VisitThrowExpression(node);
}
@@ -750,9 +710,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitThrowStatement(ThrowStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.ThrowExpressionOrStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.ThrowExpressionOrStatement, node);
return base.VisitThrowStatement(node);
}
@@ -760,9 +718,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitTryStatement(TryStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.TryStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.TryStatement, node);
return base.VisitTryStatement(node);
}
@@ -770,9 +726,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitTupleType(TupleTypeSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.TupleType,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.TupleType, node);
return base.VisitTupleType(node);
}
@@ -780,9 +734,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitUsingStatement(UsingStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.UsingStatementOrDeclaration,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.UsingStatementOrDeclaration, node);
return base.VisitUsingStatement(node);
}
@@ -796,10 +748,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
node.Parent is VariableDeclarationSyntax declaration &&
this.semanticModel.GetTypeInfo(declaration.Type).Type is ITypeSymbol { IsUnmanagedType: false } type)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.InvalidObjectDeclaration,
- node.GetLocation(),
- type));
+ this.context.ReportDiagnostic(InvalidObjectDeclaration, node, type);
}
return updatedNode;
@@ -808,9 +757,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitYieldStatement(YieldStatementSyntax node)
{
- this.context.ReportDiagnostic(Diagnostic.Create(
- DiagnosticDescriptors.YieldStatement,
- node.GetLocation()));
+ this.context.ReportDiagnostic(DiagnosticDescriptors.YieldStatement, node);
return base.VisitYieldStatement(node);
}