Skip to content

Commit 92db71a

Browse files
committed
introduce add_format_hook(...)
This commit adds a function to extend the expression formatter at runtime.
1 parent 29d432c commit 92db71a

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/util/format_expr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ format_expr_configt::find_formatter(const exprt &expr)
614614

615615
format_expr_configt format_expr_config;
616616

617+
void add_format_hook(irep_idt id, format_expr_configt::formattert formatter)
618+
{
619+
format_expr_config.expr_map[id] = std::move(formatter);
620+
}
621+
617622
std::ostream &format_rec(std::ostream &os, const exprt &expr)
618623
{
619624
auto &formatter = format_expr_config.find_formatter(expr);

src/util/format_expr.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ Author: Daniel Kroening, [email protected]
1010
#define CPROVER_UTIL_FORMAT_EXPR_H
1111

1212
#include "format.h"
13+
#include "irep.h"
14+
15+
#include <functional>
1316

1417
class exprt;
1518

1619
//! Formats an expression in a generic syntax
1720
//! that is inspired by C/C++/Java, and is meant for debugging
1821
std::ostream &format_rec(std::ostream &, const exprt &);
1922

23+
/// Adds a formatter for expressions with the given ID at runtime.
24+
/// The formatter is given as a function that receives the output
25+
/// stream and the expression as arguments.
26+
void add_format_hook(
27+
irep_idt,
28+
std::function<std::ostream &(std::ostream &, const exprt &)>);
29+
2030
#endif // CPROVER_UTIL_FORMAT_EXPR_H

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ SRC += analyses/ai/ai.cpp \
135135
util/expr_iterator.cpp \
136136
util/file_util.cpp \
137137
util/format.cpp \
138+
util/format_expr.cpp \
138139
util/format_number_range.cpp \
139140
util/get_base_name.cpp \
140141
util/graph.cpp \

unit/util/format_expr.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for `format` function on expressions
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include <util/exception_utils.h>
10+
#include <util/expr.h>
11+
#include <util/format_expr.h>
12+
13+
#include <testing-utils/use_catch.h>
14+
15+
TEST_CASE(
16+
"Format an expression with a format hook",
17+
"[core][util][format_hook]")
18+
{
19+
irep_idt custom_id("custom_id");
20+
add_format_hook(
21+
custom_id, [](std::ostream &out, const exprt &expr) -> std::ostream & {
22+
out << "output";
23+
return out;
24+
});
25+
REQUIRE(format_to_string(exprt{custom_id}) == "output");
26+
}

0 commit comments

Comments
 (0)