Skip to content

Commit 72076ab

Browse files
authored
Merge pull request #6092 from diffblue/add_expression_formatters
Add various formatters
2 parents 55d32b5 + 4c14789 commit 72076ab

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

scripts/expected_doxygen_warnings.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ warning: Included by graph for 'invariant.h' not generated, too many nodes (172)
77
warning: Included by graph for 'irep.h' not generated, too many nodes (80), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
88
warning: Included by graph for 'message.h' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
99
warning: Included by graph for 'namespace.h' not generated, too many nodes (88), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
10-
warning: Included by graph for 'pointer_expr.h' not generated, too many nodes (120), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
11-
warning: Included by graph for 'prefix.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
10+
warning: Included by graph for 'pointer_expr.h' not generated, too many nodes (121), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
11+
warning: Included by graph for 'prefix.h' not generated, too many nodes (62), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
1212
warning: Included by graph for 'simplify_expr.h' not generated, too many nodes (67), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
1313
warning: Included by graph for 'std_code.h' not generated, too many nodes (71), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
1414
warning: Included by graph for 'std_expr.h' not generated, too many nodes (178), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/util/format_expr.cpp

+59-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Author: Daniel Kroening, [email protected]
1717
#include "ieee_float.h"
1818
#include "mathematical_expr.h"
1919
#include "mp_arith.h"
20+
#include "pointer_expr.h"
21+
#include "prefix.h"
2022
#include "std_code.h"
2123
#include "string_utils.h"
2224

@@ -177,8 +179,30 @@ static std::ostream &format_rec(std::ostream &os, const constant_exprt &src)
177179
return os << '"' << escape(id2string(src.get_value())) << '"';
178180
else if(type == ID_floatbv)
179181
return os << ieee_floatt(src);
180-
else if(type == ID_pointer && src.is_zero())
181-
return os << src.get_value();
182+
else if(type == ID_pointer)
183+
{
184+
if(src.is_zero())
185+
return os << ID_NULL;
186+
else if(has_prefix(id2string(src.get_value()), "INVALID-"))
187+
{
188+
return os << "INVALID-POINTER";
189+
}
190+
else if(src.operands().size() == 1)
191+
{
192+
const auto &unary_expr = to_unary_expr(src);
193+
const auto &pointer_type = to_pointer_type(src.type());
194+
return os << "pointer(" << format(unary_expr.op()) << ", "
195+
<< format(pointer_type.subtype()) << ')';
196+
}
197+
else
198+
{
199+
const auto &pointer_type = to_pointer_type(src.type());
200+
const auto width = pointer_type.get_width();
201+
auto int_value = bvrep2integer(src.get_value(), width, false);
202+
return os << "pointer(0x" << integer2string(int_value, 16) << ", "
203+
<< format(pointer_type.subtype()) << ')';
204+
}
205+
}
182206
else
183207
return os << src.pretty();
184208
}
@@ -445,6 +469,39 @@ void format_expr_configt::setup()
445469
return fallback_format_rec(os, expr);
446470
};
447471

472+
expr_map[ID_string_constant] =
473+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
474+
return os << '"' << expr.get_string(ID_value) << '"';
475+
};
476+
477+
expr_map[ID_function_application] =
478+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
479+
const auto &function_application_expr = to_function_application_expr(expr);
480+
os << format(function_application_expr.function()) << '(';
481+
bool first = true;
482+
for(auto &argument : function_application_expr.arguments())
483+
{
484+
if(first)
485+
first = false;
486+
else
487+
os << ", ";
488+
os << format(argument);
489+
}
490+
os << ')';
491+
return os;
492+
};
493+
494+
expr_map[ID_dereference] =
495+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
496+
const auto &dereference_expr = to_dereference_expr(expr);
497+
os << '*';
498+
if(dereference_expr.pointer().id() != ID_symbol)
499+
os << '(' << format(dereference_expr.pointer()) << ')';
500+
else
501+
os << format(dereference_expr.pointer());
502+
return os;
503+
};
504+
448505
fallback = [](std::ostream &os, const exprt &expr) -> std::ostream & {
449506
return fallback_format_rec(os, expr);
450507
};

src/util/format_type.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Author: Daniel Kroening, [email protected]
99
#include "format_type.h"
1010
#include "c_types.h"
1111
#include "format_expr.h"
12+
#include "mathematical_types.h"
1213
#include "pointer_expr.h"
14+
#include "std_types.h"
1315

1416
#include <ostream>
1517

@@ -98,6 +100,21 @@ std::ostream &format_rec(std::ostream &os, const typet &type)
98100
return os << "\xe2\x84\x95"; // u+2115, 'N'
99101
else if(id == ID_rational)
100102
return os << "\xe2\x84\x9a"; // u+211A, 'Q'
103+
else if(id == ID_mathematical_function)
104+
{
105+
const auto &mathematical_function = to_mathematical_function_type(type);
106+
bool first = true;
107+
for(const auto &domain : mathematical_function.domain())
108+
{
109+
if(first)
110+
first = false;
111+
else
112+
os << u8" \u00d7 "; // ×
113+
os << format(domain);
114+
}
115+
os << u8" \u2192 "; // → -- we don't use ⟶ since that doesn't render well
116+
return os << format(mathematical_function.codomain());
117+
}
101118
else
102119
return os << id;
103120
}

0 commit comments

Comments
 (0)