Skip to content

Commit d7b9787

Browse files
authored
Merge pull request #5975 from diffblue/rw_ok_expr
Introduce expression classes for r_ok and w_ok
2 parents 4e2e372 + d38316e commit d7b9787

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/analyses/goto_check.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ void goto_checkt::pointer_primitive_check(
12611261
return;
12621262

12631263
const exprt pointer = (expr.id() == ID_r_ok || expr.id() == ID_w_ok)
1264-
? to_binary_expr(expr).lhs()
1264+
? to_r_or_w_ok_expr(expr).pointer()
12651265
: to_unary_expr(expr).op();
12661266

12671267
CHECK_RETURN(pointer.type().id() == ID_pointer);
@@ -1822,7 +1822,7 @@ optionalt<exprt> goto_checkt::rw_ok_check(exprt expr)
18221822
expr.operands().size() == 2, "r/w_ok must have two operands");
18231823

18241824
const auto conditions = get_pointer_dereferenceable_conditions(
1825-
to_binary_expr(expr).op0(), to_binary_expr(expr).op1());
1825+
to_r_or_w_ok_expr(expr).pointer(), to_r_or_w_ok_expr(expr).size());
18261826

18271827
exprt::operandst conjuncts;
18281828

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,8 +2550,7 @@ exprt c_typecheck_baset::do_special_functions(
25502550

25512551
irep_idt id = identifier == CPROVER_PREFIX "r_ok" ? ID_r_ok : ID_w_ok;
25522552

2553-
binary_predicate_exprt ok_expr(
2554-
expr.arguments()[0], id, expr.arguments()[1]);
2553+
r_or_w_ok_exprt ok_expr(id, expr.arguments()[0], expr.arguments()[1]);
25552554
ok_expr.add_source_location() = source_location;
25562555

25572556
return std::move(ok_expr);

src/util/pointer_expr.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,69 @@ class null_pointer_exprt : public constant_exprt
462462
}
463463
};
464464

465+
/// \brief A base class for a predicate that indicates that an
466+
/// address range is ok to read or write
467+
class r_or_w_ok_exprt : public binary_predicate_exprt
468+
{
469+
public:
470+
explicit r_or_w_ok_exprt(irep_idt id, exprt pointer, exprt size)
471+
: binary_predicate_exprt(std::move(pointer), id, std::move(size))
472+
{
473+
}
474+
475+
const exprt &pointer() const
476+
{
477+
return op0();
478+
}
479+
480+
const exprt &size() const
481+
{
482+
return op1();
483+
}
484+
};
485+
486+
inline const r_or_w_ok_exprt &to_r_or_w_ok_expr(const exprt &expr)
487+
{
488+
PRECONDITION(expr.id() == ID_r_ok || expr.id() == ID_w_ok);
489+
auto &ret = static_cast<const r_or_w_ok_exprt &>(expr);
490+
validate_expr(ret);
491+
return ret;
492+
}
493+
494+
/// \brief A predicate that indicates that an address range is ok to read
495+
class r_ok_exprt : public r_or_w_ok_exprt
496+
{
497+
public:
498+
explicit r_ok_exprt(exprt pointer, exprt size)
499+
: r_or_w_ok_exprt(ID_r_ok, std::move(pointer), std::move(size))
500+
{
501+
}
502+
};
503+
504+
inline const r_ok_exprt &to_r_ok_expr(const exprt &expr)
505+
{
506+
PRECONDITION(expr.id() == ID_r_ok);
507+
const r_ok_exprt &ret = static_cast<const r_ok_exprt &>(expr);
508+
validate_expr(ret);
509+
return ret;
510+
}
511+
512+
/// \brief A predicate that indicates that an address range is ok to write
513+
class w_ok_exprt : public r_or_w_ok_exprt
514+
{
515+
public:
516+
explicit w_ok_exprt(exprt pointer, exprt size)
517+
: r_or_w_ok_exprt(ID_w_ok, std::move(pointer), std::move(size))
518+
{
519+
}
520+
};
521+
522+
inline const w_ok_exprt &to_w_ok_expr(const exprt &expr)
523+
{
524+
PRECONDITION(expr.id() == ID_w_ok);
525+
const w_ok_exprt &ret = static_cast<const w_ok_exprt &>(expr);
526+
validate_expr(ret);
527+
return ret;
528+
}
529+
465530
#endif // CPROVER_UTIL_POINTER_EXPR_H

0 commit comments

Comments
 (0)