[libc-commits] [libc] [libc][stdio] Implement putchar_unlocked (PR #243331)

Sachin 🗲 via libc-commits libc-commits at lists.llvm.org
Mon Nov 16 20:54:31 PST 2026


https://github.com/s4chinjha created https://github.com/llvm/llvm-project/pull/243331

Implements `putchar_unlocked` for Linux full builds.

Adds the entrypoint, internal header, stdio.yaml declaration, CMake targets,
entrypoint entries for aarch64/riscv/x86_64, and two unit tests.

Part of #242485


>From 7df5c15411edfaa13140e52917cdabc4e715e119 Mon Sep 17 00:00:00 2001
From: s4chinjha <s4chinjha at gmail.com>
Date: Sun, 15 Nov 2026 12:54:15 +0530
Subject: [PATCH] [libc][stdio] Implement putchar_unlocked

Implements `putchar_unlocked` for Linux full builds.
Adds the entrypoint, internal header, stdio.yaml declaration, CMake targets,
entrypoint entries for aarch64/riscv/x86_64, and two unit tests.
Part of #242485

Assisted with automated tooling.
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/stdio.yaml                       |  6 ++
 libc/src/stdio/CMakeLists.txt                 |  1 +
 libc/src/stdio/generic/CMakeLists.txt         | 15 +++
 libc/src/stdio/generic/putchar_unlocked.cpp   | 41 +++++++++
 libc/src/stdio/putchar_unlocked.h             | 25 +++++
 libc/test/src/stdio/CMakeLists.txt            | 23 +++++
 libc/test/src/stdio/putchar_unlocked_test.cpp | 91 +++++++++++++++++++
 10 files changed, 205 insertions(+)
 create mode 100644 libc/src/stdio/generic/putchar_unlocked.cpp
 create mode 100644 libc/src/stdio/putchar_unlocked.h
 create mode 100644 libc/test/src/stdio/putchar_unlocked_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ede4d139b150f..f3c59e6fd4e14 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1399,6 +1399,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.popen
     libc.src.stdio.putc
     libc.src.stdio.putchar
+    libc.src.stdio.putchar_unlocked
     libc.src.stdio.puts
     libc.src.stdio.rewind
     libc.src.stdio.setbuf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 863b300f83624..1f00ce729169d 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1614,6 +1614,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.popen
     libc.src.stdio.putc
     libc.src.stdio.putchar
+    libc.src.stdio.putchar_unlocked
     libc.src.stdio.puts
     libc.src.stdio.rewind
     libc.src.stdio.setbuf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 11aa163d63b91..2eb84751b5117 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1618,6 +1618,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.popen
     libc.src.stdio.putc
     libc.src.stdio.putchar
+    libc.src.stdio.putchar_unlocked
     libc.src.stdio.puts
     libc.src.stdio.rewind
     libc.src.stdio.setbuf
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index 4a54f4673c639..57ec73c609232 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -379,6 +379,12 @@ functions:
     return_type: int
     arguments:
       - type: int
+  - name: putchar_unlocked
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
   - name: puts
     standards:
       - stdc
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index cba596c67345d..be4d3cab9e04b 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -378,6 +378,7 @@ add_stdio_entrypoint_object(fwrite)
 add_stdio_entrypoint_object(fputc)
 add_stdio_entrypoint_object(putc)
 add_stdio_entrypoint_object(putchar)
+add_stdio_entrypoint_object(putchar_unlocked)
 add_stdio_entrypoint_object(printf)
 add_stdio_entrypoint_object(fprintf)
 add_stdio_entrypoint_object(fgetc)
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 33f7524c93a9b..f3940c922b63d 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -393,6 +393,21 @@ add_generic_entrypoint_object(
     libc.src.__support.File.platform_file
 )
 
+add_generic_entrypoint_object(
+  putchar_unlocked
+  SRCS
+    putchar_unlocked.cpp
+  HDRS
+    ../putchar_unlocked.h
+  DEPENDS
+    libc.hdr.types.FILE
+    libc.hdr.types.size_t
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+    libc.src.errno.errno
+    libc.src.stdio.stdout
+)
+
 add_generic_entrypoint_object(
   fgetc
   SRCS
diff --git a/libc/src/stdio/generic/putchar_unlocked.cpp b/libc/src/stdio/generic/putchar_unlocked.cpp
new file mode 100644
index 0000000000000..adeb95830d51c
--- /dev/null
+++ b/libc/src/stdio/generic/putchar_unlocked.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of putchar_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/putchar_unlocked.h"
+#include "src/__support/File/file.h"
+
+#include "hdr/types/FILE.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/stdout.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, putchar_unlocked, (int c)) {
+  unsigned char uc = static_cast<unsigned char>(c);
+
+  auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(LIBC_NAMESPACE::stdout)
+                    ->write_unlocked(&uc, 1);
+  if (result.has_error())
+    libc_errno = result.error;
+  size_t written = result.value;
+
+  if (1 != written) {
+    // The stream should be in an error state in this case.
+    return EOF;
+  }
+  return static_cast<int>(uc);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/putchar_unlocked.h b/libc/src/stdio/putchar_unlocked.h
new file mode 100644
index 0000000000000..33093ad83e089
--- /dev/null
+++ b/libc/src/stdio/putchar_unlocked.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of putchar_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_PUTCHAR_UNLOCKED_H
+#define LLVM_LIBC_SRC_STDIO_PUTCHAR_UNLOCKED_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int putchar_unlocked(int c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_PUTCHAR_UNLOCKED_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index e813627f37b8b..7b10cb6d2da93 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -523,6 +523,29 @@ add_libc_test(
     libc.src.stdio.fread
 )
 
+add_libc_test(
+  putchar_unlocked_test
+  SUITE
+    libc-stdio-tests
+  SRCS
+    putchar_unlocked_test.cpp
+  DEPENDS
+    libc.hdr.stdio_macros
+    libc.src.__support.CPP.scope
+    libc.src.__support.macros.properties.os
+    libc.src.stdio.fclose
+    libc.src.stdio.ferror
+    libc.src.stdio.fflush
+    libc.src.stdio.fopen
+    libc.src.stdio.fread
+    libc.src.stdio.freopen
+    libc.src.stdio.putchar_unlocked
+    libc.src.stdio.stdout
+    libc.src.string.memcmp
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_libc_test(
     remove_test
diff --git a/libc/test/src/stdio/putchar_unlocked_test.cpp b/libc/test/src/stdio/putchar_unlocked_test.cpp
new file mode 100644
index 0000000000000..5649405faa4a0
--- /dev/null
+++ b/libc/test/src/stdio/putchar_unlocked_test.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for putchar_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdio_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/macros/properties/os.h"
+#include "src/stdio/fclose.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fflush.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/freopen.h"
+#include "src/stdio/putchar_unlocked.h"
+#include "src/stdio/stdout.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::cpp::scope_exit;
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+class LlvmLibcPutcharUnlockedTest
+    : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {};
+
+#if defined(LIBC_TARGET_OS_IS_POSIX) || defined(LIBC_TARGET_OS_IS_LINUX)
+TEST_F(LlvmLibcPutcharUnlockedTest, WriteToStdout) {
+  const auto FILENAME =
+      libc_make_test_file_path("putchar_unlocked_stdout.test");
+
+  // Redirect stdout to FILENAME
+  ::FILE *reopened =
+      LIBC_NAMESPACE::freopen(FILENAME, "w", LIBC_NAMESPACE::stdout);
+  ASSERT_EQ(reopened, static_cast<::FILE *>(LIBC_NAMESPACE::stdout));
+
+  constexpr int INPUT_CHARS[] = {
+      'H',  'e', 'l', 'l', 'o', ',', ' ',   'W',   'o',   'r', 'l', 'd', '!',
+      '\n', 0,   1,   127, 128, 255, 0x141, 0x100, 0x1ff, 'e', 'n', 'd', '\0'};
+  constexpr size_t NUM_CHARS = sizeof(INPUT_CHARS) / sizeof(INPUT_CHARS[0]);
+  unsigned char expected[NUM_CHARS];
+
+  for (size_t i = 0; i < NUM_CHARS; ++i) {
+    expected[i] = static_cast<unsigned char>(INPUT_CHARS[i]);
+    int expected_ret = static_cast<int>(expected[i]);
+    ASSERT_THAT(LIBC_NAMESPACE::putchar_unlocked(INPUT_CHARS[i]),
+                Succeeds(expected_ret));
+  }
+
+  ASSERT_THAT(LIBC_NAMESPACE::fflush(LIBC_NAMESPACE::stdout), Succeeds(0));
+
+  // Read the file back with fopen/fread
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+  scope_exit close_file(
+      [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+  unsigned char read_buf[NUM_CHARS];
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_buf, 1, NUM_CHARS, file),
+              Succeeds(NUM_CHARS));
+  EXPECT_EQ(LIBC_NAMESPACE::memcmp(read_buf, expected, NUM_CHARS), 0);
+}
+
+TEST_F(LlvmLibcPutcharUnlockedTest, ReadOnlyStream) {
+  const auto FILENAME =
+      libc_make_test_file_path("putchar_unlocked_readonly.test");
+
+  // Ensure test file exists
+  ::FILE *init = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(init == nullptr);
+  ASSERT_THAT(LIBC_NAMESPACE::fclose(init), Succeeds());
+
+  // Redirect stdout to FILENAME in read-only mode
+  ::FILE *reopened =
+      LIBC_NAMESPACE::freopen(FILENAME, "r", LIBC_NAMESPACE::stdout);
+  ASSERT_EQ(reopened, static_cast<::FILE *>(LIBC_NAMESPACE::stdout));
+
+  ASSERT_EQ(LIBC_NAMESPACE::ferror(LIBC_NAMESPACE::stdout), 0);
+  ASSERT_THAT(LIBC_NAMESPACE::putchar_unlocked('X'), Fails(EBADF, EOF));
+  EXPECT_NE(LIBC_NAMESPACE::ferror(LIBC_NAMESPACE::stdout), 0);
+}
+#endif



More information about the libc-commits mailing list