From 757231cc6e777b8f4717d1467ef7efa01c7fde15 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Wed, 3 Jan 2018 17:41:16 +0000 Subject: Add the fmt library (#4065) * Replaces AppendVPrintf with fmt::sprintf * fmt::ArgList now used as a type safe alternative to varargs. * Removed SIZE_T_FMT compatibility macros. fmt::sprintf is fully portable and supports %zu. * Adds FLOG functions to log with fmt's native formatting style. --- src/Bindings/CMakeLists.txt | 2 +- src/Bindings/LuaState.cpp | 21 +++------------------ src/Bindings/LuaState.h | 3 ++- src/Bindings/ManualBindings.cpp | 8 +++----- src/Bindings/ManualBindings.h | 3 ++- 5 files changed, 11 insertions(+), 26 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt index 004d8be30..7aaa5f3a6 100644 --- a/src/Bindings/CMakeLists.txt +++ b/src/Bindings/CMakeLists.txt @@ -176,5 +176,5 @@ endif() if(NOT MSVC) add_library(Bindings ${SRCS} ${HDRS}) - target_link_libraries(Bindings lua sqlite tolualib mbedtls HTTPServer) + target_link_libraries(Bindings fmt::fmt lua sqlite tolualib mbedtls HTTPServer) endif() diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 095322bdd..f16b77dc8 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -2004,7 +2004,7 @@ void cLuaState::LogStackTrace(lua_State * a_LuaState, int a_StartingDepth) -int cLuaState::ApiParamError(const char * a_MsgFormat, ...) +int cLuaState::ApiParamError(const char * a_MsgFormat, fmt::ArgList argp) { // Retrieve current function name lua_Debug entry; @@ -2012,23 +2012,8 @@ int cLuaState::ApiParamError(const char * a_MsgFormat, ...) VERIFY(lua_getinfo(m_LuaState, "n", &entry)); // Compose the error message: - va_list argp; - va_start(argp, a_MsgFormat); - AString msg; - - #ifdef __clang__ - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wformat-nonliteral" - #endif - - AppendVPrintf(msg, a_MsgFormat, argp); - - #ifdef __clang__ - #pragma clang diagnostic pop - #endif - - va_end(argp); - AString errorMsg = Printf("%s: %s", (entry.name != nullptr) ? entry.name : "", msg.c_str()); + AString msg = Printf(a_MsgFormat, argp); + AString errorMsg = fmt::format("{0}: {1}", (entry.name != nullptr) ? entry.name : "", msg); // Log everything into the console: LOGWARNING("%s", errorMsg.c_str()); diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 60af36228..2510c6f0b 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -823,7 +823,8 @@ public: /** Formats and prints the message, prefixed with the current function name, then logs the stack contents and raises a Lua error. To be used for bindings when they detect bad parameters. Doesn't return, but a dummy return type is provided so that Lua API functions may do "return ApiParamError(...)". */ - int ApiParamError(const char * a_MsgFormat, ...); + int ApiParamError(const char * a_MsgFormat, fmt::ArgList); + FMT_VARIADIC(int, ApiParamError, const char *) /** Returns the type of the item on the specified position in the stack */ AString GetTypeText(int a_StackPos); diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 7dd724d44..4e7d7c8ef 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -116,7 +116,7 @@ int cManualBindings::tolua_do_error(lua_State * L, const char * a_pMsg, tolua_Er -int cManualBindings::lua_do_error(lua_State * L, const char * a_pFormat, ...) +int cManualBindings::lua_do_error(lua_State * L, const char * a_pFormat, fmt::ArgList a_ArgList) { // Retrieve current function name lua_Debug entry; @@ -128,11 +128,9 @@ int cManualBindings::lua_do_error(lua_State * L, const char * a_pFormat, ...) ReplaceString(msg, "#funcname#", (entry.name != nullptr) ? entry.name : "?"); // Copied from luaL_error and modified - va_list argp; - va_start(argp, a_pFormat); luaL_where(L, 1); - lua_pushvfstring(L, msg.c_str(), argp); - va_end(argp); + AString FmtMsg = Printf(msg.c_str(), a_ArgList); + lua_pushlstring(L, FmtMsg.data(), FmtMsg.size()); lua_concat(L, 2); return lua_error(L); } diff --git a/src/Bindings/ManualBindings.h b/src/Bindings/ManualBindings.h index cfdca7597..fb19d5a61 100644 --- a/src/Bindings/ManualBindings.h +++ b/src/Bindings/ManualBindings.h @@ -50,7 +50,8 @@ public: // Helper functions: static cPluginLua * GetLuaPlugin(lua_State * L); static int tolua_do_error(lua_State * L, const char * a_pMsg, tolua_Error * a_pToLuaError); - static int lua_do_error(lua_State * L, const char * a_pFormat, ...); + static int lua_do_error(lua_State * L, const char * a_pFormat, fmt::ArgList a_ArgList); + FMT_VARIADIC(static int, lua_do_error, lua_State *, const char *) /** Binds the DoWith(ItemName) functions of regular classes. */ -- cgit v1.2.3