Skip to content

Commit f0a3954

Browse files
[libc][cleanup] Fix most conversion warnings
This patch is large, but is almost entirely just adding casts to calls to syscall_impl. Much of the work was done programatically, with human checking when the syntax or types got confusing. Reviewed By: mcgrathr Differential Revision: https://reviews.llvm.org/D156950
1 parent f6267d3 commit f0a3954

File tree

110 files changed

+383
-333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+383
-333
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ LIBC_INLINE constexpr To bit_cast(const From &from) {
5050
#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
5151
}
5252

53+
template <class To, class From>
54+
LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
55+
if constexpr (sizeof(To) == sizeof(From)) {
56+
return bit_cast<To>(from);
57+
} else {
58+
return static_cast<To>(from);
59+
}
60+
}
61+
5362
} // namespace __llvm_libc::cpp
5463

5564
#endif // LLVM_LIBC_SUPPORT_CPP_BIT_H

libc/src/__support/File/linux/dir.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ namespace __llvm_libc {
1919
ErrorOr<int> platform_opendir(const char *name) {
2020
int open_flags = O_RDONLY | O_DIRECTORY | O_CLOEXEC;
2121
#ifdef SYS_open
22-
int fd = __llvm_libc::syscall_impl(SYS_open, name, open_flags);
22+
int fd = __llvm_libc::syscall_impl<int>(SYS_open, name, open_flags);
2323
#elif defined(SYS_openat)
24-
int fd = __llvm_libc::syscall_impl(SYS_openat, AT_FDCWD, name, open_flags);
24+
int fd =
25+
__llvm_libc::syscall_impl<int>(SYS_openat, AT_FDCWD, name, open_flags);
2526
#else
2627
#error \
2728
"SYS_open and SYS_openat syscalls not available to perform an open operation."
@@ -35,8 +36,8 @@ ErrorOr<int> platform_opendir(const char *name) {
3536

3637
ErrorOr<size_t> platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
3738
#ifdef SYS_getdents64
38-
long size = __llvm_libc::syscall_impl(SYS_getdents64, fd, buffer.data(),
39-
buffer.size());
39+
long size = __llvm_libc::syscall_impl<long>(SYS_getdents64, fd, buffer.data(),
40+
buffer.size());
4041
#else
4142
#error "getdents64 syscalls not available to perform a fetch dirents operation."
4243
#endif
@@ -48,7 +49,7 @@ ErrorOr<size_t> platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
4849
}
4950

5051
int platform_closedir(int fd) {
51-
long ret = __llvm_libc::syscall_impl(SYS_close, fd);
52+
int ret = __llvm_libc::syscall_impl<int>(SYS_close, fd);
5253
if (ret < 0) {
5354
return static_cast<int>(-ret);
5455
}

libc/src/__support/File/linux/file.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace {
4444

4545
FileIOResult write_func(File *f, const void *data, size_t size) {
4646
auto *lf = reinterpret_cast<LinuxFile *>(f);
47-
int ret = __llvm_libc::syscall_impl(SYS_write, lf->get_fd(), data, size);
47+
int ret = __llvm_libc::syscall_impl<int>(SYS_write, lf->get_fd(), data, size);
4848
if (ret < 0) {
4949
return {0, -ret};
5050
}
@@ -53,7 +53,7 @@ FileIOResult write_func(File *f, const void *data, size_t size) {
5353

5454
FileIOResult read_func(File *f, void *buf, size_t size) {
5555
auto *lf = reinterpret_cast<LinuxFile *>(f);
56-
int ret = __llvm_libc::syscall_impl(SYS_read, lf->get_fd(), buf, size);
56+
int ret = __llvm_libc::syscall_impl<int>(SYS_read, lf->get_fd(), buf, size);
5757
if (ret < 0) {
5858
return {0, -ret};
5959
}
@@ -64,16 +64,22 @@ ErrorOr<long> seek_func(File *f, long offset, int whence) {
6464
auto *lf = reinterpret_cast<LinuxFile *>(f);
6565
long result;
6666
#ifdef SYS_lseek
67-
int ret = __llvm_libc::syscall_impl(SYS_lseek, lf->get_fd(), offset, whence);
67+
int ret =
68+
__llvm_libc::syscall_impl<int>(SYS_lseek, lf->get_fd(), offset, whence);
6869
result = ret;
6970
#elif defined(SYS_llseek)
70-
int ret = __llvm_libc::syscall_impl(SYS_llseek, lf->get_fd(),
71-
(long)(((uint64_t)(offset)) >> 32),
72-
(long)offset, &result, whence);
71+
int ret = __llvm_libc::syscall_impl<int>(SYS_llseek, lf->get_fd(),
72+
(long)(((uint64_t)(offset)) >> 32),
73+
(long)offset, &result, whence);
74+
result = ret;
75+
#elif defined(SYS_llseek)
76+
int ret = __llvm_libc::syscall_impl<int>(SYS_llseek, lf->get_fd(),
77+
(long)(((uint64_t)(offset)) >> 32),
78+
(long)offset, &result, whence);
7379
result = ret;
7480
#elif defined(SYS__llseek)
75-
int ret = __llvm_libc::syscall_impl(SYS__llseek, lf->get_fd(), offset >> 32,
76-
offset, &result, whence);
81+
int ret = __llvm_libc::syscall_impl<int>(
82+
SYS__llseek, lf->get_fd(), offset >> 32, offset, &result, whence);
7783
#else
7884
#error "lseek, llseek and _llseek syscalls not available."
7985
#endif
@@ -86,7 +92,7 @@ ErrorOr<long> seek_func(File *f, long offset, int whence) {
8692

8793
int close_func(File *f) {
8894
auto *lf = reinterpret_cast<LinuxFile *>(f);
89-
int ret = __llvm_libc::syscall_impl(SYS_close, lf->get_fd());
95+
int ret = __llvm_libc::syscall_impl<int>(SYS_close, lf->get_fd());
9096
if (ret < 0) {
9197
return -ret;
9298
}
@@ -128,10 +134,11 @@ ErrorOr<File *> openfile(const char *path, const char *mode) {
128134
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
129135

130136
#ifdef SYS_open
131-
int fd = __llvm_libc::syscall_impl(SYS_open, path, open_flags, OPEN_MODE);
137+
int fd =
138+
__llvm_libc::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
132139
#elif defined(SYS_openat)
133-
int fd = __llvm_libc::syscall_impl(SYS_openat, AT_FDCWD, path, open_flags,
134-
OPEN_MODE);
140+
int fd = __llvm_libc::syscall_impl<int>(SYS_openat, AT_FDCWD, path,
141+
open_flags, OPEN_MODE);
135142
#else
136143
#error "open and openat syscalls not available."
137144
#endif

libc/src/__support/OSUtil/darwin/quick_exit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace __llvm_libc {
1717

1818
LIBC_INLINE void quick_exit(int status) {
1919
for (;;) {
20-
__llvm_libc::syscall_impl(1 /* SYS_exit */, status);
20+
__llvm_libc::syscall_impl<long>(1 /* SYS_exit */, status);
2121
}
2222
}
2323

libc/src/__support/OSUtil/darwin/syscall.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H
1010
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H
1111

12+
#include "src/__support/CPP/bit.h"
1213
#include "src/__support/common.h"
1314
#include "src/__support/macros/properties/architectures.h"
1415

@@ -20,10 +21,10 @@
2021

2122
namespace __llvm_libc {
2223

23-
template <typename... Ts>
24-
LIBC_INLINE long syscall_impl(long __number, Ts... ts) {
24+
template <typename R, typename... Ts>
25+
LIBC_INLINE R syscall_impl(long __number, Ts... ts) {
2526
static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall");
26-
return syscall_impl(__number, (long)ts...);
27+
return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));
2728
}
2829

2930
} // namespace __llvm_libc

libc/src/__support/OSUtil/linux/io.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
namespace __llvm_libc {
1818

1919
LIBC_INLINE void write_to_stderr(cpp::string_view msg) {
20-
__llvm_libc::syscall_impl(SYS_write, 2 /* stderr */, msg.data(), msg.size());
20+
__llvm_libc::syscall_impl<long>(SYS_write, 2 /* stderr */, msg.data(),
21+
msg.size());
2122
}
2223

2324
} // namespace __llvm_libc

libc/src/__support/OSUtil/linux/quick_exit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace __llvm_libc {
1919

2020
LIBC_INLINE void quick_exit(int status) {
2121
for (;;) {
22-
__llvm_libc::syscall_impl(SYS_exit_group, status);
23-
__llvm_libc::syscall_impl(SYS_exit, status);
22+
__llvm_libc::syscall_impl<long>(SYS_exit_group, status);
23+
__llvm_libc::syscall_impl<long>(SYS_exit, status);
2424
}
2525
}
2626

libc/src/__support/OSUtil/linux/syscall.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_LINUX_SYSCALL_H
1010
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_LINUX_SYSCALL_H
1111

12+
#include "src/__support/CPP/bit.h"
1213
#include "src/__support/common.h"
1314
#include "src/__support/macros/properties/architectures.h"
1415

@@ -24,10 +25,10 @@
2425

2526
namespace __llvm_libc {
2627

27-
template <typename... Ts>
28-
LIBC_INLINE long syscall_impl(long __number, Ts... ts) {
28+
template <typename R, typename... Ts>
29+
LIBC_INLINE R syscall_impl(long __number, Ts... ts) {
2930
static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall");
30-
return syscall_impl(__number, (long)ts...);
31+
return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));
3132
}
3233

3334
} // namespace __llvm_libc

libc/src/__support/threads/linux/callonce.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ int callonce(CallOnceFlag *flag, CallOnceCallback *func) {
3434
func();
3535
auto status = futex_word->exchange(FINISH);
3636
if (status == WAITING) {
37-
__llvm_libc::syscall_impl(FUTEX_SYSCALL_ID, &futex_word->val,
38-
FUTEX_WAKE_PRIVATE,
39-
INT_MAX, // Wake all waiters.
40-
0, 0, 0);
37+
__llvm_libc::syscall_impl<long>(FUTEX_SYSCALL_ID, &futex_word->val,
38+
FUTEX_WAKE_PRIVATE,
39+
INT_MAX, // Wake all waiters.
40+
0, 0, 0);
4141
}
4242
return 0;
4343
}
4444

4545
FutexWordType status = START;
4646
if (futex_word->compare_exchange_strong(status, WAITING) ||
4747
status == WAITING) {
48-
__llvm_libc::syscall_impl(
48+
__llvm_libc::syscall_impl<long>(
4949
FUTEX_SYSCALL_ID, &futex_word->val, FUTEX_WAIT_PRIVATE,
5050
WAITING, // Block only if status is still |WAITING|.
5151
0, 0, 0);

libc/src/__support/threads/linux/mutex.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ struct Mutex {
7676
// futex syscall will block if the futex data is still
7777
// `LockState::Waiting` (the 4th argument to the syscall function
7878
// below.)
79-
__llvm_libc::syscall_impl(FUTEX_SYSCALL_ID, &futex_word.val,
80-
FUTEX_WAIT_PRIVATE,
81-
FutexWordType(LockState::Waiting), 0, 0, 0);
79+
__llvm_libc::syscall_impl<long>(
80+
FUTEX_SYSCALL_ID, &futex_word.val, FUTEX_WAIT_PRIVATE,
81+
FutexWordType(LockState::Waiting), 0, 0, 0);
8282
was_waiting = true;
8383
// Once woken up/unblocked, try everything all over.
8484
continue;
@@ -91,9 +91,9 @@ struct Mutex {
9191
// we will wait for the futex to be woken up. Note again that the
9292
// following syscall will block only if the futex data is still
9393
// `LockState::Waiting`.
94-
__llvm_libc::syscall_impl(FUTEX_SYSCALL_ID, &futex_word,
95-
FUTEX_WAIT_PRIVATE,
96-
FutexWordType(LockState::Waiting), 0, 0, 0);
94+
__llvm_libc::syscall_impl<long>(
95+
FUTEX_SYSCALL_ID, &futex_word, FUTEX_WAIT_PRIVATE,
96+
FutexWordType(LockState::Waiting), 0, 0, 0);
9797
was_waiting = true;
9898
}
9999
continue;
@@ -110,8 +110,8 @@ struct Mutex {
110110
if (futex_word.compare_exchange_strong(mutex_status,
111111
FutexWordType(LockState::Free))) {
112112
// If any thread is waiting to be woken up, then do it.
113-
__llvm_libc::syscall_impl(FUTEX_SYSCALL_ID, &futex_word,
114-
FUTEX_WAKE_PRIVATE, 1, 0, 0, 0);
113+
__llvm_libc::syscall_impl<long>(FUTEX_SYSCALL_ID, &futex_word,
114+
FUTEX_WAKE_PRIVATE, 1, 0, 0, 0);
115115
return MutexError::NONE;
116116
}
117117

0 commit comments

Comments
 (0)