|
| 1 | + |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +#include <catch.hpp> |
| 5 | + |
| 6 | +#include <analyses/call_graph.h> |
| 7 | + |
| 8 | +#include <util/symbol_table.h> |
| 9 | +#include <util/std_code.h> |
| 10 | + |
| 11 | +#include <goto-programs/goto_convert_functions.h> |
| 12 | + |
| 13 | +static symbolt create_void_function_symbol( |
| 14 | + const irep_idt &name, |
| 15 | + const codet &code) |
| 16 | +{ |
| 17 | + code_typet void_function_type; |
| 18 | + symbolt function; |
| 19 | + function.name=name; |
| 20 | + function.type=void_function_type; |
| 21 | + function.mode=ID_java; |
| 22 | + function.value=code; |
| 23 | + return function; |
| 24 | +} |
| 25 | + |
| 26 | +static bool multimap_key_matches( |
| 27 | + const std::multimap<irep_idt, irep_idt> &map, |
| 28 | + const irep_idt &key, |
| 29 | + const std::set<irep_idt> &values) |
| 30 | +{ |
| 31 | + auto matching_values=map.equal_range(key); |
| 32 | + std::set<irep_idt> matching_set; |
| 33 | + for(auto it=matching_values.first; it!=matching_values.second; ++it) |
| 34 | + matching_set.insert(it->second); |
| 35 | + return matching_set==values; |
| 36 | +} |
| 37 | + |
| 38 | +SCENARIO("call_graph", |
| 39 | + "[core][util][call_graph]") |
| 40 | +{ |
| 41 | + |
| 42 | + GIVEN("Some cyclic function calls") |
| 43 | + { |
| 44 | + |
| 45 | + // Create code like: |
| 46 | + // void A() |
| 47 | + // { |
| 48 | + // A(); |
| 49 | + // B(); |
| 50 | + // } |
| 51 | + // void B() |
| 52 | + // { |
| 53 | + // C(); |
| 54 | + // D(); |
| 55 | + // } |
| 56 | + // void C() { } |
| 57 | + // void D() { } |
| 58 | + |
| 59 | + symbol_tablet symbol_table; |
| 60 | + code_typet void_function_type; |
| 61 | + |
| 62 | + { |
| 63 | + code_blockt calls; |
| 64 | + code_function_callt call1; |
| 65 | + call1.function()=symbol_exprt("A", void_function_type); |
| 66 | + code_function_callt call2; |
| 67 | + call2.function()=symbol_exprt("B", void_function_type); |
| 68 | + calls.move_to_operands(call1); |
| 69 | + calls.move_to_operands(call2); |
| 70 | + |
| 71 | + symbol_table.add(create_void_function_symbol("A", calls)); |
| 72 | + } |
| 73 | + |
| 74 | + { |
| 75 | + code_blockt calls; |
| 76 | + code_function_callt call1; |
| 77 | + call1.function()=symbol_exprt("C", void_function_type); |
| 78 | + code_function_callt call2; |
| 79 | + call2.function()=symbol_exprt("D", void_function_type); |
| 80 | + calls.move_to_operands(call1); |
| 81 | + calls.move_to_operands(call2); |
| 82 | + |
| 83 | + symbol_table.add(create_void_function_symbol("B", calls)); |
| 84 | + } |
| 85 | + |
| 86 | + symbol_table.add(create_void_function_symbol("C", code_skipt())); |
| 87 | + symbol_table.add(create_void_function_symbol("D", code_skipt())); |
| 88 | + |
| 89 | + goto_functionst goto_functions; |
| 90 | + stream_message_handlert msg(std::cout); |
| 91 | + goto_convert(symbol_table, goto_functions, msg); |
| 92 | + |
| 93 | + call_grapht call_graph_from_goto_functions(goto_functions); |
| 94 | + |
| 95 | + WHEN("A call graph is constructed from the GOTO functions") |
| 96 | + { |
| 97 | + THEN("We expect A -> { A, B }, B -> { C, D }") |
| 98 | + { |
| 99 | + const auto &check_graph=call_graph_from_goto_functions.graph; |
| 100 | + REQUIRE(check_graph.size()==4); |
| 101 | + REQUIRE(multimap_key_matches(check_graph, "A", {"A", "B"})); |
| 102 | + REQUIRE(multimap_key_matches(check_graph, "B", {"C", "D"})); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + WHEN("The call graph is inverted") |
| 107 | + { |
| 108 | + call_grapht inverse_call_graph_from_goto_functions= |
| 109 | + call_graph_from_goto_functions.get_inverted(); |
| 110 | + THEN("We expect A -> { A }, B -> { A }, C -> { B }, D -> { B }") |
| 111 | + { |
| 112 | + const auto &check_graph=inverse_call_graph_from_goto_functions.graph; |
| 113 | + REQUIRE(check_graph.size()==4); |
| 114 | + REQUIRE(multimap_key_matches(check_graph, "A", {"A"})); |
| 115 | + REQUIRE(multimap_key_matches(check_graph, "B", {"A"})); |
| 116 | + REQUIRE(multimap_key_matches(check_graph, "C", {"B"})); |
| 117 | + REQUIRE(multimap_key_matches(check_graph, "D", {"B"})); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments