[libc-commits] [libc] [llvm] [libc] Extract FileIOResult into its own header (PR #225464)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 10:53:07 PDT 2026


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/225464

>From 4aa8e31ac7ed233449f505f15f7feb6ba4192825 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 22 Sep 2026 17:15:47 +0000
Subject: [PATCH 1/2] [libc] Extract FileIOResult into its own header

This avoids the duplicate definitions.
---
 libc/src/__support/File/CMakeLists.txt        |  9 ++++++
 libc/src/__support/File/file.h                | 13 +-------
 libc/src/__support/File/file_io_result.h      | 32 +++++++++++++++++++
 libc/src/stdio/baremetal/CMakeLists.txt       |  1 +
 libc/src/stdio/baremetal/file_internal.h      | 14 +-------
 .../llvm-project-overlay/libc/BUILD.bazel     |  9 ++++++
 6 files changed, 53 insertions(+), 25 deletions(-)
 create mode 100644 libc/src/__support/File/file_io_result.h

diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt
index f369fec9a915ce..6c8f157d2d887a 100644
--- a/libc/src/__support/File/CMakeLists.txt
+++ b/libc/src/__support/File/CMakeLists.txt
@@ -1,3 +1,11 @@
+add_header_library(
+  file_io_result
+  HDRS
+    file_io_result.h
+  DEPENDS
+    libc.src.__support.macros.config
+)
+
 if(NOT (TARGET libc.src.__support.threads.mutex)
   OR LIBC_TARGET_OS_IS_GPU)
   # Not all platforms have a mutex implementation. If mutex is unvailable,
@@ -12,6 +20,7 @@ add_object_library(
   HDRS
     file.h
   DEPENDS
+    .file_io_result
     libc.hdr.stdio_macros
     libc.hdr.stdint_proxy
     libc.hdr.func.realloc
diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index f5e262ece2587d..84dde0560f467f 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_FILE_FILE_H
 #define LLVM_LIBC_SRC___SUPPORT_FILE_FILE_H
 
+#include "file_io_result.h"
 #include "file_mode.h"
 #include "hdr/stdint_proxy.h"
 #include "hdr/stdio_macros.h"
@@ -30,18 +31,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-struct FileIOResult {
-  size_t value;
-  int error;
-
-  constexpr FileIOResult(size_t val) : value(val), error(0) {}
-  constexpr FileIOResult(size_t val, int error) : value(val), error(error) {}
-
-  constexpr bool has_error() { return error != 0; }
-
-  constexpr operator size_t() { return value; }
-};
-
 // This a generic base class to encapsulate a platform independent file data
 // structure. Platform specific specializations should create a subclass as
 // suitable for their platform.
diff --git a/libc/src/__support/File/file_io_result.h b/libc/src/__support/File/file_io_result.h
new file mode 100644
index 00000000000000..b141799b699fbf
--- /dev/null
+++ b/libc/src/__support/File/file_io_result.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___SUPPORT_FILE_FILE_IO_RESULT_H
+#define LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IO_RESULT_H
+
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+struct FileIOResult {
+  size_t value;
+  int error;
+
+  constexpr FileIOResult(size_t val) : value(val), error(0) {}
+  constexpr FileIOResult(size_t val, int error) : value(val), error(error) {}
+
+  constexpr bool has_error() { return error != 0; }
+
+  constexpr operator size_t() { return value; }
+};
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IO_RESULT_H
diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt
index f33613e363d471..844d275240b4df 100644
--- a/libc/src/stdio/baremetal/CMakeLists.txt
+++ b/libc/src/stdio/baremetal/CMakeLists.txt
@@ -7,6 +7,7 @@ add_object_library(
   DEPENDS
     libc.hdr.types.FILE
     libc.src.__support.CPP.string_view
+    libc.src.__support.File.file_io_result
     libc.src.__support.OSUtil.osutil
 )
 
diff --git a/libc/src/stdio/baremetal/file_internal.h b/libc/src/stdio/baremetal/file_internal.h
index b2a84ce44e2dfc..a627de4d8fad83 100644
--- a/libc/src/stdio/baremetal/file_internal.h
+++ b/libc/src/stdio/baremetal/file_internal.h
@@ -11,6 +11,7 @@
 
 #include "hdr/types/FILE.h"
 #include "src/__support/CPP/string_view.h"
+#include "src/__support/File/file_io_result.h"
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
@@ -19,19 +20,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-// TODO: Deduplicate this with __support/File/file.h.
-struct FileIOResult {
-  size_t value;
-  int error;
-
-  constexpr FileIOResult(size_t val) : value(val), error(0) {}
-  constexpr FileIOResult(size_t val, int error) : value(val), error(error) {}
-
-  constexpr bool has_error() { return error != 0; }
-
-  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);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b3f854ad06a7d6..44854a99473245 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2221,6 +2221,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_file_file_io_result",
+    hdrs = ["src/__support/File/file_io_result.h"],
+    deps = [
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_file_file",
     srcs = [
@@ -2236,6 +2244,7 @@ libc_support_library(
         ":__support_cpp_new",
         ":__support_cpp_span",
         ":__support_error_or",
+        ":__support_file_file_io_result",
         ":__support_libc_errno",
         ":__support_macros_attributes",
         ":__support_macros_config",

>From c97f57ef21f2f2d643b2d88ed27145b384cdbef5 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 22 Sep 2026 17:50:24 +0000
Subject: [PATCH 2/2] Add file documentation comment

---
 libc/src/__support/File/file_io_result.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libc/src/__support/File/file_io_result.h b/libc/src/__support/File/file_io_result.h
index b141799b699fbf..2a589113938d06 100644
--- a/libc/src/__support/File/file_io_result.h
+++ b/libc/src/__support/File/file_io_result.h
@@ -5,6 +5,11 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Result type for file I/O operations.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IO_RESULT_H
 #define LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IO_RESULT_H



More information about the libc-commits mailing list