Browse Source

Merge branch 'master' of https://github.com/icsharpcode/ILSpy into srm

pull/1198/head
Siegfried Pammer 8 years ago
parent
commit
70592cf6b8
  1. 1
      ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs
  2. 38
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

1
ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs

@ -29,6 +29,7 @@ using NUnit.Framework; @@ -29,6 +29,7 @@ using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests.TypeSystem
{
[TestFixture]
public class TypeSystemLoaderTests
{
static readonly Lazy<IUnresolvedAssembly> mscorlib = new Lazy<IUnresolvedAssembly>(

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

@ -285,6 +285,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -285,6 +285,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
sections.Add(new SwitchSection() { Body = switchBlock.Instructions[1], Labels = new LongSet(0).Invert(), ILRange = switchBlock.Instructions[1].ILRange });
break;
}
// mcs: map sections without a value to the default section, if possible
if (!FixCasesWithoutValue(sections, stringValues))
return false;
// switch contains case null:
if (nullValueCaseBlock != defaultBlock) {
if (!AddNullSection(sections, stringValues, nullValueCaseBlock)) {
@ -312,6 +315,41 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -312,6 +315,41 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return true;
}
bool FixCasesWithoutValue(List<SwitchSection> sections, List<(string, int)> stringValues)
{
bool HasLabel(SwitchSection section)
{
return section.Labels.Values.Any(i => stringValues.Any(value => i == value.Item2));
}
// Pick the section with the most labels as default section.
// And collect all sections that have no value mapped to them.
SwitchSection defaultSection = sections.First();
List<SwitchSection> sectionsWithoutLabels = new List<SwitchSection>();
foreach (var section in sections) {
if (section == defaultSection) continue;
if (section.Labels.Count() > defaultSection.Labels.Count()) {
if (!HasLabel(defaultSection))
sectionsWithoutLabels.Add(defaultSection);
defaultSection = section;
continue;
}
if (!HasLabel(section))
sectionsWithoutLabels.Add(section);
}
foreach (var section in sectionsWithoutLabels) {
if (!section.Body.Match(defaultSection.Body).Success)
return false;
defaultSection.Labels = defaultSection.Labels.UnionWith(section.Labels);
if (section.HasNullLabel)
defaultSection.HasNullLabel = true;
sections.Remove(section);
}
return true;
}
bool AddNullSection(List<SwitchSection> sections, List<(string, int)> stringValues, Block nullValueCaseBlock)
{
var label = new LongSet(sections.Count);

Loading…
Cancel
Save