Skip to content

[libc][c23] make inttypes.h compat with c23 #84493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 134 additions & 3 deletions libc/include/llvm-libc-macros/inttypes-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#define LLVM_LIBC_MACROS_INTTYPES_MACROS_H

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

// clang provides these macros, so we don't need to define them.
// TODO: ISO C23 will provide binary notations.

#ifndef __clang__
#if __UINTPTR_MAX__ == __UINT64_MAX__
#define __PRI64 "l"
Expand Down Expand Up @@ -117,6 +115,94 @@
#define __UINTPTR_FMTX__ __PRIPTR "X"
#endif

// only recent clang provides these macros, so we need to check if they are
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: using time based adjectives such as "recent" or "modern" in comments doesn't tend to age well. In 10 years, consider how this comment will have aged. Instead, it's better to put explicit compiler versions in sources since that's easily grep-able. That has the benefit that when we raise the minimum required supported compiler versions, we can easily grep for the work arounds previously deployed to support older toolchains, then delete them.

// available.
#ifndef __UINT8_FMTb__
#define __UINT8_FMTb__ "hhb"
#endif
#ifndef __UINT16_FMTb__
#define __UINT16_FMTb__ "hb"
#endif
#ifndef __UINT32_FMTb__
#define __UINT32_FMTb__ "b"
#endif
#ifndef __UINT64_FMTb__
#define __UINT64_FMTb__ __PRI64 "b"
#endif
#ifndef __UINT_LEAST8_FMTb__
#define __UINT_LEAST8_FMTb__ "hhb"
#endif
#ifndef __UINT_LEAST16_FMTb__
#define __UINT_LEAST16_FMTb__ "hb"
#endif
#ifndef __UINT_LEAST32_FMTb__
#define __UINT_LEAST32_FMTb__ "b"
#endif
#ifndef __UINT_LEAST64_FMTb__
#define __UINT_LEAST64_FMTb__ __PRI64 "b"
#endif
#ifndef __UINT_FAST8_FMTb__
#define __UINT_FAST8_FMTb__ "hhb"
#endif
#ifndef __UINT_FAST16_FMTb__
#define __UINT_FAST16_FMTb__ "hb"
#endif
#ifndef __UINT_FAST32_FMTb__
#define __UINT_FAST32_FMTb__ "b"
#endif
#ifndef __UINT_FAST64_FMTb__
#define __UINT_FAST64_FMTb__ __PRI64 "b"
#endif
#ifndef __UINTMAX_FMTb__
#define __UINTMAX_FMTb__ __PRI64 "b"
#endif
#ifndef __UINTPTR_FMTb__
#define __UINTPTR_FMTb__ __PRIPTR "b"
#endif

#ifndef __UINT8_FMTB__
#define __UINT8_FMTB__ "hhB"
#endif
#ifndef __UINT16_FMTB__
#define __UINT16_FMTB__ "hB"
#endif
#ifndef __UINT32_FMTB__
#define __UINT32_FMTB__ "B"
#endif
#ifndef __UINT64_FMTB__
#define __UINT64_FMTB__ __PRI64 "B"
#endif
#ifndef __UINT_LEAST8_FMTB__
#define __UINT_LEAST8_FMTB__ "hhB"
#endif
#ifndef __UINT_LEAST16_FMTB__
#define __UINT_LEAST16_FMTB__ "hB"
#endif
#ifndef __UINT_LEAST32_FMTB__
#define __UINT_LEAST32_FMTB__ "B"
#endif
#ifndef __UINT_LEAST64_FMTB__
#define __UINT_LEAST64_FMTB__ __PRI64 "B"
#endif
#ifndef __UINT_FAST8_FMTB__
#define __UINT_FAST8_FMTB__ "hhB"
#endif
#ifndef __UINT_FAST16_FMTB__
#define __UINT_FAST16_FMTB__ "hB"
#endif
#ifndef __UINT_FAST32_FMTB__
#define __UINT_FAST32_FMTB__ "B"
#endif
#ifndef __UINT_FAST64_FMTB__
#define __UINT_FAST64_FMTB__ __PRI64 "B"
#endif
#ifndef __UINTMAX_FMTB__
#define __UINTMAX_FMTB__ __PRI64 "B"
#endif
#ifndef __UINTPTR_FMTB__
#define __UINTPTR_FMTB__ __PRIPTR "B"
#endif

// The fprintf() macros for signed integers.
#define PRId8 __INT8_FMTd__
#define PRId16 __INT16_FMTd__
Expand Down Expand Up @@ -209,6 +295,36 @@
#define PRIXMAX __UINTMAX_FMTX__
#define PRIXPTR __UINTPTR_FMTX__

#define PRIb8 __UINT8_FMTb__
#define PRIb16 __UINT16_FMTb__
#define PRIb32 __UINT32_FMTb__
#define PRIb64 __UINT64_FMTb__
#define PRIbLEAST8 __UINT_LEAST8_FMTb__
#define PRIbLEAST16 __UINT_LEAST16_FMTb__
#define PRIbLEAST32 __UINT_LEAST32_FMTb__
#define PRIbLEAST64 __UINT_LEAST64_FMTb__
#define PRIbFAST8 __UINT_FAST8_FMTb__
#define PRIbFAST16 __UINT_FAST16_FMTb__
#define PRIbFAST32 __UINT_FAST32_FMTb__
#define PRIbFAST64 __UINT_FAST64_FMTb__
#define PRIbMAX __UINTMAX_FMTb__
#define PRIbPTR __UINTPTR_FMTb__

#define PRIB8 __UINT8_FMTB__
#define PRIB16 __UINT16_FMTB__
#define PRIB32 __UINT32_FMTB__
#define PRIB64 __UINT64_FMTB__
#define PRIBLEAST8 __UINT_LEAST8_FMTB__
#define PRIBLEAST16 __UINT_LEAST16_FMTB__
#define PRIBLEAST32 __UINT_LEAST32_FMTB__
#define PRIBLEAST64 __UINT_LEAST64_FMTB__
#define PRIBFAST8 __UINT_FAST8_FMTB__
#define PRIBFAST16 __UINT_FAST16_FMTB__
#define PRIBFAST32 __UINT_FAST32_FMTB__
#define PRIBFAST64 __UINT_FAST64_FMTB__
#define PRIBMAX __UINTMAX_FMTB__
#define PRIBPTR __UINTPTR_FMTB__

// The fscanf() macros for signed integers.
#define SCNd8 __INT8_FMTd__
#define SCNd16 __INT16_FMTd__
Expand Down Expand Up @@ -286,4 +402,19 @@
#define SCNxMAX __UINTMAX_FMTx__
#define SCNxPTR __UINTPTR_FMTx__

#define SCNb8 __UINT8_FMTb__
#define SCNb16 __UINT16_FMTb__
#define SCNb32 __UINT32_FMTb__
#define SCNb64 __UINT64_FMTb__
#define SCNbLEAST8 __UINT_LEAST8_FMTb__
#define SCNbLEAST16 __UINT_LEAST16_FMTb__
#define SCNbLEAST32 __UINT_LEAST32_FMTb__
#define SCNbLEAST64 __UINT_LEAST64_FMTb__
#define SCNbFAST8 __UINT_FAST8_FMTb__
#define SCNbFAST16 __UINT_FAST16_FMTb__
#define SCNbFAST32 __UINT_FAST32_FMTb__
#define SCNbFAST64 __UINT_FAST64_FMTb__
#define SCNbMAX __UINTMAX_FMTb__
#define SCNbPTR __UINTPTR_FMTb__

#endif // LLVM_LIBC_MACROS_INTTYPES_MACROS_H
4 changes: 3 additions & 1 deletion libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,9 @@ def StdC : StandardSpec<"stdc"> {

HeaderSpec IntTypes = HeaderSpec<
"inttypes.h",
[], // Macros
[
Macro<"__STDC_VERSION_INTTYPES_H__">,
], // Macros
[
IMaxDivTType,
], // Types
Expand Down