using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Extensions;
///
/// Extension methods for , specifically for reporting diagnostics.
///
internal static class DiagnosticsExtensions
{
///
/// Adds a new diagnostics to the target builder.
///
/// The collection of produced instances.
/// 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 Add(
this ImmutableArrayBuilder diagnostics,
DiagnosticDescriptor descriptor,
ISymbol symbol,
params object[] args)
{
diagnostics.Add(DiagnosticInfo.Create(descriptor, symbol, args));
}
///
/// Adds a new diagnostics to the target builder.
///
/// The collection of produced instances.
/// 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 Add(
this ImmutableArrayBuilder diagnostics,
DiagnosticDescriptor descriptor,
SyntaxNode node,
params object[] args)
{
diagnostics.Add(DiagnosticInfo.Create(descriptor, node, args));
}
///
/// Registers an output node into an to output diagnostics.
///
/// The input instance.
/// The input sequence of diagnostics.
public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider diagnostics)
{
context.RegisterSourceOutput(diagnostics, static (context, diagnostic) => context.ReportDiagnostic(diagnostic.ToDiagnostic()));
}
///
/// Registers an output node into an to output diagnostics.
///
/// The input instance.
/// The input sequence of diagnostics.
public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics)
{
context.RegisterSourceOutput(diagnostics, static (context, diagnostics) =>
{
foreach (DiagnosticInfo diagnostic in diagnostics)
{
context.ReportDiagnostic(diagnostic.ToDiagnostic());
}
});
}
}