Skip to content

Commit c048b5d

Browse files
committed
Helper for extracting function identifier
from a function call expression.
1 parent f1625d7 commit c048b5d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/goto-programs/remove_function_pointers.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ class remove_function_pointerst
174174
void remove_function_pointer_log(
175175
goto_programt::targett target,
176176
const functionst &functions) const;
177+
178+
/// Extract function name from \p called_functions
179+
/// \param: called_function: the function call expression
180+
/// \return function identifier
181+
irep_idt get_callee_id(const exprt &called_function) const;
177182
};
178183

179184
remove_function_pointerst::remove_function_pointerst(
@@ -723,3 +728,40 @@ void remove_function_pointerst::remove_function_pointer_log(
723728
mstream << messaget::eom;
724729
});
725730
}
731+
732+
irep_idt
733+
remove_function_pointerst::get_callee_id(const exprt &called_function) const
734+
{
735+
irep_idt callee_id;
736+
bool contains_code = false;
737+
auto type_contains_code = [&contains_code](const typet &t) {
738+
if(t.id() == ID_code)
739+
contains_code = true;
740+
};
741+
742+
called_function.visit_post(
743+
[&callee_id, &type_contains_code, &contains_code](const exprt &e) {
744+
if(e.id() == ID_symbol)
745+
{
746+
e.type().visit(type_contains_code);
747+
if(contains_code)
748+
{
749+
callee_id = to_symbol_expr(e).get_identifier();
750+
return;
751+
}
752+
}
753+
if(e.id() == ID_dereference)
754+
{
755+
const auto &pointer = to_dereference_expr(e).pointer();
756+
if(pointer.id() == ID_symbol)
757+
callee_id = to_symbol_expr(pointer).get_identifier();
758+
if(pointer.id() == ID_member)
759+
{
760+
pointer.type().visit(type_contains_code);
761+
if(contains_code)
762+
callee_id = to_member_expr(pointer).get_component_name();
763+
}
764+
}
765+
});
766+
return callee_id;
767+
}

src/util/type.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ class typet:public irept
139139

140140
validate_type(type, ns, vm);
141141
}
142+
143+
void visit(std::function<void(const typet &)> f) const
144+
{
145+
f(*this);
146+
for(const auto &sub_irep : get_sub())
147+
{
148+
const typet &subtype = static_cast<const typet &>(sub_irep);
149+
subtype.visit(f);
150+
}
151+
}
142152
};
143153

144154
/// Type with a single subtype.

0 commit comments

Comments
 (0)