Skip to content

Commit 29afd64

Browse files
[libc][c23] make inttypes.h compat with c23 (#84493)
1 parent 2b4d818 commit 29afd64

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

libc/include/llvm-libc-macros/inttypes-macros.h

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
#define LLVM_LIBC_MACROS_INTTYPES_MACROS_H
1010

1111
// fprintf/scanf format macros.
12-
// POSIX.1-2008, Technical Corrigendum 1, XBD/TC1-2008/0050 [211] is applied.
12+
#define __STDC_VERSION_INTTYPES_H__ 202311L
1313

1414
// clang provides these macros, so we don't need to define them.
15-
// TODO: ISO C23 will provide binary notations.
16-
1715
#ifndef __clang__
1816
#if __UINTPTR_MAX__ == __UINT64_MAX__
1917
#define __PRI64 "l"
@@ -117,6 +115,94 @@
117115
#define __UINTPTR_FMTX__ __PRIPTR "X"
118116
#endif
119117

118+
// only recent clang provides these macros, so we need to check if they are
119+
// available.
120+
#ifndef __UINT8_FMTb__
121+
#define __UINT8_FMTb__ "hhb"
122+
#endif
123+
#ifndef __UINT16_FMTb__
124+
#define __UINT16_FMTb__ "hb"
125+
#endif
126+
#ifndef __UINT32_FMTb__
127+
#define __UINT32_FMTb__ "b"
128+
#endif
129+
#ifndef __UINT64_FMTb__
130+
#define __UINT64_FMTb__ __PRI64 "b"
131+
#endif
132+
#ifndef __UINT_LEAST8_FMTb__
133+
#define __UINT_LEAST8_FMTb__ "hhb"
134+
#endif
135+
#ifndef __UINT_LEAST16_FMTb__
136+
#define __UINT_LEAST16_FMTb__ "hb"
137+
#endif
138+
#ifndef __UINT_LEAST32_FMTb__
139+
#define __UINT_LEAST32_FMTb__ "b"
140+
#endif
141+
#ifndef __UINT_LEAST64_FMTb__
142+
#define __UINT_LEAST64_FMTb__ __PRI64 "b"
143+
#endif
144+
#ifndef __UINT_FAST8_FMTb__
145+
#define __UINT_FAST8_FMTb__ "hhb"
146+
#endif
147+
#ifndef __UINT_FAST16_FMTb__
148+
#define __UINT_FAST16_FMTb__ "hb"
149+
#endif
150+
#ifndef __UINT_FAST32_FMTb__
151+
#define __UINT_FAST32_FMTb__ "b"
152+
#endif
153+
#ifndef __UINT_FAST64_FMTb__
154+
#define __UINT_FAST64_FMTb__ __PRI64 "b"
155+
#endif
156+
#ifndef __UINTMAX_FMTb__
157+
#define __UINTMAX_FMTb__ __PRI64 "b"
158+
#endif
159+
#ifndef __UINTPTR_FMTb__
160+
#define __UINTPTR_FMTb__ __PRIPTR "b"
161+
#endif
162+
163+
#ifndef __UINT8_FMTB__
164+
#define __UINT8_FMTB__ "hhB"
165+
#endif
166+
#ifndef __UINT16_FMTB__
167+
#define __UINT16_FMTB__ "hB"
168+
#endif
169+
#ifndef __UINT32_FMTB__
170+
#define __UINT32_FMTB__ "B"
171+
#endif
172+
#ifndef __UINT64_FMTB__
173+
#define __UINT64_FMTB__ __PRI64 "B"
174+
#endif
175+
#ifndef __UINT_LEAST8_FMTB__
176+
#define __UINT_LEAST8_FMTB__ "hhB"
177+
#endif
178+
#ifndef __UINT_LEAST16_FMTB__
179+
#define __UINT_LEAST16_FMTB__ "hB"
180+
#endif
181+
#ifndef __UINT_LEAST32_FMTB__
182+
#define __UINT_LEAST32_FMTB__ "B"
183+
#endif
184+
#ifndef __UINT_LEAST64_FMTB__
185+
#define __UINT_LEAST64_FMTB__ __PRI64 "B"
186+
#endif
187+
#ifndef __UINT_FAST8_FMTB__
188+
#define __UINT_FAST8_FMTB__ "hhB"
189+
#endif
190+
#ifndef __UINT_FAST16_FMTB__
191+
#define __UINT_FAST16_FMTB__ "hB"
192+
#endif
193+
#ifndef __UINT_FAST32_FMTB__
194+
#define __UINT_FAST32_FMTB__ "B"
195+
#endif
196+
#ifndef __UINT_FAST64_FMTB__
197+
#define __UINT_FAST64_FMTB__ __PRI64 "B"
198+
#endif
199+
#ifndef __UINTMAX_FMTB__
200+
#define __UINTMAX_FMTB__ __PRI64 "B"
201+
#endif
202+
#ifndef __UINTPTR_FMTB__
203+
#define __UINTPTR_FMTB__ __PRIPTR "B"
204+
#endif
205+
120206
// The fprintf() macros for signed integers.
121207
#define PRId8 __INT8_FMTd__
122208
#define PRId16 __INT16_FMTd__
@@ -209,6 +295,36 @@
209295
#define PRIXMAX __UINTMAX_FMTX__
210296
#define PRIXPTR __UINTPTR_FMTX__
211297

298+
#define PRIb8 __UINT8_FMTb__
299+
#define PRIb16 __UINT16_FMTb__
300+
#define PRIb32 __UINT32_FMTb__
301+
#define PRIb64 __UINT64_FMTb__
302+
#define PRIbLEAST8 __UINT_LEAST8_FMTb__
303+
#define PRIbLEAST16 __UINT_LEAST16_FMTb__
304+
#define PRIbLEAST32 __UINT_LEAST32_FMTb__
305+
#define PRIbLEAST64 __UINT_LEAST64_FMTb__
306+
#define PRIbFAST8 __UINT_FAST8_FMTb__
307+
#define PRIbFAST16 __UINT_FAST16_FMTb__
308+
#define PRIbFAST32 __UINT_FAST32_FMTb__
309+
#define PRIbFAST64 __UINT_FAST64_FMTb__
310+
#define PRIbMAX __UINTMAX_FMTb__
311+
#define PRIbPTR __UINTPTR_FMTb__
312+
313+
#define PRIB8 __UINT8_FMTB__
314+
#define PRIB16 __UINT16_FMTB__
315+
#define PRIB32 __UINT32_FMTB__
316+
#define PRIB64 __UINT64_FMTB__
317+
#define PRIBLEAST8 __UINT_LEAST8_FMTB__
318+
#define PRIBLEAST16 __UINT_LEAST16_FMTB__
319+
#define PRIBLEAST32 __UINT_LEAST32_FMTB__
320+
#define PRIBLEAST64 __UINT_LEAST64_FMTB__
321+
#define PRIBFAST8 __UINT_FAST8_FMTB__
322+
#define PRIBFAST16 __UINT_FAST16_FMTB__
323+
#define PRIBFAST32 __UINT_FAST32_FMTB__
324+
#define PRIBFAST64 __UINT_FAST64_FMTB__
325+
#define PRIBMAX __UINTMAX_FMTB__
326+
#define PRIBPTR __UINTPTR_FMTB__
327+
212328
// The fscanf() macros for signed integers.
213329
#define SCNd8 __INT8_FMTd__
214330
#define SCNd16 __INT16_FMTd__
@@ -286,4 +402,19 @@
286402
#define SCNxMAX __UINTMAX_FMTx__
287403
#define SCNxPTR __UINTPTR_FMTx__
288404

405+
#define SCNb8 __UINT8_FMTb__
406+
#define SCNb16 __UINT16_FMTb__
407+
#define SCNb32 __UINT32_FMTb__
408+
#define SCNb64 __UINT64_FMTb__
409+
#define SCNbLEAST8 __UINT_LEAST8_FMTb__
410+
#define SCNbLEAST16 __UINT_LEAST16_FMTb__
411+
#define SCNbLEAST32 __UINT_LEAST32_FMTb__
412+
#define SCNbLEAST64 __UINT_LEAST64_FMTb__
413+
#define SCNbFAST8 __UINT_FAST8_FMTb__
414+
#define SCNbFAST16 __UINT_FAST16_FMTb__
415+
#define SCNbFAST32 __UINT_FAST32_FMTb__
416+
#define SCNbFAST64 __UINT_FAST64_FMTb__
417+
#define SCNbMAX __UINTMAX_FMTb__
418+
#define SCNbPTR __UINTPTR_FMTb__
419+
289420
#endif // LLVM_LIBC_MACROS_INTTYPES_MACROS_H

libc/spec/stdc.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,9 @@ def StdC : StandardSpec<"stdc"> {
948948

949949
HeaderSpec IntTypes = HeaderSpec<
950950
"inttypes.h",
951-
[], // Macros
951+
[
952+
Macro<"__STDC_VERSION_INTTYPES_H__">,
953+
], // Macros
952954
[
953955
IMaxDivTType,
954956
], // Types

0 commit comments

Comments
 (0)