Skip to content

Commit 7429e79

Browse files
Merge pull request #6535 from remi-delmas-3000/function-pointer-call-site-add-decl-dead
Add missing DECL and DEAD instructions in function pointer call site labelling
2 parents 4ec7d9b + 6642254 commit 7429e79

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/goto-programs/goto_program.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,22 @@ class goto_programt
625625

626626
/// Insertion that preserves jumps to "target".
627627
/// The instruction is destroyed.
628+
///
629+
/// Turns:
630+
/// ```
631+
/// ...->[a]->...
632+
/// ^
633+
/// target
634+
/// ```
635+
///
636+
/// Into:
637+
/// ```
638+
/// ...->[i]->[a]->...
639+
/// ^
640+
/// target
641+
/// ```
642+
///
643+
/// So that jumps to `a` now jump to the newly inserted `i`.
628644
void insert_before_swap(targett target, instructiont &instruction)
629645
{
630646
insert_before_swap(target);

src/goto-programs/label_function_pointer_call_sites.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,32 @@ void label_function_pointer_call_sites(goto_modelt &goto_model)
5252
goto_model.symbol_table.lookup_ref(call_site_symbol_name)
5353
.symbol_expr();
5454

55-
// add assignment to the new function pointer variable, followed by a
56-
// call of the new variable
55+
// add a DECL instruction for the function pointer variable
56+
auto decl_instruction =
57+
goto_programt::make_decl(new_function_pointer, source_location);
58+
59+
goto_function.second.body.insert_before_swap(it, decl_instruction);
60+
++it;
61+
62+
// add assignment to the new variable
5763
auto assign_instruction = goto_programt::make_assignment(
5864
code_assignt{new_function_pointer,
5965
function_pointer_dereference.pointer()},
6066
source_location);
6167

6268
goto_function.second.body.insert_before_swap(it, assign_instruction);
63-
const auto next = std::next(it);
64-
to_code_function_call(next->code_nonconst()).function() =
65-
dereference_exprt{new_function_pointer};
66-
// we need to increment the iterator once more (in addition to the
67-
// increment already done by for_each_goto_function_if()). This is
68-
// because insert_before_swap() inserts a new instruction after the
69-
// instruction pointed to by it (and then swaps the contents with the
70-
// previous instruction). We need to increment the iterator as we also
71-
// need to skip over this newly inserted instruction.
72-
it++;
69+
++it;
70+
71+
// transform original call into a call to the new variable
72+
it->call_function() = dereference_exprt{new_function_pointer};
73+
++it;
74+
75+
// add a DEAD instruction for the new variable
76+
auto dead_instruction =
77+
goto_programt::make_dead(new_function_pointer, source_location);
78+
goto_function.second.body.insert_before_swap(it, dead_instruction);
79+
// the iterator now points to the DEAD instruction and will be
80+
// incremented by the outer loop
7381
});
7482
}
7583
}

0 commit comments

Comments
 (0)