From 3390f2e64790eb44667654cf11a0c87d41b18ea4 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 17 Oct 2024 19:25:39 +1000 Subject: [PATCH] test: make the TEST_COLLECTION() macro re-usable in the same file Concat the line number to the generated variable names, this way we can have more than one TEST_COLLECTION() in the same file. Part-of: --- src/util-macros.h | 9 +++++++++ test/litest.h | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/util-macros.h b/src/util-macros.h index d56a8a3f..29a34f6a 100644 --- a/src/util-macros.h +++ b/src/util-macros.h @@ -62,4 +62,13 @@ #define CASE_RETURN_STRING(a) case a: return #a +/** + * Concatenate two macro args into one, e.g.: + * int CONCAT(foo_, __LINE__); + * will produce: + * int foo_123; + */ +#define CONCAT2(X,Y) X##Y +#define CONCAT(X,Y) CONCAT2(X,Y) + #define _fallthrough_ __attribute__((fallthrough)) diff --git a/test/litest.h b/test/litest.h index 448faf47..c1bf8b77 100644 --- a/test/litest.h +++ b/test/litest.h @@ -74,14 +74,14 @@ struct test_collection { void (*setup)(void); } __attribute__((aligned(16))); -#define TEST_COLLECTION(name) \ - static void (name##_setup)(void); \ - static const struct test_collection _test_collection \ +#define TEST_COLLECTION(name_) \ + static void (CONCAT(name_ , __LINE__))(void); \ + static const struct test_collection CONCAT(_test_collection_, __LINE__) \ __attribute__ ((used)) \ __attribute__ ((section ("test_collection_section"))) = { \ - #name, name##_setup \ + #name_, CONCAT(name_, __LINE__) \ }; \ - static void (name##_setup)(void) + static void (CONCAT(name_, __LINE__))(void) __attribute__ ((format (printf, 3, 0))) void _litest_checkpoint(const char *func, -- 2.34.1