|
1 | 1 | // Author: Diffblue Ltd.
|
2 | 2 |
|
| 3 | +#include <util/arith_tools.h> |
3 | 4 | #include <util/c_types.h>
|
4 | 5 | #include <util/namespace.h>
|
5 |
| -#include <util/optional.h> |
6 | 6 | #include <util/std_expr.h>
|
7 | 7 | #include <util/symbol_table.h>
|
8 | 8 |
|
|
12 | 12 | #include <string>
|
13 | 13 | #include <utility>
|
14 | 14 |
|
| 15 | +TEST_CASE("find_object_base_expression", "[core][smt2_incremental]") |
| 16 | +{ |
| 17 | + const std::size_t pointer_bits = 64; |
| 18 | + SECTION("Address of symbol") |
| 19 | + { |
| 20 | + // Constructed address of expression should be equivalent to the following |
| 21 | + // C expression - `&base`. |
| 22 | + const typet base_type = unsignedbv_typet{8}; |
| 23 | + const symbol_exprt object_base{"base", base_type}; |
| 24 | + const address_of_exprt address_of{ |
| 25 | + object_base, pointer_typet{base_type, pointer_bits}}; |
| 26 | + INFO("Address of expression is: " + address_of.pretty(2, 0)); |
| 27 | + CHECK(find_object_base_expression(address_of) == object_base); |
| 28 | + } |
| 29 | + SECTION("Address of index") |
| 30 | + { |
| 31 | + // Constructed address of expression should be equivalent to the following |
| 32 | + // C expression - `&(base[index])`. |
| 33 | + const unsignedbv_typet element_type{8}; |
| 34 | + const signedbv_typet index_type{pointer_bits}; |
| 35 | + const array_typet base_type{element_type, from_integer(42, index_type)}; |
| 36 | + const symbol_exprt object_base{"base", base_type}; |
| 37 | + const symbol_exprt index{"index", index_type}; |
| 38 | + const pointer_typet pointer_type{element_type, pointer_bits}; |
| 39 | + const address_of_exprt address_of{ |
| 40 | + index_exprt{object_base, index}, pointer_type}; |
| 41 | + INFO("Address of expression is: " + address_of.pretty(2, 0)); |
| 42 | + CHECK(find_object_base_expression(address_of) == object_base); |
| 43 | + } |
| 44 | + SECTION("Address of struct member") |
| 45 | + { |
| 46 | + // Constructed address of expression should be equivalent to the following |
| 47 | + // C expression - `&(base.baz)`. |
| 48 | + const struct_tag_typet base_type{"structt"}; |
| 49 | + const symbol_exprt object_base{"base", base_type}; |
| 50 | + const unsignedbv_typet member_type{8}; |
| 51 | + const address_of_exprt address_of{ |
| 52 | + member_exprt{object_base, "baz", member_type}, |
| 53 | + pointer_typet{member_type, pointer_bits}}; |
| 54 | + INFO("Address of expression is: " + address_of.pretty(2, 0)); |
| 55 | + CHECK(find_object_base_expression(address_of) == object_base); |
| 56 | + } |
| 57 | + SECTION("Address of index of struct member") |
| 58 | + { |
| 59 | + // Constructed address of expression should be equivalent to the following |
| 60 | + // C expression - `&(base.baz[index])`. |
| 61 | + const struct_tag_typet base_type{"structt"}; |
| 62 | + const symbol_exprt object_base{"base", base_type}; |
| 63 | + |
| 64 | + const unsignedbv_typet element_type{8}; |
| 65 | + const signedbv_typet index_type{pointer_bits}; |
| 66 | + const array_typet member_type{element_type, from_integer(42, index_type)}; |
| 67 | + const symbol_exprt index{"index", index_type}; |
| 68 | + |
| 69 | + const address_of_exprt address_of{ |
| 70 | + index_exprt{member_exprt{object_base, "baz", member_type}, index}, |
| 71 | + pointer_typet{element_type, pointer_bits}}; |
| 72 | + INFO("Address of expression is: " + address_of.pretty(2, 0)); |
| 73 | + CHECK(find_object_base_expression(address_of) == object_base); |
| 74 | + } |
| 75 | + SECTION("Address of struct member at index") |
| 76 | + { |
| 77 | + // Constructed address of expression should be equivalent to the following |
| 78 | + // C expression - `&(base[index].qux)`. |
| 79 | + const struct_tag_typet element_type{"struct_elementt"}; |
| 80 | + const signedbv_typet index_type{pointer_bits}; |
| 81 | + const array_typet base_type{element_type, from_integer(42, index_type)}; |
| 82 | + const symbol_exprt object_base{"base", base_type}; |
| 83 | + const symbol_exprt index{"index", index_type}; |
| 84 | + const unsignedbv_typet member_type{8}; |
| 85 | + const address_of_exprt address_of{ |
| 86 | + member_exprt{index_exprt{object_base, index}, "qux", member_type}, |
| 87 | + pointer_typet{member_type, pointer_bits}}; |
| 88 | + INFO("Address of expression is: " + address_of.pretty(2, 0)); |
| 89 | + CHECK(find_object_base_expression(address_of) == object_base); |
| 90 | + } |
| 91 | +} |
| 92 | + |
15 | 93 | TEST_CASE("Tracking object base expressions", "[core][smt2_incremental]")
|
16 | 94 | {
|
17 | 95 | const typet base_type = pointer_typet{signedbv_typet{16}, 18};
|
|
0 commit comments