From 87dad4adc5bd4791e8d1570c5f2ab3e0b41f9c98 Mon Sep 17 00:00:00 2001 From: Michal Jerzy Wierzbicki Date: Tue, 6 Nov 2018 02:11:08 +0100 Subject: [PATCH] preproc: META_COUNT_VARARGS_BEFORE_COMPILE: bugfix for tokens starting with parenthesis not being counted ; also introduced unit tests to check the issue Signed-off-by: Michal Jerzy Wierzbicki --- src/include/sof/preproc-private.h | 6 ++-- src/include/sof/preproc.h | 9 +++-- test/cmocka/src/debugability/macros.c | 32 +++++++++-------- test/cmocka/src/lib/preproc/varargs_count.c | 38 ++++++++++++++------- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/include/sof/preproc-private.h b/src/include/sof/preproc-private.h index eae699cab..5e670e598 100644 --- a/src/include/sof/preproc-private.h +++ b/src/include/sof/preproc-private.h @@ -65,7 +65,7 @@ * META_COUNT_VARAGS_BEFORE_COMPILE(A,B,C,D) evaluates to 4 */ #define _META_PP_NARG_BEFORE_COMPILE_(...) \ - _META_PP_ARG_N(__VA_ARGS__) + _META_PP_ARG_N(__VA_ARGS__) #define _META_PP_ARG_N(\ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ @@ -224,7 +224,7 @@ /* used by macro MAP, don't use on its own */ #define _META_MAP_BODY(arg_count, m, ...)\ META_IF_ELSE(META_COUNT_VARAGS_BEFORE_COMPILE(__VA_ARGS__))(\ - META_DEFER(2, _META_MAP)()\ + _META_DEFER_2(_META_MAP)()\ (arg_count, m, __VA_ARGS__)\ )() @@ -246,7 +246,7 @@ /* used by macro MAP_AGGREGATE, don't use on its own */ #define _META_MAP_AGGREGATE_BODY(arg_count, m, aggr, ...)\ META_IF_ELSE(META_COUNT_VARAGS_BEFORE_COMPILE(__VA_ARGS__))(\ - META_DEFER(2, _META_MAP_AGGREGATE)()\ + _META_DEFER_2(_META_MAP_AGGREGATE)()\ (arg_count, m, aggr, __VA_ARGS__)\ )(aggr) diff --git a/src/include/sof/preproc.h b/src/include/sof/preproc.h index 8e6180e12..2bb28d26c 100644 --- a/src/include/sof/preproc.h +++ b/src/include/sof/preproc.h @@ -46,11 +46,10 @@ * META_COUNT_VARAGS_BEFORE_COMPILE(A,B,C,D) evaluates to 4 */ #define META_COUNT_VARAGS_BEFORE_COMPILE(...)\ - META_IF_ELSE(META_HAS_ARGS(__VA_ARGS__)) (\ - _META_PP_NARG_BEFORE_COMPILE_(__VA_ARGS__, _META_PP_RSEQ_N())\ - )\ - (\ - 0\ + META_DEC(\ + _META_PP_NARG_BEFORE_COMPILE_(\ + _, ##__VA_ARGS__, _META_PP_RSEQ_N()\ + )\ ) /* treat x as string while forcing x expansion beforehand */ diff --git a/test/cmocka/src/debugability/macros.c b/test/cmocka/src/debugability/macros.c index dcda29922..8b4fe50ee 100644 --- a/test/cmocka/src/debugability/macros.c +++ b/test/cmocka/src/debugability/macros.c @@ -45,7 +45,11 @@ static void test_debugability_macros_declare_log_entry(void **state) { const char *macro_result = CAPTURE(_DECLARE_LOG_ENTRY( - LOG_LEVEL_CRITICAL, "Message", TRACE_CLASS_DMA, 1)); + LOG_LEVEL_CRITICAL, + "Message", + TRACE_CLASS_DMA, + 1 + )); const char *should_be_eq = "__attribute__((section(\".static_log.\"" " \"LOG_LEVEL_CRITICAL\"))) " @@ -53,7 +57,8 @@ static void test_debugability_macros_declare_log_entry(void **state) "uint32_t params_num; uint32_t line_idx; uint32_t file_name_len; " "const char file_name[sizeof(\"src/debugability/macros.c\")]; " "uint32_t text_len; const char text[sizeof(\"Message\")]; } " - "log_entry = { 1"; + "log_entry = { 1(6 << 24)152sizeof(\"src/debugability/macros.c\")" + "\"src/debugability/macros.c\"sizeof(\"Message\")\"Message\" }"; (void)state; assert_string_equal(macro_result, should_be_eq); @@ -78,32 +83,29 @@ static char *get_should_be(const int param_count) { char *result = malloc(sizeof(char) * 1024); char *paramlist = get_param_list(param_count); - char *maybe_space = " "; char *maybe_comma = ","; - if (!param_count) - maybe_space = ""; - else + if (param_count) maybe_comma = ""; - /* which format: 0 1 2 3 4 5 6 7 8 9 A*/ - sprintf(result, "%s%d%s%d%s%s%d%s%s%s%s", + /* which format: 0 1 2 3 4 5 6 7 8 9*/ + sprintf(result, "%s%d%s%d%s%d%s%s%s%s", /*0*/"{ __attribute__((unused)) typedef char assertion_failed_" META_QUOTE(BASE_LOG_ASSERT_FAIL_MSG) "[(", /*1*/_TRACE_EVENT_MAX_ARGUMENT_COUNT, /*2*/" >= ", /*3*/param_count, - /*4*/maybe_space, - /*5*/") ? 1 : -1]; log_func log_function = (log_func)& _trace_event", - /*6*/param_count, - /*7*/"; log_function(&log_entry", - /*8*/maybe_comma, - /*9*/paramlist, - /*A*/");}" + /*4*/") ? 1 : -1]; log_func log_function = (log_func)& _trace_event", + /*5*/param_count, + /*6*/"; log_function(&log_entry", + /*7*/maybe_comma, + /*8*/paramlist, + /*9*/");}" ); if (paramlist) free(paramlist); + // TODO: maybe remove all whitespace chars; they're not important here return result; } diff --git a/test/cmocka/src/lib/preproc/varargs_count.c b/test/cmocka/src/lib/preproc/varargs_count.c index b67983b2e..a00134aee 100644 --- a/test/cmocka/src/lib/preproc/varargs_count.c +++ b/test/cmocka/src/lib/preproc/varargs_count.c @@ -51,29 +51,42 @@ static void META_CONCAT_SEQ_DELIM_(prefix, test_func, postfix)(void **state)\ #define A 1 #define B 2 #define C 3 +#define PARENTHESIS_PRE() (1+3)/2 +#define PARENTHESIS_POST() 4/(3-1) #define META_NAME META_COUNT_VARAGS_BEFORE_COMPILE -TEST_HERE_DECLARE(META_NAME, 0, 0, ) -TEST_HERE_DECLARE(META_NAME, 1, 1, A ) -TEST_HERE_DECLARE(META_NAME, 3, 3, A, B, C) -TEST_HERE_DECLARE(PP_NARG , 0, 0, ) -TEST_HERE_DECLARE(PP_NARG , 1, 1, A ) -TEST_HERE_DECLARE(PP_NARG , 3, 3, A, B, C) +#define TEST_HERE_DECLARE_GROUP(func_name)\ + TEST_HERE_DECLARE(func_name, 0, 0, )\ + TEST_HERE_DECLARE(func_name, 1, 1, A )\ + TEST_HERE_DECLARE(func_name, 3, 3, A, B, C)\ + TEST_HERE_DECLARE(func_name, with_parenthesis_pre, 1,\ + PARENTHESIS_PRE())\ + TEST_HERE_DECLARE(func_name, with_parenthesis_post, 1,\ + PARENTHESIS_POST()) +TEST_HERE_DECLARE_GROUP(META_NAME) +TEST_HERE_DECLARE_GROUP(PP_NARG) + +#undef TEST_HERE_DECLARE_GROUP +#undef PARENTHESIS_POST +#undef PARENTHESIS_PRE #undef C #undef B #undef A #undef TEST_FUNC +#define TEST_HERE_USE_GROUP(func_name)\ + TEST_HERE_USE(func_name, 0),\ + TEST_HERE_USE(func_name, 1),\ + TEST_HERE_USE(func_name, 3),\ + TEST_HERE_USE(func_name, with_parenthesis_pre),\ + TEST_HERE_USE(func_name, with_parenthesis_post), + int main(void) { const struct CMUnitTest tests[] = { - TEST_HERE_USE(META_NAME, 0), - TEST_HERE_USE(META_NAME, 1), - TEST_HERE_USE(META_NAME, 3), - TEST_HERE_USE(PP_NARG , 0), - TEST_HERE_USE(PP_NARG , 1), - TEST_HERE_USE(PP_NARG , 3), + TEST_HERE_USE_GROUP(META_NAME) + TEST_HERE_USE_GROUP(PP_NARG) }; cmocka_set_message_output(CM_OUTPUT_TAP); @@ -81,5 +94,6 @@ int main(void) return cmocka_run_group_tests(tests, NULL, NULL); } +#undef TEST_HERE_USE_GROUP #undef META_NAME #undef TEST_PREFIX