diff mbox series

[05/19] tst_test_macros: Add TST_TOSTR macro

Message ID 2a67385fe34905b5b631abadb6daefb6f595a924.1651176646.git.luke.nowakowskikrijger@canonical.com
State Changes Requested
Headers show
Series Expand Cgroup lib and modify controller tests | expand

Commit Message

Luke Nowakowski-Krijger April 28, 2022, 8:39 p.m. UTC
Add the TST_TOSTR macro which uses a preprocessor trick to concetenate
variables into strings.

Useful when needing to create strings from other #define variables.

Signed-off-by: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
---
 include/tst_test_macros.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Petr Vorel May 3, 2022, 2:38 p.m. UTC | #1
Hi Luke,

> Add the TST_TOSTR macro which uses a preprocessor trick to concetenate
> variables into strings.

> Useful when needing to create strings from other #define variables.

> Signed-off-by: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
> ---
>  include/tst_test_macros.h | 3 +++
>  1 file changed, 3 insertions(+)

> diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
> index 2e7b7871c..f5d86c421 100644
> --- a/include/tst_test_macros.h
> +++ b/include/tst_test_macros.h
include/tst_common.h would be probably a better place.
Because tst_test_macros.h contains tests which call TEST() helpers.

> @@ -36,6 +36,9 @@ extern void *TST_RET_PTR;

>  #define TST_2_(_1, _2, ...) _2

> +#define _TST_TOSTR(STR) #STR
At least 3 tests have:
#define str(s) #s (or STR)

(testcases/network/stress/route/route-change-netlink.c
testcases/kernel/controllers/freezer/vfork.c
testcases/kernel/pty/pty04.c)

therefore IMHO it'd be better to define:
#define TST_STR(s) #s
#define TST_TOSTR(s) TST_STR(s)

> +#define TST_TOSTR(STR) _TST_TOSTR(STR)

Kind regards,
Petr
Cyril Hrubis May 5, 2022, 1:34 p.m. UTC | #2
Hi!
> Signed-off-by: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
> ---
>  include/tst_test_macros.h | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
> index 2e7b7871c..f5d86c421 100644
> --- a/include/tst_test_macros.h
> +++ b/include/tst_test_macros.h
> @@ -36,6 +36,9 @@ extern void *TST_RET_PTR;
>  
>  #define TST_2_(_1, _2, ...) _2
>  
> +#define _TST_TOSTR(STR) #STR
> +#define TST_TOSTR(STR) _TST_TOSTR(STR)

Just minor thing, anything starting with underscore is reserved for libc
and kernel. So we try to add the underscore at the end instead when
defining intemediate macros. This one should be TST_TO_STR_() instead.

>  #define TST_FMT_(FMT, _1, ...) FMT, ##__VA_ARGS__
>  
>  #define TST_MSG_(RES, FMT, SCALL, ...) \
> -- 
> 2.32.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
diff mbox series

Patch

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index 2e7b7871c..f5d86c421 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -36,6 +36,9 @@  extern void *TST_RET_PTR;
 
 #define TST_2_(_1, _2, ...) _2
 
+#define _TST_TOSTR(STR) #STR
+#define TST_TOSTR(STR) _TST_TOSTR(STR)
+
 #define TST_FMT_(FMT, _1, ...) FMT, ##__VA_ARGS__
 
 #define TST_MSG_(RES, FMT, SCALL, ...) \