[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for truncate (PR #241770)
Connector Switch via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 12 02:26:45 PST 2026
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/241770
This PR move truncate behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
>From afa49e892f5879af96c8ba8585641061fabb92f7 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 12 Nov 2026 18:25:38 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for truncate
---
.../linux/syscall_wrappers/CMakeLists.txt | 14 ++++++
.../OSUtil/linux/syscall_wrappers/truncate.h | 44 +++++++++++++++++++
libc/src/unistd/linux/CMakeLists.txt | 10 ++---
libc/src/unistd/linux/truncate.cpp | 28 +++++-------
.../llvm-project-overlay/libc/BUILD.bazel | 20 ++++++++-
5 files changed, 91 insertions(+), 25 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/truncate.h
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 6058f7716b8e71..5e59166b736883 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1522,6 +1522,20 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ truncate
+ HDRS
+ truncate.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.off_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
uname
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/truncate.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/truncate.h
new file mode 100644
index 00000000000000..96198300e59490
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/truncate.h
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Syscall wrapper for truncate.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TRUNCATE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TRUNCATE_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/off_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> truncate(const char *path, off_t len) {
+#ifdef SYS_truncate
+ return syscall_checked<int>(SYS_truncate, path, len);
+#elif defined(SYS_truncate64)
+ // Same as truncate but can handle large offsets
+ static_assert(sizeof(off_t) == 8);
+ return syscall_checked<int>(SYS_truncate64, path, (long)len,
+ (long)(((uint64_t)(len)) >> 32));
+#else
+#error "truncate and truncate64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TRUNCATE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c95357e505b8ce..f179b86543e5d3 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -1133,11 +1133,11 @@ add_entrypoint_object(
../truncate.h
DEPENDS
libc.hdr.types.off_t
- libc.hdr.fcntl_macros
- libc.hdr.stdint_proxy
- libc.include.unistd
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.hdr.unistd_macros
+ libc.src.__support.OSUtil.linux.syscall_wrappers.truncate
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/truncate.cpp b/libc/src/unistd/linux/truncate.cpp
index 204b27f7308d23..b349ae81db4249 100644
--- a/libc/src/unistd/linux/truncate.cpp
+++ b/libc/src/unistd/linux/truncate.cpp
@@ -1,37 +1,29 @@
-//===-- Linux implementation of truncate ----------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Linux implementation for truncate.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/truncate.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/truncate.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "hdr/stdint_proxy.h" // For uint64_t.
-#include "hdr/unistd_macros.h"
-#include <sys/syscall.h> // For syscall numbers.
-
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, truncate, (const char *path, off_t len)) {
-#ifdef SYS_truncate
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_truncate, path, len);
-#elif defined(SYS_truncate64)
- // Same as truncate but can handle large offsets
- static_assert(sizeof(off_t) == 8);
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_truncate64, path, (long)len,
- (long)(((uint64_t)(len)) >> 32));
-#else
-#error "truncate and truncate64 syscalls not available."
-#endif
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::truncate(path, len);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
return 0;
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4e6b21a39f5337..fc7c2c6698bebb 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3436,6 +3436,23 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_truncate",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/truncate.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":hdr_stdint_proxy",
+ ":types_off_t",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_uname",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/uname.h"],
@@ -19511,9 +19528,8 @@ libc_function(
":__support_common",
":__support_libc_errno",
":__support_macros_config",
- ":__support_osutil_syscall",
+ ":__support_osutil_linux_syscall_wrappers_truncate",
":errno",
- ":hdr_stdint_proxy",
":hdr_unistd_macros",
":types_off_t",
],
More information about the llvm-branch-commits
mailing list