From 5e307c806645f4e45abe457087df48c9ac8df8ba Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Tue, 2 Mar 2021 19:42:41 -0800 Subject: [PATCH] base: Add a macro to expand parameter pack expressions in order. This wraps up the strange compiler goop necessary to evaluate expressions based on parameter pack expansions in order. Change-Id: I16fbd53d22492a8c20524e3ef8bb8ff5e5d59b14 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/42033 Reviewed-by: Gabe Black Maintainer: Gabe Black Tested-by: kokoro --- src/base/compiler.hh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/base/compiler.hh b/src/base/compiler.hh index 643352cd05..c003bfa64b 100644 --- a/src/base/compiler.hh +++ b/src/base/compiler.hh @@ -112,6 +112,16 @@ // we can't do that with direct substitution. # define M5_LIKELY(cond) __builtin_expect(!!(cond), 1) # define M5_UNLIKELY(cond) __builtin_expect(!!(cond), 0) + +// Evaluate an expanded parameter pack in order. Multiple arguments can be +// passed in which be evaluated in order relative to each other as a group. +// The argument(s) must include a parameter pack to expand. This works because +// the elements of a brace inclosed initializer list are evaluated in order, +// as are the arguments to the comma operator, which evaluates to the last +// value. This is compiler specific because it uses variadic macros. +#define M5_FOR_EACH_IN_PACK(...) \ +do { M5_VAR_USED int i[] = { 0, ((void)(__VA_ARGS__), 0)... }; } while (false) + #else # error "Don't know what to do for your compiler." #endif