Browse Source

Fixed bug in switch transform.

pull/150/head
Daniel Grunwald 14 years ago
parent
commit
3875839485
  1. 26
      ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs
  2. 6
      ICSharpCode.Decompiler/Tests/Switch.cs
  3. 2
      ICSharpCode.Decompiler/Tests/TestRunner.cs

26
ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs

@ -630,8 +630,6 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
Match m = switchOnStringPattern.Match(node); Match m = switchOnStringPattern.Match(node);
if (!m.Success) if (!m.Success)
return null; return null;
if (m.Has("nonNullDefaultStmt") && !m.Has("nullStmt"))
return null;
// switchVar must be the same as switchExpr; or switchExpr must be an assignment and switchVar the left side of that assignment // switchVar must be the same as switchExpr; or switchExpr must be an assignment and switchVar the left side of that assignment
if (!m.Get("switchVar").Single().IsMatch(m.Get("switchExpr").Single())) { if (!m.Get("switchVar").Single().IsMatch(m.Get("switchExpr").Single())) {
AssignmentExpression assign = m.Get("switchExpr").Single() as AssignmentExpression; AssignmentExpression assign = m.Get("switchExpr").Single() as AssignmentExpression;
@ -666,15 +664,21 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
block.Statements.Add(new BreakStatement()); block.Statements.Add(new BreakStatement());
section.Statements.Add(block.Detach()); section.Statements.Add(block.Detach());
sw.SwitchSections.Add(section); sw.SwitchSections.Add(section);
if (m.Has("nonNullDefaultStmt")) { } else if (m.Has("nonNullDefaultStmt")) {
section = new SwitchSection(); sw.SwitchSections.Add(
section.CaseLabels.Add(new CaseLabel()); new SwitchSection {
block = new BlockStatement(); CaseLabels = { new CaseLabel { Expression = new NullReferenceExpression() } },
block.Statements.AddRange(m.Get<Statement>("nonNullDefaultStmt").Select(s => s.Detach())); Statements = { new BlockStatement { new BreakStatement() } }
block.Add(new BreakStatement()); });
section.Statements.Add(block); }
sw.SwitchSections.Add(section); if (m.Has("nonNullDefaultStmt")) {
} SwitchSection section = new SwitchSection();
section.CaseLabels.Add(new CaseLabel());
BlockStatement block = new BlockStatement();
block.Statements.AddRange(m.Get<Statement>("nonNullDefaultStmt").Select(s => s.Detach()));
block.Add(new BreakStatement());
section.Statements.Add(block);
sw.SwitchSections.Add(section);
} }
node.ReplaceWith(sw); node.ReplaceWith(sw);
return sw; return sw;

6
ICSharpCode.Decompiler/Tests/Switch.cs

@ -17,7 +17,8 @@ public static class Switch
public static string SwitchOverString1(string text) public static string SwitchOverString1(string text)
{ {
switch (text) { switch (text)
{
case "First case": case "First case":
return "Text1"; return "Text1";
case "Second case": case "Second case":
@ -40,7 +41,8 @@ public static class Switch
public static string SwitchOverString2() public static string SwitchOverString2()
{ {
switch (Environment.UserName) { switch (Environment.UserName)
{
case "First case": case "First case":
return "Text1"; return "Text1";
case "Second case": case "Second case":

2
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -85,7 +85,7 @@ namespace ICSharpCode.Decompiler.Tests
TestFile(@"..\..\Tests\QueryExpressions.cs"); TestFile(@"..\..\Tests\QueryExpressions.cs");
} }
[Test, Ignore("switch transform is currently broken")] [Test, Ignore("switch transform doesn't recreate the exact original switch")]
public void Switch() public void Switch()
{ {
TestFile(@"..\..\Tests\Switch.cs"); TestFile(@"..\..\Tests\Switch.cs");

Loading…
Cancel
Save