Browse Source

Merge pull request #1920 from icsharpcode/fix-1919

Fix #1919: Use unmapped IL offsets at the start of a catch-block for the 'exception specifier' sequence point.
pull/2016/head
Daniel Grunwald 5 years ago committed by GitHub
parent
commit
c71802fd7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs
  2. 45
      ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs
  3. 1
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs
  4. 166
      ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs
  5. 25
      ICSharpCode.Decompiler/IL/Instructions/ILInstruction.cs
  6. 13
      ICSharpCode.Decompiler/IL/Instructions/TryInstruction.cs
  7. 14
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

4
ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1812,11 +1812,11 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -1812,11 +1812,11 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
Space();
WriteKeyword(CatchClause.WhenKeywordRole);
Space(policy.SpaceBeforeIfParentheses);
LPar();
WriteToken(CatchClause.CondLPar);
Space(policy.SpacesWithinIfParentheses);
catchClause.Condition.AcceptVisitor(this);
Space(policy.SpacesWithinIfParentheses);
RPar();
WriteToken(CatchClause.CondRPar);
}
WriteBlock(catchClause.Body, policy.StatementBraceStyle);
EndNode(catchClause);

45
ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs

@ -113,7 +113,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -113,7 +113,13 @@ namespace ICSharpCode.Decompiler.CSharp
ILInstruction blockContainer = blockStatement.Annotations.OfType<ILInstruction>().FirstOrDefault();
if (blockContainer != null) {
StartSequencePoint(blockStatement.LBraceToken);
int intervalStart = blockContainer.ILRanges.First().Start;
int intervalStart;
if (blockContainer.Parent is TryCatchHandler handler && !handler.ExceptionSpecifierILRange.IsEmpty) {
// if this block container is part of a TryCatchHandler, do not steal the exception-specifier IL range
intervalStart = handler.ExceptionSpecifierILRange.End;
} else {
intervalStart = blockContainer.StartILOffset;
}
// The end will be set to the first sequence point candidate location before the first statement of the function when the seqeunce point is adjusted
int intervalEnd = intervalStart + 1;
@ -256,12 +262,15 @@ namespace ICSharpCode.Decompiler.CSharp @@ -256,12 +262,15 @@ namespace ICSharpCode.Decompiler.CSharp
foreachStatement.InExpression.AcceptVisitor(this);
AddToSequencePoint(foreachInfo.GetEnumeratorCall);
EndSequencePoint(foreachStatement.InExpression.StartLocation, foreachStatement.InExpression.EndLocation);
StartSequencePoint(foreachStatement);
AddToSequencePoint(foreachInfo.MoveNextCall);
EndSequencePoint(foreachStatement.InToken.StartLocation, foreachStatement.InToken.EndLocation);
StartSequencePoint(foreachStatement);
AddToSequencePoint(foreachInfo.GetCurrentCall);
EndSequencePoint(foreachStatement.VariableType.StartLocation, foreachStatement.VariableNameToken.EndLocation);
VisitAsSequencePoint(foreachStatement.EmbeddedStatement);
}
@ -310,6 +319,33 @@ namespace ICSharpCode.Decompiler.CSharp @@ -310,6 +319,33 @@ namespace ICSharpCode.Decompiler.CSharp
VisitAsSequencePoint(fixedStatement.EmbeddedStatement);
}
public override void VisitTryCatchStatement(TryCatchStatement tryCatchStatement)
{
VisitAsSequencePoint(tryCatchStatement.TryBlock);
foreach (var c in tryCatchStatement.CatchClauses) {
VisitAsSequencePoint(c);
}
VisitAsSequencePoint(tryCatchStatement.FinallyBlock);
}
public override void VisitCatchClause(CatchClause catchClause)
{
if (catchClause.Condition.IsNull) {
var tryCatchHandler = catchClause.Annotation<TryCatchHandler>();
if (!tryCatchHandler.ExceptionSpecifierILRange.IsEmpty) {
StartSequencePoint(catchClause.CatchToken);
var function = tryCatchHandler.Ancestors.OfType<ILFunction>().FirstOrDefault();
AddToSequencePointRaw(function, new[] { tryCatchHandler.ExceptionSpecifierILRange });
EndSequencePoint(catchClause.CatchToken.StartLocation, catchClause.RParToken.IsNull ? catchClause.CatchToken.EndLocation : catchClause.RParToken.EndLocation);
}
} else {
StartSequencePoint(catchClause.WhenToken);
AddToSequencePoint(catchClause.Condition);
EndSequencePoint(catchClause.WhenToken.StartLocation, catchClause.CondRParToken.EndLocation);
}
VisitAsSequencePoint(catchClause.Body);
}
/// <summary>
/// Start a new C# statement = new sequence point.
/// </summary>
@ -339,6 +375,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -339,6 +375,13 @@ namespace ICSharpCode.Decompiler.CSharp
current = outerStates.Pop();
}
void AddToSequencePointRaw(ILFunction function, IEnumerable<Interval> ranges)
{
current.Intervals.AddRange(ranges);
Debug.Assert(current.Function == null || current.Function == function);
current.Function = function;
}
/// <summary>
/// Add the ILAst instruction associated with the AstNode to the sequence point.
/// Also add all its ILAst sub-instructions (unless they were already added to another sequence point).

1
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -363,6 +363,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -363,6 +363,7 @@ namespace ICSharpCode.Decompiler.CSharp
tryCatch.TryBlock = ConvertAsBlock(inst.TryBlock);
foreach (var handler in inst.Handlers) {
var catchClause = new CatchClause();
catchClause.AddAnnotation(handler);
var v = handler.Variable;
if (v != null) {
catchClause.AddAnnotation(new ILVariableResolveResult(v, v.Type));

166
ICSharpCode.Decompiler/CSharp/Syntax/Statements/TryCatchStatement.cs

@ -32,68 +32,70 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -32,68 +32,70 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
/// </summary>
public class TryCatchStatement : Statement
{
public static readonly TokenRole TryKeywordRole = new TokenRole ("try");
public static readonly TokenRole TryKeywordRole = new TokenRole("try");
public static readonly Role<BlockStatement> TryBlockRole = new Role<BlockStatement>("TryBlock", BlockStatement.Null);
public static readonly Role<CatchClause> CatchClauseRole = new Role<CatchClause>("CatchClause", CatchClause.Null);
public static readonly TokenRole FinallyKeywordRole = new TokenRole ("finally");
public static readonly TokenRole FinallyKeywordRole = new TokenRole("finally");
public static readonly Role<BlockStatement> FinallyBlockRole = new Role<BlockStatement>("FinallyBlock", BlockStatement.Null);
public CSharpTokenNode TryToken {
get { return GetChildByRole (TryKeywordRole); }
get { return GetChildByRole(TryKeywordRole); }
}
public BlockStatement TryBlock {
get { return GetChildByRole (TryBlockRole); }
set { SetChildByRole (TryBlockRole, value); }
get { return GetChildByRole(TryBlockRole); }
set { SetChildByRole(TryBlockRole, value); }
}
public AstNodeCollection<CatchClause> CatchClauses {
get { return GetChildrenByRole (CatchClauseRole); }
get { return GetChildrenByRole(CatchClauseRole); }
}
public CSharpTokenNode FinallyToken {
get { return GetChildByRole (FinallyKeywordRole); }
get { return GetChildByRole(FinallyKeywordRole); }
}
public BlockStatement FinallyBlock {
get { return GetChildByRole (FinallyBlockRole); }
set { SetChildByRole (FinallyBlockRole, value); }
get { return GetChildByRole(FinallyBlockRole); }
set { SetChildByRole(FinallyBlockRole, value); }
}
public override void AcceptVisitor (IAstVisitor visitor)
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitTryCatchStatement (this);
visitor.VisitTryCatchStatement(this);
}
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitTryCatchStatement (this);
return visitor.VisitTryCatchStatement(this);
}
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitTryCatchStatement (this, data);
return visitor.VisitTryCatchStatement(this, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
TryCatchStatement o = other as TryCatchStatement;
return o != null && this.TryBlock.DoMatch(o.TryBlock, match) && this.CatchClauses.DoMatch(o.CatchClauses, match) && this.FinallyBlock.DoMatch(o.FinallyBlock, match);
}
}
/// <summary>
/// catch (Type VariableName) { Body }
/// </summary>
public class CatchClause : AstNode
{
public static readonly TokenRole CatchKeywordRole = new TokenRole ("catch");
public static readonly TokenRole WhenKeywordRole = new TokenRole ("when");
public static readonly TokenRole CatchKeywordRole = new TokenRole("catch");
public static readonly TokenRole WhenKeywordRole = new TokenRole("when");
public static readonly Role<Expression> ConditionRole = Roles.Condition;
public static readonly TokenRole CondLPar = new TokenRole("(");
public static readonly TokenRole CondRPar = new TokenRole(")");
#region Null
public new static readonly CatchClause Null = new NullCatchClause ();
public new static readonly CatchClause Null = new NullCatchClause();
sealed class NullCatchClause : CatchClause
{
public override bool IsNull {
@ -101,22 +103,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -101,22 +103,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return true;
}
}
public override void AcceptVisitor (IAstVisitor visitor)
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitNullNode(this);
}
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitNullNode(this);
}
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitNullNode(this, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
return other == null || other.IsNull;
@ -129,26 +131,26 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -129,26 +131,26 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
return pattern != null ? new PatternPlaceholder(pattern) : null;
}
sealed class PatternPlaceholder : CatchClause, PatternMatching.INode
{
readonly PatternMatching.Pattern child;
public PatternPlaceholder(PatternMatching.Pattern child)
{
this.child = child;
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
}
public override void AcceptVisitor (IAstVisitor visitor)
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitPatternPlaceholder(this, child);
}
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitPatternPlaceholder(this, child);
}
@ -157,90 +159,98 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -157,90 +159,98 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
return visitor.VisitPatternPlaceholder(this, child, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
return child.DoMatch(other, match);
}
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
{
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
}
}
#endregion
public override NodeType NodeType {
get {
return NodeType.Unknown;
}
}
public CSharpTokenNode CatchToken {
get { return GetChildByRole (CatchKeywordRole); }
get { return GetChildByRole(CatchKeywordRole); }
}
public CSharpTokenNode LParToken {
get { return GetChildByRole (Roles.LPar); }
get { return GetChildByRole(Roles.LPar); }
}
public AstType Type {
get { return GetChildByRole (Roles.Type); }
set { SetChildByRole (Roles.Type, value); }
get { return GetChildByRole(Roles.Type); }
set { SetChildByRole(Roles.Type, value); }
}
public string VariableName {
get { return GetChildByRole (Roles.Identifier).Name; }
get { return GetChildByRole(Roles.Identifier).Name; }
set {
if (string.IsNullOrEmpty(value))
SetChildByRole (Roles.Identifier, null);
SetChildByRole(Roles.Identifier, null);
else
SetChildByRole (Roles.Identifier, Identifier.Create (value));
SetChildByRole(Roles.Identifier, Identifier.Create(value));
}
}
public Identifier VariableNameToken {
get {
return GetChildByRole (Roles.Identifier);
return GetChildByRole(Roles.Identifier);
}
set {
SetChildByRole(Roles.Identifier, value);
}
}
public CSharpTokenNode RParToken {
get { return GetChildByRole (Roles.RPar); }
get { return GetChildByRole(Roles.RPar); }
}
public CSharpTokenNode WhenToken {
get { return GetChildByRole (WhenKeywordRole); }
get { return GetChildByRole(WhenKeywordRole); }
}
public CSharpTokenNode CondLParToken {
get { return GetChildByRole(CondLPar); }
}
public Expression Condition {
get { return GetChildByRole(ConditionRole); }
get { return GetChildByRole(ConditionRole); }
set { SetChildByRole(ConditionRole, value); }
}
public CSharpTokenNode CondRParToken {
get { return GetChildByRole(CondRPar); }
}
public BlockStatement Body {
get { return GetChildByRole (Roles.Body); }
set { SetChildByRole (Roles.Body, value); }
get { return GetChildByRole(Roles.Body); }
set { SetChildByRole(Roles.Body, value); }
}
public override void AcceptVisitor (IAstVisitor visitor)
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitCatchClause (this);
visitor.VisitCatchClause(this);
}
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitCatchClause (this);
return visitor.VisitCatchClause(this);
}
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitCatchClause (this, data);
return visitor.VisitCatchClause(this, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
CatchClause o = other as CatchClause;

25
ICSharpCode.Decompiler/IL/Instructions/ILInstruction.cs

@ -214,24 +214,29 @@ namespace ICSharpCode.Decompiler.IL @@ -214,24 +214,29 @@ namespace ICSharpCode.Decompiler.IL
public void AddILRange(Interval newRange)
{
if (this.ILRange.IsEmpty) {
this.ILRange = newRange;
return;
this.ILRange = CombineILRange(this.ILRange, newRange);
}
protected static Interval CombineILRange(Interval oldRange, Interval newRange)
{
if (oldRange.IsEmpty) {
return newRange;
}
if (newRange.IsEmpty) {
return;
return oldRange;
}
if (newRange.Start <= this.StartILOffset) {
if (newRange.End < this.StartILOffset) {
this.ILRange = newRange; // use the earlier range
if (newRange.Start <= oldRange.Start) {
if (newRange.End < oldRange.Start) {
return newRange; // use the earlier range
} else {
// join overlapping ranges
this.ILRange = new Interval(newRange.Start, Math.Max(newRange.End, this.ILRange.End));
return new Interval(newRange.Start, Math.Max(newRange.End, oldRange.End));
}
} else if (newRange.Start <= this.ILRange.End) {
} else if (newRange.Start <= oldRange.End) {
// join overlapping ranges
this.ILRange = new Interval(this.StartILOffset, Math.Max(newRange.End, this.ILRange.End));
return new Interval(oldRange.Start, Math.Max(newRange.End, oldRange.End));
}
return oldRange;
}
public void AddILRange(ILInstruction sourceInstruction)

13
ICSharpCode.Decompiler/IL/Instructions/TryInstruction.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.IL
{
@ -175,6 +176,18 @@ namespace ICSharpCode.Decompiler.IL @@ -175,6 +176,18 @@ namespace ICSharpCode.Decompiler.IL
output.Write(' ');
body.WriteTo(output, options);
}
/// <summary>
/// Gets the ILRange of the instructions at the start of the catch-block,
/// that take the exception object and store it in the exception variable slot.
/// Note: This range is empty, if Filter is not empty, i.e., ldloc 1.
/// </summary>
public Interval ExceptionSpecifierILRange { get; private set; }
public void AddExceptionSpecifierILRange(Interval newRange)
{
ExceptionSpecifierILRange = CombineILRange(ExceptionSpecifierILRange, newRange);
}
}
partial class TryFinally

14
ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

@ -17,10 +17,12 @@ @@ -17,10 +17,12 @@
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.IL.Transforms
{
@ -692,7 +694,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -692,7 +694,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
TransformCatchWhen(inst, filterContainer.EntryPoint);
}
if (inst.Body is BlockContainer catchContainer)
TransformCatchVariable(inst, catchContainer.EntryPoint);
TransformCatchVariable(inst, catchContainer.EntryPoint, isCatchBlock: true);
}
/// <summary>
@ -709,7 +711,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -709,7 +711,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
/// }
/// }
/// </summary>
void TransformCatchVariable(TryCatchHandler handler, Block entryPoint)
void TransformCatchVariable(TryCatchHandler handler, Block entryPoint, bool isCatchBlock)
{
if (!handler.Variable.IsSingleDefinition || handler.Variable.LoadCount != 1)
return; // handle.Variable already has non-trivial uses
@ -720,6 +722,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -720,6 +722,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (inlinedUnboxAny.Type.Equals(handler.Variable.Type)) {
context.Step("TransformCatchVariable - remove inlined UnboxAny", inlinedUnboxAny);
inlinedUnboxAny.ReplaceWith(inlinedUnboxAny.Argument);
foreach (var range in inlinedUnboxAny.ILRanges)
handler.AddExceptionSpecifierILRange(range);
}
}
return;
@ -746,6 +750,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -746,6 +750,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
exceptionVar.Kind = VariableKind.ExceptionLocal;
exceptionVar.Type = handler.Variable.Type;
handler.Variable = exceptionVar;
if (isCatchBlock) {
foreach (var offset in entryPoint.Instructions[0].Descendants.SelectMany(o => o.ILRanges))
handler.AddExceptionSpecifierILRange(offset);
}
entryPoint.Instructions.RemoveAt(0);
}
@ -754,7 +762,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -754,7 +762,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
/// </summary>
void TransformCatchWhen(TryCatchHandler handler, Block entryPoint)
{
TransformCatchVariable(handler, entryPoint);
TransformCatchVariable(handler, entryPoint, isCatchBlock: false);
if (entryPoint.Instructions.Count == 1 && entryPoint.Instructions[0].MatchLeave(out _, out var condition)) {
context.Step("TransformCatchWhen", entryPoint.Instructions[0]);
handler.Filter = condition;

Loading…
Cancel
Save