From 1de2962ae373744a05e06308357a89513b7d38ae Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Apr 2022 03:11:43 +0200 Subject: [PATCH] Improve HLSL build errors for D2D1 shaders output message --- ...haderGenerator.CreateLoadBytecodeMethod.cs | 53 ++++++++++++++++++- .../Shaders/Translation/D2D1ShaderCompiler.cs | 2 +- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 0682d904..e7ec8eab 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -1,8 +1,10 @@ using System; +using System.Buffers; using System.Collections.Immutable; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; using System.Threading; using ComputeSharp.D2D1.Exceptions; using ComputeSharp.D2D1.Shaders.Translation; @@ -111,15 +113,62 @@ partial class ID2D1ShaderGenerator } catch (Win32Exception e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); } catch (FxcCompilationException e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); } End: return bytecode; } + + /// + /// Fixes up an exception message to improve the way it's displayed in VS. + /// + /// The input exception message. + /// The updated exception message. + private static string FixupExceptionMessage(string message) + { + // Add square brackets around error headers + message = Regex.Replace(message, @"((?:error|warning) \w+):", static m => $"[{m.Groups[1].Value}]:"); + + char[] buffer = ArrayPool.Shared.Rent(message.Length); + int index = 0; + char lastCharacter = '\0'; + + foreach (char newCharacter in message) + { + char pendingCharacter = newCharacter; + + // Ignore '\r' characters + if (pendingCharacter is '\r') + { + continue; + } + + // Convert newlines to spaces (this avoids VS droppping multiline diagnostics) + if (pendingCharacter is '\n') + { + pendingCharacter = ' '; + } + + // Drop duplicate spaces + if (pendingCharacter is ' ' && + lastCharacter is ' ') + { + continue; + } + + buffer[index++] = lastCharacter = pendingCharacter; + } + + string text = buffer.AsSpan(0, index).ToString(); + + ArrayPool.Shared.Return(buffer); + + return text; + } } } diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs index a570cf48..cd931470 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs @@ -226,7 +226,7 @@ internal static unsafe partial class D2D1ShaderCompiler // The error message will be in a format like this: // "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Shader@0x0000019AD1B4BA70(22,32-35): error X3004: undeclared identifier 'this'" // This regex tries to match the unnecessary header and remove it, if present. This doesn't need to be bulletproof, and this regex should match all cases anyway. - message = Regex.Replace(message, @"^[A-Z]:\\(?:[^\\]+\\)+Shader@0x[0-9A-F]+\([\d,-]+\): ", ""); + message = Regex.Replace(message, @"[A-Z]:\\[^:]+: (\w+ \w+:)", static m => m.Groups[1].Value).Trim(); // Add a trailing '.' if not present if (message is { Length: > 0 } &&