From d4473c2ed727577065949c7bed121cc1cfdac95f Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 28 Jun 2026 21:21:56 +0200 Subject: [PATCH] Fix doubled index on macro opcodes in the IL view The IL view renders a macro opcode like ldarg.0 as the mnemonic (an opcode reference) followed by the index written as a separate clickable local or parameter reference. AvaloniaEditTextOutput's omitSuffix path used TrimEnd('.') to drop the index, but the name ends in the index digit, not a dot, so it was a no-op: the full "ldarg.0" was written and the index appended again, producing "ldarg.00" (and likewise ldloc.NN / stloc.NN). PlainTextOutput and the WPF AvalonEditTextOutput strip everything after the last dot; the Avalonia port regressed this. Restore that behavior. Only the UI was affected -- ilspycmd and the disassembler tests use PlainTextOutput. Assisted-by: Claude:claude-opus-4-8:Claude Code --- ILSpy.Tests/TextView/OpCodeReferenceTests.cs | 74 ++++++++++++++++++++ ILSpy/TextView/AvaloniaEditTextOutput.cs | 8 ++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 ILSpy.Tests/TextView/OpCodeReferenceTests.cs diff --git a/ILSpy.Tests/TextView/OpCodeReferenceTests.cs b/ILSpy.Tests/TextView/OpCodeReferenceTests.cs new file mode 100644 index 000000000..e1602039e --- /dev/null +++ b/ILSpy.Tests/TextView/OpCodeReferenceTests.cs @@ -0,0 +1,74 @@ +// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System.Reflection.Metadata; + +using AwesomeAssertions; + +using ICSharpCode.Decompiler.Disassembler; +using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.ILSpy.TextView; + +using NUnit.Framework; + +namespace ICSharpCode.ILSpy.Tests.TextView; + +/// +/// The IL view renders a macro opcode such as "ldarg.0" by writing the mnemonic as an opcode +/// reference with the index omitted, then writing the index as a separate clickable local or +/// parameter reference. The omitSuffix path must keep the mnemonic up to the dot ("ldarg.") so the +/// index is written exactly once; otherwise the opcode reads "ldarg.00". +/// +[TestFixture] +public class OpCodeReferenceTests +{ + static OpCodeInfo MacroOpCode(ILOpCode opCode) => new(opCode, opCode.GetDisplayName()); + + [Test] + public void WriteReference_OmitSuffix_Keeps_The_Mnemonic_Up_To_The_Dot() + { + var output = new AvaloniaEditTextOutput(); + + output.WriteReference(MacroOpCode(ILOpCode.Ldarg_0), omitSuffix: true); + + output.GetText().Should().Be("ldarg."); + } + + [Test] + public void WriteReference_Without_OmitSuffix_Writes_The_Full_Opcode() + { + var output = new AvaloniaEditTextOutput(); + + output.WriteReference(MacroOpCode(ILOpCode.Ldarg_0)); + + output.GetText().Should().Be("ldarg.0"); + } + + [Test] + public void Macro_Opcode_Renders_Its_Index_Exactly_Once() + { + // Mirrors MethodBodyDisassembler.WriteOpCode for a macro opcode: the mnemonic (index omitted) + // followed by the index as a local/parameter reference. + var output = new AvaloniaEditTextOutput(); + + output.WriteReference(MacroOpCode(ILOpCode.Ldarg_0), omitSuffix: true); + output.WriteLocalReference("0", "param_0"); + + output.GetText().Should().Be("ldarg.0"); + } +} diff --git a/ILSpy/TextView/AvaloniaEditTextOutput.cs b/ILSpy/TextView/AvaloniaEditTextOutput.cs index dbe688d94..64611b91e 100644 --- a/ILSpy/TextView/AvaloniaEditTextOutput.cs +++ b/ILSpy/TextView/AvaloniaEditTextOutput.cs @@ -180,7 +180,13 @@ namespace ICSharpCode.ILSpy.TextView { WriteIndentIfNeeded(); int start = builder.Length; - var name = omitSuffix ? opCode.Name.TrimEnd('.') : opCode.Name; + string name = opCode.Name; + if (omitSuffix) + { + int lastDot = name.LastIndexOf('.'); + if (lastDot > 0) + name = name.Remove(lastDot + 1); + } builder.Append(name); References.Add(new ReferenceSegment { StartOffset = start,