[libc-commits] [libc] [libc] implement getwc (PR #196163)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed May 6 12:57:46 PDT 2026


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/196163

Add getwc function and tests. Part 7/11. All build file changes are in
part 11.

Assisted by Gemini


>From 25e30486b6098359bc7fb7a14cbf1c2d71b76d8d Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 6 May 2026 18:28:07 +0000
Subject: [PATCH] [libc] implement getwc

Add getwc function and tests. Part 7/11. All build file changes are in
part 11.

Assisted by Gemini
---
 libc/src/wchar/getwc.cpp           |  34 ++++++++
 libc/src/wchar/getwc.h             |  22 ++++++
 libc/test/src/wchar/getwc_test.cpp | 122 +++++++++++++++++++++++++++++
 3 files changed, 178 insertions(+)
 create mode 100644 libc/src/wchar/getwc.cpp
 create mode 100644 libc/src/wchar/getwc.h
 create mode 100644 libc/test/src/wchar/getwc_test.cpp

diff --git a/libc/src/wchar/getwc.cpp b/libc/src/wchar/getwc.cpp
new file mode 100644
index 0000000000000..f7b1de5521d82
--- /dev/null
+++ b/libc/src/wchar/getwc.cpp
@@ -0,0 +1,34 @@
+//===-- Implementation of getwc -------------------------------------------===//
+//
+// 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/wchar/getwc.h"
+#include "hdr/types/FILE.h"
+#include "hdr/types/wint_t.h"
+#include "hdr/wchar_macros.h" // For WEOF
+#include "src/__support/File/file.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wint_t, getwc, (::FILE *stream)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  auto *f = reinterpret_cast<File *>(stream);
+  wchar_t wc;
+  FileIOResult result = f->read(&wc, 1);
+  if (result.has_error() || result.value < 1) {
+    if (result.has_error())
+      libc_errno = result.error;
+    return WEOF;
+  }
+  return static_cast<wint_t>(wc);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/getwc.h b/libc/src/wchar/getwc.h
new file mode 100644
index 0000000000000..635573adaf318
--- /dev/null
+++ b/libc/src/wchar/getwc.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for getwc -----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_GETWC_H
+#define LLVM_LIBC_SRC_WCHAR_GETWC_H
+
+#include "hdr/types/FILE.h"
+#include "hdr/types/wint_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+wint_t getwc(::FILE *stream);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_GETWC_H
diff --git a/libc/test/src/wchar/getwc_test.cpp b/libc/test/src/wchar/getwc_test.cpp
new file mode 100644
index 0000000000000..ba513e3eaf860
--- /dev/null
+++ b/libc/test/src/wchar/getwc_test.cpp
@@ -0,0 +1,122 @@
+//===-- Unittests for getwc -----------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/wchar_macros.h" // For WEOF
+#include "src/stdio/fclose.h"
+#include "src/stdio/feof.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fwrite.h"
+#include "src/wchar/fwide.h"
+#include "src/wchar/getwc.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcGetwcTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcGetwcTest, ReadValidCharacters) {
+  auto FILENAME =
+      libc_make_test_file_path(APPEND_LIBC_TEST("getwc_valid.test"));
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  // Write "12"
+  constexpr char CONTENT[] = "12";
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file),
+            sizeof(CONTENT) - 1);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+
+  // Open for reading
+  file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Initial orientation
+  EXPECT_EQ(LIBC_NAMESPACE::fwide(file, 0), 0);
+
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'1'));
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'2'));
+
+  // Stream orientation should now be wide
+  EXPECT_GT(LIBC_NAMESPACE::fwide(file, 0), 0);
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcGetwcTest, ReadUtf8) {
+  auto FILENAME = libc_make_test_file_path(APPEND_LIBC_TEST("getwc_utf8.test"));
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  // Write "a¢€𐍈"
+  constexpr unsigned char CONTENT[] = {0x61, 0xC2, 0xA2, 0xE2, 0x82,
+                                       0xAC, 0xF0, 0x90, 0x8D, 0x88};
+  ASSERT_EQ(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT), file),
+            sizeof(CONTENT));
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+
+  // Open for reading
+  file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'a'));
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'¢'));
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'€'));
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(L'𐍈'));
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcGetwcTest, EndOfFile) {
+  auto FILENAME = libc_make_test_file_path(APPEND_LIBC_TEST("getwc_eof.test"));
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+
+  file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Read past EOF
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(WEOF));
+  EXPECT_NE(LIBC_NAMESPACE::feof(file), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcGetwcTest, ReadError) {
+  auto FILENAME =
+      libc_make_test_file_path(APPEND_LIBC_TEST("getwc_readerr.test"));
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  // Try to read from write-only file
+  ASSERT_ERRNO_SUCCESS();
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(WEOF));
+  ASSERT_ERRNO_EQ(EBADF);
+  EXPECT_NE(LIBC_NAMESPACE::ferror(file), 0);
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}
+
+TEST_F(LlvmLibcGetwcTest, ByteOrientedStreamFail) {
+  auto FILENAME =
+      libc_make_test_file_path(APPEND_LIBC_TEST("getwc_bytemode.test"));
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w+");
+  ASSERT_FALSE(file == nullptr);
+
+  // Orient to byte mode
+  EXPECT_LT(LIBC_NAMESPACE::fwide(file, -1), 0);
+
+  // Reading should fail and set errno to EINVAL
+  EXPECT_EQ(LIBC_NAMESPACE::getwc(file), static_cast<wint_t>(WEOF));
+  ASSERT_ERRNO_EQ(EINVAL);
+  EXPECT_NE(LIBC_NAMESPACE::ferror(file), 0);
+
+  ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
+}



More information about the libc-commits mailing list