Skip to content

Commit c0fb7c6

Browse files
authored
Merge pull request #1231 from smowton/smowton/feature/invariant_with_irep
Add standard invariant subclass with irep member
2 parents 565f5bf + 92f70d6 commit c0fb7c6

File tree

7 files changed

+97
-2
lines changed

7 files changed

+97
-2
lines changed

regression/invariants/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ SRC = driver.cpp
44

55
INCLUDES = -I ../../src
66

7-
OBJ += ../../src/util/util$(LIBEXT)
7+
OBJ += ../../src/big-int/big-int$(LIBEXT) \
8+
../../src/util/util$(LIBEXT)
89

910
include ../../src/config.inc
1011
include ../../src/common

regression/invariants/driver.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Author: Chris Smowton, [email protected]
1212
#include <string>
1313
#include <sstream>
1414
#include <util/invariant.h>
15+
#include <util/invariant_utils.h>
16+
#include <util/std_types.h>
1517

1618
/// An example of structured invariants-- this contains fields to
1719
/// describe the error to a catcher, and also produces a human-readable
@@ -83,6 +85,8 @@ int main(int argc, char** argv)
8385
DATA_INVARIANT_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT
8486
else if(arg=="data-invariant-string")
8587
DATA_INVARIANT(false, "Test invariant failure");
88+
else if(arg=="irep")
89+
INVARIANT_WITH_IREP(false, "error with irep", pointer_typet(void_typet()));
8690
else
8791
return 1;
8892
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
dummy_parameter.c
3+
irep
4+
^EXIT=(0|127|134|137)$
5+
^SIGNAL=0$
6+
--- begin invariant violation report ---
7+
Invariant check failed
8+
error with irep
9+
pointer
10+
0: empty
11+
^(Backtrace)|(Backtraces not supported)$
12+
--
13+
^warning: ignoring
14+
^VERIFICATION SUCCESSFUL$

src/util/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SRC = arith_tools.cpp \
3131
irep_hash_container.cpp \
3232
irep_ids.cpp \
3333
irep_serialization.cpp \
34+
invariant_utils.cpp \
3435
json.cpp \
3536
json_expr.cpp \
3637
json_irep.cpp \

src/util/invariant.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ std::string invariant_failedt::get_invariant_failed_message(
129129
<< " function " << function
130130
<< " line " << line << '\n'
131131
<< "Reason: " << reason
132-
<< "Backtrace:\n"
132+
<< "\nBacktrace:\n"
133133
<< backtrace << '\n';
134134
return out.str();
135135
}

src/util/invariant_utils.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************\
2+
3+
Module: Invariant helper utilities
4+
5+
Author: Chris Smowton, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Invariant helper utilities
11+
12+
#include "invariant_utils.h"
13+
14+
std::string pretty_print_invariant_with_irep(
15+
const irept &problem_node,
16+
const std::string &description)
17+
{
18+
if(problem_node.is_nil())
19+
return description;
20+
else
21+
{
22+
std::string ret=description;
23+
ret+="\nProblem irep:\n";
24+
ret+=problem_node.pretty(0,0);
25+
return ret;
26+
}
27+
}

src/util/invariant_utils.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************\
2+
3+
Module: Invariant helper utilities
4+
5+
Author: Chris Smowton, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_INVARIANT_TYPES_H
10+
#define CPROVER_UTIL_INVARIANT_TYPES_H
11+
12+
#include "invariant.h"
13+
14+
#include <util/irep.h>
15+
16+
#include <string>
17+
18+
/// Produces a plain string error description from an irep and some
19+
/// explanatory text. If `problem_node` is nil, returns `description`.
20+
/// \param problem_node: irep to pretty-print
21+
/// \param description: descriptive text to prepend
22+
/// \return error message
23+
std::string pretty_print_invariant_with_irep(
24+
const irept &problem_node,
25+
const std::string &description);
26+
27+
/// Equivalent to
28+
/// `INVARIANT_STRUCTURED(CONDITION, invariant_failedt,
29+
/// pretty_print_invariant_with_irep(IREP, DESCRIPTION))`
30+
#define INVARIANT_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
31+
INVARIANT_STRUCTURED( \
32+
CONDITION, \
33+
invariant_failedt, \
34+
pretty_print_invariant_with_irep((IREP), (DESCRIPTION)))
35+
36+
/// See `INVARIANT_WITH_IREP`
37+
#define PRECONDITION_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
38+
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
39+
#define POSTCONDITION_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
40+
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
41+
#define CHECK_RETURN_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
42+
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
43+
#define UNREACHABLE_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
44+
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
45+
#define DATA_INVARIANT_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
46+
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
47+
48+
#endif

0 commit comments

Comments
 (0)