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