mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
esp_macro: Fix CHOOSE_MACRO_VA_ARG() for C++20
Closes https://github.com/espressif/esp-idf/pull/8372 Closes https://github.com/espressif/esp-idf/issues/8371 [darian@espressif.com: Updated macro description and commit message] Signed-off-by: Darian Leung <darian@espressif.com>
This commit is contained in:
parent
8b902739ac
commit
216703c0ef
@ -22,24 +22,53 @@ extern "C" {
|
||||
*
|
||||
* "CHOOSE_MACRO_VA_ARG" make use of the fact that "##__VA_ARGS__," will eliminate the trailing comma if there are no
|
||||
* arguments, thus allows subsequent arguments in "CHOOSE_MACRO_VA_ARG" to be left shifted in the parameter list.
|
||||
* Therefore, if we call "CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, MACRO1, MACRO2)(__VA_ARGS__)", the result will be:
|
||||
*
|
||||
* - MACRO1(__VA_ARGS__) if __VA_ARGS__ was not empty
|
||||
* - MACRO2() if __VA_ARGS__ was empty
|
||||
* Therefore, if we call the following:
|
||||
* - CHOOSE_MACRO_VA_ARG(MACRO_ARGS, MACRO_NO_ARGS, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
*
|
||||
* The result will be:
|
||||
* - MACRO_ARGS(__VA_ARGS__) if __VA_ARGS__ was not empty
|
||||
* - MACRO_NO_ARGS() if __VA_ARGS__ was empty
|
||||
*
|
||||
* @note In the future, we want to switch to C++20. We also want to become compatible with clang. Hence, we provide two
|
||||
* versions of the following macros which are using variadic arguments. The first one is using the GNU extension
|
||||
* ##__VA_ARGS__. The second one is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with
|
||||
* standard C++20 enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using
|
||||
* ##__VA_ARGS__.
|
||||
* versions of the following macros which are using variadic arguments. One is using the GNU extension ##__VA_ARGS__.
|
||||
* The other is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with standard C++20
|
||||
* enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using ##__VA_ARGS__.
|
||||
*/
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define CHOOSE_MACRO_VA_ARG(_0 __VA_OPT__(,) __VA_ARGS__, MACRO, ...) MACRO
|
||||
#define CHOOSE_MACRO_VA_ARG_INN_IMPL(...) __VA_OPT__(0)
|
||||
#define CHOOSE_MACRO_VA_ARG_INN(one, MACRO1, MACRO2, ...) MACRO1
|
||||
#define CHOOSE_MACRO_VA_ARG(MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, ...) CHOOSE_MACRO_VA_ARG_INN(CHOOSE_MACRO_VA_ARG_INN_IMPL(__VA_ARGS__) __VA_OPT__(,) MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, 0)
|
||||
#else
|
||||
#define CHOOSE_MACRO_VA_ARG(_0, _1, MACRO, ...) MACRO
|
||||
#define CHOOSE_MACRO_VA_ARG_INN(one, two, MACRO1, MACRO2, ...) MACRO1
|
||||
#define CHOOSE_MACRO_VA_ARG(MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, ...) CHOOSE_MACRO_VA_ARG_INN(0, ##__VA_ARGS__, MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, 0)
|
||||
#endif
|
||||
_Static_assert(CHOOSE_MACRO_VA_ARG(_0, x, 0, 1) == 0, "CHOOSE_MACRO_VA_ARG() result does not match for 0 arguments");
|
||||
_Static_assert(CHOOSE_MACRO_VA_ARG(_0, 0, 1) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for 1 argument");
|
||||
|
||||
/* test macros */
|
||||
#define foo_args(...) 1
|
||||
#define foo_no_args() 2
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
#define MY_STATIC_ASSERT static_assert
|
||||
#else
|
||||
#define MY_STATIC_ASSERT _Static_assert
|
||||
#endif
|
||||
|
||||
MY_STATIC_ASSERT(foo() == 2, "CHOOSE_MACRO_VA_ARG() result does not match for 0 arguments");
|
||||
MY_STATIC_ASSERT(foo(42) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for 1 argument");
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
static_assert(foo(42, 87) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for n arguments");
|
||||
#endif
|
||||
|
||||
#undef MY_STATIC_ASSERT
|
||||
#undef foo
|
||||
#undef foo_args
|
||||
#undef foo_no_args
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -170,8 +170,13 @@ extern void vTaskExitCritical( void );
|
||||
#define portENTER_CRITICAL_SMP() vTaskEnterCritical();
|
||||
#define portEXIT_CRITICAL_SMP() vTaskExitCritical();
|
||||
|
||||
#define portENTER_CRITICAL(...) CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, portENTER_CRITICAL_IDF, portENTER_CRITICAL_SMP)(__VA_ARGS__)
|
||||
#define portEXIT_CRITICAL(...) CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, portEXIT_CRITICAL_IDF, portEXIT_CRITICAL_SMP)(__VA_ARGS__)
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define portENTER_CRITICAL(...) CHOOSE_MACRO_VA_ARG(portENTER_CRITICAL_IDF, portENTER_CRITICAL_SMP __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#define portEXIT_CRITICAL(...) CHOOSE_MACRO_VA_ARG(portEXIT_CRITICAL_IDF, portEXIT_CRITICAL_SMP __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define portENTER_CRITICAL(...) CHOOSE_MACRO_VA_ARG(portENTER_CRITICAL_IDF, portENTER_CRITICAL_SMP, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#define portEXIT_CRITICAL(...) CHOOSE_MACRO_VA_ARG(portEXIT_CRITICAL_IDF, portEXIT_CRITICAL_SMP, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#define portSET_INTERRUPT_MASK_FROM_ISR() ({ \
|
||||
unsigned int cur_level; \
|
||||
@ -188,7 +193,11 @@ extern void vTaskExitCritical( void );
|
||||
// ---------------------- Yielding -------------------------
|
||||
|
||||
#define portYIELD() vPortYield()
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, portYIELD_FROM_ISR_CHECK, portYIELD_FROM_ISR_NO_CHECK)(__VA_ARGS__)
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_CHECK, portYIELD_FROM_ISR_NO_CHECK __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_CHECK, portYIELD_FROM_ISR_NO_CHECK, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#endif
|
||||
#define portYIELD_CORE(x) vPortYieldCore(x)
|
||||
|
||||
// ----------------------- System --------------------------
|
||||
|
@ -391,9 +391,9 @@ static inline BaseType_t IRAM_ATTR xPortGetCoreID(void)
|
||||
* might result in undesired behavior
|
||||
*/
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(_0 __VA_OPT__(,) ##__VA_ARGS__, portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG)(__VA_ARGS__)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG)(__VA_ARGS__)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -524,9 +524,9 @@ extern void _frxt_setup_switch( void ); //Defined in portasm.S
|
||||
* might result in undesired behavior
|
||||
*/
|
||||
#if defined(__cplusplus) && (__cplusplus > 201703L)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(_0 __VA_OPT__(,) ##__VA_ARGS__, portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG)(__VA_ARGS__)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(_0, ##__VA_ARGS__, portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG)(__VA_ARGS__)
|
||||
#define portYIELD_FROM_ISR(...) CHOOSE_MACRO_VA_ARG(portYIELD_FROM_ISR_ARG, portYIELD_FROM_ISR_NO_ARG, ##__VA_ARGS__)(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
/* Yielding within an API call (when interrupts are off), means the yield should be delayed
|
||||
|
Loading…
Reference in New Issue
Block a user