Browse Source

ILAst: show unused IL ranges

pull/923/head
Daniel Grunwald 8 years ago
parent
commit
d2b6651507
  1. 20
      ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs
  2. 21
      ICSharpCode.Decompiler/IL/ILAstWritingOptions.cs
  3. 1
      ICSharpCode.Decompiler/IL/Instructions/Block.cs
  4. 1
      ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs
  5. 31
      ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs

20
ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs

@ -171,9 +171,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -171,9 +171,11 @@ namespace ICSharpCode.Decompiler.CSharp
return;
}
// Add the IL range associated with this instruction to the current sequence point.
if (!inst.ILRange.IsEmpty && current.Intervals != null) {
if (HasUsableILRange(inst) && current.Intervals != null) {
current.Intervals.Add(inst.ILRange);
current.Function = inst.Parent.Ancestors.OfType<ILFunction>().FirstOrDefault();
var function = inst.Parent.Ancestors.OfType<ILFunction>().FirstOrDefault();
Debug.Assert(current.Function == null || current.Function == function);
current.Function = function;
}
// Do not add instructions of lambdas/delegates.
@ -187,6 +189,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -187,6 +189,13 @@ namespace ICSharpCode.Decompiler.CSharp
}
}
internal static bool HasUsableILRange(ILInstruction inst)
{
if (inst.ILRange.IsEmpty)
return false;
return !(inst is BlockContainer || inst is Block);
}
/// <summary>
/// Called after the visitor is done to return the results.
/// </summary>
@ -227,6 +236,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -227,6 +236,13 @@ namespace ICSharpCode.Decompiler.CSharp
newList.Add(sequencePoint);
pos = sequencePoint.EndOffset;
}
if (pos < function.CecilMethod.Body.CodeSize) {
var hidden = new SequencePoint();
hidden.Offset = pos;
hidden.EndOffset = function.CecilMethod.Body.CodeSize;
hidden.SetHidden();
newList.Add(hidden);
}
dict[function] = newList;
}
return dict;

21
ICSharpCode.Decompiler/IL/ILAstWritingOptions.cs

@ -1,8 +1,23 @@ @@ -1,8 +1,23 @@
using System;
using System.Collections.Generic;
// Copyright (c) 2017 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace ICSharpCode.Decompiler.IL
{

1
ICSharpCode.Decompiler/IL/Instructions/Block.cs

@ -129,6 +129,7 @@ namespace ICSharpCode.Decompiler.IL @@ -129,6 +129,7 @@ namespace ICSharpCode.Decompiler.IL
public override void WriteTo(ITextOutput output, ILAstWritingOptions options)
{
ILRange.WriteTo(output, options);
output.Write("Block ");
output.WriteDefinition(Label, this);
if (Parent is BlockContainer)

1
ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs

@ -107,6 +107,7 @@ namespace ICSharpCode.Decompiler.IL @@ -107,6 +107,7 @@ namespace ICSharpCode.Decompiler.IL
public override void WriteTo(ITextOutput output, ILAstWritingOptions options)
{
ILRange.WriteTo(output, options);
output.WriteDefinition("BlockContainer", this);
output.Write(' ');
output.MarkFoldStart("{...}");

31
ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs

@ -114,12 +114,41 @@ namespace ICSharpCode.Decompiler.IL @@ -114,12 +114,41 @@ namespace ICSharpCode.Decompiler.IL
}
body.WriteTo(output, options);
output.WriteLine();
if (options.ShowILRanges) {
var unusedILRanges = FindUnusedILRanges();
if (!unusedILRanges.IsEmpty) {
output.Write("// Unused IL Ranges: ");
output.Write(string.Join(", ", unusedILRanges.Intervals.Select(
range => $"[{range.Start:x4}..{range.InclusiveEnd:x4}]")));
output.WriteLine();
}
}
output.Unindent();
output.WriteLine("}");
}
LongSet FindUnusedILRanges()
{
var usedILRanges = new List<LongInterval>();
MarkUsedILRanges(body);
return new LongSet(new LongInterval(0, CecilMethod.Body.CodeSize)).ExceptWith(new LongSet(usedILRanges));
void MarkUsedILRanges(ILInstruction inst)
{
if (CSharp.SequencePointBuilder.HasUsableILRange(inst)) {
usedILRanges.Add(new LongInterval(inst.ILRange.Start, inst.ILRange.End));
}
if (!(inst is ILFunction)) {
foreach (var child in inst.Children) {
MarkUsedILRanges(child);
}
}
}
}
protected override InstructionFlags ComputeFlags()
{
// Creating a lambda may throw OutOfMemoryException

Loading…
Cancel
Save