Skip to content

Commit 5408542

Browse files
authored
Better error messages from hooks (#599)
* Better errors generically * Iostream
1 parent 7a258a7 commit 5408542

File tree

10 files changed

+104
-97
lines changed

10 files changed

+104
-97
lines changed

include/runtime/header.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,11 @@ block *strip_injection(block *);
273273
std::string floatToString(const floating *);
274274
void init_float2(floating *, std::string);
275275

276+
#define KLLVM_HOOK_INVALID_ARGUMENT(msg) \
277+
do { \
278+
auto err_msg = std::string("[") + std::string(__func__) \
279+
+ std::string("]: ") + std::string(msg); \
280+
throw std::invalid_argument(err_msg); \
281+
} while (false)
282+
276283
#endif // RUNTIME_HEADER_H

runtime/arithmetic/float.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ floating *move_float(floating *);
5555
mpz_ptr move_int(mpz_t);
5656
void add_hash64(void *, uint64_t);
5757
void *move_mint(mpz_t, uint64_t) {
58-
throw std::invalid_argument("not yet implemented");
58+
KLLVM_HOOK_INVALID_ARGUMENT("not yet implemented");
5959
}
6060

6161
SortFloat hook_FLOAT_ceil(SortFloat a) {
@@ -84,11 +84,11 @@ SortFloat hook_FLOAT_trunc(SortFloat a) {
8484

8585
SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {
8686
if (!mpz_fits_ulong_p(prec)) {
87-
throw std::invalid_argument("Precision out of range");
87+
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
8888
}
8989
unsigned long uprec = mpz_get_ui(prec);
9090
if (!mpz_fits_ulong_p(exp)) {
91-
throw std::invalid_argument("Exponent out of range");
91+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
9292
}
9393
unsigned long uexp = mpz_get_ui(exp);
9494
floating result[1];
@@ -100,7 +100,7 @@ SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {
100100

101101
mpz_ptr hook_FLOAT_float2int(SortFloat a) {
102102
if (!mpfr_number_p(a->f)) {
103-
throw std::invalid_argument("Not a finite number");
103+
KLLVM_HOOK_INVALID_ARGUMENT("Not a finite number");
104104
}
105105
mpz_t result;
106106
mpz_init(result);
@@ -110,11 +110,11 @@ mpz_ptr hook_FLOAT_float2int(SortFloat a) {
110110

111111
SortFloat hook_FLOAT_int2float(SortInt a, SortInt prec, SortInt exp) {
112112
if (!mpz_fits_ulong_p(prec)) {
113-
throw std::invalid_argument("Precision out of range");
113+
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
114114
}
115115
unsigned long uprec = mpz_get_ui(prec);
116116
if (!mpz_fits_ulong_p(exp)) {
117-
throw std::invalid_argument("Exponent out of range");
117+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
118118
}
119119
unsigned long uexp = mpz_get_ui(exp);
120120
floating result[1];
@@ -237,7 +237,7 @@ mpz_ptr hook_FLOAT_exponent(SortFloat a) {
237237

238238
void *hook_FLOAT_significand(SortFloat a) {
239239
if (mpfr_nan_p(a->f)) {
240-
throw std::invalid_argument("NaN payload is undefined");
240+
KLLVM_HOOK_INVALID_ARGUMENT("NaN payload is undefined");
241241
}
242242
mpfr_prec_t prec = mpfr_get_prec(a->f);
243243
uint64_t len = (prec + 7) / 8;
@@ -263,11 +263,11 @@ bool hook_FLOAT_isNaN(SortFloat a) {
263263

264264
SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {
265265
if (!mpz_fits_ulong_p(prec)) {
266-
throw std::invalid_argument("Precision out of range");
266+
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
267267
}
268268
unsigned long uprec = mpz_get_ui(prec);
269269
if (!mpz_fits_ulong_p(exp)) {
270-
throw std::invalid_argument("Exponent out of range");
270+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
271271
}
272272
unsigned long uexp = mpz_get_ui(exp);
273273
floating result[1];
@@ -280,11 +280,11 @@ SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {
280280

281281
SortFloat hook_FLOAT_minValue(SortInt prec, SortInt exp) {
282282
if (!mpz_fits_ulong_p(prec)) {
283-
throw std::invalid_argument("Precision out of range");
283+
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
284284
}
285285
unsigned long uprec = mpz_get_ui(prec);
286286
if (!mpz_fits_ulong_p(exp)) {
287-
throw std::invalid_argument("Exponent out of range");
287+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
288288
}
289289
unsigned long uexp = mpz_get_ui(exp);
290290
floating result[1];
@@ -423,7 +423,7 @@ SortFloat hook_FLOAT_pow(SortFloat a, SortFloat b) {
423423

424424
SortFloat hook_FLOAT_root(SortFloat a, SortInt b) {
425425
if (!mpz_fits_ulong_p(b)) {
426-
throw std::invalid_argument("Root out of range");
426+
KLLVM_HOOK_INVALID_ARGUMENT("Root out of range");
427427
}
428428
unsigned long root = mpz_get_ui(b);
429429
floating result[1];
@@ -456,11 +456,11 @@ bool hook_FLOAT_sign(SortFloat a) {
456456
SortFloat hook_FLOAT_rat2float(
457457
SortInt numerator, SortInt denominator, SortInt prec, SortInt exp) {
458458
if (!mpz_fits_ulong_p(prec)) {
459-
throw std::invalid_argument("Precision out of range");
459+
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
460460
}
461461
unsigned long uprec = mpz_get_ui(prec);
462462
if (!mpz_fits_ulong_p(exp)) {
463-
throw std::invalid_argument("Exponent out of range");
463+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
464464
}
465465
unsigned long uexp = mpz_get_ui(exp);
466466

runtime/arithmetic/int.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void add_hash64(void *, uint64_t);
1313
SortInt hook_INT_tmod(SortInt a, SortInt b) {
1414
mpz_t result;
1515
if (mpz_sgn(b) == 0) {
16-
throw std::invalid_argument("Modulus by zero");
16+
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
1717
}
1818
mpz_init(result);
1919
mpz_tdiv_r(result, a, b);
@@ -23,7 +23,7 @@ SortInt hook_INT_tmod(SortInt a, SortInt b) {
2323
SortInt hook_INT_emod(SortInt a, SortInt b) {
2424
mpz_t result;
2525
if (mpz_sgn(b) == 0) {
26-
throw std::invalid_argument("Modulus by zero");
26+
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
2727
}
2828
mpz_init(result);
2929
mpz_tdiv_r(result, a, b);
@@ -81,7 +81,7 @@ SortInt hook_INT_sub(SortInt a, SortInt b) {
8181
SortInt hook_INT_tdiv(SortInt a, SortInt b) {
8282
mpz_t result;
8383
if (mpz_sgn(b) == 0) {
84-
throw std::invalid_argument("Division by zero");
84+
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
8585
}
8686
mpz_init(result);
8787
mpz_tdiv_q(result, a, b);
@@ -91,7 +91,7 @@ SortInt hook_INT_tdiv(SortInt a, SortInt b) {
9191
SortInt hook_INT_ediv(SortInt a, SortInt b) {
9292
mpz_t result;
9393
if (mpz_sgn(b) == 0) {
94-
throw std::invalid_argument("Division by zero");
94+
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
9595
}
9696
mpz_init(result);
9797
if (mpz_sgn(b) >= 0) {
@@ -105,7 +105,7 @@ SortInt hook_INT_ediv(SortInt a, SortInt b) {
105105
SortInt hook_INT_shl(SortInt a, SortInt b) {
106106
mpz_t result;
107107
if (!mpz_fits_ulong_p(b)) {
108-
throw std::invalid_argument("Shift amount out of range");
108+
KLLVM_HOOK_INVALID_ARGUMENT("Shift amount out of range");
109109
}
110110
mpz_init(result);
111111
unsigned long blong = mpz_get_ui(b);
@@ -126,7 +126,7 @@ SortInt hook_INT_shr(SortInt a, SortInt b) {
126126
mpz_init(result);
127127
if (!mpz_fits_ulong_p(b)) {
128128
if (mpz_sgn(b) < 0) {
129-
throw std::invalid_argument("Negative shift amount");
129+
KLLVM_HOOK_INVALID_ARGUMENT("Negative shift amount");
130130
}
131131
if (mpz_sgn(a) < 0) {
132132
mpz_set_si(result, -1);
@@ -145,7 +145,7 @@ bool hook_INT_gt(SortInt a, SortInt b) {
145145
SortInt hook_INT_pow(SortInt a, SortInt b) {
146146
mpz_t result;
147147
if (!mpz_fits_ulong_p(b)) {
148-
throw std::invalid_argument("Exponent out of range");
148+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
149149
}
150150
mpz_init(result);
151151
unsigned long blong = mpz_get_ui(b);
@@ -160,7 +160,7 @@ SortInt hook_INT_powmod(SortInt a, SortInt b, SortInt mod) {
160160
mpz_gcd(result, a, mod);
161161
if (mpz_cmp_ui(result, 1) != 0) {
162162
mpz_clear(result);
163-
throw std::invalid_argument("Modular inverse not defined");
163+
KLLVM_HOOK_INVALID_ARGUMENT("Modular inverse not defined");
164164
}
165165
}
166166
mpz_powm(result, a, b, mod);
@@ -220,7 +220,7 @@ SortInt hook_INT_min(SortInt a, SortInt b) {
220220
SortInt hook_INT_log2(SortInt a) {
221221
mpz_t result;
222222
if (mpz_sgn(a) <= 0) {
223-
throw std::invalid_argument("Logarithm of nonpositive integer");
223+
KLLVM_HOOK_INVALID_ARGUMENT("Logarithm of nonpositive integer");
224224
}
225225
mpz_init(result);
226226
size_t log = mpz_sizeinbase(a, 2) - 1;
@@ -278,12 +278,12 @@ SortInt hook_INT_bitRange(SortInt i, SortInt off, SortInt len) {
278278
return move_int(result);
279279
}
280280
if (!mpz_fits_ulong_p(len)) {
281-
throw std::invalid_argument("Length out of range");
281+
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
282282
}
283283
unsigned long lenlong = mpz_get_ui(len);
284284
if (!mpz_fits_ulong_p(off)) {
285285
if (mpz_sgn(off) < 0) {
286-
throw std::invalid_argument("Negative offset");
286+
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
287287
}
288288
mpz_init(result);
289289
if (mpz_sgn(i) < 0) {
@@ -324,7 +324,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
324324
mpz_t result;
325325
if (!mpz_fits_ulong_p(off)) {
326326
if (mpz_sgn(off) < 0) {
327-
throw std::invalid_argument("Negative offset");
327+
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
328328
}
329329
mpz_init(result);
330330
if (mpz_sgn(i) < 0) {
@@ -333,7 +333,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
333333
return move_int(result);
334334
}
335335
if (!mpz_fits_ulong_p(len)) {
336-
throw std::invalid_argument("Length out of range");
336+
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
337337
}
338338
unsigned long offlong = mpz_get_ui(off);
339339
unsigned long lenlong = mpz_get_ui(len);

runtime/collections/lists.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool hook_LIST_in(SortKItem value, SortList list) {
3636

3737
bool hook_LIST_in_keys(SortInt index, SortList list) {
3838
if (!mpz_fits_ulong_p(index)) {
39-
throw std::invalid_argument("Index is too large for in_keys");
39+
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for in_keys");
4040
}
4141
size_t idx = mpz_get_ui(index);
4242
return idx < list->size();
@@ -50,7 +50,7 @@ SortKItem hook_LIST_get_long(SortList list, ssize_t idx) {
5050

5151
SortKItem hook_LIST_get(SortList list, SortInt index) {
5252
if (!mpz_fits_slong_p(index)) {
53-
throw std::invalid_argument("Index is too large for get");
53+
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for get");
5454
}
5555
ssize_t idx = mpz_get_si(index);
5656
return hook_LIST_get_long(list, idx);
@@ -75,7 +75,7 @@ list hook_LIST_range_long(SortList list, size_t front, size_t back) {
7575

7676
list hook_LIST_range(SortList list, SortInt from_front, SortInt from_back) {
7777
if (!mpz_fits_ulong_p(from_front) || !mpz_fits_ulong_p(from_back)) {
78-
throw std::invalid_argument("Range index too large for range");
78+
KLLVM_HOOK_INVALID_ARGUMENT("Range index too large for range");
7979
}
8080

8181
size_t front = mpz_get_ui(from_front);
@@ -96,7 +96,7 @@ SortInt hook_LIST_size(SortList list) {
9696

9797
list hook_LIST_make(SortInt len, SortKItem value) {
9898
if (!mpz_fits_ulong_p(len)) {
99-
throw std::invalid_argument("Length is too large for make");
99+
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for make");
100100
}
101101

102102
size_t length = mpz_get_ui(len);
@@ -105,28 +105,28 @@ list hook_LIST_make(SortInt len, SortKItem value) {
105105

106106
list hook_LIST_update(SortList list, SortInt index, SortKItem value) {
107107
if (!mpz_fits_ulong_p(index)) {
108-
throw std::invalid_argument("Length is too large for update");
108+
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for update");
109109
}
110110

111111
size_t idx = mpz_get_ui(index);
112112
if (idx >= list->size()) {
113-
throw std::invalid_argument("Index out of range for update");
113+
KLLVM_HOOK_INVALID_ARGUMENT("Index out of range for update");
114114
}
115115

116116
return list->set(idx, value);
117117
}
118118

119119
list hook_LIST_updateAll(SortList l1, SortInt index, SortList l2) {
120120
if (!mpz_fits_ulong_p(index)) {
121-
throw std::invalid_argument("Length is too large for updateAll");
121+
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for updateAll");
122122
}
123123

124124
size_t idx = mpz_get_ui(index);
125125
size_t size = l1->size();
126126
size_t size2 = l2->size();
127127
if (idx != 0 && size2 != 0) {
128128
if (idx + size2 - 1 >= size) {
129-
throw std::invalid_argument("Index out of range for updateAll");
129+
KLLVM_HOOK_INVALID_ARGUMENT("Index out of range for updateAll");
130130
}
131131
}
132132

@@ -151,11 +151,11 @@ list hook_LIST_updateAll(SortList l1, SortInt index, SortList l2) {
151151

152152
list hook_LIST_fill(SortList l, SortInt index, SortInt len, SortKItem val) {
153153
if (!mpz_fits_ulong_p(index)) {
154-
throw std::invalid_argument("Index is too large for fill");
154+
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for fill");
155155
}
156156

157157
if (!mpz_fits_ulong_p(len)) {
158-
throw std::invalid_argument("Length is too large for fill");
158+
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for fill");
159159
}
160160

161161
size_t idx = mpz_get_ui(index);

runtime/collections/maps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ map hook_MAP_concat(SortMap m1, SortMap m2) {
2828
for (auto iter = from->begin(); iter != from->end(); ++iter) {
2929
auto entry = *iter;
3030
if (to.find(entry.first)) {
31-
throw std::invalid_argument("Duplicate keys in map concatenation");
31+
KLLVM_HOOK_INVALID_ARGUMENT("Duplicate keys in map concatenation");
3232
}
3333
to = to.insert(*iter);
3434
}
@@ -45,7 +45,7 @@ SortKItem hook_MAP_lookup_null(SortMap m, SortKItem key) {
4545
SortKItem hook_MAP_lookup(SortMap m, SortKItem key) {
4646
auto res = hook_MAP_lookup_null(m, key);
4747
if (!res) {
48-
throw std::invalid_argument("Key not found for map lookup");
48+
KLLVM_HOOK_INVALID_ARGUMENT("Key not found for map lookup");
4949
}
5050
return res;
5151
}
@@ -116,7 +116,7 @@ list hook_MAP_values(SortMap m) {
116116

117117
SortKItem hook_MAP_choice(SortMap m) {
118118
if (m->empty()) {
119-
throw std::invalid_argument("Cannot choose from an empty map");
119+
KLLVM_HOOK_INVALID_ARGUMENT("Cannot choose from an empty map");
120120
}
121121
return m->begin()->first;
122122
}

runtime/collections/sets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ set hook_SET_intersection(SortSet s1, SortSet s2) {
7777

7878
SortKItem hook_SET_choice(SortSet s) {
7979
if (s->empty()) {
80-
throw std::invalid_argument("Set is empty");
80+
KLLVM_HOOK_INVALID_ARGUMENT("Set is empty");
8181
}
8282
return *s->begin();
8383
}

0 commit comments

Comments
 (0)