Skip to content

Commit ca5c304

Browse files
brksipma
authored andcommitted
Add more spans for control flow stmts
1 parent de2dc92 commit ca5c304

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

chb/app/Cfg.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,14 @@ def do_branch(src: str, tgt: str, ctx: ControlFlowContext) -> List[AST.ASTStmt]:
522522
return []
523523

524524
if tgt == ctx.continue_to:
525-
return [astree.mk_continue_stmt()]
525+
return [with_last_insn_span(src, astree.mk_continue_stmt())]
526526

527527
if tgt == ctx.break_to:
528-
return [astree.mk_break_stmt()]
528+
return [with_last_insn_span(src, astree.mk_break_stmt())]
529529

530530
gotolabels.add(labelize(tgt))
531-
return [astree.mk_goto_stmt(labelize(tgt), tgt)]
531+
s = astree.mk_goto_stmt(labelize(tgt), tgt)
532+
return [with_last_insn_span(src, s)]
532533

533534
def is_loop_header(x: str) -> bool:
534535
return any(is_backward(pred, x) for pred in self.flowgraph.pre(x))
@@ -540,6 +541,15 @@ def is_backward(src: str, tgt: str) -> bool:
540541
# Could compare rpo numbers instead but this seems clearer.
541542
return self.flowgraph._edge_flavors[(src, tgt)] == "back"
542543

544+
def with_last_insn_span(x: str, s: AST.ASTStmt) -> AST.ASTStmt:
545+
"""Adds span information from the last insn in basic block `x`
546+
to the stmt `s`. Returns `s`."""
547+
astblock = astfn.astblock(x)
548+
astlastinstr = astblock.last_instruction
549+
astree.add_instruction_span(s.locationid,
550+
astlastinstr.iaddr, astlastinstr.bytestring)
551+
return s
552+
543553
def labeled_if_needed(x: str) -> List[AST.ASTStmt]:
544554
xstmts = blockstmts[x]
545555
xlabel = labelize(x)
@@ -622,6 +632,10 @@ def switch_case_stmts(succ, mb_nextsucc):
622632
switchstmt = cast(AST.ASTSwitchStmt,
623633
astree.mk_switch_stmt(switchcondition, switchbody, mergeaddr))
624634
return (xstmts + [switchstmt])
635+
# We don't use with_last_insn_span(x, switchstmt) because when we have
636+
# a switch lifted from a conditional jump (for explicit cases) plus an
637+
# unconditional jump (for the default case), the unconditional jump
638+
# is the last insn but we want both spans.
625639

626640
assert nsuccs == 2
627641
astblock = astfn.astblock(x)

0 commit comments

Comments
 (0)