Fixed clang compilation and added support for making assertions and erros throw exceptions for easier testing.
This commit is contained in:
@@ -10,6 +10,11 @@
|
||||
|
||||
#include "../internal/common.hpp"
|
||||
|
||||
#if MIJIN_THROWING_ASSERTS
|
||||
#include <sstream>
|
||||
#include <stdexcept> // I'd prefer mijin Exceptions here, but that would result in a circual include :/
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma comment(lib, "kernel32")
|
||||
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
|
||||
@@ -127,6 +132,49 @@ enum class ErrorHandling
|
||||
ABORT = 2
|
||||
};
|
||||
|
||||
#if MIJIN_THROWING_ASSERTS
|
||||
namespace impl
|
||||
{
|
||||
inline std::string formatAssertionExceptionMessage(const char* condition, const char* message, const std::source_location& location)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Assertion failed!\n"
|
||||
<< "Condition: " << condition << "\n"
|
||||
<< "Message: " << message << "\n"
|
||||
<< "Location: " << location.file_name() << ":" << location.line() << ":" << location.column() << "\n"
|
||||
<< "Function: " << location.function_name();
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
inline std::string formatErrorExceptionMessage(const char* message, const std::source_location& location)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Mijin error!\n"
|
||||
<< "Message: " << message << "\n"
|
||||
<< "Location: " << location.file_name() << ":" << location.line() << ":" << location.column() << "\n"
|
||||
<< "Function: " << location.function_name();
|
||||
return oss.str();
|
||||
}
|
||||
}
|
||||
class AssertionException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
AssertionException(const char* condition, const char* message, const std::source_location& location)
|
||||
: std::runtime_error(impl::formatAssertionExceptionMessage(condition, message, location))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class ErrorException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
ErrorException(const char* message, const std::source_location& location)
|
||||
: std::runtime_error(impl::formatErrorExceptionMessage(message, location))
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
@@ -134,9 +182,14 @@ enum class ErrorHandling
|
||||
#ifdef MIJIN_USE_CUSTOM_ASSERTION_HANDLER
|
||||
AssertionResult handleAssert(const char* condition,
|
||||
const char* message, const std::source_location& location);
|
||||
#elif MIJIN_THROWING_ASSERTS
|
||||
inline AssertionResult handleAssert(const char* condition, const char* message, const std::source_location& location)
|
||||
{
|
||||
throw AssertionException(condition, message, location);
|
||||
}
|
||||
#else
|
||||
constexpr AssertionResult handleAssert(const char* /* condition */,
|
||||
const char* /* message */, const std::source_location& /* location */)
|
||||
constexpr AssertionResult handleAssert(const char* /* condition */, const char* /* message */,
|
||||
const std::source_location& /* location */)
|
||||
{
|
||||
return AssertionResult::ERROR;
|
||||
}
|
||||
@@ -147,11 +200,15 @@ ErrorHandling handleError(const char* message, const std::source_location& locat
|
||||
#else
|
||||
inline ErrorHandling handleError(const char* message, const std::source_location& location) MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_THROWING_ASSERTS
|
||||
throw ErrorException(message, location);
|
||||
#else
|
||||
std::puts(message);
|
||||
std::printf("Function: %s\n", location.function_name());
|
||||
std::printf("Location: %s:%d:%d\n", location.file_name(), location.line(), location.column());
|
||||
(void) std::fflush(stdout);
|
||||
return ErrorHandling::TRAP;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user