Skip to content

Commit 574b5b1

Browse files
committed
abstract_environmentt::to_predicate
1 parent c90017c commit 574b5b1

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

src/analyses/variable-sensitivity/abstract_environment.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,29 @@ void abstract_environmentt::output(
419419
out << "}\n";
420420
}
421421

422+
exprt abstract_environmentt::to_predicate() const
423+
{
424+
if(is_bottom())
425+
return false_exprt();
426+
if(is_top())
427+
return true_exprt();
428+
429+
auto predicates = std::vector<exprt> { };
430+
for(const auto &entry : map.get_view())
431+
{
432+
auto sym = entry.first;
433+
auto val = entry.second;
434+
435+
auto pred = val->to_predicate(symbol_exprt(sym, val->type()));
436+
437+
predicates.push_back(pred);
438+
}
439+
440+
if(predicates.size() == 1)
441+
return predicates.front();
442+
return and_exprt(predicates);
443+
}
444+
422445
bool abstract_environmentt::verify() const
423446
{
424447
decltype(map)::viewt view;

src/analyses/variable-sensitivity/abstract_environment.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ class abstract_environmentt
224224
void output(std::ostream &out, const class ai_baset &ai, const namespacet &ns)
225225
const;
226226

227+
/// Gives a boolean condition that is true for all values represented by the
228+
/// environment.
229+
///
230+
/// \return An exprt describing the environment
231+
exprt to_predicate() const;
232+
227233
/// Check the structural invariants are maintained.
228234
/// In this case this is checking there aren't any null pointer mapped values
229235
bool verify() const;

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SRC += analyses/ai/ai.cpp \
1414
analyses/does_remove_const/does_expr_lose_const.cpp \
1515
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
17+
analyses/variable-sensitivity/abstract_environment/to_predicate.cpp \
1718
analyses/variable-sensitivity/abstract_object/merge.cpp \
1819
analyses/variable-sensitivity/abstract_object/index_range.cpp \
1920
analyses/variable-sensitivity/constant_abstract_value/meet.cpp \
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
analyses
2+
testing-utils
3+
util
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*******************************************************************\
2+
3+
Module: Tests for abstract_environmentt::to_predicate
4+
5+
Author: Jez Higgins
6+
7+
\*******************************************************************/
8+
9+
#include <analyses/variable-sensitivity/variable_sensitivity_object_factory.h>
10+
#include <analyses/variable-sensitivity/variable_sensitivity_test_helpers.h>
11+
#include <testing-utils/use_catch.h>
12+
#include <util/arith_tools.h>
13+
#include <util/bitvector_types.h>
14+
#include <util/mathematical_types.h>
15+
16+
SCENARIO(
17+
"abstract_environment to predicate",
18+
"[core][analyses][variable-sensitivity][abstract_environment][to_predicate]")
19+
{
20+
auto config = vsd_configt::constant_domain();
21+
config.context_tracking.data_dependency_context = false;
22+
config.context_tracking.last_write_context = false;
23+
auto object_factory =
24+
variable_sensitivity_object_factoryt::configured_with(config);
25+
symbol_tablet symbol_table;
26+
namespacet ns(symbol_table);
27+
28+
GIVEN("an abstract environment")
29+
{
30+
WHEN("it is TOP")
31+
{
32+
auto env = abstract_environmentt{object_factory};
33+
env.make_top();
34+
THEN_PREDICATE(env, "TRUE");
35+
}
36+
WHEN("it is BOTTOM")
37+
{
38+
auto env = abstract_environmentt{object_factory};
39+
env.make_bottom();
40+
THEN_PREDICATE(env, "FALSE");
41+
}
42+
WHEN("contains x = 2")
43+
{
44+
auto env = abstract_environmentt{object_factory};
45+
env.make_top();
46+
47+
auto type = signedbv_typet(32);
48+
auto val2 = make_constant(from_integer(2, type), env, ns);
49+
auto x_name = symbol_exprt(dstringt("x"), type);
50+
51+
env.assign(x_name, val2, ns);
52+
53+
THEN_PREDICATE(env, "x == 2");
54+
}
55+
WHEN("contains x = 2, y = 3")
56+
{
57+
auto env = abstract_environmentt{object_factory};
58+
env.make_top();
59+
60+
auto type = signedbv_typet(32);
61+
auto val2 = make_constant(from_integer(2, type), env, ns);
62+
auto x_name = symbol_exprt(dstringt("x"), type);
63+
64+
auto val3 = make_constant(from_integer(3, type), env, ns);
65+
auto y_name = symbol_exprt(dstringt("y"), type);
66+
67+
env.assign(x_name, val2, ns);
68+
env.assign(y_name, val3, ns);
69+
70+
THEN_PREDICATE(env, "y == 3 && x == 2");
71+
}
72+
}
73+
}

unit/analyses/variable-sensitivity/variable_sensitivity_test_helpers.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,13 @@ void THEN_PREDICATE(const abstract_object_pointert &obj, const std::string &out)
538538
REQUIRE(repr == out);
539539
}
540540
}
541+
542+
void THEN_PREDICATE(const abstract_environmentt &env, const std::string &out)
543+
{
544+
auto pred = env.to_predicate();
545+
THEN("predicate is " + out)
546+
{
547+
auto repr = expr_to_str(pred);
548+
REQUIRE(repr == out);
549+
}
550+
}

unit/analyses/variable-sensitivity/variable_sensitivity_test_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,6 @@ std::string exprs_to_str(const std::vector<exprt> &values);
255255
void THEN_PREDICATE(
256256
const abstract_object_pointert &obj,
257257
const std::string &out);
258+
void THEN_PREDICATE(const abstract_environmentt &env, const std::string &out);
258259

259260
#endif

0 commit comments

Comments
 (0)