1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
#ifndef C4CORE_SINGLE_HEADER
#include "c4/error.hpp"
#endif
#include "c4/test.hpp"
#include "c4/libtest/supprwarn_push.hpp"
#ifndef C4_EXCEPTIONS
#include <csetjmp>
#endif
C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4611) // interaction between '_setjmp' and C++ object destruction is non-portable
namespace c4 {
TEST_CASE("Error.scoped_callback")
{
auto orig = get_error_callback();
C4_EXPECT_ERROR_OCCURS([&]{
CHECK_EQ(get_error_callback() != orig, true);
C4_ERROR("bla bla");
});
}
} // namespace c4
TEST_CASE("Error.outside_of_c4_namespace")
{
auto orig = c4::get_error_callback();
C4_EXPECT_ERROR_OCCURS([&]{
CHECK_EQ(c4::get_error_callback() != orig, true);
C4_ERROR("bla bla");
});
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// WIP: new error handling code
#include <string> // temporary; just for the exception example
#include <iostream> // temporary; just for the exception example
C4_IF_EXCEPTIONS_( , static std::jmp_buf s_jmp_buf; static std::string s_jmp_errmsg; );
namespace c4 {
#define C4_ERR_FMT_BUFFER_SIZE 256
using locref = c4::srcloc const& C4_RESTRICT;
using pfn_err = void (*)(locref loc, void *data);
using pfn_warn = void (*)(locref loc, void *data);
using pfn_msg_begin = void (*)(locref loc, void *data);
using pfn_msg_part = void (*)(const char* msg, size_t size, void *data);
using pfn_msg_end = void (*)(void *data);
struct ErrorCallbacks
{
void *user_data;
pfn_err err;
pfn_warn warn;
pfn_msg_begin msg_begin;
pfn_msg_part msg_part;
pfn_msg_end msg_end;
bool msg_enabled() const { return msg_begin != nullptr; }
template<size_t N>
void msg(const char (&s)[N])
{
msg_part(s, N-1, user_data);
}
void msg(const char *msg, size_t sz)
{
msg_part(msg, sz, user_data);
}
void msg(char c)
{
msg_part(&c, 1, user_data);
}
};
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
TEST_CASE("ErrorCallbacks.default_obj")
{
ErrorCallbacks cb {};
CHECK_EQ(cb.user_data, nullptr);
CHECK_EQ(cb.err, nullptr);
CHECK_EQ(cb.warn, nullptr);
CHECK_EQ(cb.msg_begin, nullptr);
CHECK_EQ(cb.msg_part, nullptr);
CHECK_EQ(cb.msg_end, nullptr);
}
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
#pragma GCC diagnostic pop
#endif
template<class ErrBhv>
struct ErrorCallbacksBridgeFull
{
ErrorCallbacks callbacks() const
{
C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wcast-qual")
return {
(ErrBhv*)this,
ErrorCallbacksBridgeFull<ErrBhv>::on_err,
ErrorCallbacksBridgeFull<ErrBhv>::on_warn,
ErrorCallbacksBridgeFull<ErrBhv>::on_msg_begin,
ErrorCallbacksBridgeFull<ErrBhv>::on_msg_part,
ErrorCallbacksBridgeFull<ErrBhv>::on_msg_end,
};
C4_SUPPRESS_WARNING_GCC_CLANG_POP
}
static void on_err(locref loc, void *data)
{
((ErrBhv*)data)->err(loc);
}
static void on_warn(locref loc, void *data)
{
((ErrBhv*)data)->warn(loc);
}
static void on_msg_begin(locref loc, void *data)
{
((ErrBhv*)data)->msg_begin(loc);
}
static void on_msg_part(const char *part, size_t size, void *data)
{
((ErrBhv*)data)->msg_part(part, size);
}
static void on_msg_end(void *data)
{
((ErrBhv*)data)->msg_end();
}
};
template<class ErrBhv>
struct ErrorCallbacksBridge
{
ErrorCallbacks callbacks() const
{
C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wcast-qual")
return {
(ErrBhv*)this,
ErrorCallbacksBridge<ErrBhv>::on_err,
ErrorCallbacksBridge<ErrBhv>::on_warn,
(pfn_msg_begin)nullptr,
(pfn_msg_part)nullptr,
(pfn_msg_end)nullptr
};
C4_SUPPRESS_WARNING_GCC_CLANG_POP
}
static void on_err(locref loc, void *data)
{
((ErrBhv*)data)->err(loc);
}
static void on_warn(locref loc, void *data)
{
((ErrBhv*)data)->warn(loc);
}
};
void fputi(int val, FILE *f);
void _errmsg(locref loc)
{
fputs(loc.file, stderr);
fputc(':', stderr);
fputi(loc.line, stderr);
fputs(": ", stderr);
fflush(stderr);
}
void _errmsg(const char *part, size_t part_size)
{
fwrite(part, 1u, part_size, stderr);
fflush(stderr);
}
/** example implementation using old-style abort */
struct ErrorBehaviorAbort : public ErrorCallbacksBridgeFull<ErrorBehaviorAbort>
{
static void msg_begin(locref loc)
{
fputc('\n', stderr);
_errmsg(loc);
}
static void msg_part(const char *part, size_t part_size)
{
_errmsg(part, part_size);
}
static void msg_end()
{
fputc('\n', stderr);
fflush(stderr);
}
static void err(locref)
{
abort();
}
static void warn(locref)
{
// nothing to do
}
};
TEST_CASE("ErrorBehaviorAbort.default_obj")
{
ErrorBehaviorAbort bhv;
auto cb = bhv.callbacks();
CHECK_NE(cb.user_data, nullptr);
CHECK_NE(cb.err, nullptr);
CHECK_NE(cb.warn, nullptr);
CHECK_NE(cb.msg_begin, nullptr);
CHECK_NE(cb.msg_part, nullptr);
CHECK_NE(cb.msg_end, nullptr);
}
void fputi(int val, FILE *f);
void _append(std::string *s, int line);
/** example implementation using vanilla c++ std::runtime_error (or setjmp when exceptions are disabled) */
struct ErrorBehaviorRuntimeError : public ErrorCallbacksBridgeFull<ErrorBehaviorRuntimeError>
{
std::string exc_msg{};
void msg_begin(locref loc)
{
exc_msg.reserve(strlen(loc.file) + 16);
exc_msg = '\n';
exc_msg += loc.file;
exc_msg += ':';
_append(&exc_msg, loc.line);
exc_msg += ": ";
}
void msg_part(const char *part, size_t part_size)
{
exc_msg.append(part, part_size);
}
void msg_end()
{
std::cerr << exc_msg << "\n";
}
void err(locref)
{
C4_IF_EXCEPTIONS_(throw std::runtime_error(exc_msg), { s_jmp_errmsg = exc_msg; std::longjmp(s_jmp_buf, 1); });
}
void warn(locref)
{
// nothing to do
}
};
TEST_CASE("ErrorBehaviorRuntimeError.default_obj")
{
ErrorBehaviorRuntimeError bhv;
auto cb = bhv.callbacks();
CHECK_NE(cb.user_data, nullptr);
CHECK_NE(cb.err, nullptr);
CHECK_NE(cb.warn, nullptr);
CHECK_NE(cb.msg_begin, nullptr);
CHECK_NE(cb.msg_part, nullptr);
CHECK_NE(cb.msg_end, nullptr);
}
ErrorBehaviorAbort s_err_abort = ErrorBehaviorAbort();
ErrorCallbacks s_err_callbacks = s_err_abort.callbacks();
void new_handle_error(locref loc, size_t msg_size, const char *msg)
{
if(s_err_callbacks.msg_enabled())
{
s_err_callbacks.msg_begin(loc, s_err_callbacks.user_data);
s_err_callbacks.msg("ERROR: ");
s_err_callbacks.msg(msg, msg_size);
s_err_callbacks.msg_end(s_err_callbacks.user_data);
}
s_err_callbacks.err(loc, s_err_callbacks.user_data);
}
void new_handle_warning(locref loc, size_t msg_size, const char *msg)
{
if(s_err_callbacks.msg_enabled())
{
s_err_callbacks.msg_begin(loc, s_err_callbacks.user_data);
s_err_callbacks.msg("WARNING: ");
s_err_callbacks.msg(msg, msg_size);
s_err_callbacks.msg_end(s_err_callbacks.user_data);
}
s_err_callbacks.warn(loc, s_err_callbacks.user_data);
}
template<size_t N>
C4_ALWAYS_INLINE void new_handle_error(locref loc, const char (&msg)[N])
{
new_handle_error(loc, N-1, msg);
}
template<size_t N>
C4_ALWAYS_INLINE void new_handle_warning(locref loc, const char (&msg)[N])
{
new_handle_warning(loc, N-1, msg);
}
#define C4_ERROR_NEW(msg) c4::new_handle_error(C4_SRCLOC(), msg)
#define C4_WARNING_NEW(msg) c4::new_handle_warning(C4_SRCLOC(), msg)
#define C4_ERROR_NEW_SZ(msg, msglen) c4::new_handle_error(C4_SRCLOC(), msglen, msg)
#define C4_WARNING_NEW_SZ(msg, msglen) c4::new_handle_warning(C4_SRCLOC(), msglen, msg)
} // namespace c4
#ifndef C4CORE_SINGLE_HEADER
#include <c4/substr.hpp>
#include <c4/charconv.hpp>
#endif
namespace c4 {
void fputi(int val, FILE *f)
{
char buf[16];
size_t ret = c4::itoa(buf, val);
ret = ret < sizeof(buf) ? ret : sizeof(buf);
fwrite(buf, 1u, ret, f);
}
// to avoid using std::to_string()
void _append(std::string *s, int line)
{
auto sz = s->size();
s->resize(sz + 16);
auto ret = itoa(substr(&((*s)[0]) + sz, 16u), line);
s->resize(sz + ret);
if(ret >= sz)
{
itoa(substr(&((*s)[0]) + sz, 16u), line);
}
}
} // namespace c4
template<class ErrorBehavior>
struct ScopedErrorBehavior
{
c4::ErrorCallbacks m_prev;
ErrorBehavior m_tmp;
const char *m_name;
ScopedErrorBehavior(const char* name) : m_prev(c4::s_err_callbacks), m_tmp(), m_name(name)
{
c4::s_err_callbacks = m_tmp.callbacks();
}
~ScopedErrorBehavior()
{
c4::s_err_callbacks = m_prev;
}
};
#define C4_TMP_ERR_BHV(bhv_ty) ScopedErrorBehavior<c4::bhv_ty>(#bhv_ty)
template<size_t N>
void test_error_exception(const char (&msg)[N])
{
INFO(msg);
{
auto tmp1 = C4_TMP_ERR_BHV(ErrorBehaviorAbort);
{
auto tmp2 = C4_TMP_ERR_BHV(ErrorBehaviorRuntimeError);
{
bool got_exc = false;
C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_buf) == 0))
{
C4_ERROR_NEW(msg);
}
C4_IF_EXCEPTIONS_(catch(std::exception const& e), else)
{
// check that the given message is found verbatim on the error message
c4::csubstr errmsg = c4::to_csubstr(C4_IF_EXCEPTIONS_(e.what(), s_jmp_errmsg.c_str()));
INFO("full message: '''" << errmsg << "'''");
size_t pos = errmsg.find(msg);
CHECK_NE(pos, c4::csubstr::npos);
got_exc = (pos != c4::csubstr::npos);
}
CHECK(got_exc);
}
{
bool got_exc = false;
C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_buf) == 0))
{
C4_ERROR_NEW_SZ(msg, N-1);
}
C4_IF_EXCEPTIONS_(catch(std::exception const& e), else)
{
// check that the given message is found verbatim on the error message
c4::csubstr errmsg = c4::to_csubstr(C4_IF_EXCEPTIONS_(e.what(), s_jmp_errmsg.c_str()));
INFO("full message: '''" << errmsg << "'''");
size_t pos = errmsg.find(msg);
CHECK_NE(pos, c4::csubstr::npos);
got_exc = (pos != c4::csubstr::npos);
}
CHECK(got_exc);
}
}
}
}
template<size_t N>
void test_warning_exception(const char (&msg)[N])
{
auto tmp = C4_TMP_ERR_BHV(ErrorBehaviorRuntimeError);
C4_WARNING_NEW(msg);
auto const& wmsg = tmp.m_tmp.exc_msg;
REQUIRE_FALSE(wmsg.empty());
REQUIRE_GT(wmsg.size(), N);
auto what = c4::to_csubstr(wmsg.c_str()).last(N-1);
CHECK_EQ(what, msg);
C4_WARNING_NEW_SZ(msg, N-1);
REQUIRE_FALSE(wmsg.empty());
REQUIRE_GT(wmsg.size(), N);
what = c4::to_csubstr(wmsg.c_str()).last(N-1);
CHECK_EQ(what, msg);
}
TEST_CASE("error.exception")
{
test_error_exception("some error with some message");
test_error_exception("some error with another message");
}
TEST_CASE("warning.exception")
{
test_warning_exception("some warning");
test_warning_exception("some other warning");
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef C4CORE_SINGLE_HEADER
#include <c4/substr.hpp>
#include <c4/charconv.hpp>
#endif
namespace c4 {
template<class T>
void _err_send(T const& arg)
{
char buf[C4_ERR_FMT_BUFFER_SIZE];
size_t num = to_chars(buf, arg);
num = num < C4_ERR_FMT_BUFFER_SIZE ? num : C4_ERR_FMT_BUFFER_SIZE;
s_err_callbacks.msg_part(buf, num, s_err_callbacks.user_data);
}
size_t _find_fmt(const char *C4_RESTRICT fmt, size_t len)
{
for(size_t i = 0; i < len; ++i)
{
if(fmt[i] != '{')
{
continue;
}
if(i + 1 == len)
{
break;
}
if(fmt[i+1] == '}')
{
return i;
}
}
return (size_t)-1;
}
void _err_fmt(size_t fmt_size, const char *C4_RESTRICT fmt)
{
s_err_callbacks.msg_part(fmt, fmt_size, s_err_callbacks.user_data);
}
template<class Arg, class ...Args>
void _err_fmt(size_t fmt_size, const char *C4_RESTRICT fmt, Arg const& C4_RESTRICT arg, Args const& C4_RESTRICT ...args)
{
size_t pos = _find_fmt(fmt, fmt_size);
if(pos == (size_t)-1)
{
s_err_callbacks.msg_part(fmt, fmt_size, s_err_callbacks.user_data);
return;
}
s_err_callbacks.msg_part(fmt, pos, s_err_callbacks.user_data);
_err_send(arg);
pos += 2;
_err_fmt(fmt_size - pos, fmt + pos, args...);
}
template<class ...Args>
void err_fmt(locref loc, size_t fmt_size, const char *fmt, Args const& C4_RESTRICT ...args)
{
s_err_callbacks.msg_begin(loc, s_err_callbacks.user_data);
s_err_callbacks.msg("ERROR: ");
_err_fmt(fmt_size, fmt, args...);
s_err_callbacks.msg_end(s_err_callbacks.user_data);
s_err_callbacks.err(loc, s_err_callbacks.user_data);
}
template<class ...Args>
void warn_fmt(locref loc, size_t fmt_size, const char *fmt, Args const& C4_RESTRICT ...args)
{
s_err_callbacks.msg_begin(loc, s_err_callbacks.user_data);
s_err_callbacks.msg("WARNING: ");
_err_fmt(fmt_size, fmt, args...);
s_err_callbacks.msg_end(s_err_callbacks.user_data);
s_err_callbacks.warn(loc, s_err_callbacks.user_data);
}
template<size_t N, class ...Args>
void err_fmt(locref loc, const char (&fmt)[N], Args const& C4_RESTRICT ...args)
{
err_fmt(loc, N-1, fmt, args...);
}
template<size_t N, class ...Args>
void warn_fmt(locref loc, const char (&fmt)[N], Args const& C4_RESTRICT ...args)
{
warn_fmt(loc, N-1, fmt, args...);
}
} // namespace c4
template<size_t N, size_t M, class... Args>
void test_error_fmt_exception(const char (&expected)[M], const char (&fmt)[N], Args const& ...args)
{
INFO("expected is: '" << expected << "'");
{
auto tmp1 = C4_TMP_ERR_BHV(ErrorBehaviorAbort);
{
auto tmp2 = C4_TMP_ERR_BHV(ErrorBehaviorRuntimeError);
{
bool got_exc = false;
C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_buf) == 0))
{
c4::err_fmt(C4_SRCLOC(), fmt, args...);
}
C4_IF_EXCEPTIONS_(catch(std::exception const& e), else)
{
// check that the given message is found verbatim on the error message
c4::csubstr errmsg = c4::to_csubstr(C4_IF_EXCEPTIONS_(e.what(), s_jmp_errmsg.c_str()));
INFO("full message: '''" << errmsg << "'''");
size_t pos = errmsg.find(expected);
CHECK_NE(pos, c4::csubstr::npos);
got_exc = (pos != c4::csubstr::npos);
}
CHECK(got_exc);
}
{
bool got_exc = false;
C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_buf) == 0))
{
c4 ::err_fmt(C4_SRCLOC(), N - 1, fmt, args...);
}
C4_IF_EXCEPTIONS_(catch(std::exception const& e), else)
{
// check that the given message is found verbatim on the error message
c4::csubstr errmsg = c4::to_csubstr(C4_IF_EXCEPTIONS_(e.what(), s_jmp_errmsg.c_str()));
INFO("full message: '''" << errmsg << "'''");
size_t pos = errmsg.find(expected);
CHECK_NE(pos, c4::csubstr::npos);
got_exc = (pos != c4::csubstr::npos);
}
CHECK(got_exc);
}
}
}
}
template<size_t N, size_t M, class... Args>
void test_warning_fmt_exception(const char (&expected)[M], const char (&fmt)[N], Args const& ...args)
{
INFO(expected);
auto tmp = C4_TMP_ERR_BHV(ErrorBehaviorRuntimeError);
auto const& wmsg = tmp.m_tmp.exc_msg;
c4 ::warn_fmt(C4_SRCLOC(), fmt, args...);
REQUIRE_FALSE(wmsg.empty());
REQUIRE_GT(wmsg.size(), M);
auto what = c4::to_csubstr(wmsg.c_str()).last(M-1);
CHECK_EQ(what, expected);
c4 ::warn_fmt(C4_SRCLOC(), N - 1, fmt, args...);
REQUIRE_FALSE(wmsg.empty());
REQUIRE_GT(wmsg.size(), M);
what = c4::to_csubstr(wmsg.c_str()).last(M-1);
CHECK_EQ(what, expected);
}
TEST_CASE("error.fmt")
{
test_error_fmt_exception("abc is 2 is it not?",
"{} is {} is it not?", "abc", 2);
test_error_fmt_exception("abc is bbb is it not?",
"{} is {} is it not?", "abc", "bbb");
test_error_fmt_exception("abc is {} is it not?",
"{} is {} is it not?", "abc");
test_error_fmt_exception("abc is {} is it not?",
"{} is {} is it not?", "abc");
}
TEST_CASE("warning.fmt")
{
test_warning_fmt_exception("abc is 2 is it not?",
"{} is {} is it not?", "abc", 2);
test_warning_fmt_exception("abc is bbb is it not?",
"{} is {} is it not?", "abc", "bbb");
test_warning_fmt_exception("abc is {} is it not?",
"{} is {} is it not?", "abc");
test_warning_fmt_exception("abc is {} is it not?",
"{} is {} is it not?", "abc");
}
C4_SUPPRESS_WARNING_MSVC_POP
#include "c4/libtest/supprwarn_pop.hpp"
|