Skip to content

Commit 3f7f5ae

Browse files
authored
Merge pull request #6724 from diffblue/more_formatters
add more formatters
2 parents 5d89f25 + 9053c5f commit 3f7f5ae

File tree

2 files changed

+103
-11
lines changed

2 files changed

+103
-11
lines changed

src/util/format_expr.cpp

+74-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include "bitvector_expr.h"
1616
#include "byte_operators.h"
1717
#include "expr_util.h"
18+
#include "floatbv_expr.h"
1819
#include "format_type.h"
1920
#include "ieee_float.h"
2021
#include "mathematical_expr.h"
@@ -158,6 +159,14 @@ static std::ostream &format_rec(std::ostream &os, const unary_exprt &src)
158159
return os << format(src.op());
159160
}
160161

162+
/// This formats a ternary expression
163+
static std::ostream &format_rec(std::ostream &os, const ternary_exprt &src)
164+
{
165+
os << src.id() << '(' << format(src.op0()) << ", " << format(src.op1())
166+
<< ", " << format(src.op2()) << ')';
167+
return os;
168+
}
169+
161170
/// This formats a constant
162171
static std::ostream &format_rec(std::ostream &os, const constant_exprt &src)
163172
{
@@ -186,7 +195,9 @@ static std::ostream &format_rec(std::ostream &os, const constant_exprt &src)
186195
{
187196
if(is_null_pointer(src))
188197
return os << ID_NULL;
189-
else if(has_prefix(id2string(src.get_value()), "INVALID-"))
198+
else if(
199+
src.get_value() == "INVALID" ||
200+
has_prefix(id2string(src.get_value()), "INVALID-"))
190201
{
191202
return os << "INVALID-POINTER";
192203
}
@@ -276,19 +287,30 @@ void format_expr_configt::setup()
276287
expr_map[ID_or] = multi_ary_expr;
277288
expr_map[ID_xor] = multi_ary_expr;
278289

279-
auto binary_expr = [](std::ostream &os, const exprt &expr) -> std::ostream & {
290+
auto binary_infix_expr =
291+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
280292
return format_rec(os, to_binary_expr(expr));
281293
};
282294

283-
expr_map[ID_lt] = binary_expr;
284-
expr_map[ID_gt] = binary_expr;
285-
expr_map[ID_ge] = binary_expr;
286-
expr_map[ID_le] = binary_expr;
287-
expr_map[ID_div] = binary_expr;
288-
expr_map[ID_minus] = binary_expr;
289-
expr_map[ID_implies] = binary_expr;
290-
expr_map[ID_equal] = binary_expr;
291-
expr_map[ID_notequal] = binary_expr;
295+
expr_map[ID_lt] = binary_infix_expr;
296+
expr_map[ID_gt] = binary_infix_expr;
297+
expr_map[ID_ge] = binary_infix_expr;
298+
expr_map[ID_le] = binary_infix_expr;
299+
expr_map[ID_div] = binary_infix_expr;
300+
expr_map[ID_minus] = binary_infix_expr;
301+
expr_map[ID_implies] = binary_infix_expr;
302+
expr_map[ID_equal] = binary_infix_expr;
303+
expr_map[ID_notequal] = binary_infix_expr;
304+
305+
auto binary_prefix_expr =
306+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
307+
os << expr.id() << '(' << format(to_binary_expr(expr).op0()) << ", "
308+
<< format(to_binary_expr(expr).op1()) << ')';
309+
return os;
310+
};
311+
312+
expr_map[ID_ieee_float_equal] = binary_prefix_expr;
313+
expr_map[ID_ieee_float_notequal] = binary_prefix_expr;
292314

293315
auto unary_expr = [](std::ostream &os, const exprt &expr) -> std::ostream & {
294316
return format_rec(os, to_unary_expr(expr));
@@ -297,11 +319,38 @@ void format_expr_configt::setup()
297319
expr_map[ID_not] = unary_expr;
298320
expr_map[ID_unary_minus] = unary_expr;
299321

322+
auto unary_with_parentheses_expr =
323+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
324+
return os << expr.id() << '(' << format(to_unary_expr(expr).op()) << ')';
325+
};
326+
327+
expr_map[ID_isnan] = unary_with_parentheses_expr;
328+
expr_map[ID_isinf] = unary_with_parentheses_expr;
329+
expr_map[ID_isfinite] = unary_with_parentheses_expr;
330+
expr_map[ID_isnormal] = unary_with_parentheses_expr;
331+
332+
auto ternary_expr =
333+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
334+
return format_rec(os, to_ternary_expr(expr));
335+
};
336+
337+
expr_map[ID_floatbv_plus] = ternary_expr;
338+
expr_map[ID_floatbv_minus] = ternary_expr;
339+
expr_map[ID_floatbv_mult] = ternary_expr;
340+
expr_map[ID_floatbv_div] = ternary_expr;
341+
expr_map[ID_floatbv_mod] = ternary_expr;
342+
300343
expr_map[ID_constant] =
301344
[](std::ostream &os, const exprt &expr) -> std::ostream & {
302345
return format_rec(os, to_constant_expr(expr));
303346
};
304347

348+
expr_map[ID_address_of] =
349+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
350+
const auto &address_of = to_address_of_expr(expr);
351+
return os << "address_of(" << format(address_of.object()) << ')';
352+
};
353+
305354
expr_map[ID_annotated_pointer_constant] =
306355
[](std::ostream &os, const exprt &expr) -> std::ostream & {
307356
const auto &annotated_pointer = to_annotated_pointer_constant_expr(expr);
@@ -314,6 +363,14 @@ void format_expr_configt::setup()
314363
<< format(expr.type()) << ')';
315364
};
316365

366+
expr_map[ID_floatbv_typecast] =
367+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
368+
const auto &floatbv_typecast_expr = to_floatbv_typecast_expr(expr);
369+
return os << "floatbv_typecast(" << format(floatbv_typecast_expr.op())
370+
<< ", " << format(floatbv_typecast_expr.type()) << ", "
371+
<< format(floatbv_typecast_expr.rounding_mode()) << ')';
372+
};
373+
317374
auto byte_extract =
318375
[](std::ostream &os, const exprt &expr) -> std::ostream & {
319376
const auto &byte_extract_expr = to_byte_extract_expr(expr);
@@ -454,6 +511,12 @@ void format_expr_configt::setup()
454511
expr_map[ID_array] = compound;
455512
expr_map[ID_struct] = compound;
456513

514+
expr_map[ID_array_of] =
515+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
516+
const auto &array_of_expr = to_array_of_expr(expr);
517+
return os << "array_of(" << format(array_of_expr.what()) << ')';
518+
};
519+
457520
expr_map[ID_if] = [](std::ostream &os, const exprt &expr) -> std::ostream & {
458521
const auto &if_expr = to_if_expr(expr);
459522
return os << '(' << format(if_expr.cond()) << " ? "

src/util/std_expr.h

+29
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,37 @@ class ternary_exprt : public expr_protectedt
6969

7070
const exprt &op3() const = delete;
7171
exprt &op3() = delete;
72+
73+
static void check(
74+
const exprt &expr,
75+
const validation_modet vm = validation_modet::INVARIANT)
76+
{
77+
DATA_CHECK(
78+
vm,
79+
expr.operands().size() == 3,
80+
"ternary expression must have three operands");
81+
}
7282
};
7383

84+
/// \brief Cast an exprt to a \ref ternary_exprt
85+
///
86+
/// \a expr must be known to be \ref ternary_exprt.
87+
///
88+
/// \param expr: Source expression
89+
/// \return Object of type \ref ternary_exprt
90+
inline const ternary_exprt &to_ternary_expr(const exprt &expr)
91+
{
92+
ternary_exprt::check(expr);
93+
return static_cast<const ternary_exprt &>(expr);
94+
}
95+
96+
/// \copydoc to_ternary_expr(const exprt &)
97+
inline ternary_exprt &to_ternary_expr(exprt &expr)
98+
{
99+
ternary_exprt::check(expr);
100+
return static_cast<ternary_exprt &>(expr);
101+
}
102+
74103
/// Expression to hold a symbol (variable)
75104
class symbol_exprt : public nullary_exprt
76105
{

0 commit comments

Comments
 (0)