[libc-commits] [libc] 34d1f71 - [libc] Extract FileIOResult into its own header (#225464)
via libc-commits
libc-commits at lists.llvm.org
Wed Sep 23 00:04:52 PDT 2026
Author: Petr Hosek
Date: 2026-09-23T00:04:42-07:00
New Revision: 34d1f71c636d9ea6169b54b10992599030b7496a
URL: https://github.com/llvm/llvm-project/commit/34d1f71c636d9ea6169b54b10992599030b7496a
DIFF: https://github.com/llvm/llvm-project/commit/34d1f71c636d9ea6169b54b10992599030b7496a.diff
LOG: [libc] Extract FileIOResult into its own header (#225464)
This avoids the duplicate definitions.
Added:
libc/src/__support/File/file_io_result.h
Modified:
libc/src/__support/File/CMakeLists.txt
libc/src/__support/File/file.h
libc/src/stdio/baremetal/CMakeLists.txt
libc/src/stdio/baremetal/file_internal.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
################################################################################
diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt
index f369fec9a915c..6c8f157d2d887 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 f5e262ece2587..84dde0560f467 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 0000000000000..2a589113938d0
--- /dev/null
+++ b/libc/src/__support/File/file_io_result.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// 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
+
+#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 f33613e363d47..844d275240b4d 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 b2a84ce44e2df..a627de4d8fad8 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 3a57403dfcf9d..43f712d695956 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2237,6 +2237,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 = [
@@ -2252,6 +2260,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",
More information about the libc-commits
mailing list