Skip to contentSkip to conversation

include: move typeof_strip_qual to compiler.h, use it in QAPI_LIST_LENGTH()

PATCH In review Paolo Bonzini 4 participants 8 messages Jun 25, 2024 – Jun 27, 2024 Atomic infrastructure

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 include: move typeof_strip_qual to compiler.h, use it in QAPI_LIST_LENGTH()

The typeof_strip_qual() is most useful for the atomic fetch-and-modify operations in atomic.h, but it can be used elsewhere as well. For example, QAPI_LIST_LENGTH() assumes that the argument is not const, which is not a requirement.

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

The patch was tested by changing the declaration of strv_from_str_list() in qapi/qapi-type-helpers.c to:

char **strv_from_str_list(const strList *const list)

This is valid C code, and it fails to compile without this change.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
 include/qapi/util.h     |  2 +-
 include/qemu/atomic.h   | 42 -------------------------------------
 include/qemu/compiler.h | 46 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 43 deletions(-)
include/qapi/util.h +1 -1
index 20dfea8a545..b8254247b8d 100644
@@ -62,7 +62,7 @@ int parse_qapi_name(const char *name, bool complete);
 #define QAPI_LIST_LENGTH(list)                                      \
     ({                                                              \
         size_t _len = 0;                                            \
-        typeof(list) _tail;                                         \
+        typeof_strip_qual(list) _tail;                              \
         for (_tail = list; _tail != NULL; _tail = _tail->next) {    \
             _len++;                                                 \
         }                                                           \
include/qemu/atomic.h +0 -42

48 lines not on this page · raw message

include/qemu/compiler.h +46 -0

50 lines not on this page · raw message

Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Paolo Bonzini

The typeof_strip_qual() is most useful for the atomic fetch-and-modify operations in atomic.h, but it can be used elsewhere as well. For example, QAPI_LIST_LENGTH() assumes that the argument is not const, which is not a requirement.

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

The patch was tested by changing the declaration of strv_from_str_list() in qapi/qapi-type-helpers.c to:

char **strv_from_str_list(const strList *const list)

This is valid C code, and it fails to compile without this change.

Quoted 8 lines

[1]https://lore.kernel.org/qemu-devel/[email protected]/

---
  include/qapi/util.h     |  2 +-
  include/qemu/atomic.h   | 42 -------------------------------------
  include/qemu/compiler.h | 46 +++++++++++++++++++++++++++++++++++++++++
  3 files changed, 47 insertions(+), 43 deletions(-)

r~

Tested-by Reviewed-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 136 lines · Paolo Bonzini

The typeof_strip_qual() is most useful for the atomic fetch-and-modify operations in atomic.h, but it can be used elsewhere as well. For example, QAPI_LIST_LENGTH() assumes that the argument is not const, which is not a requirement.

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

The patch was tested by changing the declaration of strv_from_str_list() in qapi/qapi-type-helpers.c to:

char **strv_from_str_list(const strList *const list)

This is valid C code, and it fails to compile without this change.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

---

include/qapi/util.h     |  2 +-
include/qemu/atomic.h   | 42 -------------------------------------
include/qemu/compiler.h | 46 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 43 deletions(-)
include/qapi/util.h +1 -1
index 20dfea8a545..b8254247b8d 100644
@@ -62,7 +62,7 @@ int parse_qapi_name(const char *name, bool complete);
 #define QAPI_LIST_LENGTH(list)                                      \
    ({                                                              \
        size_t _len = 0;                                            \
-        typeof(list) _tail;                                         \
+        typeof_strip_qual(list) _tail;                              \
        for (_tail = list; _tail != NULL; _tail = _tail->next) {    \
            _len++;                                                 \
        }                                                           \
include/qemu/atomic.h +0 -42
index 99110abefb3..dc4118ddd9e 100644
@@ -20,48 +20,6 @@
 /* Compiler barrier */
 #define barrier()   ({ asm volatile("" ::: "memory"); (void)0; })
 
-/* The variable that receives the old value of an atomically-accessed
- * variable must be non-qualified, because atomic builtins return values
- * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
- *
- * This macro has to handle types smaller than int manually, because of
- * implicit promotion.  int and larger types, as well as pointers, can be
- * converted to a non-qualified type just by applying a binary operator.
- */
-#define typeof_strip_qual(expr)                                                    \
-  typeof(                                                                          \
-    __builtin_choose_expr(                                                         \
-      __builtin_types_compatible_p(typeof(expr), bool) ||                          \
-        __builtin_types_compatible_p(typeof(expr), const bool) ||                  \
-        __builtin_types_compatible_p(typeof(expr), volatile bool) ||               \
-        __builtin_types_compatible_p(typeof(expr), const volatile bool),           \
-        (bool)1,                                                                   \
-    __builtin_choose_expr(                                                         \
-      __builtin_types_compatible_p(typeof(expr), signed char) ||                   \
-        __builtin_types_compatible_p(typeof(expr), const signed char) ||           \
-        __builtin_types_compatible_p(typeof(expr), volatile signed char) ||        \
-        __builtin_types_compatible_p(typeof(expr), const volatile signed char),    \
-        (signed char)1,                                                            \
-    __builtin_choose_expr(                                                         \
-      __builtin_types_compatible_p(typeof(expr), unsigned char) ||                 \
-        __builtin_types_compatible_p(typeof(expr), const unsigned char) ||         \
-        __builtin_types_compatible_p(typeof(expr), volatile unsigned char) ||      \
-        __builtin_types_compatible_p(typeof(expr), const volatile unsigned char),  \
-        (unsigned char)1,                                                          \
-    __builtin_choose_expr(                                                         \
-      __builtin_types_compatible_p(typeof(expr), signed short) ||                  \
-        __builtin_types_compatible_p(typeof(expr), const signed short) ||          \
-        __builtin_types_compatible_p(typeof(expr), volatile signed short) ||       \
-        __builtin_types_compatible_p(typeof(expr), const volatile signed short),   \
-        (signed short)1,                                                           \
-    __builtin_choose_expr(                                                         \
-      __builtin_types_compatible_p(typeof(expr), unsigned short) ||                \
-        __builtin_types_compatible_p(typeof(expr), const unsigned short) ||        \
-        __builtin_types_compatible_p(typeof(expr), volatile unsigned short) ||     \
-        __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
-        (unsigned short)1,                                                         \
-      (expr)+0))))))
-
 #ifndef __ATOMIC_RELAXED
 #error "Expecting C11 atomic ops"
 #endif
include/qemu/compiler.h +35 -0
index c797f0d4572..554c5ce7df7 100644
@@ -227,4 +227,50 @@
 #define SECOND_ARG(first, second, ...) second
 #define IS_EMPTY_(junk_maybecomma)     SECOND_ARG(junk_maybecomma 1, 0)
 
+#ifndef __cplusplus
+/*
+ * Useful in macros that need to declare temporary variables.  For example,
+ * the variable that receives the old value of an atomically-accessed
+ * variable must be non-qualified, because atomic builtins return values
+ * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
+ *
+ * This macro has to handle types smaller than int manually, because of
+ * implicit promotion.  int and larger types, as well as pointers, can be
+ * converted to a non-qualified type just by applying a binary operator.
+ */
+#define typeof_strip_qual(expr)                                                    \
+  typeof(                                                                          \
+    __builtin_choose_expr(                                                         \
+      __builtin_types_compatible_p(typeof(expr), bool) ||                          \
+        __builtin_types_compatible_p(typeof(expr), const bool) ||                  \
+        __builtin_types_compatible_p(typeof(expr), volatile bool) ||               \
+        __builtin_types_compatible_p(typeof(expr), const volatile bool),           \
+        (bool)1,                                                                   \
+    __builtin_choose_expr(                                                         \
+      __builtin_types_compatible_p(typeof(expr), signed char) ||                   \
+        __builtin_types_compatible_p(typeof(expr), const signed char) ||           \
+        __builtin_types_compatible_p(typeof(expr), volatile signed char) ||        \
+        __builtin_types_compatible_p(typeof(expr), const volatile signed char),    \
+        (signed char)1,                                                            \
+    __builtin_choose_expr(                                                         \
+      __builtin_types_compatible_p(typeof(expr), unsigned char) ||                 \
+        __builtin_types_compatible_p(typeof(expr), const unsigned char) ||         \
+        __builtin_types_compatible_p(typeof(expr), volatile unsigned char) ||      \
+        __builtin_types_compatible_p(typeof(expr), const volatile unsigned char),  \
+        (unsigned char)1,                                                          \
+    __builtin_choose_expr(                                                         \
+      __builtin_types_compatible_p(typeof(expr), signed short) ||                  \
+        __builtin_types_compatible_p(typeof(expr), const signed short) ||          \
+        __builtin_types_compatible_p(typeof(expr), volatile signed short) ||       \

include/qemu/compiler.h line 274

Quoted 10 lines
include/qemu/compiler.h +10 -0
+        __builtin_types_compatible_p(typeof(expr), const volatile signed short),   \
+        (signed short)1,                                                           \
+    __builtin_choose_expr(                                                         \
+      __builtin_types_compatible_p(typeof(expr), unsigned short) ||                \
+        __builtin_types_compatible_p(typeof(expr), const unsigned short) ||        \
+        __builtin_types_compatible_p(typeof(expr), volatile unsigned short) ||     \
+        __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
+        (unsigned short)1,                                                         \
+      (expr)+0))))))
+#endif

Should we add an #else to provide a fallback for cplusplus until the alternative is merged?

-#endif
+#else /* __cpluplus */
+#define typeof_strip_qual typeof
+#endif /* __cplusplus */

Quoted 6 lines
include/qemu/compiler.h +1 -0
+
 #endif /* COMPILER_H */
Signature · 1 line
2.45.2

With that comment addressed,

#3 lore raw

Hi Manos, thank you for looking.

Review

Quoted 7 lines · Manos Pitsidianakis

Should we add an #else to provide a fallback for cplusplus until the alternative is merged?

(quoted diff) +3 -1
-#endif
+#else /* __cpluplus */
+#define typeof_strip_qual typeof
+#endif /* __cplusplus */

You probably want decltype for C++ and, unfortunately, you need somewhat more than just decltype to make this drop-in.

I am +1 with this patch (without '#error something'), we will define typeof_strip_qual for C++ on our side.

Regards,
Roman.

#4 lore raw
In reply to Manos Pitsidianakis
Quoted 8 lines · Manos Pitsidianakis
Quoted 5 lines

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

Should we add an #else to provide a fallback for cplusplus until the alternative is merged?

As the commit message says, I don't think we need to include the C++ code in QEMU since it would be essentially dead.

Paolo

#5 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Manos Pitsidianakis
Quoted 5 lines

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

Quoted 5 lines
Quoted 2 lines

Should we add an #else to provide a fallback for cplusplus until the alternative is merged?

As the commit message says, I don't think we need to include the C++ code in QEMU since it would be essentially dead.

The #ifndef __cplusplus part stood out for me, if it's not required for the qemu build then it's similarly unnecessary. Thinking out loud here.

Anyway, ACK and tested-by and rob as in my previous mail.

Thanks,
Manos

#6 lore raw
Quoted 10 lines · Manos Pitsidianakis
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Manos Pitsidianakis
Quoted 5 lines

Move the macro to compiler.h and, while at it, move it under #ifndef __cplusplus to emphasize that it uses C-only constructs. A C++ version of typeof_strip_qual() using type traits is possible[1], but beyond the scope of this patch because the little C++ code that is in QEMU does not use QAPI.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Should we add an #else to provide a fallback for cplusplus until the alternative is merged?

As the commit message says, I don't think we need to include the C++ code in QEMU since it would be essentially dead.

The #ifndef __cplusplus part stood out for me, if it's not required for the qemu build then it's similarly unnecessary. Thinking out loud here.

Yeah, the patch had actually two purposes, only one of which is explicit in the commit message because the other is more relevant to Google than to the upstream project:

1) Google wants some help making QEMU headers compilable with C++, which is generally not a goal of the project but not something we want to hinder. And if that brings to our attention things that could be improved in C as well, why not.

2) it is true that typeof_strip_qual() is useful in other places than qemu/atomic.h

Since qemu/compiler.h already has #ifdef __cplusplus in various places, and could reasonably be included in the few C++ files that QEMU has, it makes sense to have a #ifndef __cplusplus in qemu/compiler.h (unlike in qemu/atomic.h). Personally I prefer to have a little more cognitive load in common headers such as qemu/osdep.h and qemu/compiler.h, than to have different styles (e.g. no qemu/osdep.h inclusion) in C++ sources, even though most likely I'll never touch those C++ sources.

The #ifndef is not absolutely necessary---if we removed it, Google could work around that with "#ifdef __cplusplus / #undef typeof_strip_qual". It is mostly for documentation purposes to point out the C-only compiler builtins, and because if anyone ended up using typeof_strip_qual in C++ code the error message would be very very long.

Paolo

#7 lore raw
Quoted 1 line · Paolo Bonzini

because if anyone ended up using [..] C++ [..] the error message would be very very long.

I thought that comes with the territory 😀