Skip to content

Commit 0079ab1

Browse files
duda-patrykcarlescufi
authored andcommitted
toolchain/llvm: Fix integer macros implementation
Currently __INT64_C() doesn't add any suffix to provided constant which causes the constant to have 'int' type. On the other side, __UINT64_C() adds ULL suffix which makes the constant 'unsigned long long'. Of course, __INT64_C() is wrong here because 'int' type, in most cases, has 32 bit width. According to the C standard, these macros are for minimum-width integer constants. For example, it means that using INT16_C() macro will give you constant with type the same as 'int_least16_t' type. Clang doesn't provide defines like __INT_LEAST16_C_SUFFIX__, which makes implementation difficult, because we need to determine appropriate type first. Signed-off-by: Patryk Duda <[email protected]>
1 parent 1f4e911 commit 0079ab1

File tree

1 file changed

+83
-8
lines changed
  • include/zephyr/toolchain

1 file changed

+83
-8
lines changed

include/zephyr/toolchain/llvm.h

+83-8
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,94 @@
3737
#ifdef CONFIG_MINIMAL_LIBC
3838
#ifndef CONFIG_ENFORCE_ZEPHYR_STDINT
3939

40-
#define __INT8_C(x) x
41-
#define __UINT8_C(x) x ## U
42-
#define __INT16_C(x) x
43-
#define __UINT16_C(x) x ## U
40+
#define __int_c(v, suffix) v ## suffix
41+
#define int_c(v, suffix) __int_c(v, suffix)
42+
#define uint_c(v, suffix) __int_c(v ## U, suffix)
43+
44+
#ifdef __INT64_TYPE__
45+
#undef __int_least64_c_suffix__
46+
#undef __int_least32_c_suffix__
47+
#undef __int_least16_c_suffix__
48+
#undef __int_least8_c_suffix__
49+
#ifdef __INT64_C_SUFFIX__
50+
#define __int_least64_c_suffix__ __INT64_C_SUFFIX__
51+
#define __int_least32_c_suffix__ __INT64_C_SUFFIX__
52+
#define __int_least16_c_suffix__ __INT64_C_SUFFIX__
53+
#define __int_least8_c_suffix__ __INT64_C_SUFFIX__
54+
#endif /* __INT64_C_SUFFIX__ */
55+
#endif /* __INT64_TYPE__ */
56+
57+
#ifdef __INT_LEAST64_TYPE__
58+
#ifdef __int_least64_c_suffix__
59+
#define __INT64_C(x) int_c(x, __int_least64_c_suffix__)
60+
#define __UINT64_C(x) uint_c(x, __int_least64_c_suffix__)
61+
#else
62+
#define __INT64_C(x) x
63+
#define __UINT64_C(x) x ## U
64+
#endif /* __int_least64_c_suffix__ */
65+
#endif /* __INT_LEAST64_TYPE__ */
66+
67+
#ifdef __INT32_TYPE__
68+
#undef __int_least32_c_suffix__
69+
#undef __int_least16_c_suffix__
70+
#undef __int_least8_c_suffix__
71+
#ifdef __INT32_C_SUFFIX__
72+
#define __int_least32_c_suffix__ __INT32_C_SUFFIX__
73+
#define __int_least16_c_suffix__ __INT32_C_SUFFIX__
74+
#define __int_least8_c_suffix__ __INT32_C_SUFFIX__
75+
#endif /* __INT32_C_SUFFIX__ */
76+
#endif /* __INT32_TYPE__ */
77+
78+
#ifdef __INT_LEAST32_TYPE__
79+
#ifdef __int_least32_c_suffix__
80+
#define __INT32_C(x) int_c(x, __int_least32_c_suffix__)
81+
#define __UINT32_C(x) uint_c(x, __int_least32_c_suffix__)
82+
#else
4483
#define __INT32_C(x) x
4584
#define __UINT32_C(x) x ## U
46-
#define __INT64_C(x) x
47-
#define __UINT64_C(x) x ## ULL
85+
#endif /* __int_least32_c_suffix__ */
86+
#endif /* __INT_LEAST32_TYPE__ */
87+
88+
#ifdef __INT16_TYPE__
89+
#undef __int_least16_c_suffix__
90+
#undef __int_least8_c_suffix__
91+
#ifdef __INT16_C_SUFFIX__
92+
#define __int_least16_c_suffix__ __INT16_C_SUFFIX__
93+
#define __int_least8_c_suffix__ __INT16_C_SUFFIX__
94+
#endif /* __INT16_C_SUFFIX__ */
95+
#endif /* __INT16_TYPE__ */
96+
97+
#ifdef __INT_LEAST16_TYPE__
98+
#ifdef __int_least16_c_suffix__
99+
#define __INT16_C(x) int_c(x, __int_least16_c_suffix__)
100+
#define __UINT16_C(x) uint_c(x, __int_least16_c_suffix__)
101+
#else
102+
#define __INT16_C(x) x
103+
#define __UINT16_C(x) x ## U
104+
#endif /* __int_least16_c_suffix__ */
105+
#endif /* __INT_LEAST16_TYPE__ */
106+
107+
#ifdef __INT8_TYPE__
108+
#undef __int_least8_c_suffix__
109+
#ifdef __INT8_C_SUFFIX__
110+
#define __int_least8_c_suffix__ __INT8_C_SUFFIX__
111+
#endif /* __INT8_C_SUFFIX__ */
112+
#endif /* __INT8_TYPE__ */
113+
114+
#ifdef __INT_LEAST8_TYPE__
115+
#ifdef __int_least8_c_suffix__
116+
#define __INT8_C(x) int_c(x, __int_least8_c_suffix__)
117+
#define __UINT8_C(x) uint_c(x, __int_least8_c_suffix__)
118+
#else
119+
#define __INT8_C(x) x
120+
#define __UINT8_C(x) x ## U
121+
#endif /* __int_least8_c_suffix__ */
122+
#endif /* __INT_LEAST8_TYPE__ */
48123

49124
#endif /* !CONFIG_ENFORCE_ZEPHYR_STDINT */
50125

51-
#define __INTMAX_C(x) x
52-
#define __UINTMAX_C(x) x ## ULL
126+
#define __INTMAX_C(x) int_c(x, __INTMAX_C_SUFFIX__)
127+
#define __UINTMAX_C(x) int_c(x, __UINTMAX_C_SUFFIX__)
53128

54129
#endif /* CONFIG_MINIMAL_LIBC */
55130

0 commit comments

Comments
 (0)