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 <michalx.wierzbicki@linux.intel.com>
This commit is contained in:
Michal Jerzy Wierzbicki 2018-11-06 02:11:08 +01:00
parent 33e4b99351
commit 87dad4adc5
4 changed files with 50 additions and 35 deletions

View File

@ -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)

View File

@ -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())\
META_DEC(\
_META_PP_NARG_BEFORE_COMPILE_(\
_, ##__VA_ARGS__, _META_PP_RSEQ_N()\
)\
(\
0\
)
/* treat x as string while forcing x expansion beforehand */

View File

@ -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;
}

View File

@ -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