From 4e28f43845468f67d607be0d6f3769c1e7d72275 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 12 Jun 2024 16:28:12 +0200 Subject: Replace __FILE__/__LINE__ with std::source_location --- src/debug.h | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/debug.h b/src/debug.h index 99fe48a..e199c3e 100644 --- a/src/debug.h +++ b/src/debug.h @@ -7,14 +7,25 @@ #if HAVE_LUA_ASSERT() -inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_) +// file name, line number, function name +using SourceLocation = std::tuple; +inline SourceLocation Where(std::source_location const& where_ = std::source_location::current()) +{ + std::string_view const _path{ where_.file_name() }; + // drop the directory structure + std::string_view const _fileName{ _path.substr(_path.find_last_of("\\/")+1) }; + std::string_view const _func{ where_.function_name() }; + return std::make_tuple(_fileName, where_.line(), _func); +} + +inline void LUA_ASSERT_IMPL(lua_State* const L_, bool cond_, std::string_view const& txt_, SourceLocation const& where_ = Where()) { if (!cond_) { - raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_); + raise_luaL_error(L_, "%s:%d: LUA_ASSERT '%s' IN %s", std::get<0>(where_).data(), std::get<1>(where_), txt_.data(), std::get<2>(where_).data()); } } -#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_) +#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, #cond_) #define LUA_ASSERT_CODE(code_) code_ class StackChecker @@ -38,23 +49,23 @@ class StackChecker operator int() const { return offset; } }; - StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_) + StackChecker(lua_State* const L_, Relative const offset_, SourceLocation const& where_ = Where()) : L{ L_ } , oldtop{ lua_gettop(L_) - offset_ } { if ((offset_ < 0) || (oldtop < 0)) { assert(false); - raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_); + raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), lua_gettop(L), offset_, std::get<2>(where_).data()); } } - StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_) + StackChecker(lua_State* const L_, Absolute const pos_, SourceLocation const& where_ = Where()) : L{ L_ } , oldtop{ 0 } { if (lua_gettop(L) != pos_) { assert(false); - raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_); + raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), lua_gettop(L), pos_, std::get<2>(where_).data()); } } @@ -66,13 +77,13 @@ class StackChecker } // verify if the distance between the current top and the initial one is what we expect - void check(int expected_, char const* file_, size_t const line_) + void check(int expected_, SourceLocation const& where_ = Where()) { if (expected_ != LUA_MULTRET) { - int const actual{ lua_gettop(L) - oldtop }; - if (actual != expected_) { + int const _actual{ lua_gettop(L) - oldtop }; + if (_actual != expected_) { assert(false); - raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); + raise_luaL_error(L, "%s:%d: STACK CHECK ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), _actual, expected_, std::get<2>(where_).data()); } } } @@ -81,24 +92,24 @@ class StackChecker #define STACK_CHECK_START_REL(L, offset_) \ StackChecker _stackChecker_##L \ { \ - L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ + L, StackChecker::Relative{ offset_ }, \ } #define STACK_CHECK_START_ABS(L, offset_) \ StackChecker _stackChecker_##L \ { \ - L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ + L, StackChecker::Absolute{ offset_ }, \ } #define STACK_CHECK_RESET_REL(L, offset_) \ _stackChecker_##L = StackChecker \ { \ - L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ + L, StackChecker::Relative{ offset_ }, \ } #define STACK_CHECK_RESET_ABS(L, offset_) \ _stackChecker_##L = StackChecker \ { \ - L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ + L, StackChecker::Absolute{ offset_ }, \ } -#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__) +#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_) #else // HAVE_LUA_ASSERT() -- cgit v1.2.3-55-g6feb