From: David Laight <[email protected]>

Autoterminating nested for() loops can be used inside #defines to
declare variables that are scoped to the statement that follows.
These are used by __scoped_user_access() but may have other uses
and the gory details are best separated from the use.

Using 'with (declaration)' and 'and_with (declaration)' seems to
read reasonably well and doesn't seem to collide with any existing
code.

As an example the scoped user access definition becomes:
        with (auto _tmpptr = __scoped_user_access_begin(mode, uptr, size, 
elbl)) \
                /* Force modified pointer usage within the scope */             
\
                and_with (const auto uptr __cleanup(...) = _tmpptr)

Signed-off-by: David Laight <[email protected]>
---
 include/linux/compiler.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index af16624b29fd..1098a91b5591 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -369,6 +369,32 @@ static inline void *offset_to_ptr(const int *off)
  */
 #define prevent_tail_call_optimization()       mb()
 
+/*
+ * Sometimes a #define needs to declare a variable that is scoped
+ * to the statement that follows without having mismatched {}.
+ *     with (int x = expression) {
+ *             statements
+ *     }
+ * is the same as:
+ *     {
+ *             int x = expression;
+ *             statements
+ *     }
+ * but lets it all be hidden from the call site, eg:
+ *     frobnicate(x, args) {
+ *             statements
+ *     } 
+ * Only a single variable can be defined, and_with() allows extra ones
+ * without adding an additional outer loop.
+ *
+ * The controlled scope can be terminated using return, break, continue or 
goto.
+ */
+#define with(declaration) \
+       for (bool _with_done = false; !_with_done; _with_done = true)   \
+               and_with (declaration)
+#define and_with(declaration) \
+       for (declaration; !_with_done; _with_done = true)
+
 #include <asm/rwonce.h>
 
 #endif /* __LINUX_COMPILER_H */
-- 
2.39.5


Reply via email to