Browse Source

Fix bug in SwitchOnStringTransform introduced by 9719926b6bc1e43c9d43d811e5c27d9e797209b8: Ignore exit instruction if condition is not inverted. Otherwise we need a special case for br and leave.

pull/887/head
Siegfried Pammer 8 years ago
parent
commit
930d142a62
  1. 2
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  2. 37
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs
  3. 6
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -97,7 +97,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -97,7 +97,7 @@ namespace ICSharpCode.Decompiler.Tests
Run(cscOptions: cscOptions);
}
[Test]
[Test, Ignore("unnecessary casts on null literals, control-flow issues with switch in loops, goto, goto case, etc.")]
public void Switch([ValueSource("defaultOptions")] CompilerOptions cscOptions)
{
Run(cscOptions: cscOptions);

37
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs

@ -107,8 +107,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -107,8 +107,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
case "Sixth case": {
return "Text6";
}
case (string)null: {
return (string)null;
case null: {
return null;
}
default: {
return "Default";
@ -119,7 +119,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -119,7 +119,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public static string SwitchOverString2()
{
Console.WriteLine("SwitchOverString2:");
switch (Environment.UserName) {
string userName = Environment.UserName;
switch (userName) {
case "First case": {
return "Text1";
}
@ -180,23 +181,28 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -180,23 +181,28 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine("SwitchInLoop: " + i);
while (true) {
switch (i) {
case 1:
case 1: {
Console.WriteLine("one");
break;
case 2:
}
case 2: {
Console.WriteLine("two");
break;
case 3:
}
case 3: {
Console.WriteLine("three");
continue;
case 4:
}
case 4: {
Console.WriteLine("four");
return;
default:
}
default: {
Console.WriteLine("default");
Console.WriteLine("more code");
return;
}
}
i++;
}
}
@ -205,22 +211,27 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -205,22 +211,27 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
Console.WriteLine("SwitchWithGoto: " + i);
switch (i) {
case 1:
case 1: {
Console.WriteLine("one");
goto default;
case 2:
}
case 2: {
Console.WriteLine("two");
goto case 3;
case 3:
}
case 3: {
Console.WriteLine("three");
break;
case 4:
}
case 4: {
Console.WriteLine("four");
return;
default:
}
default: {
Console.WriteLine("default");
break;
}
}
}
}
}

6
ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

@ -315,15 +315,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -315,15 +315,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false;
if (!target.Instructions[0].MatchIfInstruction(out var condition, out var bodyBranch))
return false;
if (!target.Instructions[1].MatchBranch(out Block exit))
return false;
if (!bodyBranch.MatchBranch(out Block body))
return false;
if (!MatchStringEqualityComparison(condition, switchValue.Variable, out string stringValue)) {
if (condition.MatchLogicNot(out condition) && MatchStringEqualityComparison(condition, switchValue.Variable, out stringValue)) {
var swap = body;
if (!target.Instructions[1].MatchBranch(out Block exit))
return false;
body = exit;
exit = swap;
} else
return false;
}

Loading…
Cancel
Save