[libc-commits] [libc] Revert "[libc] Add getc, ungetc, fflush to enable libc++ iostream on baremetal" (PR #181931)

Paul Kirth via libc-commits libc-commits at lists.llvm.org
Tue Feb 17 14:50:57 PST 2026


https://github.com/ilovepi created https://github.com/llvm/llvm-project/pull/181931

Reverts llvm/llvm-project#175530

This PR breaks libc header generation on Windows stage2 builds for a to be determined reason.

>From 4162ab1c4e96cd213cfda2c99dbeb6d70df36fd6 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 17 Feb 2026 14:50:09 -0800
Subject: [PATCH] =?UTF-8?q?Revert=20"[libc]=20Add=20getc,=20ungetc,=20fflu?=
 =?UTF-8?q?sh=20to=20enable=20libc++=20iostream=20on=20baremeta=E2=80=A6"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This reverts commit 907eb11cc128630c52cd2191b925873b6ee56d6e.
---
 libc/config/baremetal/aarch64/entrypoints.txt |  3 --
 libc/config/baremetal/arm/entrypoints.txt     |  3 --
 libc/src/stdio/baremetal/CMakeLists.txt       | 38 +-------------
 libc/src/stdio/baremetal/fflush.cpp           | 22 --------
 libc/src/stdio/baremetal/file_internal.cpp    | 52 -------------------
 libc/src/stdio/baremetal/file_internal.h      | 30 ++---------
 libc/src/stdio/baremetal/getc.cpp             | 31 -----------
 libc/src/stdio/baremetal/ungetc.cpp           | 20 -------
 libc/src/stdio/baremetal/vfscanf_internal.h   |  5 +-
 9 files changed, 6 insertions(+), 198 deletions(-)
 delete mode 100644 libc/src/stdio/baremetal/fflush.cpp
 delete mode 100644 libc/src/stdio/baremetal/file_internal.cpp
 delete mode 100644 libc/src/stdio/baremetal/ungetc.cpp

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index e0282bcb4fa23..742d96761c415 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -126,7 +126,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.asprintf
     libc.src.stdio.feof
     libc.src.stdio.ferror
-    libc.src.stdio.fflush
     libc.src.stdio.fgetc
     libc.src.stdio.fgets
     libc.src.stdio.fprintf
@@ -135,7 +134,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.fread
     libc.src.stdio.fscanf
     libc.src.stdio.fwrite
-    libc.src.stdio.getc
     libc.src.stdio.getchar
     libc.src.stdio.printf
     libc.src.stdio.putc
@@ -154,7 +152,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsnprintf
     libc.src.stdio.vsprintf
     libc.src.stdio.vsscanf
-    libc.src.stdio.ungetc
 
     # 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 f7ef4ea09f5b6..95cb0dea8e49e 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -126,7 +126,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.asprintf
     libc.src.stdio.feof
     libc.src.stdio.ferror
-    libc.src.stdio.fflush
     libc.src.stdio.fgetc
     libc.src.stdio.fgets
     libc.src.stdio.fprintf
@@ -135,7 +134,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.fread
     libc.src.stdio.fscanf
     libc.src.stdio.fwrite
-    libc.src.stdio.getc
     libc.src.stdio.getchar
     libc.src.stdio.printf
     libc.src.stdio.putc
@@ -154,7 +152,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.vsnprintf
     libc.src.stdio.vsprintf
     libc.src.stdio.vsscanf
-    libc.src.stdio.ungetc
 
     # stdbit.h entrypoints
     libc.src.stdbit.stdc_bit_ceil_uc
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index abe134959e09b..a706accecf152 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -1,7 +1,5 @@
-add_object_library(
+add_header_library(
   file_internal
-  SRCS
-    file_internal.cpp
   HDRS
     file_internal.h
   DEPENDS
@@ -33,7 +31,6 @@ add_header_library(
   HDRS
     vfscanf_internal.h
   DEPENDS
-    .file_internal
     libc.hdr.types.FILE
     libc.hdr.stdio_macros
     libc.src.__support.arg_list
@@ -79,27 +76,6 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
-add_entrypoint_object(
-  getc
-  SRCS
-    getc.cpp
-  HDRS
-    ../getc.h
-  DEPENDS
-    libc.hdr.types.FILE
-    libc.src.stdio.baremetal.fgetc
-)
-
-add_entrypoint_object(
-  fflush
-  SRCS
-    fflush.cpp
-  HDRS
-    ../fflush.h
-  DEPENDS
-    .file_internal
-)
-
 add_entrypoint_object(
   fgets
   SRCS
@@ -247,18 +223,6 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
-add_entrypoint_object(
-  ungetc
-  SRCS
-    ungetc.cpp
-  HDRS
-    ../ungetc.h
-  DEPENDS
-    .file_internal
-    libc.hdr.stdio_macros
-    libc.hdr.types.FILE
-)
-
 add_entrypoint_object(
   puts
   SRCS
diff --git a/libc/src/stdio/baremetal/fflush.cpp b/libc/src/stdio/baremetal/fflush.cpp
deleted file mode 100644
index 4a39c25ae11c9..0000000000000
--- a/libc/src/stdio/baremetal/fflush.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-//===-- Implementation of fflush for baremetal -----------------*- 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/fflush.h"
-
-#include "src/__support/common.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-// Baremetal uses unbuffered I/O, so there is nothing to flush.
-LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
-  (void)stream;
-  // TODO: Shall we have an embedding API for fflush?
-  return 0;
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/file_internal.cpp b/libc/src/stdio/baremetal/file_internal.cpp
deleted file mode 100644
index c12ab1c90d55b..0000000000000
--- a/libc/src/stdio/baremetal/file_internal.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-//===--- Helpers for file I/O on baremetal ----------------------*- 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/baremetal/file_internal.h"
-#include "src/__support/CPP/optional.h"
-
-#include "hdr/stdio_macros.h" // for EOF, FILE
-
-namespace LIBC_NAMESPACE_DECL {
-
-// Out of standard streams only stdin supports ungetc,
-// because stdin is readable - ungetc on stdout/stderr is undefined.
-// Only one value is required by the C standard to be stored by ungetc.
-// This minimal implementation only handles stdin and returns error on all
-// other streams.
-// TODO: Shall we have an embedding API for ungetc?
-
-static cpp::optional<unsigned char> ungetc_state_stdin;
-
-bool pop_ungetc_value(::FILE *stream, unsigned char &out) {
-  if (stream != stdin)
-    return false;
-
-  if (!ungetc_state_stdin)
-    return false;
-
-  out = *ungetc_state_stdin;
-  ungetc_state_stdin.reset();
-  return true;
-}
-
-int push_ungetc_value(::FILE *stream, int c) {
-  if (c == EOF || stream == nullptr)
-    return EOF;
-
-  if (stream != stdin)
-    return EOF;
-
-  if (ungetc_state_stdin)
-    return EOF;
-
-  ungetc_state_stdin =
-      cpp::optional<unsigned char>{static_cast<unsigned char>(c)};
-  return c;
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/file_internal.h b/libc/src/stdio/baremetal/file_internal.h
index b2a84ce44e2df..292a1e22c954b 100644
--- a/libc/src/stdio/baremetal/file_internal.h
+++ b/libc/src/stdio/baremetal/file_internal.h
@@ -32,35 +32,11 @@ struct FileIOResult {
   constexpr operator size_t() { return value; }
 };
 
-// ungetc handling.
-int push_ungetc_value(::FILE *stream, int c);
-bool pop_ungetc_value(::FILE *stream, unsigned char &out);
-
-LIBC_INLINE int ungetc_internal(int c, ::FILE *stream) {
-  return push_ungetc_value(stream, c);
-}
-
 LIBC_INLINE FileIOResult read_internal(char *buf, size_t size, ::FILE *stream) {
-  if (size == 0)
-    return 0;
-
-  unsigned char ungetc_value = 0;
-  size_t ungetc_value_copied = 0;
-
-  if (pop_ungetc_value(stream, ungetc_value)) {
-    buf[0] = static_cast<char>(ungetc_value);
-    ungetc_value_copied = 1;
-
-    if (size == 1)
-      return 1;
-  }
-
-  ssize_t ret = __llvm_libc_stdio_read(stream, buf + ungetc_value_copied,
-                                       size - ungetc_value_copied);
+  ssize_t ret = __llvm_libc_stdio_read(stream, buf, size);
   if (ret < 0)
-    return {ungetc_value_copied, static_cast<int>(-ret)};
-
-  return ret + ungetc_value_copied;
+    return {0, static_cast<int>(-ret)};
+  return ret;
 }
 
 LIBC_INLINE FileIOResult write_internal(const char *buf, size_t size,
diff --git a/libc/src/stdio/baremetal/getc.cpp b/libc/src/stdio/baremetal/getc.cpp
index b56d23bc4807a..e69de29bb2d1d 100644
--- a/libc/src/stdio/baremetal/getc.cpp
+++ b/libc/src/stdio/baremetal/getc.cpp
@@ -1,31 +0,0 @@
-//===-- Implementation of getc for baremetal -------------------*- 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/getc.h"
-
-#include "hdr/stdio_macros.h" // for EOF.
-#include "hdr/types/FILE.h"
-#include "src/__support/common.h"
-#include "src/__support/libc_errno.h"
-#include "src/__support/macros/config.h"
-#include "src/stdio/baremetal/file_internal.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(int, getc, (::FILE * stream)) {
-  unsigned char c;
-  auto result = read_internal(reinterpret_cast<char *>(&c), 1, stream);
-  if (result.has_error())
-    libc_errno = result.error;
-
-  if (result.value != 1)
-    return EOF;
-  return c;
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/ungetc.cpp b/libc/src/stdio/baremetal/ungetc.cpp
deleted file mode 100644
index 72a25dbf8fb9f..0000000000000
--- a/libc/src/stdio/baremetal/ungetc.cpp
+++ /dev/null
@@ -1,20 +0,0 @@
-//===-- Implementation of ungetc for baremetal -----------------*- 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/ungetc.h"
-
-#include "src/__support/common.h"
-#include "src/stdio/baremetal/file_internal.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(int, ungetc, (int c, ::FILE *stream)) {
-  return ungetc_internal(c, stream);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/baremetal/vfscanf_internal.h b/libc/src/stdio/baremetal/vfscanf_internal.h
index 41df455f36e7a..fc90dc3bc044f 100644
--- a/libc/src/stdio/baremetal/vfscanf_internal.h
+++ b/libc/src/stdio/baremetal/vfscanf_internal.h
@@ -18,7 +18,6 @@
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/stdio/baremetal/file_internal.h"
 #include "src/stdio/scanf_core/reader.h"
 #include "src/stdio/scanf_core/scanf_main.h"
 
@@ -34,12 +33,12 @@ class StreamReader : public scanf_core::Reader<StreamReader> {
 
   LIBC_INLINE char getc() {
     char c;
-    auto result = read_internal(&c, 1, stream);
+    auto result = __llvm_libc_stdio_read(stream, &c, 1);
     if (result != 1)
       return '\0';
     return c;
   }
-  LIBC_INLINE void ungetc(int c) { (void)ungetc_internal(c, stream); }
+  LIBC_INLINE void ungetc(int) {}
 };
 
 } // namespace internal



More information about the libc-commits mailing list