[libc-commits] [libc] [libc] implement rewind (PR #191302)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Apr 9 16:27:54 PDT 2026


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/191302

>From 002a1c8939159b8ebd1cad1f3fff0360bd4134a3 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 9 Apr 2026 21:13:49 +0000
Subject: [PATCH 1/2] [libc] implement rewind

Add the "rewind" function defined in the C standard, and adds tests.
---
 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     | 13 ++++
 libc/src/stdio/generic/rewind.cpp         | 28 +++++++++
 libc/src/stdio/rewind.h                   | 21 +++++++
 libc/test/src/stdio/CMakeLists.txt        | 18 ++++++
 libc/test/src/stdio/rewind_test.cpp       | 77 +++++++++++++++++++++++
 10 files changed, 167 insertions(+)
 create mode 100644 libc/src/stdio/generic/rewind.cpp
 create mode 100644 libc/src/stdio/rewind.h
 create mode 100644 libc/test/src/stdio/rewind_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 83088456fb4ac..262f49066df8d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1098,6 +1098,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1f7180d23a840..057cd48c694df 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1227,6 +1227,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 20434fb9e838f..3542f2aaea343 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1285,6 +1285,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putc
     libc.src.stdio.putchar
     libc.src.stdio.puts
+    libc.src.stdio.rewind
     libc.src.stdio.setbuf
     libc.src.stdio.setvbuf
     libc.src.stdio.stderr
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index cf8675f73495a..9caf4d789dbb7 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -201,6 +201,12 @@ functions:
       - type: FILE *
       - type: long
       - type: int
+  - name: rewind
+    standards:
+      - stdc
+    return_type: void
+    arguments:
+      - type: FILE *
   - name: fseeko
     standards:
       - POSIX
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 1079a6ac51dfc..feee8d60d1c60 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -263,6 +263,7 @@ add_stdio_entrypoint_object(feof_unlocked)
 add_stdio_entrypoint_object(ferror)
 add_stdio_entrypoint_object(ferror_unlocked)
 add_stdio_entrypoint_object(fseek)
+add_stdio_entrypoint_object(rewind)
 add_stdio_entrypoint_object(ftell)
 add_stdio_entrypoint_object(fseeko)
 add_stdio_entrypoint_object(fileno)
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 9cde0980d6f32..a9c5ae698c3bb 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -123,6 +123,19 @@ add_generic_entrypoint_object(
     libc.src.__support.File.platform_file
 )
 
+add_generic_entrypoint_object(
+  rewind
+  SRCS
+    rewind.cpp
+  HDRS
+    ../rewind.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+    libc.src.__support.macros.null_check
+)
+
 add_generic_entrypoint_object(
   ftell
   SRCS
diff --git a/libc/src/stdio/generic/rewind.cpp b/libc/src/stdio/generic/rewind.cpp
new file mode 100644
index 0000000000000..d2d22c68eb7c0
--- /dev/null
+++ b/libc/src/stdio/generic/rewind.cpp
@@ -0,0 +1,28 @@
+//===-- Implementation of rewind ------------------------------------------===//
+//
+// 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/rewind.h"
+#include "src/__support/File/file.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(void, rewind, (::FILE * stream)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  File *FilePtr = reinterpret_cast<File *>(stream);
+  auto Result = FilePtr->seek(0, SEEK_SET);
+  FilePtr->clearerr();
+
+  if (!Result.has_value())
+    libc_errno = Result.error();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/rewind.h b/libc/src/stdio/rewind.h
new file mode 100644
index 0000000000000..1c8b154842cdf
--- /dev/null
+++ b/libc/src/stdio/rewind.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of rewind -------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_REWIND_H
+#define LLVM_LIBC_SRC_STDIO_REWIND_H
+
+#include "hdr/types/FILE.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void rewind(FILE *stream);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_REWIND_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 689845ea231ef..038ac9766b80b 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -23,6 +23,24 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_test(
+  rewind_test
+  SUITE
+    libc_stdio_unittests
+  SRCS
+    rewind_test.cpp
+  DEPENDS
+    libc.include.stdio
+    libc.src.stdio.fclose
+    libc.src.stdio.ferror
+    libc.src.stdio.fopen
+    libc.src.stdio.fwrite
+    libc.src.stdio.fread
+    libc.src.stdio.rewind
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   ungetc_test
   SUITE
diff --git a/libc/test/src/stdio/rewind_test.cpp b/libc/test/src/stdio/rewind_test.cpp
new file mode 100644
index 0000000000000..e36f21267f4b0
--- /dev/null
+++ b/libc/test/src/stdio/rewind_test.cpp
@@ -0,0 +1,77 @@
+//===-- Unittests for file operations like fopen, flcose etc --------------===//
+//
+// 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/fclose.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/fwrite.h"
+#include "src/stdio/rewind.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcRewindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
+
+TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
+  constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/rewind.test");
+  auto FILEPATH = libc_make_test_file_path(FILENAME);
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILEPATH, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char FIRST_DATA[] = "123456789";
+  ASSERT_THAT(LIBC_NAMESPACE::fwrite(FIRST_DATA, 1, sizeof(FIRST_DATA), file),
+              returns(EQ(sizeof(FIRST_DATA))).with_errno(EQ(0)));
+
+  // File state is "123456789"
+
+  LIBC_NAMESPACE::rewind(file);
+
+  // Cursor is back to the start
+
+  constexpr char SECOND_DATA[] = "abc";
+  ASSERT_THAT(
+      LIBC_NAMESPACE::fwrite(SECOND_DATA, 1, sizeof(SECOND_DATA) - 1, file),
+      returns(EQ(sizeof(SECOND_DATA) - 1)).with_errno(EQ(0)));
+
+  // File state is "abc456789"
+
+  // attempt to read from write-only file causing error state
+  char read_data[sizeof(FIRST_DATA)];
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
+              returns(EQ(size_t(0))).with_errno(NE(0)));
+  ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
+
+  // rewind to start and check that that clears the error.
+  LIBC_NAMESPACE::rewind(file);
+  ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+
+  // Close out the file and reopen in read mode.
+  LIBC_NAMESPACE::fclose(file);
+  file = LIBC_NAMESPACE::fopen(FILEPATH, "r");
+  ASSERT_FALSE(file == nullptr);
+
+  // Read the file to check that it was written correctly.
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, 3, file),
+              returns(EQ(size_t(3))).with_errno(EQ(0)));
+  read_data[3] = '\0';
+  ASSERT_STREQ(read_data, "abc");
+  ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+
+  // check that rewind also works on read files.
+  LIBC_NAMESPACE::rewind(file);
+  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
+              returns(EQ(sizeof(FIRST_DATA))).with_errno(EQ(0)));
+  read_data[sizeof(FIRST_DATA) - 1] = '\0';
+  ASSERT_STREQ(read_data, "abc456789");
+
+  LIBC_NAMESPACE::fclose(file);
+}

>From ad9b00bb42b32f9fd366a323bd32c07bc3c1a4b2 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 9 Apr 2026 23:27:28 +0000
Subject: [PATCH 2/2] address comments and simplify tests

---
 libc/test/src/stdio/rewind_test.cpp | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/libc/test/src/stdio/rewind_test.cpp b/libc/test/src/stdio/rewind_test.cpp
index e36f21267f4b0..41dcbbdb8dd80 100644
--- a/libc/test/src/stdio/rewind_test.cpp
+++ b/libc/test/src/stdio/rewind_test.cpp
@@ -17,9 +17,7 @@
 #include "test/UnitTest/Test.h"
 
 using LlvmLibcRewindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 
 TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
   constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/rewind.test");
@@ -29,7 +27,7 @@ TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
 
   constexpr char FIRST_DATA[] = "123456789";
   ASSERT_THAT(LIBC_NAMESPACE::fwrite(FIRST_DATA, 1, sizeof(FIRST_DATA), file),
-              returns(EQ(sizeof(FIRST_DATA))).with_errno(EQ(0)));
+              Succeeds(sizeof(FIRST_DATA)));
 
   // File state is "123456789"
 
@@ -40,14 +38,15 @@ TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
   constexpr char SECOND_DATA[] = "abc";
   ASSERT_THAT(
       LIBC_NAMESPACE::fwrite(SECOND_DATA, 1, sizeof(SECOND_DATA) - 1, file),
-      returns(EQ(sizeof(SECOND_DATA) - 1)).with_errno(EQ(0)));
+      Succeeds(sizeof(SECOND_DATA) - 1));
 
   // File state is "abc456789"
 
   // attempt to read from write-only file causing error state
   char read_data[sizeof(FIRST_DATA)];
-  ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
-              returns(EQ(size_t(0))).with_errno(NE(0)));
+  ASSERT_EQ(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
+            size_t(0));
+  ASSERT_ERRNO_FAILURE();
   ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
 
   // rewind to start and check that that clears the error.
@@ -61,7 +60,7 @@ TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
 
   // Read the file to check that it was written correctly.
   ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, 3, file),
-              returns(EQ(size_t(3))).with_errno(EQ(0)));
+              Succeeds(size_t(3)));
   read_data[3] = '\0';
   ASSERT_STREQ(read_data, "abc");
   ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
@@ -69,7 +68,7 @@ TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
   // check that rewind also works on read files.
   LIBC_NAMESPACE::rewind(file);
   ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(read_data), file),
-              returns(EQ(sizeof(FIRST_DATA))).with_errno(EQ(0)));
+              Succeeds(sizeof(FIRST_DATA)));
   read_data[sizeof(FIRST_DATA) - 1] = '\0';
   ASSERT_STREQ(read_data, "abc456789");
 



More information about the libc-commits mailing list