From ad5da0c9246337e23b22afa1ee8f5f8c2be4a048 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 12 Sep 2024 07:06:15 +0100 Subject: [PATCH] Codechange: Use `__VA_OPT__(,)` instead of `, ##` (#12921) `__VA_OPT__` is a C++20 standard, `##` is, apparently, a GNU extension. MSVC needs /Zc:preprocessor adding for whatever reason. --- cmake/CompileFlags.cmake | 3 +++ src/debug.h | 4 ++-- src/error_func.h | 4 ++-- src/newgrf.h | 2 +- src/strgen/strgen.h | 6 +++--- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmake/CompileFlags.cmake b/cmake/CompileFlags.cmake index 8da4be8bab..073c01c2fd 100644 --- a/cmake/CompileFlags.cmake +++ b/cmake/CompileFlags.cmake @@ -8,6 +8,9 @@ macro(compile_flags) # C++11 standard". We need C++11 for the way we use threads. add_compile_options(/Zc:rvalueCast) + # Needed for __VA_OPT__() in macros. + add_compile_options(/Zc:preprocessor) + if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang") add_compile_options( /MP # Enable multi-threaded compilation. diff --git a/src/debug.h b/src/debug.h index c5afb9180a..d9ac37207e 100644 --- a/src/debug.h +++ b/src/debug.h @@ -34,7 +34,7 @@ * @param level The maximum debug level this message should be shown at. When the debug level for this category is set lower, then the message will not be shown. * @param format_string The formatting string of the message. */ -#define Debug(category, level, format_string, ...) do { if ((level) == 0 || _debug_ ## category ## _level >= (level)) DebugPrint(#category, level, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)); } while (false) +#define Debug(category, level, format_string, ...) do { if ((level) == 0 || _debug_ ## category ## _level >= (level)) DebugPrint(#category, level, fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)); } while (false) void DebugPrint(const char *category, int level, const std::string &message); extern int _debug_driver_level; @@ -93,7 +93,7 @@ struct TicToc { }; void ShowInfoI(const std::string &str); -#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) +#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) std::string GetLogPrefix(bool force = false); diff --git a/src/error_func.h b/src/error_func.h index afbe763abe..0f5c9c1d66 100644 --- a/src/error_func.h +++ b/src/error_func.h @@ -14,7 +14,7 @@ [[noreturn]] void UserErrorI(const std::string &str); [[noreturn]] void FatalErrorI(const std::string &str); -#define UserError(format_string, ...) UserErrorI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) -#define FatalError(format_string, ...) FatalErrorI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) +#define UserError(format_string, ...) UserErrorI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) +#define FatalError(format_string, ...) FatalErrorI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) #endif /* ERROR_FUNC_H */ diff --git a/src/newgrf.h b/src/newgrf.h index e49c22ee5c..b08376dc57 100644 --- a/src/newgrf.h +++ b/src/newgrf.h @@ -202,7 +202,7 @@ void ResetNewGRFData(); void ResetPersistentNewGRFData(); void GrfMsgI(int severity, const std::string &msg); -#define GrfMsg(severity, format_string, ...) do { if ((severity) == 0 || _debug_grf_level >= (severity)) GrfMsgI(severity, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)); } while (false) +#define GrfMsg(severity, format_string, ...) do { if ((severity) == 0 || _debug_grf_level >= (severity)) GrfMsgI(severity, fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)); } while (false) bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile); diff --git a/src/strgen/strgen.h b/src/strgen/strgen.h index 99594b2019..3cd83c05cb 100644 --- a/src/strgen/strgen.h +++ b/src/strgen/strgen.h @@ -150,9 +150,9 @@ ParsedCommandStruct ExtractCommandString(const char *s, bool warnings); void StrgenWarningI(const std::string &msg); void StrgenErrorI(const std::string &msg); [[noreturn]] void StrgenFatalI(const std::string &msg); -#define StrgenWarning(format_string, ...) StrgenWarningI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) -#define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) -#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__)) +#define StrgenWarning(format_string, ...) StrgenWarningI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) +#define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) +#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)) char *ParseWord(char **buf); extern const char *_file;