Skip to content

Commit

Permalink
Fixing bug #10 related with filter exception handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardozoppi committed Jul 18, 2018
1 parent e7bcffd commit b19b53f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 36 deletions.
19 changes: 4 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*.suo
Backend/*.user
Backend/bin
Backend/obj
Console/*.user
Console/bin
Console/obj
Test/bin
Test/obj
*.suo
*.user
bin
obj
.vs
/Model/bin
/Model/obj
/CCILoader/bin
/CCILoader/obj
/packages
/CCIProvider/bin/Debug
/CCIProvider/obj/Debug
5 changes: 4 additions & 1 deletion Backend/Analyses/TypeInferenceAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public override void Visit(CreateArrayInstruction instruction)

public override void Visit(CatchInstruction instruction)
{
instruction.Result.Type = instruction.ExceptionType;
if (instruction.HasResult)
{
instruction.Result.Type = instruction.ExceptionType;
}
}

public override void Visit(CreateObjectInstruction instruction)
Expand Down
24 changes: 19 additions & 5 deletions Backend/Transformations/Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private void ProcessEndFinally(Bytecode.BasicInstruction op)

private void ProcessEndFilter(Bytecode.BasicInstruction op)
{
stack.Clear();
//stack.Clear();

ProcessEmptyOperation(op);
}
Expand Down Expand Up @@ -1067,6 +1067,14 @@ private void FillExceptionHandlersStart()
exceptionHandlersStart.Add(protectedBlock.Handler.Start, protectedBlock.Handler);
//exceptionHandlersEnd.Add(protectedBlock.End, protectedBlock);
//exceptionHandlersEnd.Add(protectedBlock.Handler.End, protectedBlock.Handler);

if (protectedBlock.Handler.Kind == ExceptionHandlerBlockKind.Filter)
{
var filterHandler = protectedBlock.Handler as FilterExceptionHandler;

exceptionHandlersStart.Add(filterHandler.Handler.Start, filterHandler);
//exceptionHandlersEnd.Add(filterHandler.Handler.End, filterHandler);
}
}
}

Expand Down Expand Up @@ -1098,15 +1106,21 @@ private void ProcessExceptionHandling(MethodBody body, IInstruction operation)

case ExceptionHandlerBlockKind.Filter:
// Push the exception into the stack
var filterException = stack.Push();
var filterBlock = block as FilterExceptionHandler;
instruction = new Tac.FilterInstruction(operation.Offset, filterException, filterBlock.ExceptionType);
var filterException = stack.Push();
instruction = new Tac.FilterInstruction(operation.Offset, filterException, filterBlock.Handler.ExceptionType);
break;

case ExceptionHandlerBlockKind.Catch:
// Push the exception into the stack
var catchException = stack.Push();
var catchBlock = block as CatchExceptionHandler;
IVariable catchException = null;

if (!catchBlock.HasAssociatedFilter)
{
// Push the exception into the stack
catchException = stack.Push();
}

instruction = new Tac.CatchInstruction(operation.Offset, catchException, catchBlock.ExceptionType);
break;

Expand Down
23 changes: 13 additions & 10 deletions CCIProvider/CodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,32 @@ private void ExtractExceptionInformation(IEnumerable<Cci.IOperationExceptionInfo
foreach (var cciExceptionInfo in cciExceptionInformation)
{
var tryHandler = new ProtectedBlock(cciExceptionInfo.TryStartOffset, cciExceptionInfo.TryEndOffset);
IExceptionHandler handler;

switch (cciExceptionInfo.HandlerKind)
{
case Cci.HandlerKind.Filter:
var filterExceptionType = typeExtractor.ExtractType(cciExceptionInfo.ExceptionType);
var filterHandler = new FilterExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset, filterExceptionType);
tryHandler.Handler = filterHandler;
var catchExceptionType = typeExtractor.ExtractType(cciExceptionInfo.ExceptionType);
var catchHandler = new CatchExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset, catchExceptionType);
handler = new FilterExceptionHandler(cciExceptionInfo.FilterDecisionStartOffset, cciExceptionInfo.HandlerStartOffset, catchHandler);
catchHandler.HasAssociatedFilter = true;
tryHandler.Handler = handler;
break;

case Cci.HandlerKind.Catch:
var catchExceptionType = typeExtractor.ExtractType(cciExceptionInfo.ExceptionType);
var catchHandler = new CatchExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset, catchExceptionType);
tryHandler.Handler = catchHandler;
catchExceptionType = typeExtractor.ExtractType(cciExceptionInfo.ExceptionType);
handler = new CatchExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset, catchExceptionType);
tryHandler.Handler = handler;
break;

case Cci.HandlerKind.Fault:
var faultHandler = new FaultExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset);
tryHandler.Handler = faultHandler;
handler = new FaultExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset);
tryHandler.Handler = handler;
break;

case Cci.HandlerKind.Finally:
var finallyHandler = new FinallyExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset);
tryHandler.Handler = finallyHandler;
handler = new FinallyExceptionHandler(cciExceptionInfo.HandlerStartOffset, cciExceptionInfo.HandlerEndOffset);
tryHandler.Handler = handler;
break;

default:
Expand Down
9 changes: 5 additions & 4 deletions Model/ThreeAddressCode/ExceptionHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ public class FilterExceptionHandler : IExceptionHandler
public ExceptionHandlerBlockKind Kind { get; private set; }
public string Start { get; set; }
public string End { get; set; }
public IType ExceptionType { get; set; }
public CatchExceptionHandler Handler { get; set; }

public FilterExceptionHandler(uint start, uint end, IType exceptionType)
public FilterExceptionHandler(uint start, uint end, CatchExceptionHandler handler)
{
this.Kind = ExceptionHandlerBlockKind.Filter;
this.Start = string.Format("L_{0:X4}", start);
this.End = string.Format("L_{0:X4}", end);
this.ExceptionType = exceptionType;
this.Handler = handler;
}

public override string ToString()
{
return string.Format("filter {0} handler {1} to {2}", this.ExceptionType, this.Start, this.End);
return string.Format("filter {0} to {1} for {2}", this.Start, this.End, this.Handler);
}
}

Expand All @@ -75,6 +75,7 @@ public class CatchExceptionHandler : IExceptionHandler
public string Start { get; set; }
public string End { get; set; }
public IType ExceptionType { get; set; }
public bool HasAssociatedFilter { get; set; }

public CatchExceptionHandler(uint start, uint end, IType exceptionType)
{
Expand Down
9 changes: 8 additions & 1 deletion Model/ThreeAddressCode/Instructions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,14 @@ public override IExpression ToExpression()

public override string ToString()
{
return this.ToString("catch {0} {1}", this.ExceptionType, this.Result);
var result = string.Empty;

if (this.HasResult)
{
result = string.Format(" {0}", this.Result);
}

return this.ToString("catch {0}{1}", this.ExceptionType, result);
}
}

Expand Down
13 changes: 13 additions & 0 deletions Test/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ public void ExampleNestedTryCatchFinally()
a = 7;
}

public void ExampleTryCatchWhen(int y)
{
int x = 5;
try
{
x = x / y;
}
catch (Exception ex) when (ex.InnerException == null)
{
x = ex.Data.Count;
}
}

public void ExampleIf()
{
var a = 0;
Expand Down

0 comments on commit b19b53f

Please sign in to comment.