In macros it is common to declare local variables using typeof(param) in order
to ensure that side effects are only evaluated once.  A consequence of this is
double textural expansion of the parameter, which can get out of hand very
quickly with nested macros.

A GCC extension, __auto_type, is now avaialble in the new toolchain baseline
and avoids the double textural expansion.

Signed-off-by: Andrew Cooper <andrew.coop...@citrix.com>
---
CC: Anthony PERARD <anthony.per...@vates.tech>
CC: Michal Orzel <michal.or...@amd.com>
CC: Jan Beulich <jbeul...@suse.com>
CC: Julien Grall <jul...@xen.org>
CC: Roger Pau Monné <roger....@citrix.com>
CC: Stefano Stabellini <sstabell...@kernel.org>
CC: Roberto Bagnara <roberto.bagn...@bugseng.com>
CC: Nicola Vetrini <nicola.vetr...@bugseng.com>
CC: consult...@bugseng.com <consult...@bugseng.com>

The resulting build is identical.

RFC.  This requires a MISRA change, as it currently manifests as a R1.1
violation.  Nevertheless, I think we want to start using in places where we
currently use typeof(expression of <initilaiser>).

Eclair run on this patch (expecting a failure):
  https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/1800631949

Min toolchain check:
  https://godbolt.org/z/f9WjooPYj

GCC Manual:
  https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Auto-Type.html
---
 xen/include/xen/macros.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/xen/include/xen/macros.h b/xen/include/xen/macros.h
index cd528fbdb127..b5e5ff4b1c2f 100644
--- a/xen/include/xen/macros.h
+++ b/xen/include/xen/macros.h
@@ -71,18 +71,18 @@
 /* Hide a value from the optimiser. */
 #define HIDE(x)                                 \
     ({                                          \
-        typeof(x) _x = (x);                     \
+        __auto_type _x = (x);                   \
         asm volatile ( "" : "+r" (_x) );        \
         _x;                                     \
     })
 
 #define ABS(x) ({                              \
-    typeof(x) x_ = (x);                        \
+    __auto_type x_ = (x);                      \
     (x_ < 0) ? -x_ : x_;                       \
 })
 
 #define SWAP(a, b) \
-   do { typeof(a) t_ = (a); (a) = (b); (b) = t_; } while ( 0 )
+   do { __auto_type t_ = (a); (a) = (b); (b) = t_; } while ( 0 )
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
 
@@ -110,15 +110,15 @@
  */
 #define min(x, y)                               \
     ({                                          \
-        const typeof(x) _x = (x);               \
-        const typeof(y) _y = (y);               \
+        const __auto_type _x = (x);             \
+        const __auto_type _y = (y);             \
         (void)(&_x == &_y); /* typecheck */     \
         _x < _y ? _x : _y;                      \
     })
 #define max(x, y)                               \
     ({                                          \
-        const typeof(x) _x = (x);               \
-        const typeof(y) _y = (y);               \
+        const __auto_type _x = (x);             \
+        const __auto_type _y = (y);             \
         (void)(&_x == &_y); /* typecheck */     \
         _x > _y ? _x : _y;                      \
     })

base-commit: 78ce2be733b1e45e2e190c1765fe31da318d435f
-- 
2.39.5


Reply via email to