aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 09:51:45 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 09:51:45 +0200
commit57ca292c8844e566184e3f7e5c98fa98991684bd (patch)
tree9356cf33935ac4dc130cb71ba701d8d9c286597d
parent91a34bd09900967e8b9cccdbd6d94a9f8cc8506c (diff)
downloadlanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.gz
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.bz2
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.zip
Reorganized debug stuff
-rw-r--r--src/debug.h112
-rw-r--r--src/lanesconf.h7
-rw-r--r--src/luaerrors.h57
-rw-r--r--src/macros_and_utils.h154
4 files changed, 179 insertions, 151 deletions
diff --git a/src/debug.h b/src/debug.h
new file mode 100644
index 0000000..a0a4b8d
--- /dev/null
+++ b/src/debug.h
@@ -0,0 +1,112 @@
1#pragma once
2
3#include "lanesconf.h"
4#include "luaerrors.h"
5
6// #################################################################################################
7
8#if HAVE_LUA_ASSERT()
9
10inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_)
11{
12 if (!cond_) {
13 raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_);
14 }
15}
16
17#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_)
18
19class StackChecker
20{
21 private:
22 lua_State* const L;
23 int oldtop;
24
25 public:
26 struct Relative
27 {
28 int const offset;
29
30 operator int() const { return offset; }
31 };
32
33 struct Absolute
34 {
35 int const offset;
36
37 operator int() const { return offset; }
38 };
39
40 StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_)
41 : L{ L_ }
42 , oldtop{ lua_gettop(L_) - offset_ }
43 {
44 if ((offset_ < 0) || (oldtop < 0)) {
45 assert(false);
46 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_);
47 }
48 }
49
50 StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_)
51 : L{ L_ }
52 , oldtop{ 0 }
53 {
54 if (lua_gettop(L) != pos_) {
55 assert(false);
56 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_);
57 }
58 }
59
60 StackChecker& operator=(StackChecker const& rhs_)
61 {
62 assert(L == rhs_.L);
63 oldtop = rhs_.oldtop;
64 return *this;
65 }
66
67 // verify if the distance between the current top and the initial one is what we expect
68 void check(int expected_, char const* file_, size_t const line_)
69 {
70 if (expected_ != LUA_MULTRET) {
71 int const actual{ lua_gettop(L) - oldtop };
72 if (actual != expected_) {
73 assert(false);
74 raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_);
75 }
76 }
77 }
78};
79
80#define STACK_CHECK_START_REL(L, offset_) \
81 StackChecker _stackChecker_##L \
82 { \
83 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
84 }
85#define STACK_CHECK_START_ABS(L, offset_) \
86 StackChecker _stackChecker_##L \
87 { \
88 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
89 }
90#define STACK_CHECK_RESET_REL(L, offset_) \
91 _stackChecker_##L = StackChecker \
92 { \
93 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
94 }
95#define STACK_CHECK_RESET_ABS(L, offset_) \
96 _stackChecker_##L = StackChecker \
97 { \
98 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
99 }
100#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__)
101
102#else // HAVE_LUA_ASSERT()
103
104#define LUA_ASSERT(L_, c) nullptr // nothing
105
106#define STACK_CHECK_START_REL(L_, offset_)
107#define STACK_CHECK_START_ABS(L_, offset_)
108#define STACK_CHECK_RESET_REL(L_, offset_)
109#define STACK_CHECK_RESET_ABS(L_, offset_)
110#define STACK_CHECK(L_, offset_)
111
112#endif // HAVE_LUA_ASSERT()
diff --git a/src/lanesconf.h b/src/lanesconf.h
index 938d743..f20dfff 100644
--- a/src/lanesconf.h
+++ b/src/lanesconf.h
@@ -41,5 +41,12 @@
41#endif // __cplusplus 41#endif // __cplusplus
42#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 42#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
43 43
44// kind of MSVC-specific
45#ifdef _DEBUG
46#define HAVE_LUA_ASSERT() 1
47#else // NDEBUG
48#define HAVE_LUA_ASSERT() 0
49#endif // NDEBUG
50
44#define USE_DEBUG_SPEW() 0 51#define USE_DEBUG_SPEW() 0
45#define HAVE_DECODA_SUPPORT() 0 52#define HAVE_DECODA_SUPPORT() 0
diff --git a/src/luaerrors.h b/src/luaerrors.h
new file mode 100644
index 0000000..9399427
--- /dev/null
+++ b/src/luaerrors.h
@@ -0,0 +1,57 @@
1#pragma once
2
3#ifdef __cplusplus
4extern "C"
5{
6#endif // __cplusplus
7#include "lua.h"
8#include "lualib.h"
9#include "lauxlib.h"
10#ifdef __cplusplus
11}
12#endif // __cplusplus
13
14#include <cassert>
15#include <string_view>
16#include <tuple>
17
18// #################################################################################################
19
20// use this instead of Lua's lua_error
21[[noreturn]] static inline void raise_lua_error(lua_State* const L_)
22{
23 std::ignore = lua_error(L_); // doesn't return
24 assert(false); // we should never get here, but i'm paranoid
25}
26
27// #################################################################################################
28
29// use this instead of Lua's luaL_error
30template <typename... ARGS>
31[[noreturn]] static inline void raise_luaL_error(lua_State* const L_, std::string_view const& fmt_, ARGS... args_)
32{
33 std::ignore = luaL_error(L_, fmt_.data(), std::forward<ARGS>(args_)...); // doesn't return
34 assert(false); // we should never get here, but i'm paranoid
35}
36
37// #################################################################################################
38
39// use this instead of Lua's luaL_argerror
40template <typename... ARGS>
41[[noreturn]] static inline void raise_luaL_argerror(lua_State* const L_, int const arg_, std::string_view const& extramsg_)
42{
43 std::ignore = luaL_argerror(L_, arg_, extramsg_.data()); // doesn't return
44 assert(false); // we should never get here, but i'm paranoid
45}
46
47// #################################################################################################
48
49#if LUA_VERSION_NUM >= 504
50// use this instead of Lua's luaL_typeerror
51template <typename... ARGS>
52[[noreturn]] static inline void raise_luaL_typeerror(lua_State* const L_, int const arg_, std::string_view const& tname_)
53{
54 std::ignore = luaL_typeerror(L_, arg_, tname_.data()); // doesn't return
55 assert(false); // we should never get here, but i'm paranoid
56}
57#endif // LUA_VERSION_NUM
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index 926e756..d540cdb 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -11,164 +11,16 @@ extern "C"
11} 11}
12#endif // __cplusplus 12#endif // __cplusplus
13 13
14#include <cassert> 14#include "debug.h"
15#include "luaerrors.h"
16
15#include <chrono> 17#include <chrono>
16#include <tuple>
17#include <type_traits> 18#include <type_traits>
18 19
19using namespace std::chrono_literals; 20using namespace std::chrono_literals;
20 21
21// ################################################################################################# 22// #################################################################################################
22 23
23// use this instead of Lua's lua_error
24[[noreturn]] static inline void raise_lua_error(lua_State* const L_)
25{
26 std::ignore = lua_error(L_); // doesn't return
27 assert(false); // we should never get here, but i'm paranoid
28}
29
30// #################################################################################################
31
32// use this instead of Lua's luaL_error
33template <typename... ARGS>
34[[noreturn]] static inline void raise_luaL_error(lua_State* const L_, std::string_view const& fmt_, ARGS... args_)
35{
36 std::ignore = luaL_error(L_, fmt_.data(), std::forward<ARGS>(args_)...); // doesn't return
37 assert(false); // we should never get here, but i'm paranoid
38}
39
40// #################################################################################################
41
42// use this instead of Lua's luaL_argerror
43template <typename... ARGS>
44[[noreturn]] static inline void raise_luaL_argerror(lua_State* const L_, int const arg_, std::string_view const& extramsg_)
45{
46 std::ignore = luaL_argerror(L_, arg_, extramsg_.data()); // doesn't return
47 assert(false); // we should never get here, but i'm paranoid
48}
49
50// #################################################################################################
51
52#if LUA_VERSION_NUM >= 504
53// use this instead of Lua's luaL_typeerror
54template <typename... ARGS>
55[[noreturn]] static inline void raise_luaL_typeerror(lua_State* const L_, int const arg_, std::string_view const& tname_)
56{
57 std::ignore = luaL_typeerror(L_, arg_, tname_.data()); // doesn't return
58 assert(false); // we should never get here, but i'm paranoid
59}
60#endif // LUA_VERSION_NUM
61
62// #################################################################################################
63
64#ifdef NDEBUG
65
66#define LUA_ASSERT(L_, c) ; // nothing
67
68#define STACK_CHECK_START_REL(L_, offset_)
69#define STACK_CHECK_START_ABS(L_, offset_)
70#define STACK_CHECK_RESET_REL(L_, offset_)
71#define STACK_CHECK_RESET_ABS(L_, offset_)
72#define STACK_CHECK(L_, offset_)
73
74#else // NDEBUG
75
76inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_)
77{
78 if (!cond_) {
79 raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_);
80 }
81}
82
83#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_)
84
85class StackChecker
86{
87 private:
88 lua_State* const L;
89 int oldtop;
90
91 public:
92 struct Relative
93 {
94 int const offset;
95
96 operator int() const { return offset; }
97 };
98
99 struct Absolute
100 {
101 int const offset;
102
103 operator int() const { return offset; }
104 };
105
106 StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_)
107 : L{ L_ }
108 , oldtop{ lua_gettop(L_) - offset_ }
109 {
110 if ((offset_ < 0) || (oldtop < 0)) {
111 assert(false);
112 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_);
113 }
114 }
115
116 StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_)
117 : L{ L_ }
118 , oldtop{ 0 }
119 {
120 if (lua_gettop(L) != pos_) {
121 assert(false);
122 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_);
123 }
124 }
125
126 StackChecker& operator=(StackChecker const& rhs_)
127 {
128 assert(L == rhs_.L);
129 oldtop = rhs_.oldtop;
130 return *this;
131 }
132
133 // verify if the distance between the current top and the initial one is what we expect
134 void check(int expected_, char const* file_, size_t const line_)
135 {
136 if (expected_ != LUA_MULTRET) {
137 int const actual{ lua_gettop(L) - oldtop };
138 if (actual != expected_) {
139 assert(false);
140 raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_);
141 }
142 }
143 }
144};
145
146#define STACK_CHECK_START_REL(L, offset_) \
147 StackChecker _stackChecker_##L \
148 { \
149 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
150 }
151#define STACK_CHECK_START_ABS(L, offset_) \
152 StackChecker _stackChecker_##L \
153 { \
154 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
155 }
156#define STACK_CHECK_RESET_REL(L, offset_) \
157 _stackChecker_##L = StackChecker \
158 { \
159 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
160 }
161#define STACK_CHECK_RESET_ABS(L, offset_) \
162 _stackChecker_##L = StackChecker \
163 { \
164 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
165 }
166#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__)
167
168#endif // NDEBUG
169
170// #################################################################################################
171
172inline void STACK_GROW(lua_State* L_, int n_) 24inline void STACK_GROW(lua_State* L_, int n_)
173{ 25{
174 if (!lua_checkstack(L_, n_)) { 26 if (!lua_checkstack(L_, n_)) {