Skip to content

Commit 87dad4a

Browse files
committed
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>
1 parent 33e4b99 commit 87dad4a

4 files changed

Lines changed: 50 additions & 35 deletions

File tree

src/include/sof/preproc-private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* META_COUNT_VARAGS_BEFORE_COMPILE(A,B,C,D) evaluates to 4
6666
*/
6767
#define _META_PP_NARG_BEFORE_COMPILE_(...) \
68-
_META_PP_ARG_N(__VA_ARGS__)
68+
_META_PP_ARG_N(__VA_ARGS__)
6969
#define _META_PP_ARG_N(\
7070
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
7171
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
@@ -224,7 +224,7 @@
224224
/* used by macro MAP, don't use on its own */
225225
#define _META_MAP_BODY(arg_count, m, ...)\
226226
META_IF_ELSE(META_COUNT_VARAGS_BEFORE_COMPILE(__VA_ARGS__))(\
227-
META_DEFER(2, _META_MAP)()\
227+
_META_DEFER_2(_META_MAP)()\
228228
(arg_count, m, __VA_ARGS__)\
229229
)()
230230

@@ -246,7 +246,7 @@
246246
/* used by macro MAP_AGGREGATE, don't use on its own */
247247
#define _META_MAP_AGGREGATE_BODY(arg_count, m, aggr, ...)\
248248
META_IF_ELSE(META_COUNT_VARAGS_BEFORE_COMPILE(__VA_ARGS__))(\
249-
META_DEFER(2, _META_MAP_AGGREGATE)()\
249+
_META_DEFER_2(_META_MAP_AGGREGATE)()\
250250
(arg_count, m, aggr, __VA_ARGS__)\
251251
)(aggr)
252252

src/include/sof/preproc.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@
4646
* META_COUNT_VARAGS_BEFORE_COMPILE(A,B,C,D) evaluates to 4
4747
*/
4848
#define META_COUNT_VARAGS_BEFORE_COMPILE(...)\
49-
META_IF_ELSE(META_HAS_ARGS(__VA_ARGS__)) (\
50-
_META_PP_NARG_BEFORE_COMPILE_(__VA_ARGS__, _META_PP_RSEQ_N())\
51-
)\
52-
(\
53-
0\
49+
META_DEC(\
50+
_META_PP_NARG_BEFORE_COMPILE_(\
51+
_, ##__VA_ARGS__, _META_PP_RSEQ_N()\
52+
)\
5453
)
5554

5655
/* treat x as string while forcing x expansion beforehand */

test/cmocka/src/debugability/macros.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@
4545
static void test_debugability_macros_declare_log_entry(void **state)
4646
{
4747
const char *macro_result = CAPTURE(_DECLARE_LOG_ENTRY(
48-
LOG_LEVEL_CRITICAL, "Message", TRACE_CLASS_DMA, 1));
48+
LOG_LEVEL_CRITICAL,
49+
"Message",
50+
TRACE_CLASS_DMA,
51+
1
52+
));
4953
const char *should_be_eq =
5054
"__attribute__((section(\".static_log.\""
5155
" \"LOG_LEVEL_CRITICAL\"))) "
5256
"static const struct { uint32_t level; uint32_t component_id; "
5357
"uint32_t params_num; uint32_t line_idx; uint32_t file_name_len; "
5458
"const char file_name[sizeof(\"src/debugability/macros.c\")]; "
5559
"uint32_t text_len; const char text[sizeof(\"Message\")]; } "
56-
"log_entry = { 1";
60+
"log_entry = { 1(6 << 24)152sizeof(\"src/debugability/macros.c\")"
61+
"\"src/debugability/macros.c\"sizeof(\"Message\")\"Message\" }";
5762
(void)state;
5863

5964
assert_string_equal(macro_result, should_be_eq);
@@ -78,32 +83,29 @@ static char *get_should_be(const int param_count)
7883
{
7984
char *result = malloc(sizeof(char) * 1024);
8085
char *paramlist = get_param_list(param_count);
81-
char *maybe_space = " ";
8286
char *maybe_comma = ",";
8387

84-
if (!param_count)
85-
maybe_space = "";
86-
else
88+
if (param_count)
8789
maybe_comma = "";
8890

89-
/* which format: 0 1 2 3 4 5 6 7 8 9 A*/
90-
sprintf(result, "%s%d%s%d%s%s%d%s%s%s%s",
91+
/* which format: 0 1 2 3 4 5 6 7 8 9*/
92+
sprintf(result, "%s%d%s%d%s%d%s%s%s%s",
9193
/*0*/"{ __attribute__((unused)) typedef char assertion_failed_"
9294
META_QUOTE(BASE_LOG_ASSERT_FAIL_MSG)
9395
"[(",
9496
/*1*/_TRACE_EVENT_MAX_ARGUMENT_COUNT,
9597
/*2*/" >= ",
9698
/*3*/param_count,
97-
/*4*/maybe_space,
98-
/*5*/") ? 1 : -1]; log_func log_function = (log_func)& _trace_event",
99-
/*6*/param_count,
100-
/*7*/"; log_function(&log_entry",
101-
/*8*/maybe_comma,
102-
/*9*/paramlist,
103-
/*A*/");}"
99+
/*4*/") ? 1 : -1]; log_func log_function = (log_func)& _trace_event",
100+
/*5*/param_count,
101+
/*6*/"; log_function(&log_entry",
102+
/*7*/maybe_comma,
103+
/*8*/paramlist,
104+
/*9*/");}"
104105
);
105106
if (paramlist)
106107
free(paramlist);
108+
// TODO: maybe remove all whitespace chars; they're not important here
107109
return result;
108110
}
109111

test/cmocka/src/lib/preproc/varargs_count.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,49 @@ static void META_CONCAT_SEQ_DELIM_(prefix, test_func, postfix)(void **state)\
5151
#define A 1
5252
#define B 2
5353
#define C 3
54+
#define PARENTHESIS_PRE() (1+3)/2
55+
#define PARENTHESIS_POST() 4/(3-1)
5456

5557
#define META_NAME META_COUNT_VARAGS_BEFORE_COMPILE
56-
TEST_HERE_DECLARE(META_NAME, 0, 0, )
57-
TEST_HERE_DECLARE(META_NAME, 1, 1, A )
58-
TEST_HERE_DECLARE(META_NAME, 3, 3, A, B, C)
59-
TEST_HERE_DECLARE(PP_NARG , 0, 0, )
60-
TEST_HERE_DECLARE(PP_NARG , 1, 1, A )
61-
TEST_HERE_DECLARE(PP_NARG , 3, 3, A, B, C)
58+
#define TEST_HERE_DECLARE_GROUP(func_name)\
59+
TEST_HERE_DECLARE(func_name, 0, 0, )\
60+
TEST_HERE_DECLARE(func_name, 1, 1, A )\
61+
TEST_HERE_DECLARE(func_name, 3, 3, A, B, C)\
62+
TEST_HERE_DECLARE(func_name, with_parenthesis_pre, 1,\
63+
PARENTHESIS_PRE())\
64+
TEST_HERE_DECLARE(func_name, with_parenthesis_post, 1,\
65+
PARENTHESIS_POST())
6266

67+
TEST_HERE_DECLARE_GROUP(META_NAME)
68+
TEST_HERE_DECLARE_GROUP(PP_NARG)
69+
70+
#undef TEST_HERE_DECLARE_GROUP
71+
#undef PARENTHESIS_POST
72+
#undef PARENTHESIS_PRE
6373
#undef C
6474
#undef B
6575
#undef A
6676
#undef TEST_FUNC
6777

78+
#define TEST_HERE_USE_GROUP(func_name)\
79+
TEST_HERE_USE(func_name, 0),\
80+
TEST_HERE_USE(func_name, 1),\
81+
TEST_HERE_USE(func_name, 3),\
82+
TEST_HERE_USE(func_name, with_parenthesis_pre),\
83+
TEST_HERE_USE(func_name, with_parenthesis_post),
84+
6885
int main(void)
6986
{
7087
const struct CMUnitTest tests[] = {
71-
TEST_HERE_USE(META_NAME, 0),
72-
TEST_HERE_USE(META_NAME, 1),
73-
TEST_HERE_USE(META_NAME, 3),
74-
TEST_HERE_USE(PP_NARG , 0),
75-
TEST_HERE_USE(PP_NARG , 1),
76-
TEST_HERE_USE(PP_NARG , 3),
88+
TEST_HERE_USE_GROUP(META_NAME)
89+
TEST_HERE_USE_GROUP(PP_NARG)
7790
};
7891

7992
cmocka_set_message_output(CM_OUTPUT_TAP);
8093

8194
return cmocka_run_group_tests(tests, NULL, NULL);
8295
}
8396

97+
#undef TEST_HERE_USE_GROUP
8498
#undef META_NAME
8599
#undef TEST_PREFIX

0 commit comments

Comments
 (0)