[libc-commits] [libc] [libc] Support for scanf on baremetal (PR #131043)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Thu Mar 20 13:09:59 PDT 2025


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/131043

>From dee82741b51822fb0780b030b4f747e07d76e527 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 12 Mar 2025 16:08:42 -0700
Subject: [PATCH 1/6] [libc] Support for scanf on baremetal

This uses the templatized scanf Reader interface introduced in #131037.
---
 libc/config/baremetal/aarch64/entrypoints.txt |  6 +-
 libc/config/baremetal/arm/entrypoints.txt     |  6 +-
 libc/config/baremetal/riscv/entrypoints.txt   |  6 +-
 libc/src/stdio/CMakeLists.txt                 | 58 ++-----------------
 libc/src/stdio/baremetal/CMakeLists.txt       | 26 +++++++++
 libc/src/stdio/baremetal/scanf.cpp            | 52 +++++++++++++++++
 libc/src/stdio/baremetal/vscanf.cpp           | 51 ++++++++++++++++
 libc/src/stdio/generic/CMakeLists.txt         | 54 +++++++++++++++++
 libc/src/stdio/{ => generic}/fscanf.cpp       |  0
 libc/src/stdio/{ => generic}/scanf.cpp        |  0
 libc/src/stdio/{ => generic}/vfscanf.cpp      |  0
 libc/src/stdio/{ => generic}/vscanf.cpp       |  0
 libc/src/stdio/scanf_core/CMakeLists.txt      |  9 +--
 13 files changed, 201 insertions(+), 67 deletions(-)
 create mode 100644 libc/src/stdio/baremetal/scanf.cpp
 create mode 100644 libc/src/stdio/baremetal/vscanf.cpp
 rename libc/src/stdio/{ => generic}/fscanf.cpp (100%)
 rename libc/src/stdio/{ => generic}/scanf.cpp (100%)
 rename libc/src/stdio/{ => generic}/vfscanf.cpp (100%)
 rename libc/src/stdio/{ => generic}/vscanf.cpp (100%)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 2c226ef176c08..ffe7e622279a0 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -128,13 +128,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.remove
+    libc.src.stdio.scanf
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
-    libc.src.stdio.asprintf
+    libc.src.stdio.sscanf
     libc.src.stdio.vprintf
+    libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf
     libc.src.stdio.vsprintf
-    libc.src.stdio.vasprintf
+    libc.src.stdio.vsscanf
 
     # stdbit.h entrypoints
     libc.src.stdbit.stdc_bit_ceil_uc
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index d7a01bdf90b3f..a6628d1c56219 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -128,13 +128,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.remove
+    libc.src.stdio.scanf
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
-    libc.src.stdio.asprintf
+    libc.src.stdio.sscanf
     libc.src.stdio.vprintf
+    libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf
     libc.src.stdio.vsprintf
-    libc.src.stdio.vasprintf
+    libc.src.stdio.vsscanf
 
     # stdbit.h entrypoints
     libc.src.stdbit.stdc_bit_ceil_uc
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ae00803dd0def..ccbcb85a216a8 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -124,13 +124,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.remove
+    libc.src.stdio.scanf
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
-    libc.src.stdio.asprintf
+    libc.src.stdio.sscanf
     libc.src.stdio.vprintf
+    libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf
     libc.src.stdio.vsprintf
-    libc.src.stdio.vasprintf
+    libc.src.stdio.vsscanf
 
     # stdbit.h entrypoints
     libc.src.stdbit.stdc_bit_ceil_uc
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index b9bc904471df9..23c103c1d6465 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -95,20 +95,6 @@ add_entrypoint_object(
     libc.src.__support.File.platform_file
 )
 
-list(APPEND scanf_deps
-      libc.src.__support.arg_list
-      libc.src.stdio.scanf_core.vfscanf_internal
-      libc.hdr.types.FILE
-)
-
-if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_GPU)
-  list(APPEND scanf_deps
-      libc.src.__support.File.file
-      libc.src.__support.File.platform_file
-      libc.src.__support.File.platform_stdin
-  )
-endif()
-
 add_entrypoint_object(
   sscanf
   SRCS
@@ -133,46 +119,6 @@ add_entrypoint_object(
     libc.src.stdio.scanf_core.scanf_main
 )
 
-add_entrypoint_object(
-  fscanf
-  SRCS
-    fscanf.cpp
-  HDRS
-    fscanf.h
-  DEPENDS
-    ${scanf_deps}
-)
-
-add_entrypoint_object(
-  vfscanf
-  SRCS
-    vfscanf.cpp
-  HDRS
-    vfscanf.h
-  DEPENDS
-    ${scanf_deps}
-)
-
-add_entrypoint_object(
-  scanf
-  SRCS
-    scanf.cpp
-  HDRS
-    scanf.h
-  DEPENDS
-    ${scanf_deps}
-)
-
-add_entrypoint_object(
-  vscanf
-  SRCS
-    vscanf.cpp
-  HDRS
-    vscanf.h
-  DEPENDS
-    ${scanf_deps}
-)
-
 add_entrypoint_object(
   sprintf
   SRCS
@@ -295,8 +241,12 @@ add_stdio_entrypoint_object(getchar)
 add_stdio_entrypoint_object(getchar_unlocked)
 add_stdio_entrypoint_object(fgets)
 add_stdio_entrypoint_object(ungetc)
+add_stdio_entrypoint_object(scanf)
+add_stdio_entrypoint_object(fscanf)
 add_stdio_entrypoint_object(stdin)
 add_stdio_entrypoint_object(stdout)
 add_stdio_entrypoint_object(stderr)
 add_stdio_entrypoint_object(vprintf)
 add_stdio_entrypoint_object(vfprintf)
+add_stdio_entrypoint_object(vscanf)
+add_stdio_entrypoint_object(vfscanf)
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index c5cf4a8e0e5b5..4abe8bc66b25b 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -55,6 +55,19 @@ add_entrypoint_object(
     libc.src.__support.CPP.string_view
 )
 
+add_entrypoint_object(
+  scanf
+  SRCS
+    scanf.cpp
+  HDRS
+    ../scanf.h
+  DEPENDS
+    libc.src.stdio.scanf_core.scanf_main
+    libc.src.stdio.scanf_core.reader
+    libc.src.__support.arg_list
+    libc.src.__support.OSUtil.osutil
+)
+
 add_entrypoint_object(
   vprintf
   SRCS
@@ -67,3 +80,16 @@ add_entrypoint_object(
     libc.src.__support.arg_list
     libc.src.__support.OSUtil.osutil
 )
+
+add_entrypoint_object(
+  vscanf
+  SRCS
+    vscanf.cpp
+  HDRS
+    ../vscanf.h
+  DEPENDS
+    libc.src.stdio.scanf_core.scanf_main
+    libc.src.stdio.scanf_core.reader
+    libc.src.__support.arg_list
+    libc.src.__support.OSUtil.osutil
+)
diff --git a/libc/src/stdio/baremetal/scanf.cpp b/libc/src/stdio/baremetal/scanf.cpp
new file mode 100644
index 0000000000000..1b0bf3e3afd54
--- /dev/null
+++ b/libc/src/stdio/baremetal/scanf.cpp
@@ -0,0 +1,52 @@
+//===-- Implementation of scanf ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/scanf.h"
+
+#include "hdr/stdio_macros.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/scanf_core/reader.h"
+#include "src/stdio/scanf_core/scanf_main.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace {
+
+struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
+  LIBC_INLINE char getc() {
+    char buf[1];
+    read_from_stdin(buf, sizeof(buf));
+    return buf[0];
+  }
+  LIBC_INLINE void ungetc(int) {}
+};
+
+} // namespace
+
+LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
+  va_list vlist;
+  va_start(vlist, format);
+  internal::ArgList args(vlist); // This holder class allows for easier copying
+                                 // and pointer semantics, as well as handling
+                                 // destruction automatically.
+  va_end(vlist);
+
+  StreamBuffer buffer;
+  scanf_core::Reader<StreamBuffer> reader(&buffer);
+
+  int retval = scanf_core::scanf_main(&reader, format, args);
+  // This is done to avoid including stdio.h in the internals. On most systems
+  // EOF is -1, so this will be transformed into just "return retval".
+  return (retval == -1) ? EOF : retval;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/vscanf.cpp b/libc/src/stdio/baremetal/vscanf.cpp
new file mode 100644
index 0000000000000..e0119d6146d89
--- /dev/null
+++ b/libc/src/stdio/baremetal/vscanf.cpp
@@ -0,0 +1,51 @@
+//===-- Implementation of vscanf --------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/vscanf.h"
+
+#include "hdr/stdio_macros.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/scanf_core/reader.h"
+#include "src/stdio/scanf_core/scanf_main.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace {
+
+struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
+  LIBC_INLINE char getc() {
+    char buf[1];
+    read_from_stdin(buf, sizeof(buf));
+    return buf[0];
+  }
+  LIBC_INLINE void ungetc(int) {}
+};
+
+} // namespace
+
+LLVM_LIBC_FUNCTION(int, vscanf,
+                   (const char *__restrict format, va_list vlist)) {
+  internal::ArgList args(vlist); // This holder class allows for easier copying
+                                 // and pointer semantics, as well as handling
+                                 // destruction automatically.
+  va_end(vlist);
+
+  StreamBuffer buffer;
+  scanf_core::Reader<StreamBuffer> reader(&buffer);
+
+  int retval = scanf_core::scanf_main(&reader, format, args);
+  // This is done to avoid including stdio.h in the internals. On most systems
+  // EOF is -1, so this will be transformed into just "return retval".
+  return (retval == -1) ? EOF : retval;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index bf301a6b0cb3c..9f568c5ab8d3a 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -425,6 +425,60 @@ add_entrypoint_object(
     ${fprintf_deps}
 )
 
+list(APPEND scanf_deps
+      libc.src.__support.arg_list
+      libc.src.stdio.scanf_core.vfscanf_internal
+      libc.hdr.types.FILE
+)
+
+if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_GPU)
+  list(APPEND scanf_deps
+      libc.src.__support.File.file
+      libc.src.__support.File.platform_file
+      libc.src.__support.File.platform_stdin
+  )
+endif()
+
+add_entrypoint_object(
+  fscanf
+  SRCS
+    fscanf.cpp
+  HDRS
+    ../fscanf.h
+  DEPENDS
+    ${scanf_deps}
+)
+
+add_entrypoint_object(
+  vfscanf
+  SRCS
+    vfscanf.cpp
+  HDRS
+    ../vfscanf.h
+  DEPENDS
+    ${scanf_deps}
+)
+
+add_entrypoint_object(
+  scanf
+  SRCS
+    scanf.cpp
+  HDRS
+    ../scanf.h
+  DEPENDS
+    ${scanf_deps}
+)
+
+add_entrypoint_object(
+  vscanf
+  SRCS
+    vscanf.cpp
+  HDRS
+    ../vscanf.h
+  DEPENDS
+    ${scanf_deps}
+)
+
 add_entrypoint_object(
   fgets
   SRCS
diff --git a/libc/src/stdio/fscanf.cpp b/libc/src/stdio/generic/fscanf.cpp
similarity index 100%
rename from libc/src/stdio/fscanf.cpp
rename to libc/src/stdio/generic/fscanf.cpp
diff --git a/libc/src/stdio/scanf.cpp b/libc/src/stdio/generic/scanf.cpp
similarity index 100%
rename from libc/src/stdio/scanf.cpp
rename to libc/src/stdio/generic/scanf.cpp
diff --git a/libc/src/stdio/vfscanf.cpp b/libc/src/stdio/generic/vfscanf.cpp
similarity index 100%
rename from libc/src/stdio/vfscanf.cpp
rename to libc/src/stdio/generic/vfscanf.cpp
diff --git a/libc/src/stdio/vscanf.cpp b/libc/src/stdio/generic/vscanf.cpp
similarity index 100%
rename from libc/src/stdio/vscanf.cpp
rename to libc/src/stdio/generic/vscanf.cpp
diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 014413ccaa8da..714b174892504 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -16,6 +16,8 @@ if(LIBC_TARGET_OS_IS_GPU)
     libc.src.stdio.ungetc
     libc.src.stdio.ferror
   )
+elseif(LIBC_TARGET_OS_IS_BAREMETAL)
+  # There's no FILE* for baremetal.
 elseif(LLVM_LIBC_FULL_BUILD)
   list(APPEND file_deps
     libc.src.__support.File.file
@@ -54,13 +56,6 @@ add_header_library(
     libc.src.__support.CPP.string_view
 )
 
-if(NOT(TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD AND
-  (NOT LIBC_TARGET_OS_IS_GPU))
-  # Not all platforms have a file implementation. If file is unvailable, and a
-  # full build is requested, then we must skip all file based scanf sections.
-  return()
-endif()
-
 add_object_library(
   scanf_main
   SRCS

>From cdad78efba3b4f07bed2741cf732d69c876abb4f Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 17 Mar 2025 23:57:00 -0700
Subject: [PATCH 2/6] Update the implementation

---
 libc/src/stdio/baremetal/getchar.cpp |  2 +-
 libc/src/stdio/baremetal/scanf.cpp   | 10 +++++-----
 libc/src/stdio/baremetal/vscanf.cpp  | 10 +++++-----
 3 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/libc/src/stdio/baremetal/getchar.cpp b/libc/src/stdio/baremetal/getchar.cpp
index 0a78a1ff8adf1..8fb7bc32537e7 100644
--- a/libc/src/stdio/baremetal/getchar.cpp
+++ b/libc/src/stdio/baremetal/getchar.cpp
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, getchar, ()) {
   char buf[1];
   auto result = read_from_stdin(buf, sizeof(buf));
-  if (result < 0)
+  if (result <= 0)
     return EOF;
   return buf[0];
 }
diff --git a/libc/src/stdio/baremetal/scanf.cpp b/libc/src/stdio/baremetal/scanf.cpp
index 1b0bf3e3afd54..d2ce36c9be733 100644
--- a/libc/src/stdio/baremetal/scanf.cpp
+++ b/libc/src/stdio/baremetal/scanf.cpp
@@ -21,10 +21,12 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace {
 
-struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
+struct StreamReader : scanf_core::Reader<StreamReader> {
   LIBC_INLINE char getc() {
     char buf[1];
-    read_from_stdin(buf, sizeof(buf));
+    auto result = read_from_stdin(buf, sizeof(buf));
+    if (result <= 0)
+      return EOF;
     return buf[0];
   }
   LIBC_INLINE void ungetc(int) {}
@@ -40,9 +42,7 @@ LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
                                  // destruction automatically.
   va_end(vlist);
 
-  StreamBuffer buffer;
-  scanf_core::Reader<StreamBuffer> reader(&buffer);
-
+  StreamReader reader;
   int retval = scanf_core::scanf_main(&reader, format, args);
   // This is done to avoid including stdio.h in the internals. On most systems
   // EOF is -1, so this will be transformed into just "return retval".
diff --git a/libc/src/stdio/baremetal/vscanf.cpp b/libc/src/stdio/baremetal/vscanf.cpp
index e0119d6146d89..a3aee63711a8e 100644
--- a/libc/src/stdio/baremetal/vscanf.cpp
+++ b/libc/src/stdio/baremetal/vscanf.cpp
@@ -21,10 +21,12 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace {
 
-struct StreamBuffer : scanf_core::ReadBuffer<StreamBuffer> {
+struct StreamReader : scanf_core::Reader<StreamReader> {
   LIBC_INLINE char getc() {
     char buf[1];
-    read_from_stdin(buf, sizeof(buf));
+    auto result = read_from_stdin(buf, sizeof(buf));
+    if (result <= 0)
+      return EOF;
     return buf[0];
   }
   LIBC_INLINE void ungetc(int) {}
@@ -39,9 +41,7 @@ LLVM_LIBC_FUNCTION(int, vscanf,
                                  // destruction automatically.
   va_end(vlist);
 
-  StreamBuffer buffer;
-  scanf_core::Reader<StreamBuffer> reader(&buffer);
-
+  StreamReader reader;
   int retval = scanf_core::scanf_main(&reader, format, args);
   // This is done to avoid including stdio.h in the internals. On most systems
   // EOF is -1, so this will be transformed into just "return retval".

>From c9d8ccc77a55e0df8f1eb71c3afa8b6eddfc49e3 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 18 Mar 2025 00:10:53 -0700
Subject: [PATCH 3/6] Remove the unnecessary file_deps

---
 libc/src/stdio/scanf_core/CMakeLists.txt | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 52101132e9ef6..dee125c234a10 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -16,8 +16,6 @@ if(LIBC_TARGET_OS_IS_GPU)
     libc.src.stdio.ungetc
     libc.src.stdio.ferror
   )
-elseif(LIBC_TARGET_OS_IS_BAREMETAL)
-  # There's no FILE* for baremetal.
 elseif(LLVM_LIBC_FULL_BUILD)
   list(APPEND file_deps
     libc.src.__support.File.file
@@ -66,7 +64,6 @@ add_header_library(
     .converter
     .core_structs
     libc.src.__support.arg_list
-    ${file_deps}
   ${use_system_file}
 )
 
@@ -107,7 +104,6 @@ add_header_library(
     libc.src.__support.CPP.limits
     libc.src.__support.char_vector
     libc.src.__support.str_to_float
-    ${file_deps}
   ${use_system_file}
 )
 

>From 8f461ff3e4a8bb60a18c8cb98142e02b78c09655 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 18 Mar 2025 11:37:47 -0700
Subject: [PATCH 4/6] Reintroduce an accidentally dropped asprintf and vasprinf

---
 libc/config/baremetal/aarch64/entrypoints.txt | 2 ++
 libc/config/baremetal/arm/entrypoints.txt     | 2 ++
 libc/config/baremetal/riscv/entrypoints.txt   | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index ffe7e622279a0..8b51942d30fe8 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -123,6 +123,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.inttypes.strtoumax
 
     # stdio.h entrypoints
+    libc.src.stdio.asprintf
     libc.src.stdio.getchar
     libc.src.stdio.printf
     libc.src.stdio.putchar
@@ -132,6 +133,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
     libc.src.stdio.sscanf
+    libc.src.stdio.vasprintf
     libc.src.stdio.vprintf
     libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index a6628d1c56219..511d9d22f1450 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -123,6 +123,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.inttypes.strtoumax
 
     # stdio.h entrypoints
+    libc.src.stdio.asprintf
     libc.src.stdio.getchar
     libc.src.stdio.printf
     libc.src.stdio.putchar
@@ -132,6 +133,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
     libc.src.stdio.sscanf
+    libc.src.stdio.vasprintf
     libc.src.stdio.vprintf
     libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ccbcb85a216a8..bbefb75ee16b5 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -119,6 +119,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.inttypes.strtoumax
 
     # stdio.h entrypoints
+    libc.src.stdio.asprintf
     libc.src.stdio.getchar
     libc.src.stdio.printf
     libc.src.stdio.putchar
@@ -128,6 +129,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.snprintf
     libc.src.stdio.sprintf
     libc.src.stdio.sscanf
+    libc.src.stdio.vasprintf
     libc.src.stdio.vprintf
     libc.src.stdio.vscanf
     libc.src.stdio.vsnprintf

>From 3cffce606f939f40f2769da132be884adf8a84b6 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Thu, 20 Mar 2025 01:00:58 -0700
Subject: [PATCH 5/6] Extract `StdinReader` into a shared header

---
 libc/src/stdio/baremetal/CMakeLists.txt | 13 +++++++++++--
 libc/src/stdio/baremetal/scanf.cpp      | 19 ++-----------------
 libc/src/stdio/baremetal/vscanf.cpp     | 19 ++-----------------
 3 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index 4abe8bc66b25b..e879230a9d02c 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -55,6 +55,15 @@ add_entrypoint_object(
     libc.src.__support.CPP.string_view
 )
 
+add_header_library(
+  scanf_internal
+  HDRS
+    scanf_internal.h
+  DEPENDS
+    libc.src.stdio.scanf_core.reader
+    libc.src.__support.OSUtil.osutil
+)
+
 add_entrypoint_object(
   scanf
   SRCS
@@ -62,8 +71,8 @@ add_entrypoint_object(
   HDRS
     ../scanf.h
   DEPENDS
+    .scanf_internal
     libc.src.stdio.scanf_core.scanf_main
-    libc.src.stdio.scanf_core.reader
     libc.src.__support.arg_list
     libc.src.__support.OSUtil.osutil
 )
@@ -88,8 +97,8 @@ add_entrypoint_object(
   HDRS
     ../vscanf.h
   DEPENDS
+    .scanf_internal
     libc.src.stdio.scanf_core.scanf_main
-    libc.src.stdio.scanf_core.reader
     libc.src.__support.arg_list
     libc.src.__support.OSUtil.osutil
 )
diff --git a/libc/src/stdio/baremetal/scanf.cpp b/libc/src/stdio/baremetal/scanf.cpp
index d2ce36c9be733..8d07aa1da76aa 100644
--- a/libc/src/stdio/baremetal/scanf.cpp
+++ b/libc/src/stdio/baremetal/scanf.cpp
@@ -12,28 +12,13 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/stdio/scanf_core/reader.h"
+#include "src/stdio/baremetal/scanf_internal.h"
 #include "src/stdio/scanf_core/scanf_main.h"
 
 #include <stdarg.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
-namespace {
-
-struct StreamReader : scanf_core::Reader<StreamReader> {
-  LIBC_INLINE char getc() {
-    char buf[1];
-    auto result = read_from_stdin(buf, sizeof(buf));
-    if (result <= 0)
-      return EOF;
-    return buf[0];
-  }
-  LIBC_INLINE void ungetc(int) {}
-};
-
-} // namespace
-
 LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
   va_list vlist;
   va_start(vlist, format);
@@ -42,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
                                  // destruction automatically.
   va_end(vlist);
 
-  StreamReader reader;
+  scanf_core::StdinReader reader;
   int retval = scanf_core::scanf_main(&reader, format, args);
   // This is done to avoid including stdio.h in the internals. On most systems
   // EOF is -1, so this will be transformed into just "return retval".
diff --git a/libc/src/stdio/baremetal/vscanf.cpp b/libc/src/stdio/baremetal/vscanf.cpp
index a3aee63711a8e..249f8fd5dbe3c 100644
--- a/libc/src/stdio/baremetal/vscanf.cpp
+++ b/libc/src/stdio/baremetal/vscanf.cpp
@@ -12,28 +12,13 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/stdio/scanf_core/reader.h"
+#include "src/stdio/baremetal/scanf_internal.h"
 #include "src/stdio/scanf_core/scanf_main.h"
 
 #include <stdarg.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
-namespace {
-
-struct StreamReader : scanf_core::Reader<StreamReader> {
-  LIBC_INLINE char getc() {
-    char buf[1];
-    auto result = read_from_stdin(buf, sizeof(buf));
-    if (result <= 0)
-      return EOF;
-    return buf[0];
-  }
-  LIBC_INLINE void ungetc(int) {}
-};
-
-} // namespace
-
 LLVM_LIBC_FUNCTION(int, vscanf,
                    (const char *__restrict format, va_list vlist)) {
   internal::ArgList args(vlist); // This holder class allows for easier copying
@@ -41,7 +26,7 @@ LLVM_LIBC_FUNCTION(int, vscanf,
                                  // destruction automatically.
   va_end(vlist);
 
-  StreamReader reader;
+  scanf_core::StdinReader reader;
   int retval = scanf_core::scanf_main(&reader, format, args);
   // This is done to avoid including stdio.h in the internals. On most systems
   // EOF is -1, so this will be transformed into just "return retval".

>From 062804fe4f92ff2f6b74b23f9cfdf5ca01ac37be Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Thu, 20 Mar 2025 13:09:33 -0700
Subject: [PATCH 6/6] Include `scanf_internal.h`

---
 libc/src/stdio/baremetal/scanf_internal.h | 30 +++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 libc/src/stdio/baremetal/scanf_internal.h

diff --git a/libc/src/stdio/baremetal/scanf_internal.h b/libc/src/stdio/baremetal/scanf_internal.h
new file mode 100644
index 0000000000000..57d4b8c8d1a61
--- /dev/null
+++ b/libc/src/stdio/baremetal/scanf_internal.h
@@ -0,0 +1,30 @@
+//===-- Internal implementation header of scanf -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/OSUtil/io.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/scanf_core/reader.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace scanf_core {
+
+struct StdinReader : public Reader<StdinReader> {
+  LIBC_INLINE char getc() {
+    char buf[1];
+    auto result = read_from_stdin(buf, sizeof(buf));
+    if (result <= 0)
+      return EOF;
+    return buf[0];
+  }
+  LIBC_INLINE void ungetc(int) {}
+};
+
+} // namespace scanf_core
+
+} // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list