From e27b7e4379bb1dc35d1be03f8b63c25d91481495 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Dec 2023 12:22:50 +0100 Subject: [PATCH] Minor tweaks to DiagnosticInfo --- ...DPixelShaderDescriptorGenerator.HlslBytecode.cs | 7 +++++-- .../D2DPixelShaderSourceGenerator.Execute.cs | 9 +++++++-- .../Models/DiagnosticInfo.cs | 14 ++++++++++++-- ...omputeShaderDescriptorGenerator.HlslBytecode.cs | 7 +++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 334bb349..210e2ee1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -257,14 +257,17 @@ partial class D2DPixelShaderDescriptorGenerator diagnostic = DiagnosticInfo.Create( HlslBytecodeFailedWithWin32Exception, structDeclarationSymbol, - [structDeclarationSymbol, win32Error.HResult, win32Error.Message]); + structDeclarationSymbol, + win32Error.HResult, + win32Error.Message); } else if (info is HlslBytecodeInfo.CompilerError fxcError) { diagnostic = DiagnosticInfo.Create( HlslBytecodeFailedWithFxcCompilationException, structDeclarationSymbol, - [structDeclarationSymbol, fxcError.Message]); + structDeclarationSymbol, + fxcError.Message); } if (diagnostic is not null) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 78ed7ef1..0de6514d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -140,14 +140,19 @@ partial class D2DPixelShaderSourceGenerator diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithWin32Exception, methodSymbol, - [methodSymbol, methodSymbol.ContainingType, win32Error.HResult, win32Error.Message]); + methodSymbol, + methodSymbol.ContainingType, + win32Error.HResult, + win32Error.Message); } else if (info is HlslBytecodeInfo.CompilerError fxcError) { diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, methodSymbol, - [methodSymbol, methodSymbol.ContainingType, fxcError.Message]); + methodSymbol, + methodSymbol.ContainingType, + fxcError.Message); } if (diagnostic is not null) diff --git a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs index 72970bd5..c5bfaeca 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs @@ -42,7 +42,17 @@ internal sealed record DiagnosticInfo( /// A new instance with the specified parameters. public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location? location, params object[] args) { - return new(descriptor, location?.SourceTree, location?.SourceSpan ?? default, args.Select(static arg => arg.ToString()).ToImmutableArray()); + // The returned DiagnosticInfo instances will be used inside incremental collections. Because of + // that, we pre-transform all arguments with ToString(), so they are guaranteed to be equatable + // from the start and not to keep compilations alive (if eg. they happen to be symbol objects). + EquatableArray textArgs = args.Select(static arg => arg.ToString()).ToImmutableArray(); + + if (location is null) + { + return new(descriptor, null, default, textArgs); + } + + return new(descriptor, location.SourceTree, location.SourceSpan, textArgs); } /// @@ -54,7 +64,7 @@ internal sealed record DiagnosticInfo( /// A new instance with the specified parameters. public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, ISymbol symbol, params object[] args) { - return Create(descriptor, symbol.Locations.First(), args); + return Create(descriptor, symbol.Locations.FirstOrDefault(), args); } /// diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index ab84b2fe..298bffbd 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -124,14 +124,17 @@ partial class ComputeShaderDescriptorGenerator diagnostic = DiagnosticInfo.Create( HlslBytecodeFailedWithWin32Exception, structDeclarationSymbol, - [structDeclarationSymbol, win32Error.HResult, win32Error.Message]); + structDeclarationSymbol, + win32Error.HResult, + win32Error.Message); } else if (info is HlslBytecodeInfo.CompilerError dxcError) { diagnostic = DiagnosticInfo.Create( HlslBytecodeFailedWithDxcCompilationException, structDeclarationSymbol, - [structDeclarationSymbol, dxcError.Message]); + structDeclarationSymbol, + dxcError.Message); } if (diagnostic is not null)