|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Inline calls in program unit tests |
| 4 | +
|
| 5 | +Author: Remi Delmas |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <util/message.h> |
| 10 | + |
| 11 | +#include <goto-programs/goto_inline.h> |
| 12 | + |
| 13 | +#include <testing-utils/get_goto_model_from_c.h> |
| 14 | +#include <testing-utils/use_catch.h> |
| 15 | + |
| 16 | +TEST_CASE("Goto program inline", "[core][goto-programs][goto_program_inline]") |
| 17 | +{ |
| 18 | + const std::string code = R"( |
| 19 | + int x; |
| 20 | + int y; |
| 21 | + void f() { y = 0; } |
| 22 | + void g() { x = 0; f(); } |
| 23 | + void h() { g(); } |
| 24 | + void main() { h(); } |
| 25 | + )"; |
| 26 | + |
| 27 | + goto_modelt goto_model = get_goto_model_from_c(code); |
| 28 | + |
| 29 | + auto &function = goto_model.goto_functions.function_map.at("h"); |
| 30 | + |
| 31 | + null_message_handlert message_handler; |
| 32 | + goto_program_inline( |
| 33 | + goto_model.goto_functions, |
| 34 | + function.body, |
| 35 | + namespacet(goto_model.symbol_table), |
| 36 | + message_handler); |
| 37 | + |
| 38 | + static int assign_count = 0; |
| 39 | + for_each_instruction_if( |
| 40 | + function, |
| 41 | + [&](goto_programt::const_targett it) { |
| 42 | + return it->is_function_call() || it->is_assign(); |
| 43 | + }, |
| 44 | + [&](goto_programt::const_targett it) { |
| 45 | + if(it->is_function_call()) |
| 46 | + { |
| 47 | + // there are no calls left |
| 48 | + FAIL(); |
| 49 | + } |
| 50 | + |
| 51 | + if(it->is_assign()) |
| 52 | + { |
| 53 | + // the two assignments were inlined |
| 54 | + const auto &lhs = it->assign_lhs(); |
| 55 | + if(assign_count == 0) |
| 56 | + { |
| 57 | + REQUIRE(to_symbol_expr(lhs).get_identifier() == "x"); |
| 58 | + } |
| 59 | + else if(assign_count == 1) |
| 60 | + { |
| 61 | + REQUIRE(to_symbol_expr(lhs).get_identifier() == "y"); |
| 62 | + } |
| 63 | + assign_count++; |
| 64 | + } |
| 65 | + }); |
| 66 | + REQUIRE(assign_count == 2); |
| 67 | +} |
0 commit comments