Skip to content

Commit a7b972d

Browse files
committed
Pass the condition to the invariant_failedt constructor.
1 parent acc1586 commit a7b972d

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/util/invariant.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Martin Brain, [email protected]
88

99
#include "invariant.h"
1010

11-
#include "util/freer.h"
11+
#include "freer.h"
1212

1313
#include <memory>
1414
#include <string>
@@ -121,13 +121,14 @@ std::string invariant_failedt::get_invariant_failed_message(
121121
const std::string &function,
122122
int line,
123123
const std::string &backtrace,
124-
const std::string &reason)
124+
const std::string &reason) const
125125
{
126126
std::ostringstream out;
127127
out << "Invariant check failed\n"
128128
<< "File " << file
129129
<< " function " << function
130130
<< " line " << line << '\n'
131+
<< "Condition: " << condition << '\n'
131132
<< "Reason: " << reason
132133
<< "\nBacktrace:\n"
133134
<< backtrace << '\n';

src/util/invariant.h

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,43 @@ Author: Martin Brain, [email protected]
7272
/// family of macros, allowing constructs like
7373
/// `INVARIANT(x==y, my_invariantt, (T1)actual1, (T2)actual2, ...)`
7474
///
75-
class invariant_failedt: public std::logic_error
75+
class invariant_failedt
7676
{
7777
private:
78-
std::string get_invariant_failed_message(
79-
const std::string &file,
80-
const std::string &function,
81-
int line,
82-
const std::string &backtrace,
83-
const std::string &reason);
78+
std::string get_invariant_failed_message(
79+
const std::string &file,
80+
const std::string &function,
81+
int line,
82+
const std::string &backtrace,
83+
const std::string &reason) const;
8484

8585
public:
8686
const std::string file;
8787
const std::string function;
8888
const int line;
8989
const std::string backtrace;
9090
const std::string reason;
91+
const std::string condition;
92+
93+
std::string what() const noexcept
94+
{
95+
return get_invariant_failed_message(
96+
file, function, line, backtrace, reason);
97+
};
9198

9299
invariant_failedt(
93100
const std::string &_file,
94101
const std::string &_function,
95102
int _line,
96103
const std::string &_backtrace,
97-
const std::string &_reason):
98-
logic_error(
99-
get_invariant_failed_message(
100-
_file,
101-
_function,
102-
_line,
103-
_backtrace,
104-
_reason)),
105-
file(_file),
106-
function(_function),
107-
line(_line),
108-
backtrace(_backtrace),
109-
reason(_reason)
104+
const std::string &_reason,
105+
const std::string &_condition)
106+
: file(_file),
107+
function(_function),
108+
line(_line),
109+
backtrace(_backtrace),
110+
reason(_reason),
111+
condition(_condition)
110112
{
111113
}
112114
};
@@ -151,7 +153,7 @@ void report_exception_to_stderr(const invariant_failedt &);
151153
/// \param line : The line number of the invariant
152154
/// \param params : (variadic) parameters to forward to ET's constructor
153155
/// its backtrace member will be set before it is used.
154-
template<class ET, typename ...Params>
156+
template <class ET, typename... Params>
155157
#ifdef __GNUC__
156158
__attribute__((noreturn))
157159
#endif
@@ -160,10 +162,17 @@ invariant_violated_structured(
160162
const std::string &file,
161163
const std::string &function,
162164
const int line,
165+
const std::string &condition,
163166
Params &&... params)
164167
{
165168
std::string backtrace=get_backtrace();
166-
ET to_throw(file, function, line, backtrace, std::forward<Params>(params)...);
169+
ET to_throw(
170+
file,
171+
function,
172+
line,
173+
backtrace,
174+
std::forward<Params>(params)...,
175+
condition);
167176
// We now have a structured exception ready to use;
168177
// in future this is the place to put a 'throw'.
169178
report_exception_to_stderr(to_throw);
@@ -180,17 +189,16 @@ invariant_violated_structured(
180189
#ifdef __GNUC__
181190
__attribute__((noreturn))
182191
#endif
183-
inline void invariant_violated_string(
192+
inline void
193+
invariant_violated_string(
184194
const std::string &file,
185195
const std::string &function,
186196
const int line,
187-
const std::string &reason)
197+
const std::string &reason,
198+
const std::string &condition)
188199
{
189200
invariant_violated_structured<invariant_failedt>(
190-
file,
191-
function,
192-
line,
193-
reason);
201+
file, function, line, condition, reason);
194202
}
195203

196204
// These require a trailing semicolon by the user, such that INVARIANT
@@ -207,15 +215,23 @@ inline void invariant_violated_string(
207215
{ \
208216
if(!(CONDITION)) \
209217
invariant_violated_string( \
210-
__FILE__, __this_function__, __LINE__, (REASON)); /* NOLINT */ \
218+
__FILE__, \
219+
__this_function__, \
220+
__LINE__, \
221+
(REASON), \
222+
#CONDITION); /* NOLINT */ \
211223
} while(false)
212224

213225
#define INVARIANT_STRUCTURED(CONDITION, TYPENAME, ...) \
214226
do /* NOLINT */ \
215227
{ \
216228
if(!(CONDITION)) \
217229
invariant_violated_structured<TYPENAME>( \
218-
__FILE__, __this_function__, __LINE__, __VA_ARGS__); /* NOLINT */ \
230+
__FILE__, \
231+
__this_function__, \
232+
__LINE__, \
233+
__VA_ARGS__, \
234+
#CONDITION); /* NOLINT */ \
219235
} while(false)
220236

221237
#endif // End CPROVER_DO_NOT_CHECK / CPROVER_ASSERT / ... if block

0 commit comments

Comments
 (0)