1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Mathias Preiner, Morgan Deters, Tim King |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Assertion utility classes, functions and macros. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "base/check.h" |
17 |
|
|
18 |
|
#include <cstdlib> |
19 |
|
#include <iostream> |
20 |
|
|
21 |
|
namespace cvc5 { |
22 |
|
|
23 |
|
FatalStream::FatalStream(const char* function, const char* file, int line) |
24 |
|
{ |
25 |
|
stream() << "Fatal failure within " << function << " at " << file << ":" |
26 |
|
<< line << "\n"; |
27 |
|
} |
28 |
|
|
29 |
|
FatalStream::~FatalStream() |
30 |
|
{ |
31 |
|
Flush(); |
32 |
|
abort(); |
33 |
|
} |
34 |
|
|
35 |
|
std::ostream& FatalStream::stream() { return std::cerr; } |
36 |
|
|
37 |
|
void FatalStream::Flush() |
38 |
|
{ |
39 |
|
stream() << std::endl; |
40 |
|
stream().flush(); |
41 |
|
} |
42 |
|
|
43 |
2 |
void AssertArgumentException::construct(const char* header, |
44 |
|
const char* extra, |
45 |
|
const char* function, |
46 |
|
const char* file, |
47 |
|
unsigned line, |
48 |
|
const char* fmt, |
49 |
|
va_list args) |
50 |
|
{ |
51 |
|
// try building the exception msg with a smallish buffer first, |
52 |
|
// then with a larger one if sprintf tells us to. |
53 |
2 |
int n = 512; |
54 |
|
char* buf; |
55 |
2 |
buf = new char[n]; |
56 |
|
|
57 |
|
for (;;) |
58 |
|
{ |
59 |
|
int size; |
60 |
2 |
if (extra == NULL) |
61 |
|
{ |
62 |
|
size = snprintf(buf, n, "%s\n%s\n%s:%d\n", header, function, file, line); |
63 |
|
} |
64 |
|
else |
65 |
|
{ |
66 |
2 |
size = snprintf(buf, |
67 |
|
n, |
68 |
|
"%s\n%s\n%s:%d:\n\n %s\n", |
69 |
|
header, |
70 |
|
function, |
71 |
|
file, |
72 |
|
line, |
73 |
|
extra); |
74 |
|
} |
75 |
|
|
76 |
2 |
if (size < n) |
77 |
|
{ |
78 |
|
va_list args_copy; |
79 |
2 |
va_copy(args_copy, args); |
80 |
2 |
size += vsnprintf(buf + size, n - size, fmt, args_copy); |
81 |
2 |
va_end(args_copy); |
82 |
|
|
83 |
2 |
if (size < n) |
84 |
|
{ |
85 |
2 |
break; |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
if (size >= n) |
90 |
|
{ |
91 |
|
// try again with a buffer that's large enough |
92 |
|
n = size + 1; |
93 |
|
delete[] buf; |
94 |
|
buf = new char[n]; |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
4 |
setMessage(std::string(buf)); |
99 |
|
|
100 |
|
#ifdef CVC5_DEBUG |
101 |
2 |
LastExceptionBuffer* buffer = LastExceptionBuffer::getCurrent(); |
102 |
2 |
if (buffer != NULL) |
103 |
|
{ |
104 |
|
if (buffer->getContents() == NULL) |
105 |
|
{ |
106 |
|
buffer->setContents(buf); |
107 |
|
} |
108 |
|
} |
109 |
|
#endif /* CVC5_DEBUG */ |
110 |
2 |
delete[] buf; |
111 |
2 |
} |
112 |
|
|
113 |
12 |
void AssertArgumentException::construct(const char* header, |
114 |
|
const char* extra, |
115 |
|
const char* function, |
116 |
|
const char* file, |
117 |
|
unsigned line) |
118 |
|
{ |
119 |
|
// try building the exception msg with a smallish buffer first, |
120 |
|
// then with a larger one if sprintf tells us to. |
121 |
12 |
int n = 256; |
122 |
|
char* buf; |
123 |
|
|
124 |
|
for (;;) |
125 |
|
{ |
126 |
18 |
buf = new char[n]; |
127 |
|
|
128 |
|
int size; |
129 |
18 |
if (extra == NULL) |
130 |
|
{ |
131 |
|
size = snprintf(buf, n, "%s.\n%s\n%s:%d\n", header, function, file, line); |
132 |
|
} |
133 |
|
else |
134 |
|
{ |
135 |
18 |
size = snprintf(buf, |
136 |
|
n, |
137 |
|
"%s.\n%s\n%s:%d:\n\n %s\n", |
138 |
|
header, |
139 |
|
function, |
140 |
|
file, |
141 |
|
line, |
142 |
|
extra); |
143 |
|
} |
144 |
|
|
145 |
18 |
if (size < n) |
146 |
|
{ |
147 |
12 |
break; |
148 |
|
} |
149 |
|
else |
150 |
|
{ |
151 |
|
// try again with a buffer that's large enough |
152 |
6 |
n = size + 1; |
153 |
6 |
delete[] buf; |
154 |
|
} |
155 |
6 |
} |
156 |
|
|
157 |
12 |
setMessage(std::string(buf)); |
158 |
|
|
159 |
|
#ifdef CVC5_DEBUG |
160 |
12 |
LastExceptionBuffer* buffer = LastExceptionBuffer::getCurrent(); |
161 |
12 |
if (buffer != NULL) |
162 |
|
{ |
163 |
|
if (buffer->getContents() == NULL) |
164 |
|
{ |
165 |
|
buffer->setContents(buf); |
166 |
|
} |
167 |
|
} |
168 |
|
#endif /* CVC5_DEBUG */ |
169 |
12 |
delete[] buf; |
170 |
12 |
} |
171 |
|
|
172 |
2 |
AssertArgumentException::AssertArgumentException(const char* condStr, |
173 |
|
const char* argDesc, |
174 |
|
const char* function, |
175 |
|
const char* file, |
176 |
|
unsigned line, |
177 |
|
const char* fmt, |
178 |
2 |
...) |
179 |
2 |
: Exception() |
180 |
|
{ |
181 |
|
va_list args; |
182 |
2 |
va_start(args, fmt); |
183 |
2 |
construct("Illegal argument detected", |
184 |
4 |
(std::string("`") + argDesc + "' is a bad argument; expected " |
185 |
4 |
+ condStr + " to hold") |
186 |
|
.c_str(), |
187 |
|
function, |
188 |
|
file, |
189 |
|
line, |
190 |
|
fmt, |
191 |
|
args); |
192 |
2 |
va_end(args); |
193 |
2 |
} |
194 |
|
|
195 |
12 |
AssertArgumentException::AssertArgumentException(const char* condStr, |
196 |
|
const char* argDesc, |
197 |
|
const char* function, |
198 |
|
const char* file, |
199 |
12 |
unsigned line) |
200 |
12 |
: Exception() |
201 |
|
{ |
202 |
12 |
construct("Illegal argument detected", |
203 |
24 |
(std::string("`") + argDesc + "' is a bad argument; expected " |
204 |
24 |
+ condStr + " to hold") |
205 |
|
.c_str(), |
206 |
|
function, |
207 |
|
file, |
208 |
|
line); |
209 |
12 |
} |
210 |
|
|
211 |
29508 |
} // namespace cvc5 |