From 7b695dac59d7a5187d5e040cab28b395ca8b74ca Mon Sep 17 00:00:00 2001 From: couriersud Date: Wed, 8 Jan 2020 21:32:34 +0100 Subject: [PATCH] netlist: fix github issue #6106. [Couriersud, Firewave] Workaround for a bug in msvc. __VA_ARGS__ are expanded to a single token in msvc, all other compilers expand to a list of tokens. --- src/lib/netlist/nl_config.h | 2 +- src/lib/netlist/plib/putil.h | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/lib/netlist/nl_config.h b/src/lib/netlist/nl_config.h index aef19f255dc..02b1fafddbb 100644 --- a/src/lib/netlist/nl_config.h +++ b/src/lib/netlist/nl_config.h @@ -21,7 +21,7 @@ /// /// \brief Version - Patch level. /// -#define NL_VERSION_PATCHLEVEL 1 +#define NL_VERSION_PATCHLEVEL 2 /// /// \addtogroup compiledefine diff --git a/src/lib/netlist/plib/putil.h b/src/lib/netlist/plib/putil.h index 22abf637f87..a44ebcacf21 100644 --- a/src/lib/netlist/plib/putil.h +++ b/src/lib/netlist/plib/putil.h @@ -19,11 +19,30 @@ #define PSTRINGIFY_HELP(y) # y #define PSTRINGIFY(x) PSTRINGIFY_HELP(x) -#define PNARGS_(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N -#define PNARGS(...) PNARGS_(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) +// Discussion and background of this MSVC bug: https://github.com/mamedev/mame/issues/6106 +/// +/// \brief Macro to work around a bug in MSVC treatment of __VA_ARGS__ +/// +#define PMSVC_VARARG_BUG(MACRO, ARGS) MACRO ARGS + +/// \brief Determine number of arguments in __VA_ARGS__ +/// +/// This macro works up to 16 arguments in __VA_ARGS__ +/// +/// \returns Number of arguments +/// +#define PNARGS(...) PNARGS_1(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) + +#define PNARGS_2(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N +#define PNARGS_1(...) PMSVC_VARARG_BUG(PNARGS_2, (__VA_ARGS__)) + +/// \brief Concatenate two arguments after expansion +/// +/// \returns Concatenated expanded arguments +/// +#define PCONCAT(a, b) PCONCAT_(a, b) #define PCONCAT_(a, b) a ## b -#define PCONCAT(a, b) PCONCAT_(a, b) #define PSTRINGIFY_1(x) #x #define PSTRINGIFY_2(x, x2) #x, #x2 @@ -42,7 +61,13 @@ #define PSTRINGIFY_15(x, ...) #x, PSTRINGIFY_14(__VA_ARGS__) #define PSTRINGIFY_16(x, ...) #x, PSTRINGIFY_15(__VA_ARGS__) -#define PSTRINGIFY_VA(...) PCONCAT(PSTRINGIFY_, PNARGS(__VA_ARGS__))(__VA_ARGS__) +/// \brief Individually stringify up to 16 arguments +/// +/// PSTRINGIFY_VA(a, b, c) will be expanded to "a", "b", "c" +/// +/// \returns List of stringified individual arguments +/// +#define PSTRINGIFY_VA(...) PMSVC_VARARG_BUG(PCONCAT, (PSTRINGIFY_, PNARGS(__VA_ARGS__)))(__VA_ARGS__) // FIXME:: __FUNCTION__ may be not be supported by all compilers.