2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2011-06-21 20:26:32 +10:00
|
|
|
* This file is part of libgim.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
2011-05-23 17:18:52 +10:00
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
2011-05-23 17:18:52 +10:00
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-06-21 20:26:32 +10:00
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2010-2012 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __DEBUG_HPP
|
|
|
|
#define __DEBUG_HPP
|
|
|
|
|
2011-06-16 20:54:34 +10:00
|
|
|
#include "maths.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-03 18:11:17 +10:00
|
|
|
#ifdef ENABLE_DEBUGGING
|
2012-05-15 16:04:35 +10:00
|
|
|
#define DEBUG_ONLY(X) \
|
|
|
|
X;
|
2012-05-03 18:11:17 +10:00
|
|
|
#else
|
|
|
|
#define DEBUG_ONLY(X)
|
|
|
|
#endif
|
2012-04-12 14:06:59 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define EXIT_XSUCCESS 0
|
|
|
|
#define EXIT_XSKIP 77
|
|
|
|
#define EXIT_XHARD_ERROR 99
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define TRACE { \
|
2011-07-16 14:47:10 +10:00
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-15 14:05:43 +11:00
|
|
|
#define WARN(C) do { \
|
2011-11-04 16:50:41 +11:00
|
|
|
if (C) { \
|
2015-01-15 14:05:43 +11:00
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << ", " << #C << std::endl; \
|
2011-11-04 16:50:41 +11:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define VERIFY_SOFT(C, COND) ({ \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_SOFT(__DEBUG_value COND); \
|
2011-08-29 14:32:30 +10:00
|
|
|
__DEBUG_value; \
|
2011-05-23 17:18:52 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2012-05-11 12:34:21 +10:00
|
|
|
#define VERIFY_HARD(C, COND) ({ \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD(__DEBUG_value COND); \
|
2011-08-29 14:32:30 +10:00
|
|
|
__DEBUG_value; \
|
2011-05-23 17:18:52 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define _CHECK_META(C, SUCCESS, FAILURE) do { \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
2014-10-17 19:24:53 +11:00
|
|
|
if (!__DEBUG_value) { \
|
2011-05-23 17:18:52 +10:00
|
|
|
std::cerr << PACKAGE << ": " \
|
|
|
|
<< __FILE__ << ":" \
|
|
|
|
<< __LINE__ << ": " \
|
|
|
|
<< __FUNCTION__ \
|
|
|
|
<< ". Assertion '" << #C \
|
2011-08-29 14:32:30 +10:00
|
|
|
<< "' failed: " << __DEBUG_value << std::endl; \
|
2011-05-23 17:18:52 +10:00
|
|
|
\
|
|
|
|
{ FAILURE } \
|
|
|
|
} else { \
|
|
|
|
{ SUCCESS } \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2012-05-15 16:05:27 +10:00
|
|
|
#if defined(ENABLE_DEBUGGING)
|
|
|
|
#define CHECK_HARD(C) _CHECK_META((C), { ; }, { panic (); })
|
|
|
|
#define CHECK_SOFT(C) _CHECK_META((C), { ; }, { breakpoint (); })
|
|
|
|
#define CHECK(C) CHECK_HARD(C)
|
|
|
|
#else
|
|
|
|
#define CHECK_HARD(C) { };
|
|
|
|
#define CHECK_SOFT(C) { };
|
|
|
|
#define CHECK(C) CHECK_SOFT(C)
|
|
|
|
#endif
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define CHECK_EQ(A,B) do { \
|
2011-06-21 23:39:24 +10:00
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
2012-05-11 12:34:21 +10:00
|
|
|
_CHECK_META (almost_equal (__a, __b), \
|
2011-06-21 23:39:24 +10:00
|
|
|
{ ; }, \
|
|
|
|
{ \
|
2015-01-21 23:33:56 +11:00
|
|
|
std::ostringstream __debug_os; \
|
|
|
|
__debug_os.precision (15); \
|
|
|
|
__debug_os \
|
2013-08-05 21:43:33 +10:00
|
|
|
<< "expected equality.\n" \
|
2011-06-21 23:39:24 +10:00
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n != \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
2015-01-21 23:33:56 +11:00
|
|
|
panic (__debug_os.str ()); \
|
2011-06-21 23:39:24 +10:00
|
|
|
}); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-11-09 15:11:18 +11:00
|
|
|
#define CHECK_LT(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a < __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
2013-08-05 21:43:33 +10:00
|
|
|
std::ostringstream __debug_check_lt_os; \
|
|
|
|
__debug_check_lt_os \
|
|
|
|
<< "expected less than.\n" \
|
2012-11-09 15:11:18 +11:00
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n >= \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
2013-08-05 21:43:33 +10:00
|
|
|
panic (__debug_check_lt_os.str ()); \
|
2012-11-09 15:11:18 +11:00
|
|
|
}); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-02-12 17:05:37 +11:00
|
|
|
#define CHECK_LE(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a <= __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_lt_os; \
|
|
|
|
__debug_check_lt_os \
|
|
|
|
<< "expected less or equal to\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n > \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_lt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-11-09 15:11:18 +11:00
|
|
|
#define CHECK_GT(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a > __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
2013-08-05 21:43:33 +10:00
|
|
|
std::ostringstream __debug_check_gt_os; \
|
|
|
|
__debug_check_gt_os \
|
|
|
|
<< "expected greater than.\n" \
|
2012-11-09 15:11:18 +11:00
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n <= \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
2013-08-05 21:43:33 +10:00
|
|
|
panic (__debug_check_gt_os.str ()); \
|
2012-11-09 15:11:18 +11:00
|
|
|
}); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-02-12 17:05:37 +11:00
|
|
|
#define CHECK_GE(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a >= __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_gt_os; \
|
|
|
|
__debug_check_gt_os \
|
|
|
|
<< "expected greater or equal to.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n < \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_gt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define CHECK_NEQ(A,B) do { \
|
2011-06-21 23:39:24 +10:00
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
2012-05-11 12:34:21 +10:00
|
|
|
_CHECK_META (!almost_equal (__a, __b), \
|
2011-06-21 23:39:24 +10:00
|
|
|
{ ; }, \
|
|
|
|
{ \
|
2013-08-05 21:43:33 +10:00
|
|
|
std::ostringstream __debug_neq_os; \
|
|
|
|
__debug_neq_os << "unexpected equality.\n" \
|
2011-06-21 23:39:24 +10:00
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n == \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
2013-08-05 21:43:33 +10:00
|
|
|
panic (__debug_neq_os.str ()); \
|
2011-06-21 23:39:24 +10:00
|
|
|
}); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define CHECK_THROWS(E,C) do { \
|
2011-05-23 17:18:52 +10:00
|
|
|
bool caught = false; \
|
|
|
|
\
|
|
|
|
try \
|
|
|
|
{ C; } \
|
|
|
|
catch (E) \
|
|
|
|
{ caught = true; } \
|
|
|
|
\
|
2014-10-17 19:24:53 +11:00
|
|
|
if (!caught) \
|
2011-05-23 17:18:52 +10:00
|
|
|
panic ("expected exception: " #E); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-06-20 16:47:48 +10:00
|
|
|
#define CHECK_NOTHROW(C) do { \
|
|
|
|
try { \
|
|
|
|
C; \
|
|
|
|
} catch (...) { \
|
|
|
|
panic ("unexpected exception"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
class panic_error {
|
|
|
|
protected:
|
|
|
|
std::string m_what;
|
|
|
|
|
|
|
|
public:
|
|
|
|
panic_error (const std::string &_what):
|
|
|
|
m_what (_what)
|
|
|
|
{ ; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 19:24:53 +11:00
|
|
|
void panic [[noreturn]] (const std::string&);
|
|
|
|
void panic [[noreturn]] (void);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-10-17 19:24:53 +11:00
|
|
|
void not_implemented [[noreturn]] (void);
|
|
|
|
void not_implemented [[noreturn]] (const char*);
|
2012-06-04 14:50:58 +10:00
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-10-17 19:24:53 +11:00
|
|
|
void unreachable [[noreturn]] (void);
|
|
|
|
void unreachable [[noreturn]] (const std::string&);
|
|
|
|
void unusual (void);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
void breakpoint (void);
|
|
|
|
|
2012-05-01 12:14:25 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-01 12:14:25 +10:00
|
|
|
void enable_fpe (void);
|
|
|
|
void disable_fpe (void);
|
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-08-10 17:40:19 +10:00
|
|
|
namespace debug {
|
|
|
|
void init (void);
|
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __DEBUG_HPP
|