From a0c698c720441782fcf2cb7dfd01e69baf8f1f39 Mon Sep 17 00:00:00 2001
From: Dan Streetman <ddstreet@ieee.org>
Date: Thu, 2 Feb 2023 15:58:10 -0500
Subject: [PATCH] basic/macro: add macro to iterate variadic args

(cherry picked from commit e179f2d89c9f0c951636d74de00136b4075cd1ac)
(cherry picked from commit cd4f43bf378ff33ce5cfeacd96f7f3726603bddc)

Upstream-Status: Backport [https://github.com/systemd/systemd-stable/commit/c288a3aafdf11cd93eb7a21e4d587c6fc218a29c]

Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 src/basic/macro.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/basic/macro.h b/src/basic/macro.h
index 9e62f9c71c..16242902ec 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -454,4 +454,13 @@ typedef struct {
 
 assert_cc(sizeof(dummy_t) == 0);
 
+/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
+ * convertable. The iteration variable 'entry' must already be defined. */
+#define VA_ARGS_FOREACH(entry, ...)                                     \
+        _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
+#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...)         \
+        for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
+             ((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
+             _current_++)
+
 #include "log.h"
-- 
2.34.1

