Skip to content

Commit 59e7e76

Browse files
author
Remi Delmas
committed
Function contracts: improved support for conditional targets in assigns clause checking.
1 parent 2ffa79e commit 59e7e76

File tree

2 files changed

+12
-71
lines changed

2 files changed

+12
-71
lines changed

src/goto-instrument/contracts/assigns.cpp

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -71,86 +71,27 @@ static const slicet normalize_to_slice(const exprt &expr, const namespacet &ns)
7171
UNREACHABLE;
7272
}
7373

74-
/// Result type for guarded assigns target pattern matching
75-
using if_match_resultt = optionalt<std::pair<exprt, exprt>>;
76-
77-
/// \brief Matches an expression with ID_NULL or ID_constant "0".
78-
/// \param expr The candidate expression.
79-
/// \returns True iff the match is successful.
80-
bool match_NULL_or_zero(const exprt &expr)
81-
{
82-
const auto &clean_expr = skip_typecast(expr);
83-
// FIXME clean up once #6532 lands.
84-
return clean_expr.id() == ID_NULL ||
85-
(clean_expr.id() == ID_constant &&
86-
id2string(to_constant_expr(clean_expr).get_value()) == "0");
87-
}
88-
89-
/// \brief Matches an expression of the form `guard ? target : (NULL|0)`
90-
/// \param expr The candidate expression.
91-
/// \returns A pair `(guard,target)` iff the match is successful.
92-
if_match_resultt match_if(const exprt &expr)
93-
{
94-
const auto &clean_expr = skip_typecast(expr);
95-
if(clean_expr.id() == ID_if)
96-
{
97-
const auto &if_expr = to_if_expr(clean_expr);
98-
if(!match_NULL_or_zero(if_expr.false_case()))
99-
return {};
100-
101-
return {{if_expr.cond(), if_expr.true_case()}};
102-
}
103-
else
104-
return {};
105-
}
106-
107-
/// Pattern match the given expression for a guarded pattern
108-
/// and builds guarded_slice expression as follows:
74+
/// Normalises a assigns target expression to a guarded slice struct.
10975
///
11076
/// ```
11177
/// case expr of
112-
/// | guard ? target : NULL ->
113-
/// {guard,
78+
/// | guard ? target ->
79+
/// {guard
11480
/// target,
115-
/// address_of{target},
116-
/// size_of_expr{target}}
117-
/// | __CPROVER_POINTER_OBJECT(guard ? target : NULL) ->
118-
/// {guard,
119-
/// __CPROVER_POINTER_OBJECT(target),
120-
/// address_of{target-offset(target)},
121-
/// object_size{target}}
122-
/// | other ->
81+
/// normalize_to_slice(target)}
82+
/// | target ->
12383
/// {true,
124-
/// expr,
125-
/// address_of{expr},
126-
/// object_size{expr}}
84+
/// target,
85+
/// normalize_to_slice(target)}
12786
/// ```
12887
static const guarded_slicet
12988
normalize_to_guarded_slice(const exprt &expr, const namespacet &ns)
13089
{
131-
if(expr.id() == ID_pointer_object)
132-
{
133-
if_match_resultt match_opt = match_if(expr.operands().at(0));
134-
if(match_opt.has_value())
135-
{
136-
const auto &match = match_opt.value();
137-
const auto &new_expr = pointer_object(match.second);
138-
const auto slice = normalize_to_slice(new_expr, ns);
139-
return {match.first, new_expr, slice.first, slice.second};
140-
}
141-
else
142-
{
143-
const auto slice = normalize_to_slice(expr, ns);
144-
return {true_exprt{}, expr, slice.first, slice.second};
145-
}
146-
}
147-
148-
if_match_resultt match_opt = match_if(expr);
149-
if(match_opt.has_value())
90+
if(expr.id() == ID_conditional_assigns_target)
15091
{
151-
const auto &match = match_opt.value();
152-
const auto slice = normalize_to_slice(match.second, ns);
153-
return {match.first, match.second, slice.first, slice.second};
92+
const auto &new_expr = expr.operands().at(1);
93+
const auto slice = normalize_to_slice(new_expr, ns);
94+
return {expr.operands().at(0), new_expr, slice.first, slice.second};
15495
}
15596
else
15697
{

src/goto-instrument/contracts/assigns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct guarded_slicet
3030
{
3131
}
3232

33-
/// Guard expression (may contain side effects)
33+
/// Guard expression (may contain side_effect_exprt expressions)
3434
const exprt guard;
3535

3636
/// Target expression

0 commit comments

Comments
 (0)