Skip to content

Commit 8b53dba

Browse files
authored
Merge pull request #6950 from diffblue/index_exprt_check
index_exprt now requires a type with subtype
2 parents ec395cd + 07dedc7 commit 8b53dba

File tree

6 files changed

+32
-67
lines changed

6 files changed

+32
-67
lines changed

src/analyses/variable-sensitivity/write_stack.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ void write_stackt::construct_stack_to_pointer(
102102
if(!top_stack)
103103
{
104104
// check the symbol at the bottom of the stack
105-
std::shared_ptr<const write_stack_entryt> entry = *stack.cbegin();
105+
const auto access = stack.front()->get_access_expr();
106106
INVARIANT(
107-
entry->get_access_expr().id() == ID_symbol,
107+
!access.second && access.first.id() == ID_symbol,
108108
"The base should be an addressable location (i.e. symbol)");
109109

110-
if(entry->get_access_expr().type().id() != ID_array)
110+
if(access.first.type().id() != ID_array)
111111
{
112112
top_stack = true;
113113
}
@@ -176,27 +176,15 @@ exprt write_stackt::to_expression() const
176176
{
177177
// A top stack is useless and its expression should not be evaluated
178178
PRECONDITION(!is_top_value());
179-
exprt access_expr = nil_exprt();
180-
for(const std::shared_ptr<write_stack_entryt> &entry : stack)
179+
PRECONDITION(!stack.empty());
180+
exprt access_expr = stack.front()->get_access_expr().first;
181+
for(auto it = std::next(stack.begin()); it != stack.end(); ++it)
181182
{
182-
exprt new_expr = entry->get_access_expr();
183-
if(access_expr.id() == ID_nil)
184-
{
185-
access_expr = new_expr;
186-
}
183+
const auto access = (*it)->get_access_expr();
184+
if(access.second)
185+
access_expr = index_exprt{access_expr, access.first};
187186
else
188-
{
189-
if(new_expr.operands().size() == 0)
190-
{
191-
new_expr.operands().resize(1);
192-
}
193-
new_expr.operands()[0] = access_expr;
194-
195-
// If necessary, complete the type of the new access expression
196-
entry->adjust_access_type(new_expr);
197-
198-
access_expr = new_expr;
199-
}
187+
access_expr = access.first;
200188
}
201189
address_of_exprt top_expr(access_expr);
202190
return std::move(top_expr);
@@ -210,19 +198,22 @@ size_t write_stackt::depth() const
210198
exprt write_stackt::target_expression(size_t depth) const
211199
{
212200
PRECONDITION(!is_top_value());
213-
return stack[depth]->get_access_expr();
201+
return stack[depth]->get_access_expr().first;
214202
}
215203

216204
exprt write_stackt::offset_expression() const
217205
{
218206
PRECONDITION(!is_top_value());
219-
auto const &access = stack.back()->get_access_expr();
207+
auto const access = stack.back()->get_access_expr();
208+
209+
if(access.second)
210+
return access.first;
220211

221-
if(access.id() == ID_member || access.id() == ID_symbol)
222-
return access;
212+
if(access.first.id() == ID_member || access.first.id() == ID_symbol)
213+
return access.first;
223214

224-
if(access.id() == ID_index)
225-
return to_index_expr(access).index();
215+
if(access.first.id() == ID_index)
216+
return to_index_expr(access.first).index();
226217

227218
return from_integer(0, unsigned_long_int_type());
228219
}

src/analyses/variable-sensitivity/write_stack_entry.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@ simple_entryt::simple_entryt(exprt expr) : simple_entry(expr)
3939

4040
/// Get the expression part needed to read this stack entry. For simple
4141
/// expressions this is just the expression itself.
42-
/// \return The expression to read this part of the stack
43-
exprt simple_entryt::get_access_expr() const
44-
{
45-
return simple_entry;
46-
}
47-
48-
/// For a simple entry, no type adjustment is needed for the access expression
49-
void simple_entryt::adjust_access_type(exprt &expr) const
42+
/// \return The expression to read this part of the stack and false
43+
std::pair<exprt, bool> simple_entryt::get_access_expr() const
5044
{
45+
return {simple_entry, false};
5146
}
5247

5348
offset_entryt::offset_entryt(abstract_object_pointert offset_value)
@@ -61,26 +56,11 @@ offset_entryt::offset_entryt(abstract_object_pointert offset_value)
6156
}
6257

6358
/// Get the expression part needed to read this stack entry. For offset entries
64-
/// this is an index expression with the index() part the offset.
65-
/// It is important to note that the returned index_exprt does not have a type,
66-
/// so it will be necessary for the caller to update the type whenever the index
67-
/// expression is completed using `adjust_access_type` on the resulting exprt.
68-
/// \return The untyped expression to read this part of the stack
69-
exprt offset_entryt::get_access_expr() const
70-
{
71-
// This constructs a something that is basicallyt '(null)[offset])'
72-
// meaning that we don't know what the type is at this point, as the
73-
// array part will be filled in later.
74-
return index_exprt(nil_exprt(), offset->to_constant());
75-
}
76-
77-
/// For an offset entry, the type of the access expression can only be
78-
/// determined once the access expression has been completed with the next
79-
/// entry on the write stack.
80-
void offset_entryt::adjust_access_type(exprt &expr) const
59+
/// this is the offset for an index expression.
60+
/// \return The offset expression to read this part of the stack and true
61+
std::pair<exprt, bool> offset_entryt::get_access_expr() const
8162
{
82-
PRECONDITION(expr.id() == ID_index);
83-
expr.type() = to_index_expr(expr).array().type().subtype();
63+
return {offset->to_constant(), true};
8464
}
8565

8666
/// Try to combine a new stack element with the current top of the stack. This

src/analyses/variable-sensitivity/write_stack_entry.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class write_stack_entryt
2121
{
2222
public:
2323
virtual ~write_stack_entryt() = default;
24-
virtual exprt get_access_expr() const = 0;
25-
virtual void adjust_access_type(exprt &expr) const = 0;
24+
virtual std::pair<exprt, bool> get_access_expr() const = 0;
2625
virtual bool try_squash_in(
2726
std::shared_ptr<const write_stack_entryt> new_entry,
2827
const abstract_environmentt &enviroment,
@@ -33,8 +32,7 @@ class simple_entryt : public write_stack_entryt
3332
{
3433
public:
3534
explicit simple_entryt(exprt expr);
36-
exprt get_access_expr() const override;
37-
void adjust_access_type(exprt &expr) const override;
35+
std::pair<exprt, bool> get_access_expr() const override;
3836

3937
private:
4038
exprt simple_entry;
@@ -44,8 +42,7 @@ class offset_entryt : public write_stack_entryt
4442
{
4543
public:
4644
explicit offset_entryt(abstract_object_pointert offset_value);
47-
exprt get_access_expr() const override;
48-
void adjust_access_type(exprt &expr) const override;
45+
std::pair<exprt, bool> get_access_expr() const override;
4946
bool try_squash_in(
5047
std::shared_ptr<const write_stack_entryt> new_entry,
5148
const abstract_environmentt &enviroment,

src/cpp/cpp_constructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
100100
exprt constant = from_integer(i, c_index_type());
101101
constant.add_source_location()=source_location;
102102

103-
index_exprt index(object, constant);
103+
index_exprt index = index_exprt(object_tc, constant);
104104
index.add_source_location()=source_location;
105105

106106
if(!operands.empty())

src/util/std_expr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,9 +1417,7 @@ class index_exprt:public binary_exprt
14171417
_array,
14181418
ID_index,
14191419
std::move(_index),
1420-
_array.type().has_subtype()
1421-
? to_type_with_subtype(_array.type()).subtype()
1422-
: typet(ID_nil))
1420+
to_type_with_subtype(_array.type()).subtype())
14231421
{
14241422
}
14251423

unit/solvers/smt2_incremental/object_tracking.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ TEST_CASE("find_object_base_expression", "[core][smt2_incremental]")
2929
{member_exprt{object_base, "baz", unsignedbv_typet{8}}, pointer_type}},
3030
rowt{
3131
"Address of index of struct member",
32-
{index_exprt{
33-
member_exprt{object_base, "baz", unsignedbv_typet{8}}, index},
32+
{index_exprt{member_exprt{object_base, "baz", base_type}, index},
3433
pointer_type}},
3534
rowt{
3635
"Address of struct member at index",

0 commit comments

Comments
 (0)