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

via libc-commits libc-commits at lists.llvm.org
Thu Apr 9 14:26:06 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Michael Jones (michaelrj-google)

<details>
<summary>Changes</summary>

Add the "rewind" function defined in the C standard, and adds tests.


---
Full diff: https://github.com/llvm/llvm-project/pull/191302.diff


10 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/include/stdio.yaml (+6) 
- (modified) libc/src/stdio/CMakeLists.txt (+1) 
- (modified) libc/src/stdio/generic/CMakeLists.txt (+13) 
- (added) libc/src/stdio/generic/rewind.cpp (+28) 
- (added) libc/src/stdio/rewind.h (+21) 
- (modified) libc/test/src/stdio/CMakeLists.txt (+18) 
- (added) libc/test/src/stdio/rewind_test.cpp (+77) 


``````````diff
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);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/191302


More information about the libc-commits mailing list