diff options
| author | Alejandro Colomar <alx@kernel.org> | 2022-11-12 00:48:10 +0100 |
|---|---|---|
| committer | Alejandro Colomar <alx@kernel.org> | 2022-11-12 20:22:07 +0100 |
| commit | b08dc96589cdbe65c206d36e2cd516b24967d394 (patch) | |
| tree | 4e7e274ad52684a3c0f28ff54efe79cc2b139e6a | |
| parent | 7906529a9fdda226cb5914eec4ab347f4e761a02 (diff) | |
| download | unit-b08dc96589cdbe65c206d36e2cd516b24967d394.tar.gz unit-b08dc96589cdbe65c206d36e2cd516b24967d394.tar.bz2 | |
Added nxt_char_cast() macro for casting safely.
This macro is similar to C++'s static_cast(). It allows a very-limited
set of casts to be performed, but rejects most casts. Basically, it
only allows converting from char to u_char and vice-versa, while
respecting the const qualifier.
See the _Generic(3) manual page for more details and examples.
Cc: Andrew Clayton <a.clayton@nginx.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
| -rw-r--r-- | src/nxt_clang.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/nxt_clang.h b/src/nxt_clang.h index 6803ffc8..93b91f39 100644 --- a/src/nxt_clang.h +++ b/src/nxt_clang.h @@ -13,6 +13,43 @@ #define nxt_cdecl +#if (NXT_HAVE_C11_GENERIC) +#define nxt_char_cast(t, p) \ + _Generic(&*(p), \ + u_char *: \ + _Generic((t) NULL, \ + char *: (t) (p), \ + const char *: (t) (p), \ + default: (p)), \ + u_char **: \ + _Generic((t) NULL, \ + char **: (t) (p), \ + default: (p)), \ + const u_char *: \ + _Generic((t) NULL, \ + const char *: (t) (p), \ + default: (p)), \ + char *: \ + _Generic((t) NULL, \ + u_char *: (t) (p), \ + const u_char *: (t) (p), \ + default: (p)), \ + char **: \ + _Generic((t) NULL, \ + u_char **: (t) (p), \ + default: (p)), \ + const char *: \ + _Generic((t) NULL, \ + const u_char *: (t) (p), \ + default: (p)), \ + default: \ + (p) \ + ) +#else +#define nxt_char_cast(t, p) ((t) (p)) +#endif + + #if (NXT_CLANG) /* Any __asm__ directive disables loop vectorization in GCC and Clang. */ |
