Refactor diagnostic models to be properly equatable

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 17:58:44 +02:00
1 親 bdaf402a4c
コミット 9eac3c779d
26ファイル変更
+247 -134

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

+55
ファイルの表示
@@ -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);
}
}
}
+60 -9
ファイルの表示
@@ -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 -2
ファイルの表示
@@ -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);