Skip to content

Commit

Permalink
fix position info for statements
Browse files Browse the repository at this point in the history
When converting the instructions for a branch, there might not be a line number node after the branch target.
This would result in incorrect line numbers being used until the next line number node.

The fix is to simply store the line number for branches and restore the current line number when handling that branch.
  • Loading branch information
Timbals committed Nov 8, 2023
1 parent 84d94d2 commit 10740b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ private void addEdges(
BranchedInsnInfo edge = edges.get(branchingInsn, tgt);
if (edge == null) {
// [ms] check why this edge could be already there
edge = new BranchedInsnInfo(tgt, operandStack.getStack());
edge = new BranchedInsnInfo(tgt, operandStack.getStack(), currentLineNumber);
edge.addToPrevStack(stackss);
edges.put(branchingInsn, tgt, edge);
conversionWorklist.add(edge);
Expand Down Expand Up @@ -1349,20 +1349,23 @@ private void convert() {
Operand opr = new Operand(handlerNode, ref, this);
opr.stackLocal = local;

worklist.add(new BranchedInsnInfo(handlerNode, Collections.singletonList(opr)));
worklist.add(
new BranchedInsnInfo(handlerNode, Collections.singletonList(opr), currentLineNumber));

// Save the statements
inlineExceptionHandlers.put(handlerNode, as);
} else {
worklist.add(new BranchedInsnInfo(handlerNode, new ArrayList<>()));
worklist.add(new BranchedInsnInfo(handlerNode, new ArrayList<>(), currentLineNumber));
}
}
worklist.add(new BranchedInsnInfo(instructions.getFirst(), Collections.emptyList()));
worklist.add(
new BranchedInsnInfo(instructions.getFirst(), Collections.emptyList(), currentLineNumber));
Table<AbstractInsnNode, AbstractInsnNode, BranchedInsnInfo> edges = HashBasedTable.create(1, 1);

do {
BranchedInsnInfo edge = worklist.pollLast();
AbstractInsnNode insn = edge.getInsn();
currentLineNumber = edge.getLineNumber();
operandStack.setOperandStack(
new ArrayList<>(edge.getOperandStacks().get(edge.getOperandStacks().size() - 1)));
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ class BranchedInsnInfo {
@Nonnull private final LinkedList<Operand[]> prevStacks;
/* current stack at edge */
@Nullable private final List<List<Operand>> operandStacks = new ArrayList<>();
private final int lineNumber;

BranchedInsnInfo(@Nonnull AbstractInsnNode insn, @Nonnull List<Operand> operands) {
BranchedInsnInfo(
@Nonnull AbstractInsnNode insn, @Nonnull List<Operand> operands, int lineNumber) {
this.insn = insn;
this.prevStacks = new LinkedList<>();
this.operandStacks.add(operands);
this.lineNumber = lineNumber;
}

@Nonnull
Expand All @@ -62,4 +65,8 @@ public LinkedList<Operand[]> getPrevStacks() {
public void addToPrevStack(@Nonnull Operand[] stacksOperands) {
prevStacks.add(stacksOperands);
}

public int getLineNumber() {
return this.lineNumber;
}
}

0 comments on commit 10740b2

Please sign in to comment.