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 @@ -630,8 +630,6 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
Match m = switchOnStringPattern.Match(node);
if (!m.Success)
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
if (!m.Get("switchVar").Single().IsMatch(m.Get("switchExpr").Single())) {
AssignmentExpression assign = m.Get("switchExpr").Single() as AssignmentExpression;
@ -666,15 +664,21 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -666,15 +664,21 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
block.Statements.Add(new BreakStatement());
section.Statements.Add(block.Detach());
sw.SwitchSections.Add(section);
if (m.Has("nonNullDefaultStmt")) {
section = new SwitchSection();
section.CaseLabels.Add(new CaseLabel());
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);
}
} else if (m.Has("nonNullDefaultStmt")) {
sw.SwitchSections.Add(
new SwitchSection {
CaseLabels = { new CaseLabel { Expression = new NullReferenceExpression() } },
Statements = { new BlockStatement { new BreakStatement() } }
});
}
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);
return sw;

6
ICSharpCode.Decompiler/Tests/Switch.cs

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

2
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -85,7 +85,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -85,7 +85,7 @@ namespace ICSharpCode.Decompiler.Tests
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()
{
TestFile(@"..\..\Tests\Switch.cs");

Loading…
Cancel
Save