[libc-commits] [libc] [libc] Add __iso99_fscanf alias. (PR #213125)

via libc-commits libc-commits at lists.llvm.org
Fri Jul 31 14:48:24 PDT 2026


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/213125

>From 1370b6876395ff3876fd4391280e593a5c2d77ff Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 30 Jul 2026 19:59:22 +0000
Subject: [PATCH 1/3] [libc] Add __iso99_fscanf alias.

---
 libc/src/__support/common.h         | 17 +++++++++++++++++
 libc/src/stdio/baremetal/fscanf.cpp |  5 +++++
 2 files changed, 22 insertions(+)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index d90fe7b8ae98d..e946ee4e0fb20 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -56,17 +56,34 @@
       __##name##_impl__ asm(c_alias);                                          \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(c_alias)]];                 \
   type __##name##_impl__ arglist
+
+#define LLVM_LIBC_ADD_FUNCTION_C_ALIAS(name, c_alias)                          \
+  extern "C" decltype(LIBC_NAMESPACE::name) c_alias [[gnu::alias(#name)]];     \
+  static_assert(true, "Require semicolon.")
+
 #else // __APPLE__
 #define LLVM_LIBC_FUNCTION_IMPL_4(type, name, arglist, c_alias)                \
   LLVM_LIBC_ATTR(name)                                                         \
   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) name asm(             \
       "_" c_alias);                                                            \
   type name arglist
+
+// clang-format off
+#define LLVM_LIBC_ADD_FUNCTION_C_ALIAS(name, c_alias)                          \
+  asm(".text\n"                                                                \
+      ".global " "_" #c_alias "\n"                                             \
+      ".type " "_" #c_alias ", @function\n"                                    \
+      "_" #c_alias " = " "_" #name "\n");                                      \
+  static_assert(true, "Require semicolon.")
+// clang-format on
 #endif // __APPLE__
 
 #else  // LIBC_COPT_PUBLIC_PACKAGING
 #define LLVM_LIBC_FUNCTION_IMPL_4(type, name, arglist, c_alias)                \
   type name arglist
+
+#define LLVM_LIBC_ADD_FUNCTION_C_ALIAS(name, c_alias)
+
 #endif // LIBC_COPT_PUBLIC_PACKAGING
 
 #define LLVM_LIBC_FUNCTION_IMPL_3(type, name, arglist)                         \
diff --git a/libc/src/stdio/baremetal/fscanf.cpp b/libc/src/stdio/baremetal/fscanf.cpp
index eb8b2326ad6b7..d20dd51cce657 100644
--- a/libc/src/stdio/baremetal/fscanf.cpp
+++ b/libc/src/stdio/baremetal/fscanf.cpp
@@ -36,3 +36,8 @@ LLVM_LIBC_FUNCTION(int, fscanf,
 }
 
 } // namespace LIBC_NAMESPACE_DECL
+
+#if ...
+// comments...
+LLVM_LIBC_ADD_FUNCTION_C_ALIAS(fscanf, __isoc99_fscanf);
+#endif

>From 7f08fc197cdad2dd50721f9309f524c17d93c9ce Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 31 Jul 2026 14:58:58 +0000
Subject: [PATCH 2/3] Specify guard.

---
 libc/src/stdio/baremetal/fscanf.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libc/src/stdio/baremetal/fscanf.cpp b/libc/src/stdio/baremetal/fscanf.cpp
index d20dd51cce657..36ecd27ea0e98 100644
--- a/libc/src/stdio/baremetal/fscanf.cpp
+++ b/libc/src/stdio/baremetal/fscanf.cpp
@@ -37,7 +37,6 @@ LLVM_LIBC_FUNCTION(int, fscanf,
 
 } // namespace LIBC_NAMESPACE_DECL
 
-#if ...
-// comments...
+#ifdef LIBC_STDIO_ADD_ISO99_FSCANF
 LLVM_LIBC_ADD_FUNCTION_C_ALIAS(fscanf, __isoc99_fscanf);
-#endif
+#endif // LIBC_STDIO_ADD_ISO99_FSCANF

>From ab8b308fef4f90fa8616671b72394ec644e3e5b5 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 31 Jul 2026 21:47:45 +0000
Subject: [PATCH 3/3] Add config flag.

---
 libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 4 ++++
 libc/config/config.json                             | 4 ++++
 libc/src/stdio/baremetal/fscanf.cpp                 | 4 ++--
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 600df74f552b0..3e9755c221c07 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -193,6 +193,10 @@ function(_get_compile_options_from_config output_var)
     list(APPEND config_options "-DLIBC_COPT_USE_C_ASSERT")
   endif()
 
+  if(LIBC_CONF_SCANF_PROVIDE_ISO99_ALIASES)
+    list(APPEND config_options "-DLIBC_CONF_SCANF_PROVIDE_ISO99_ALIASES")
+  endif()
+
   set(${output_var} ${config_options} PARENT_SCOPE)
 endfunction(_get_compile_options_from_config)
 
diff --git a/libc/config/config.json b/libc/config/config.json
index d576d7d14e4af..94fef76a74799 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -73,6 +73,10 @@
     "LIBC_CONF_SCANF_DISABLE_INDEX_MODE": {
       "value": false,
       "doc": "Disable index mode in the scanf format string."
+    },
+    "LIBC_CONF_SCANF_PROVIDE_ISO99_ALIASES": {
+      "value": false,
+      "doc": "Add __iso99_* aliases for scanf's functions."
     }
   },
   "str_to_float": {
diff --git a/libc/src/stdio/baremetal/fscanf.cpp b/libc/src/stdio/baremetal/fscanf.cpp
index 36ecd27ea0e98..0ea3d97230269 100644
--- a/libc/src/stdio/baremetal/fscanf.cpp
+++ b/libc/src/stdio/baremetal/fscanf.cpp
@@ -37,6 +37,6 @@ LLVM_LIBC_FUNCTION(int, fscanf,
 
 } // namespace LIBC_NAMESPACE_DECL
 
-#ifdef LIBC_STDIO_ADD_ISO99_FSCANF
+#ifdef LIBC_CONF_SCANF_PROVIDE_ISO99_ALIASES
 LLVM_LIBC_ADD_FUNCTION_C_ALIAS(fscanf, __isoc99_fscanf);
-#endif // LIBC_STDIO_ADD_ISO99_FSCANF
+#endif // LIBC_CONF_SCANF_PROVIDE_ISO99_ALIASES



More information about the libc-commits mailing list