Skip to content

Commit 5361db1

Browse files
committed
array insert/replace helper functions
1 parent c9c80e3 commit 5361db1

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

src/analyses/variable-sensitivity/full_array_abstract_object.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct eval_index_resultt
2525
};
2626

2727
static eval_index_resultt eval_index(
28-
const exprt &index,
28+
const exprt &expr,
2929
const abstract_environmentt &env,
3030
const namespacet &ns);
3131

@@ -211,32 +211,25 @@ abstract_object_pointert full_array_abstract_objectt::read_element(
211211
{
212212
PRECONDITION(!is_bottom());
213213
auto index = eval_index(expr, env, ns);
214+
214215
if(index.is_good)
215-
{
216-
// Here we are assuming it is always in bounds
217-
auto const value = map.find(index.value);
218-
if(value.has_value())
219-
return value.value();
220-
return get_top_entry(env, ns);
221-
}
222-
else
223-
{
224-
// Although we don't know where we are reading from, and therefore
225-
// we should be returning a TOP value, we may still have useful
226-
// additional information in elements of the array - such as write
227-
// histories so we merge all the known array elements with TOP and return
228-
// that.
216+
return map_find_or_top(index.value, env, ns);
229217

230-
// Create a new TOP value of the appropriate element type
231-
abstract_object_pointert result = get_top_entry(env, ns);
218+
// Although we don't know where we are reading from, and therefore
219+
// we should be returning a TOP value, we may still have useful
220+
// additional information in elements of the array - such as write
221+
// histories so we merge all the known array elements with TOP and return
222+
// that.
232223

233-
// Merge each known element into the TOP value
234-
for(const auto &element : map.get_view())
235-
result =
236-
abstract_objectt::merge(result, element.second, widen_modet::no).object;
224+
// Create a new TOP value of the appropriate element type
225+
auto result = get_top_entry(env, ns);
237226

238-
return result;
239-
}
227+
// Merge each known element into the TOP value
228+
for(const auto &element : map.get_view())
229+
result =
230+
abstract_objectt::merge(result, element.second, widen_modet::no).object;
231+
232+
return result;
240233
}
241234

242235
abstract_object_pointert full_array_abstract_objectt::write_element(
@@ -277,22 +270,10 @@ abstract_object_pointert full_array_abstract_objectt::write_sub_element(
277270
{
278271
// We were able to evaluate the index to a value, which we
279272
// assume is in bounds...
280-
auto const old_value = map.find(index.value);
281-
282-
if(old_value.has_value())
283-
{
284-
result->map.replace(
285-
index.value,
286-
environment.write(old_value.value(), value, stack, ns, merging_write));
287-
}
288-
else
289-
{
290-
result->map.insert(
291-
index.value,
292-
environment.write(
293-
get_top_entry(environment, ns), value, stack, ns, merging_write));
294-
}
295-
273+
auto const existing_value = map_find_or_top(index.value, environment, ns);
274+
result->map_put(
275+
index.value,
276+
environment.write(existing_value, value, stack, ns, merging_write));
296277
result->set_not_top();
297278
DATA_INVARIANT(result->verify(), "Structural invariants maintained");
298279
return result;
@@ -354,14 +335,9 @@ abstract_object_pointert full_array_abstract_objectt::write_leaf_element(
354335
}
355336
else
356337
{
357-
auto old_value = result->map.find(index.value);
358-
359-
if(old_value.has_value())
360-
result->map.replace(index.value, value);
361-
else
362-
result->map.insert(index.value, value);
363-
338+
result->map_put(index.value, value);
364339
result->set_not_top();
340+
365341
DATA_INVARIANT(result->verify(), "Structural invariants maintained");
366342
return result;
367343
}
@@ -373,6 +349,27 @@ abstract_object_pointert full_array_abstract_objectt::write_leaf_element(
373349
environment, ns, std::stack<exprt>(), expr, value, merging_write);
374350
}
375351

352+
void full_array_abstract_objectt::map_put(
353+
mp_integer index_value,
354+
const abstract_object_pointert &value)
355+
{
356+
auto old_value = map.find(index_value);
357+
358+
if(old_value.has_value())
359+
map.replace(index_value, value);
360+
else
361+
map.insert(index_value, value);
362+
}
363+
364+
abstract_object_pointert full_array_abstract_objectt::map_find_or_top(
365+
mp_integer index_value,
366+
const abstract_environmentt &env,
367+
const namespacet &ns) const
368+
{
369+
auto value = map.find(index_value);
370+
return value.has_value() ? value.value() : get_top_entry(env, ns);
371+
}
372+
376373
abstract_object_pointert full_array_abstract_objectt::get_top_entry(
377374
const abstract_environmentt &env,
378375
const namespacet &ns) const

src/analyses/variable-sensitivity/full_array_abstract_object.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ class full_array_abstract_objectt : public abstract_aggregate_objectt<
203203

204204
shared_array_mapt map;
205205

206+
void map_put(mp_integer index, const abstract_object_pointert &value);
207+
abstract_object_pointert map_find_or_top(
208+
mp_integer index,
209+
const abstract_environmentt &env,
210+
const namespacet &ns) const;
211+
206212
/// Short hand method for creating a top element of the array
207213
///
208214
/// \param env: the abstract environment

0 commit comments

Comments
 (0)