compiler: implement rseq_unqual_scalar_typeof
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 20 Apr 2023 21:35:29 +0000 (17:35 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 20 Apr 2023 21:41:48 +0000 (17:41 -0400)
Allow defining variables and cast with a typeof which removes the
volatile and const qualitiers.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: Ibe7068e2c307ed47a2eb6db891e717fb7db47282

include/rseq/compiler.h

index 86630dd2c7897514cb8f056c270c63209354f859..e2afe061b77b6477a93a4176b6e20668b5ac963d 100644 (file)
 #ifndef RSEQ_COMPILER_H
 #define RSEQ_COMPILER_H
 
+#if defined __cplusplus
+# include <type_traits>        /* for std::remove_cv */
+#endif
+
 /*
  * gcc prior to 4.8.2 miscompiles asm goto.
  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
 #define RSEQ_BITS_PER_LONG     32
 #endif
 
+#ifdef __cplusplus
+#define rseq_unqual_scalar_typeof(x)                                   \
+       __typeof__(reinterpret_cast<std::remove_cv<__typeof__(x)>::type>((__typeof__(x))0))
+#else
+/*
+ * Use C11 _Generic to express unqualified type from expression. This removes
+ * volatile qualifier from expression type.
+ */
+#define rseq_unqual_scalar_typeof(x)                                   \
+       __typeof__(                                                     \
+               _Generic((x),                                           \
+                       char: (char)0,                                  \
+                       unsigned char: (unsigned char)0,                \
+                       signed char: (signed char)0,                    \
+                       unsigned short: (unsigned short)0,              \
+                       signed short: (signed short)0,                  \
+                       unsigned int: (unsigned int)0,                  \
+                       signed int: (signed int)0,                      \
+                       unsigned long: (unsigned long)0,                \
+                       signed long: (signed long)0,                    \
+                       unsigned long long: (unsigned long long)0,      \
+                       signed long long: (signed long long)0,          \
+                       default: (x)                                    \
+               )                                                       \
+       )
+#endif
+
 #endif  /* RSEQ_COMPILER_H_ */
This page took 0.030947 seconds and 4 git commands to generate.