|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Function Call Graph Helpers |
| 4 | +
|
| 5 | +Author: Chris Smowton, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Function Call Graph Helpers |
| 11 | + |
| 12 | +#include "call_graph_helpers.h" |
| 13 | + |
| 14 | +/// Get either callers or callees of a given function |
| 15 | +/// \param graph: call graph |
| 16 | +/// \param function: function to query |
| 17 | +/// \param forwards: if true, get callees; otherwise get callers. |
| 18 | +static std::set<irep_idt> get_neighbours( |
| 19 | + const call_grapht::directed_grapht &graph, |
| 20 | + const irep_idt &function, |
| 21 | + bool forwards) |
| 22 | +{ |
| 23 | + std::set<irep_idt> result; |
| 24 | + const auto &fnode = graph[*(graph.get_node_index(function))]; |
| 25 | + const auto &neighbours = forwards ? fnode.out : fnode.in; |
| 26 | + for(const auto &succ_edge : neighbours) |
| 27 | + result.insert(graph[succ_edge.first].function); |
| 28 | + return result; |
| 29 | +} |
| 30 | + |
| 31 | +std::set<irep_idt> get_callees( |
| 32 | + const call_grapht::directed_grapht &graph, const irep_idt &function) |
| 33 | +{ |
| 34 | + return get_neighbours(graph, function, true); |
| 35 | +} |
| 36 | + |
| 37 | +std::set<irep_idt> get_callers( |
| 38 | + const call_grapht::directed_grapht &graph, const irep_idt &function) |
| 39 | +{ |
| 40 | + return get_neighbours(graph, function, false); |
| 41 | +} |
| 42 | + |
| 43 | +/// Get either reachable functions or functions that can reach a given function. |
| 44 | +/// In both cases the query function itself is included. |
| 45 | +/// \param graph: call graph |
| 46 | +/// \param function: function to query |
| 47 | +/// \param forwards: if true, get reachable functions; otherwise get functions |
| 48 | +/// that can reach the given function. |
| 49 | +static std::set<irep_idt> get_connected_functions( |
| 50 | + const call_grapht::directed_grapht &graph, |
| 51 | + const irep_idt &function, |
| 52 | + bool forwards) |
| 53 | +{ |
| 54 | + std::vector<call_grapht::directed_grapht::node_indext> connected_nodes = |
| 55 | + graph.get_reachable(*(graph.get_node_index(function)), forwards); |
| 56 | + std::set<irep_idt> result; |
| 57 | + for(const auto i : connected_nodes) |
| 58 | + result.insert(graph[i].function); |
| 59 | + return result; |
| 60 | +} |
| 61 | + |
| 62 | +std::set<irep_idt> get_reachable_functions( |
| 63 | + const call_grapht::directed_grapht &graph, const irep_idt &function) |
| 64 | +{ |
| 65 | + return get_connected_functions(graph, function, true); |
| 66 | +} |
| 67 | + |
| 68 | +std::set<irep_idt> get_reaching_functions( |
| 69 | + const call_grapht::directed_grapht &graph, const irep_idt &function) |
| 70 | +{ |
| 71 | + return get_connected_functions(graph, function, false); |
| 72 | +} |
0 commit comments