Browse Source

Fix bug in SimplifyCascadingIfStatements

pull/887/head
Siegfried Pammer 8 years ago
parent
commit
800a635663
  1. 6
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

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

@ -88,15 +88,12 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false; return false;
if (!firstBlockJump.MatchBranch(out var firstBlock)) if (!firstBlockJump.MatchBranch(out var firstBlock))
return false; return false;
bool isLegacy;
List<(string, Block)> values = new List<(string, Block)>(); List<(string, Block)> values = new List<(string, Block)>();
// match null check: this is used by the old C# compiler. // match null check: this is used by the old C# compiler.
if (condition.MatchCompEquals(out var left, out var right) && right.MatchLdNull() && left.MatchLdLoc(out var switchValueVar)) { if (condition.MatchCompEquals(out var left, out var right) && right.MatchLdNull() && left.MatchLdLoc(out var switchValueVar)) {
isLegacy = true;
values.Add((null, firstBlock)); values.Add((null, firstBlock));
// Roslyn: match call to operator ==(string, string) // Roslyn: match call to operator ==(string, string)
} else if (MatchStringEqualityComparison(condition, out switchValueVar, out string value)) { } else if (MatchStringEqualityComparison(condition, out switchValueVar, out string value)) {
isLegacy = false;
values.Add((value, firstBlock)); values.Add((value, firstBlock));
} else return false; } else return false;
// switchValueVar must be assigned only once and must be of type string. // switchValueVar must be assigned only once and must be of type string.
@ -115,9 +112,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// We didn't find enough cases, exit // We didn't find enough cases, exit
if (values.Count < 3) if (values.Count < 3)
return false; return false;
// The block after all cases should only be reachable from the previous block and the null-check (in legacy code).
if (currentCaseBlock.IncomingEdgeCount != (isLegacy ? 2 : 1))
return false;
var sections = new List<SwitchSection>(values.SelectWithIndex((index, b) => new SwitchSection { Labels = new LongSet(index), Body = new Branch(b.Item2) })); var sections = new List<SwitchSection>(values.SelectWithIndex((index, b) => new SwitchSection { Labels = new LongSet(index), Body = new Branch(b.Item2) }));
sections.Add(new SwitchSection { Labels = new LongSet(new LongInterval(0, sections.Count)).Invert(), Body = new Branch(currentCaseBlock) }); sections.Add(new SwitchSection { Labels = new LongSet(new LongInterval(0, sections.Count)).Invert(), Body = new Branch(currentCaseBlock) });
var stringToInt = new StringToInt(new LdLoc(switchValueVar), values.SelectArray(item => item.Item1)); var stringToInt = new StringToInt(new LdLoc(switchValueVar), values.SelectArray(item => item.Item1));

Loading…
Cancel
Save