[PATCH v6 06/19] string: Add [C]QChar_of() and QVoid_of()

From: Alejandro Colomar

Date: Sat Nov 14 2026 - 10:23:02 EST


These macros serve to implement qualifier-generic macros as those
specified in ISO C23.

Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
---
include/linux/string.h | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 58b8b7c02b8d..ede8883c99a1 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -14,6 +14,36 @@
#include <linux/stdarg.h>
#include <uapi/linux/string.h>

+/**
+ * QVoid_of - qualified void of
+ *
+ * @p: Pointer to test for qualifiers
+ *
+ * This macro produces the type name 'void',
+ * qualified with any qualifiers present in the element of @p.
+ */
+#define QVoid_of(p) typeof(*(__QVoidptr_of(p)){0})
+#define __QVoidptr_of(p) typeof(1?(p):(void*){0})
+
+/**
+ * QChar_of - qualified char of
+ *
+ * @Q: Qualifier to test.
+ * @p: Pointer to test for the @Q qualifier
+ *
+ * This macro produces the type name 'char',
+ * @Q-qualified iff the input pointer points to a @Q-qualified element.
+ */
+#define QChar_of(Q, p) typeof \
+( \
+ _Generic((QVoid_of(p) *){0}, \
+ Q void *: (Q char){0}, \
+ void *: ( char){0} \
+ ) \
+)
+// CQChar_of - const qualified char of
+#define CQChar_of(p) QChar_of(const, p)
+
/**
* streq - strings equal
*
--
2.55.0

Attachment: signature.asc
Description: PGP signature