From: Zhangjin Wu
Sent: 07 August 2023 06:58
...
+/* __auto_type is used instead of __typeof__ to workaround the build error
- 'error: assignment of read-only variable' when the argument has 'const' in
- the type, but __auto_type is a new feature from newer gcc version and it
- only works with 'const' from gcc 11.0 (__GXX_ABI_VERSION = 1016)
- */
You can use typeof((x) + 0) to lose the 'const' flag. The only downside is that char/short become int.
+#if __GXX_ABI_VERSION >= 1016 +#define __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT +#endif
+#ifdef __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT +#define __sysret(arg) \ +({ \
- __auto_type __ret = (arg); \
- if (__is_signed_type(__typeof__(arg))) { \
if (__ret < 0) { \
SET_ERRNO(-(long)__ret); \
__ret = (__typeof__(arg))(-1L); \
} \
- } else { \
if ((unsigned long)__ret >= (unsigned long)-MAX_ERRNO) { \
SET_ERRNO(-(long)__ret); \
__ret = (__typeof__(arg))(-1L); \
} \
- } \
- __ret; \
+})
+#else /* ! __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT */ +#define __sysret(arg) \ +({ \
- long __ret = (long)(arg); \
- if (__is_signed_type(__typeof__(arg))) { \
if (__ret < 0) { \
SET_ERRNO(-__ret); \
__ret = -1L; \
} \
- } else { \
if ((unsigned long)__ret >= (unsigned long)-MAX_ERRNO) { \
SET_ERRNO(-__ret); \
__ret = -1L; \
} \
- } \
- (__typeof__(arg))__ret; \
+}) +#endif /* ! __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT */
with (retyped so it may be wrong): #define is_constexpr(x) sizeof(*(0 ? (void *)((long)(x) * 0) : (int *)0)) == 1) and (because even (void *)0 isn't completely constant): #define is_pointer(x) (!is_constexpr((typeof(x))0))
You can probably do: #define __sysret(arg) \ ({ \ typeof((arg) + 0) __ret = arg; \ if (__built_choose_expr(is_pointer(arg), (unsigned long)-(MAX_ERRNO+1), __ret) \ < (__built_choose_expr(is_pointer(arg), (unsigned long)__ret, 0)) { \ SET_ERRNO(-__ret); \ __reg = typeof(ret)-1L; \ } \ __ret; \ })
Apart from the annoyance of having to reverse the conditional that only has one copy of the check.
Using two __builtin_choose_expr() saves you having to write two comparisons that are valid for both pointer and integer.
David
- Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)