72
72
// / family of macros, allowing constructs like
73
73
// / `INVARIANT(x==y, my_invariantt, (T1)actual1, (T2)actual2, ...)`
74
74
// /
75
- class invariant_failedt : public std ::logic_error
75
+ class invariant_failedt
76
76
{
77
77
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 ;
84
84
85
85
public:
86
86
const std::string file;
87
87
const std::string function;
88
88
const int line;
89
89
const std::string backtrace;
90
90
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
+ };
91
98
92
99
invariant_failedt (
93
100
const std::string &_file,
94
101
const std::string &_function,
95
102
int _line,
96
103
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)
110
112
{
111
113
}
112
114
};
@@ -151,7 +153,7 @@ void report_exception_to_stderr(const invariant_failedt &);
151
153
// / \param line : The line number of the invariant
152
154
// / \param params : (variadic) parameters to forward to ET's constructor
153
155
// / its backtrace member will be set before it is used.
154
- template <class ET , typename ...Params>
156
+ template <class ET , typename ... Params>
155
157
#ifdef __GNUC__
156
158
__attribute__ ((noreturn))
157
159
#endif
@@ -160,10 +162,17 @@ invariant_violated_structured(
160
162
const std::string &file,
161
163
const std::string &function,
162
164
const int line,
165
+ const std::string &condition,
163
166
Params &&... params)
164
167
{
165
168
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);
167
176
// We now have a structured exception ready to use;
168
177
// in future this is the place to put a 'throw'.
169
178
report_exception_to_stderr (to_throw);
@@ -180,17 +189,16 @@ invariant_violated_structured(
180
189
#ifdef __GNUC__
181
190
__attribute__ ((noreturn))
182
191
#endif
183
- inline void invariant_violated_string (
192
+ inline void
193
+ invariant_violated_string (
184
194
const std::string &file,
185
195
const std::string &function,
186
196
const int line,
187
- const std::string &reason)
197
+ const std::string &reason,
198
+ const std::string &condition)
188
199
{
189
200
invariant_violated_structured<invariant_failedt>(
190
- file,
191
- function,
192
- line,
193
- reason);
201
+ file, function, line, condition, reason);
194
202
}
195
203
196
204
// These require a trailing semicolon by the user, such that INVARIANT
@@ -207,15 +215,23 @@ inline void invariant_violated_string(
207
215
{ \
208
216
if (!(CONDITION)) \
209
217
invariant_violated_string ( \
210
- __FILE__, __this_function__, __LINE__, (REASON)); /* NOLINT */ \
218
+ __FILE__, \
219
+ __this_function__, \
220
+ __LINE__, \
221
+ (REASON), \
222
+ #CONDITION); /* NOLINT */ \
211
223
} while (false )
212
224
213
225
#define INVARIANT_STRUCTURED (CONDITION, TYPENAME, ...) \
214
226
do /* NOLINT */ \
215
227
{ \
216
228
if (!(CONDITION)) \
217
229
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 */ \
219
235
} while (false )
220
236
221
237
#endif // End CPROVER_DO_NOT_CHECK / CPROVER_ASSERT / ... if block
0 commit comments