Refactor diagnostic models to be properly equatable
このコミットが含まれているのは:
1 親
bdaf402a4c
コミット
9eac3c779d
26ファイル変更
+247
-134
検索条件に一致するファイルはありません
@@ -19,6 +19,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\Comparer{T,TSelf}.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HashCode.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SemanticModelProvider.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\DeferredDiagnosticInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\DiagnosticInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\HierarchyInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\Result{T}.cs" />
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
@@ -12,33 +12,33 @@ internal static class DiagnosticsExtensions
|
||||
/// <summary>
|
||||
/// Adds a new diagnostics to the target builder.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <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 ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
this ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
DiagnosticDescriptor descriptor,
|
||||
ISymbol symbol,
|
||||
params object[] args)
|
||||
{
|
||||
diagnostics.Add(Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), 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="Diagnostic"/> instances.</param>
|
||||
/// <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 ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
this ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
DiagnosticDescriptor descriptor,
|
||||
SyntaxNode node,
|
||||
params object[] args)
|
||||
{
|
||||
diagnostics.Add(Diagnostic.Create(descriptor, node.GetLocation(), args));
|
||||
diagnostics.Add(DiagnosticInfo.Create(descriptor, node, args));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -46,11 +46,11 @@ internal static class DiagnosticsExtensions
|
||||
/// </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<Diagnostic> diagnostics)
|
||||
public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<DiagnosticInfo> diagnostics)
|
||||
{
|
||||
context.RegisterSourceOutput(diagnostics, static (context, diagnostic) =>
|
||||
{
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
context.ReportDiagnostic(diagnostic.ToDiagnostic());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -59,13 +59,13 @@ internal static class DiagnosticsExtensions
|
||||
/// </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<Diagnostic>> diagnostics)
|
||||
public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<ImmutableArray<DiagnosticInfo>> diagnostics)
|
||||
{
|
||||
context.RegisterSourceOutput(diagnostics, static (context, diagnostics) =>
|
||||
{
|
||||
foreach (Diagnostic diagnostic in diagnostics)
|
||||
foreach (DiagnosticInfo diagnostic in diagnostics)
|
||||
{
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
context.ReportDiagnostic(diagnostic.ToDiagnostic());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ internal static class IEqualityComparerExtensions
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for an <see cref="ImmutableArray{T}"/> value.
|
||||
/// </summary>
|
||||
public sealed class Comparer<T> : IEqualityComparer<ImmutableArray<T>>
|
||||
private sealed class Comparer<T> : IEqualityComparer<ImmutableArray<T>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T"/> comparer.
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model for a serializeable diagnostic info to be reconstructed later in a pipeline.
|
||||
/// </summary>
|
||||
/// <param name="Descriptor">The wrapped <see cref="DiagnosticDescriptor"/> instance.</param>
|
||||
/// <param name="Arguments">The diagnostic arguments.</param>
|
||||
internal sealed record DeferredDiagnosticInfo(DiagnosticDescriptor Descriptor, ImmutableArray<string> Arguments)
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(DeferredDiagnosticInfo? obj) => Comparer.Default.Equals(this, obj);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="DeferredDiagnosticInfo"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="descriptor">The input <see cref="DiagnosticDescriptor"/> for the diagnostics to create.</param>
|
||||
/// <param name="args">The optional arguments for the formatted message to include.</param>
|
||||
/// <returns>A new <see cref="DeferredDiagnosticInfo"/> instance with the specified parameters.</returns>
|
||||
public static DeferredDiagnosticInfo Create(DiagnosticDescriptor descriptor, params object[] args)
|
||||
{
|
||||
return new(descriptor, args.Select(static arg => arg.ToString()).ToImmutableArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DeferredDiagnosticInfo"/>.
|
||||
/// </summary>
|
||||
private sealed class Comparer : Comparer<DeferredDiagnosticInfo, Comparer>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected override void AddToHashCode(ref HashCode hashCode, DeferredDiagnosticInfo obj)
|
||||
{
|
||||
hashCode.Add(obj.Descriptor);
|
||||
hashCode.AddRange(obj.Arguments);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool AreEqual(DeferredDiagnosticInfo x, DeferredDiagnosticInfo y)
|
||||
{
|
||||
return
|
||||
x.Descriptor.Equals(y.Descriptor) &&
|
||||
x.Arguments.SequenceEqual(y.Arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model for a serializeable diagnostic provider.
|
||||
/// A model for a serializeable diagnostic info.
|
||||
/// </summary>
|
||||
/// <param name="Descriptor">The wrapped <see cref="DiagnosticDescriptor"/> instance.</param>
|
||||
/// <param name="Args">The diagnostic args.</param>
|
||||
internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params object[] Args)
|
||||
/// <param name="SyntaxTree">The tree to use as location for the diagnostic, if available.</param>
|
||||
/// <param name="TextSpan">The span to use as location for the diagnostic.</param>
|
||||
/// <param name="Arguments">The diagnostic arguments.</param>
|
||||
internal sealed record DiagnosticInfo(
|
||||
DiagnosticDescriptor Descriptor,
|
||||
SyntaxTree? SyntaxTree,
|
||||
TextSpan TextSpan,
|
||||
ImmutableArray<string> Arguments)
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(DiagnosticInfo? obj) => Comparer.Default.Equals(this, obj);
|
||||
@@ -19,6 +28,48 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob
|
||||
/// <inheritdoc/>
|
||||
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Diagnostic"/> instance with the state from this model.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="Diagnostic"/> instance with the state from this model.</returns>
|
||||
public Diagnostic ToDiagnostic()
|
||||
{
|
||||
if (SyntaxTree is not null)
|
||||
{
|
||||
return Diagnostic.Create(Descriptor, Location.Create(SyntaxTree, TextSpan), Arguments.ToArray());
|
||||
}
|
||||
|
||||
return Diagnostic.Create(Descriptor, null, Arguments.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="DiagnosticInfo"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// <returns>A new <see cref="DiagnosticInfo"/> instance with the specified parameters.</returns>
|
||||
public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, ISymbol symbol, params object[] args)
|
||||
{
|
||||
Location location = symbol.Locations.First();
|
||||
|
||||
return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="DiagnosticInfo"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// <returns>A new <see cref="DiagnosticInfo"/> instance with the specified parameters.</returns>
|
||||
public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, SyntaxNode node, params object[] args)
|
||||
{
|
||||
Location location = node.GetLocation();
|
||||
|
||||
return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DiagnosticInfo"/>.
|
||||
/// </summary>
|
||||
@@ -28,11 +79,9 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob
|
||||
protected override void AddToHashCode(ref HashCode hashCode, DiagnosticInfo obj)
|
||||
{
|
||||
hashCode.Add(obj.Descriptor);
|
||||
|
||||
foreach (object arg in obj.Args)
|
||||
{
|
||||
hashCode.Add(arg);
|
||||
}
|
||||
hashCode.Add(obj.SyntaxTree);
|
||||
hashCode.Add(obj.TextSpan);
|
||||
hashCode.AddRange(obj.Arguments);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -40,7 +89,9 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob
|
||||
{
|
||||
return
|
||||
x.Descriptor.Equals(y.Descriptor) &&
|
||||
x.Args.SequenceEqual(y.Args);
|
||||
x.SyntaxTree == y.SyntaxTree &&
|
||||
x.TextSpan.Equals(y.TextSpan) &&
|
||||
x.Arguments.SequenceEqual(y.Arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Models;
|
||||
|
||||
@@ -9,4 +8,4 @@ namespace ComputeSharp.SourceGeneration.Models;
|
||||
/// <typeparam name="TValue">The type of the wrapped value.</typeparam>
|
||||
/// <param name="Value">The wrapped value for the current result.</param>
|
||||
/// <param name="Errors">The associated diagnostic errors, if any.</param>
|
||||
internal sealed record Result<TValue>(TValue Value, ImmutableArray<Diagnostic> Errors);
|
||||
internal sealed record Result<TValue>(TValue Value, ImmutableArray<DiagnosticInfo> Errors);
|
||||
新しいイシューから参照
ユーザーをブロックする