From 0977fb89b378613fc252cde06721aa29c1345fef Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 12 Apr 2021 16:56:06 +1000 Subject: [PATCH] debug/assert: don't use iostream --- array/parray.cpp | 1 + debug/assert.cpp | 6 +- debug/assert.hpp | 119 +++++++++++----------------------------- hash/murmur/murmur2.cpp | 2 + job/queue.hpp | 2 +- log/sink/console.hpp | 2 + parse/enum.hpp | 3 +- pool.hpp | 1 + rational.cpp | 2 + uri.cpp.rl | 2 + version.cpp.rl | 1 + 11 files changed, 50 insertions(+), 91 deletions(-) diff --git a/array/parray.cpp b/array/parray.cpp index 63c085a7..5acdd9e9 100644 --- a/array/parray.cpp +++ b/array/parray.cpp @@ -12,6 +12,7 @@ #include #include +#include using cruft::parray; diff --git a/debug/assert.cpp b/debug/assert.cpp index dcd60852..0c077e07 100644 --- a/debug/assert.cpp +++ b/debug/assert.cpp @@ -6,4 +6,8 @@ * Copyright 2010-2019 Danny Robson */ -#include "./assert.hpp" \ No newline at end of file +#include "./assert.hpp" + +#include + +#include \ No newline at end of file diff --git a/debug/assert.hpp b/debug/assert.hpp index f867c7d7..04c91cfe 100644 --- a/debug/assert.hpp +++ b/debug/assert.hpp @@ -9,18 +9,19 @@ #pragma once #include "./common.hpp" -#include "./panic.hpp" #include "./debugger.hpp" #include "../platform.hpp" #include "../maths.hpp" - #include -#ifndef NDEBUG -#include -#endif +#include + + +/////////////////////////////////////////////////////////////////////////////// +#define assert_message(MSG) __assert_fail(MSG, __FILE__, __LINE__, __PRETTY_FUNCTION__); + /////////////////////////////////////////////////////////////////////////////// #ifdef COMPILER_GCC @@ -29,7 +30,7 @@ _Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \ DEBUG_ONLY ( \ if (!(C)) \ - panic (#C); \ + assert_message (#C); \ ); \ _Pragma("GCC diagnostic pop") \ } while (0) @@ -37,7 +38,7 @@ #define CHECK(C) do { \ DEBUG_ONLY ( \ if (!(C)) \ - panic (#C); \ + assert_message (#C); \ ); \ } while (0) #else @@ -61,10 +62,8 @@ const auto &__a = (A); \ const auto &__b = (B); \ \ - if (!::cruft::almost_equal (__a, __b)) { \ - std::cerr << "expected equality\n" \ - "__a: " #A " is " << __a << "\n" \ - "__b: " #B " is " << __b << "\n"; \ + if (!::cruft::almost_equal (__a, __b)) { \ + assert_message ("expected: " #A " == " #B); \ breakpoint (); \ } \ ); \ @@ -78,9 +77,7 @@ const auto &__b = (B); \ \ if (__a >= __b) { \ - std::cerr << "expected less than\n" \ - "__a: " << #A << " is " << __a << "\n" \ - "__b: " << #B << " is " << __b << "\n"; \ + assert_message ("expected: " #A " < " #B); \ breakpoint (); \ }; \ ); \ @@ -94,9 +91,7 @@ const auto &__b = (B); \ \ if (!(__a <= __b)) { \ - std::cerr << "expected less than or equal\n" \ - "__a: " << #A << " is " << __a << "\n" \ - "__b: " << #B << " is " << __b << "\n"; \ + assert_message ("expected: " #A " <= " #B); \ breakpoint (); \ } \ ); \ @@ -110,9 +105,7 @@ const auto &__b = (B); \ \ if (__a <= __b) { \ - std::cerr << "expected greater than\n" \ - "__a: " << #A << " is " << __a << "\n" \ - "__b: " << #B << " is " << __b << "\n"; \ + assert_message ("expected: " #A " > " #B); \ breakpoint (); \ } \ ); \ @@ -126,9 +119,7 @@ const auto &__b = (B); \ \ if (__a < __b) { \ - std::cerr << "expected greater or equal\n" \ - "__a: " << #A << " is " << __a << "\n" \ - "__b: " << #B << " is " << __b << "\n"; \ + assert_message ("expected: " #A " >= " #B); \ breakpoint (); \ }; \ ); \ @@ -143,10 +134,7 @@ const auto &__h = (H); \ \ if (__v < __l || __v > __h) { \ - std::cerr << "expected inclusive\n" \ - "__l: " << #L << " is " << +__l << "\n" \ - "__h: " << #H << " is " << +__h << "\n" \ - "__v: " << #V << " is " << +__v << "\n"; \ + assert_message ("expected: " #L " <= " #V " <= " #H); \ breakpoint (); \ }; \ ); \ @@ -163,9 +151,7 @@ _Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \ \ if (intmax_t (__v) < 0 || __v >= __h) { \ - std::cerr << "expected index\n" \ - "__h: " << #H << " is " << +__h << "\n" \ - "__v: " << #V << " is " << +__v << "\n"; \ + assert_message ("expected: 0 <= " #V " <= " #H); \ breakpoint (); \ }; \ \ @@ -180,10 +166,8 @@ const auto &__a = (A); \ const auto &__b = (B); \ \ - if (::cruft::almost_equal (__a, __b)) { \ - std::cerr << "expected inequality\n" \ - "__a: " << #A << " is " << __a << "\n" \ - "__b: " << #B << " is " << __b << "\n"; \ + if (::cruft::almost_equal (__a, __b)) { \ + assert_message ("expected: " #A " != " #B); \ breakpoint (); \ }; \ ); \ @@ -195,9 +179,8 @@ DEBUG_ONLY ( \ const auto &__a = (A); \ \ - if (!::cruft::almost_zero (__a)) { \ - std::cerr << "expected zero\n" \ - "__a: " << #A << " is " << __a << "\n"; \ + if (!::cruft::almost_zero (__a)) { \ + assert_message ("expected: " #A " == 0"); \ breakpoint (); \ }; \ ); \ @@ -209,9 +192,8 @@ DEBUG_ONLY ( \ const auto &__a = (A); \ \ - if (::cruft::exactly_zero (__a)) { \ - std::cerr << "expected non-zero\n" \ - "__a: " << #A << " is " << __a << '\n'; \ + if (::cruft::exactly_zero (__a)) { \ + assert_message ("expected: " #A " != 0"); \ breakpoint (); \ } \ ); \ @@ -224,10 +206,8 @@ const auto &__check_mod_v = (V); \ const auto &__check_mod_m = (M); \ \ - if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \ - std::cerr << "expected zero modulus\n" \ - "__v: " << #V << " is " << __check_mod_v << "\n" \ - "__m: " << #M << " is " << __check_mod_m << "\n"; \ + if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \ + assert_message ("expected: " #V " % " #M " == 0"); \ breakpoint (); \ } \ ); \ @@ -243,8 +223,7 @@ if (std::find (std::cbegin (__e), \ std::cend (__e), \ __c) == std::end (__e)) { \ - std::cerr << "expect enum\n" \ - "__c: " << #C << " is " << __c << '\n'; \ + assert_message ("expected: " #C " in " #__VA_ARGS__); \ breakpoint (); \ } \ } while (0) @@ -253,51 +232,15 @@ #endif +//----------------------------------------------------------------------------- #if !defined(NDEBUG) #define CHECK_FINITE(V) do { \ - const auto &__v = (V); \ - if (!std::isfinite (__v)) { \ - std::cerr << "expected finite value\n" \ - "__v: " << #V << " is " << __v << '\n'; \ - breakpoint (); \ - } \ + const auto &__v = (V); \ + if (!std::isfinite (__v)) { \ + assert_message ("expected: " #V " is finite"); \ + breakpoint (); \ + } \ } while (0) #else #define CHECK_FINITE(V,...) #endif - - - - -/////////////////////////////////////////////////////////////////////////////// -#define CHECK_THROWS(E,C) do { \ - DEBUG_ONLY ( \ - bool caught = false; \ - \ - try \ - { C; } \ - catch (E const&) \ - { caught = true; } \ - \ - if (!caught) { \ - std::cerr << "expected exception: " << #E << '\n' \ - breakpoint (); \ - } \ - ); \ -} while (0) - - -/////////////////////////////////////////////////////////////////////////////// -#define CHECK_NOTHROW(C) do { \ - DEBUG_ONLY ( \ - try { \ - C; \ - } catch (const std::exception &e) { \ - std::cerr << "unexpected exception: " << e.what () << '\n'; \ - breakpoint (); \ - } catch (...) { \ - std::cerr << "unexpected exception: unknown\n"; \ - breakpoint (); \ - } \ - ); \ -} while (0) diff --git a/hash/murmur/murmur2.cpp b/hash/murmur/murmur2.cpp index fa4cd066..c9fa6ebd 100644 --- a/hash/murmur/murmur2.cpp +++ b/hash/murmur/murmur2.cpp @@ -11,6 +11,8 @@ #include "common.hpp" +#include "../../debug/panic.hpp" + using cruft::hash::murmur2; diff --git a/job/queue.hpp b/job/queue.hpp index 9c668f44..dfca8bb1 100644 --- a/job/queue.hpp +++ b/job/queue.hpp @@ -15,8 +15,8 @@ #include "../thread/monitor.hpp" #include "../thread/semaphore.hpp" #include "../thread/ticketlock.hpp" - #include "../parallel/queue.hpp" +#include "../debug/panic.hpp" #include #include diff --git a/log/sink/console.hpp b/log/sink/console.hpp index 9b1c2ed6..dc4f9fe6 100644 --- a/log/sink/console.hpp +++ b/log/sink/console.hpp @@ -10,6 +10,8 @@ #include "base.hpp" +#include "../../debug/panic.hpp" + namespace cruft::log::sink { class console : public crtp { public: diff --git a/parse/enum.hpp b/parse/enum.hpp index 90004bd7..5e34f5d2 100644 --- a/parse/enum.hpp +++ b/parse/enum.hpp @@ -10,10 +10,11 @@ #include "fwd.hpp" -#include "../view.hpp" +#include "../debug/panic.hpp" #include "../introspection/name.hpp" #include "../log.hpp" #include "../typeidx.hpp" +#include "../view.hpp" #include diff --git a/pool.hpp b/pool.hpp index a9f12e22..1ffcf0d3 100644 --- a/pool.hpp +++ b/pool.hpp @@ -10,6 +10,7 @@ #include "cast.hpp" #include "debug/assert.hpp" +#include "debug/panic.hpp" #include "parallel/stack.hpp" #include "view.hpp" diff --git a/rational.cpp b/rational.cpp index ae0b66fd..762b724a 100644 --- a/rational.cpp +++ b/rational.cpp @@ -11,6 +11,8 @@ #include "maths.hpp" #include "debug/assert.hpp" +#include + #include using cruft::rational; diff --git a/uri.cpp.rl b/uri.cpp.rl index bb2801f2..17573be4 100644 --- a/uri.cpp.rl +++ b/uri.cpp.rl @@ -8,6 +8,8 @@ #include "uri.hpp" +#include "debug/panic.hpp" + #include #include diff --git a/version.cpp.rl b/version.cpp.rl index 159b05ce..8a07211e 100644 --- a/version.cpp.rl +++ b/version.cpp.rl @@ -8,6 +8,7 @@ #include "version.hpp" #include "maths.hpp" +#include "debug/panic.hpp" #include #include