Skip to content

Commit 96f2d9e

Browse files
NathanJPhillipssmowton
authored andcommitted
Added a PRECONDITION assert/invariant
This better matches the existing behaviour
1 parent b8ab624 commit 96f2d9e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/util/expr_cast.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ T expr_dynamic_cast(TExpr *base)
8888
/// \param base Reference to a generic \ref exprt
8989
/// \return Reference to object of type \a T
9090
/// \throw std::bad_cast If \a base is not an instance of \a T
91+
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
92+
/// abort rather than throw if \a base is not an instance of \a T
9193
template<typename T>
9294
T expr_dynamic_cast(const exprt &base)
9395
{
@@ -102,6 +104,8 @@ T expr_dynamic_cast(const exprt &base)
102104
/// \param base Reference to a generic \ref exprt
103105
/// \return Reference to object of type \a T
104106
/// \throw std::bad_cast If \a base is not an instance of \a T
107+
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
108+
/// abort rather than throw if \a base is not an instance of \a T
105109
template<typename T>
106110
T expr_dynamic_cast(exprt &base)
107111
{
@@ -118,6 +122,8 @@ T expr_dynamic_cast(exprt &base)
118122
/// \param base Reference to a generic \ref exprt
119123
/// \return Reference to object of type \a T
120124
/// \throw std::bad_cast If \a base is not an instance of \a TUnderlying
125+
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
126+
/// abort rather than throw if \a base is not an instance of \a TUnderlying
121127
template<typename T, typename TUnderlying, typename TExpr>
122128
T expr_dynamic_cast(TExpr &base)
123129
{
@@ -130,6 +136,7 @@ T expr_dynamic_cast(TExpr &base)
130136
static_assert(
131137
std::is_base_of<exprt, TUnderlying>::value,
132138
"The template argument T must be derived from exprt.");
139+
PRECONDITION(can_cast_expr<TUnderlying>(base));
133140
if(!can_cast_expr<TUnderlying>(base))
134141
throw std::bad_cast();
135142
T value=static_cast<T>(base);

unit/util/expr_cast/expr_cast.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ SCENARIO("expr_dynamic_cast",
5656

5757
THEN("Casting from exprt reference to transt reference should throw")
5858
{
59-
REQUIRE_THROWS_AS(
60-
expr_dynamic_cast<const transt &>(expr_ref),
61-
std::bad_cast);
59+
// This no longer throws exceptions when our custom asserts are set to
60+
// abort the program
61+
// REQUIRE_THROWS_AS(
62+
// expr_dynamic_cast<const transt &>(expr_ref),
63+
// std::bad_cast);
6264
}
6365
}
6466
GIVEN("A exprt reference to a symbolt")
@@ -73,7 +75,11 @@ SCENARIO("expr_dynamic_cast",
7375

7476
THEN("Casting from exprt reference to transt reference should throw")
7577
{
76-
REQUIRE_THROWS_AS(expr_dynamic_cast<transt &>(expr_ref), std::bad_cast);
78+
// This no longer throws exceptions when our custom asserts are set to
79+
// abort the program
80+
// REQUIRE_THROWS_AS(
81+
// expr_dynamic_cast<transt &>(expr_ref),
82+
// std::bad_cast);
7783
}
7884

7985
THEN(

0 commit comments

Comments
 (0)