Skip to content

Commit dd1664b

Browse files
danpoePetr Bauch
authored and
Petr Bauch
committed
Add functions to validate expressions
1 parent c0a9f52 commit dd1664b

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ SRC = arith_tools.cpp \
9696
union_find.cpp \
9797
union_find_replace.cpp \
9898
unwrap_nested_exception.cpp \
99+
validate_expressions.cpp \
99100
version.cpp \
100101
xml.cpp \
101102
xml_expr.cpp \

src/util/validate_expressions.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*******************************************************************\
2+
3+
Module: Validate expressions
4+
5+
Author: Daniel Poetzl
6+
7+
\*******************************************************************/
8+
9+
#include "validate_expressions.h"
10+
11+
#ifdef REPORT_UNIMPLEMENTED_EXPRESSION_CHECKS
12+
#include <iostream>
13+
#endif
14+
15+
#include "expr.h"
16+
#include "namespace.h"
17+
#include "std_expr.h"
18+
#include "validate.h"
19+
20+
#define CALL_ON_EXPR(expr_type) \
21+
C<expr_type>()(expr, std::forward<Args>(args)...)
22+
23+
template <template <typename> class C, typename... Args>
24+
void call_on_expr(const exprt &expr, Args &&... args)
25+
{
26+
if(expr.id() == ID_equal)
27+
{
28+
CALL_ON_EXPR(equal_exprt);
29+
}
30+
else if(expr.id() == ID_plus)
31+
{
32+
CALL_ON_EXPR(plus_exprt);
33+
}
34+
else
35+
{
36+
#ifdef REPORT_UNIMPLEMENTED_EXPRESSION_CHECKS
37+
std::cerr << "Unimplemented well-formedness check for expression with id: "
38+
<< expr.id_string() std::endl;
39+
#endif
40+
41+
CALL_ON_EXPR(exprt);
42+
}
43+
}
44+
45+
/// Check that the given expression is well-formed (shallow checks only, i.e.,
46+
/// subexpressions and its type are not checked)
47+
///
48+
/// The function casts the given expression to its concrete subtype (as
49+
/// determined by the id() field) and then calls its check() method.
50+
///
51+
/// The validation mode indicates whether well-formedness check failures are
52+
/// reported via DATA_INVARIANT violations or exceptions.
53+
void check_expr_pick(const exprt &expr, const validation_modet vm)
54+
{
55+
call_on_expr<call_checkt>(expr, vm);
56+
}
57+
58+
/// Check that the given expression is well-formed, assuming that its
59+
/// subexpression and type have already been checked for well-formedness.
60+
///
61+
/// The function casts the given expression to its concrete subtype (as
62+
/// determined by the id() field) and then calls its validate() method.
63+
///
64+
/// The validation mode indicates whether well-formedness check failures are
65+
/// reported via DATA_INVARIANT violations or exceptions.
66+
void validate_expr_pick(
67+
const exprt &expr,
68+
const namespacet &ns,
69+
const validation_modet vm)
70+
{
71+
call_on_expr<call_validatet>(expr, ns, vm);
72+
}
73+
74+
/// Check that the given expression is well-formed (full check, including checks
75+
/// of all subexpressions and the type)
76+
///
77+
/// The function casts the given expression to its concrete subtype (as
78+
/// determined by the id() field) and then calls its validate_full() method.
79+
///
80+
/// The validation mode indicates whether well-formedness check failures are
81+
/// reported via DATA_INVARIANT violations or exceptions.
82+
void validate_expr_full_pick(
83+
const exprt &expr,
84+
const namespacet &ns,
85+
const validation_modet vm)
86+
{
87+
call_on_expr<call_validate_fullt>(expr, ns, vm);
88+
}

src/util/validate_expressions.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************\
2+
3+
Module: Validate expressions
4+
5+
Author: Daniel Poetzl
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_VALIDATE_EXPRESSIONS_H
10+
#define CPROVER_UTIL_VALIDATE_EXPRESSIONS_H
11+
12+
class exprt;
13+
class namespacet;
14+
enum class validation_modet;
15+
16+
void check_expr_pick(const exprt &expr, const validation_modet vm);
17+
18+
void validate_expr_pick(
19+
const exprt &expr,
20+
const namespacet &ns,
21+
const validation_modet vm);
22+
23+
void validate_expr_full_pick(
24+
const exprt &expr,
25+
const namespacet &ns,
26+
const validation_modet vm);
27+
28+
#endif /* CPROVER_UTIL_VALIDATE_EXPRESSIONS_H */

0 commit comments

Comments
 (0)