Browse Source

Fix an issue in IL code comparison where identical input files could incorrectly produce different output lines, such as ` 275 (-) IL_000a: stloc.0` versus ` 275 (+) IL_000a: stlocc.0`.

pull/3598/head
sonyps5201314 2 months ago
parent
commit
e4a31b8952
  1. 32
      ICSharpCode.Decompiler.Tests/Helpers/CodeAssert.cs
  2. 2
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

32
ICSharpCode.Decompiler.Tests/Helpers/CodeAssert.cs

@ -20,10 +20,20 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
AreEqual(File.ReadAllText(fileName1), File.ReadAllText(fileName2), definedSymbols); AreEqual(File.ReadAllText(fileName1), File.ReadAllText(fileName2), definedSymbols);
} }
public static void AreEqualIL(string input1, string input2, string[] definedSymbols = null)
{
AreEqual(input1, input2, false, definedSymbols);
}
public static void AreEqual(string input1, string input2, string[] definedSymbols = null) public static void AreEqual(string input1, string input2, string[] definedSymbols = null)
{
AreEqual(input1, input2, true, definedSymbols);
}
public static void AreEqual(string input1, string input2, bool isCsCode, string[] definedSymbols = null)
{ {
var diff = new StringWriter(); var diff = new StringWriter();
if (!CodeComparer.Compare(input1, input2, diff, CodeComparer.NormalizeLine, definedSymbols)) if (!CodeComparer.Compare(input1, input2, isCsCode, diff, CodeComparer.NormalizeLine, definedSymbols))
{ {
Assert.Fail(diff.ToString()); Assert.Fail(diff.ToString());
} }
@ -34,8 +44,13 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
{ {
public static bool Compare(string input1, string input2, StringWriter diff, Func<string, string> normalizeLine, string[] definedSymbols = null) public static bool Compare(string input1, string input2, StringWriter diff, Func<string, string> normalizeLine, string[] definedSymbols = null)
{ {
var collection1 = NormalizeAndSplitCode(input1, definedSymbols ?? new string[0]); return Compare(input1, input2, true, diff, normalizeLine, definedSymbols);
var collection2 = NormalizeAndSplitCode(input2, definedSymbols ?? new string[0]); }
public static bool Compare(string input1, string input2, bool isCsCode, StringWriter diff, Func<string, string> normalizeLine, string[] definedSymbols = null)
{
var collection1 = NormalizeAndSplitCode(input1, isCsCode, definedSymbols ?? new string[0]);
var collection2 = NormalizeAndSplitCode(input2, isCsCode, definedSymbols ?? new string[0]);
var diffSections = DiffLib.Diff.CalculateSections( var diffSections = DiffLib.Diff.CalculateSections(
collection1, collection2, new CodeLineEqualityComparer(normalizeLine) collection1, collection2, new CodeLineEqualityComparer(normalizeLine)
); );
@ -187,11 +202,14 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
} }
} }
private static IList<string> NormalizeAndSplitCode(string input, IEnumerable<string> definedSymbols) private static IList<string> NormalizeAndSplitCode(string input, bool isCsCode, IEnumerable<string> definedSymbols)
{ {
var syntaxTree = CSharpSyntaxTree.ParseText(input, new CSharpParseOptions(preprocessorSymbols: definedSymbols)); if (isCsCode)
var result = new DeleteDisabledTextRewriter().Visit(syntaxTree.GetRoot()); {
input = result.ToFullString(); var syntaxTree = CSharpSyntaxTree.ParseText(input, new CSharpParseOptions(preprocessorSymbols: definedSymbols));
var result = new DeleteDisabledTextRewriter().Visit(syntaxTree.GetRoot());
input = result.ToFullString();
}
return input.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); return input.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
} }
} }

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -944,7 +944,7 @@ namespace ICSharpCode.Decompiler.Tests
await Tester.Disassemble(recompiledAssembly, recompiledIl, asmOptions | AssemblerOptions.SortedOutput).ConfigureAwait(false); await Tester.Disassemble(recompiledAssembly, recompiledIl, asmOptions | AssemblerOptions.SortedOutput).ConfigureAwait(false);
CodeAssert.AreEqual(File.ReadAllText(originalIl), File.ReadAllText(recompiledIl)); CodeAssert.AreEqualIL(File.ReadAllText(originalIl), File.ReadAllText(recompiledIl));
// IL roundtrip matched: treat test as warning // IL roundtrip matched: treat test as warning
var csCompareWarning = "The writing style does not conform to the latest recommendations:\r\n" + csCompareDiff + "\r\n\r\n"; var csCompareWarning = "The writing style does not conform to the latest recommendations:\r\n" + csCompareDiff + "\r\n\r\n";
Assert.Warn(csCompareWarning + "Textual comparison failed but IL roundtrip matched."); Assert.Warn(csCompareWarning + "Textual comparison failed but IL roundtrip matched.");

Loading…
Cancel
Save