Skip to content

[llvm][Support] Add and use errnoAsErrorCode #84423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang-tools-extra/clangd/JSONTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class JSONTransport : public Transport {
return error(std::make_error_code(std::errc::operation_canceled),
"Got signal, shutting down");
if (ferror(In))
return llvm::errorCodeToError(
std::error_code(errno, std::system_category()));
return llvm::errorCodeToError(llvm::errnoAsErrorCode());
if (readRawMessage(JSON)) {
ThreadCrashReporter ScopedReporter([&JSON]() {
auto &OS = llvm::errs();
Expand Down
9 changes: 3 additions & 6 deletions clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::creat
const int InotifyFD = inotify_init1(IN_CLOEXEC);
if (InotifyFD == -1)
return llvm::make_error<llvm::StringError>(
std::string("inotify_init1() error: ") + strerror(errno),
llvm::inconvertibleErrorCode());
llvm::errnoAsErrorCode(), std::string(": inotify_init1()"));

const int InotifyWD = inotify_add_watch(
InotifyFD, Path.str().c_str(),
Expand All @@ -346,15 +345,13 @@ llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::creat
);
if (InotifyWD == -1)
return llvm::make_error<llvm::StringError>(
std::string("inotify_add_watch() error: ") + strerror(errno),
llvm::inconvertibleErrorCode());
llvm::errnoAsErrorCode(), std::string(": inotify_add_watch()"));

auto InotifyPollingStopper = SemaphorePipe::create();

if (!InotifyPollingStopper)
return llvm::make_error<llvm::StringError>(
std::string("SemaphorePipe::create() error: ") + strerror(errno),
llvm::inconvertibleErrorCode());
llvm::errnoAsErrorCode(), std::string(": SemaphorePipe::create()"));

return std::make_unique<DirectoryWatcherLinux>(
Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
Expand Down
14 changes: 14 additions & 0 deletions llvm/include/llvm/Support/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,20 @@ Error errorCodeToError(std::error_code EC);
/// will trigger a call to abort().
std::error_code errorToErrorCode(Error Err);

/// Helper to get errno as an std::error_code.
///
/// errno should always be represented using the generic category as that's what
/// both libc++ and libstdc++ do. On POSIX systems you can also represent them
/// using the system category, however this makes them compare differently for
/// values outside of those used by `std::errc` if one is generic and the other
/// is system.
///
/// See the libc++ and libstdc++ implementations of `default_error_condition` on
/// the system category for more details on what the difference is.
inline std::error_code errnoAsErrorCode() {
return std::error_code(errno, std::generic_category());
}

/// Convert an ErrorOr<T> to an Expected<T>.
template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) {
if (auto EC = EO.getError())
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ void SharedMemoryMapper::reserve(size_t NumBytes,

int SharedMemoryFile = shm_open(SharedMemoryName.c_str(), O_RDWR, 0700);
if (SharedMemoryFile < 0) {
return OnReserved(errorCodeToError(
std::error_code(errno, std::generic_category())));
return OnReserved(errorCodeToError(errnoAsErrorCode()));
}

// this prevents other processes from accessing it by name
Expand All @@ -251,8 +250,7 @@ void SharedMemoryMapper::reserve(size_t NumBytes,
LocalAddr = mmap(nullptr, NumBytes, PROT_READ | PROT_WRITE, MAP_SHARED,
SharedMemoryFile, 0);
if (LocalAddr == MAP_FAILED) {
return OnReserved(errorCodeToError(
std::error_code(errno, std::generic_category())));
return OnReserved(errorCodeToError(errnoAsErrorCode()));
}

close(SharedMemoryFile);
Expand Down Expand Up @@ -376,8 +374,7 @@ void SharedMemoryMapper::release(ArrayRef<ExecutorAddr> Bases,
#if defined(LLVM_ON_UNIX)

if (munmap(Reservations[Base].LocalAddr, Reservations[Base].Size) != 0)
Err = joinErrors(std::move(Err), errorCodeToError(std::error_code(
errno, std::generic_category())));
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));

#elif defined(_WIN32)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ ExecutorSharedMemoryMapperService::reserve(uint64_t Size) {
int SharedMemoryFile =
shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT | O_EXCL, 0700);
if (SharedMemoryFile < 0)
return errorCodeToError(std::error_code(errno, std::generic_category()));
return errorCodeToError(errnoAsErrorCode());

// by default size is 0
if (ftruncate(SharedMemoryFile, Size) < 0)
return errorCodeToError(std::error_code(errno, std::generic_category()));
return errorCodeToError(errnoAsErrorCode());

void *Addr = mmap(nullptr, Size, PROT_NONE, MAP_SHARED, SharedMemoryFile, 0);
if (Addr == MAP_FAILED)
return errorCodeToError(std::error_code(errno, std::generic_category()));
return errorCodeToError(errnoAsErrorCode());

close(SharedMemoryFile);

Expand Down Expand Up @@ -140,7 +140,7 @@ Expected<ExecutorAddr> ExecutorSharedMemoryMapperService::initialize(
NativeProt |= PROT_EXEC;

if (mprotect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt))
return errorCodeToError(std::error_code(errno, std::generic_category()));
return errorCodeToError(errnoAsErrorCode());

#elif defined(_WIN32)

Expand Down Expand Up @@ -240,8 +240,7 @@ Error ExecutorSharedMemoryMapperService::release(
#if defined(LLVM_ON_UNIX)

if (munmap(Base.toPtr<void *>(), Size) != 0)
Err = joinErrors(std::move(Err), errorCodeToError(std::error_code(
errno, std::generic_category())));
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));

#elif defined(_WIN32)
(void)Size;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) {
ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
if (!PathToOrErr || !DirFromOrErr)
return errorCodeToError(std::error_code(errno, std::generic_category()));
return errorCodeToError(errnoAsErrorCode());

const SmallString<128> &PathTo = *PathToOrErr;
const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Support/AutoConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ int enableAutoConversion(int FD) {

std::error_code llvm::disableAutoConversion(int FD) {
if (::disableAutoConversion(FD) == -1)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

return std::error_code();
}

std::error_code llvm::enableAutoConversion(int FD) {
if (::enableAutoConversion(FD) == -1)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

return std::error_code();
}

std::error_code llvm::restoreStdHandleAutoConversion(int FD) {
if (::restoreStdHandleAutoConversion(FD) == -1)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

return std::error_code();
}
Expand All @@ -111,7 +111,7 @@ std::error_code llvm::setFileTag(int FD, int CCSID, bool Text) {
Tag.ft_rsvflags = 0;

if (fcntl(FD, F_SETTAG, &Tag) == -1)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();
return std::error_code();
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/LockFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
struct timespec wait = {1, 0}; // 1 second.
uuid_t uuid;
if (gethostuuid(uuid, &wait) != 0)
return std::error_code(errno, std::system_category());
return errnoAsErrorCode();

uuid_string_t UUIDStr;
uuid_unparse(uuid, UUIDStr);
Expand Down
19 changes: 7 additions & 12 deletions llvm/lib/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include <cctype>
#include <cerrno>

#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
Expand Down Expand Up @@ -1010,7 +1009,7 @@ static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
delete[] Buf;

if (BytesRead < 0 || BytesWritten < 0)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();
return std::error_code();
}

Expand Down Expand Up @@ -1060,7 +1059,7 @@ ErrorOr<MD5::MD5Result> md5_contents(int FD) {
}

if (BytesRead < 0)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();
MD5::MD5Result Result;
Hash.final(Result);
return Result;
Expand Down Expand Up @@ -1228,7 +1227,7 @@ TempFile::~TempFile() { assert(Done); }
Error TempFile::discard() {
Done = true;
if (FD != -1 && close(FD) == -1) {
std::error_code EC = std::error_code(errno, std::generic_category());
std::error_code EC = errnoAsErrorCode();
return errorCodeToError(EC);
}
FD = -1;
Expand Down Expand Up @@ -1297,10 +1296,8 @@ Error TempFile::keep(const Twine &Name) {
if (!RenameEC)
TmpName = "";

if (close(FD) == -1) {
std::error_code EC(errno, std::generic_category());
return errorCodeToError(EC);
}
if (close(FD) == -1)
return errorCodeToError(errnoAsErrorCode());
FD = -1;

return errorCodeToError(RenameEC);
Expand All @@ -1319,10 +1316,8 @@ Error TempFile::keep() {

TmpName = "";

if (close(FD) == -1) {
std::error_code EC(errno, std::generic_category());
return errorCodeToError(EC);
}
if (close(FD) == -1)
return errorCodeToError(errnoAsErrorCode());
FD = -1;

return Error::success();
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/Support/RandomNumberGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
#ifdef _WIN32
#include "llvm/Support/Windows/WindowsSupport.h"
Expand Down Expand Up @@ -81,14 +82,14 @@ std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
std::error_code Ret;
ssize_t BytesRead = read(Fd, Buffer, Size);
if (BytesRead == -1)
Ret = std::error_code(errno, std::system_category());
Ret = errnoAsErrorCode();
else if (BytesRead != static_cast<ssize_t>(Size))
Ret = std::error_code(EIO, std::system_category());
if (close(Fd) == -1)
Ret = std::error_code(errno, std::system_category());
Ret = errnoAsErrorCode();

return Ret;
}
return std::error_code(errno, std::system_category());
return errnoAsErrorCode();
#endif
}
10 changes: 5 additions & 5 deletions llvm/lib/Support/Unix/Memory.inc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
#else
fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
EC = std::error_code(errno, std::generic_category());
EC = errnoAsErrorCode();
return MemoryBlock();
}
#endif
Expand Down Expand Up @@ -122,7 +122,7 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
}

EC = std::error_code(errno, std::generic_category());
EC = errnoAsErrorCode();
#if !defined(MAP_ANON)
close(fd);
#endif
Expand Down Expand Up @@ -153,7 +153,7 @@ std::error_code Memory::releaseMappedMemory(MemoryBlock &M) {
return std::error_code();

if (0 != ::munmap(M.Address, M.AllocatedSize))
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

M.Address = nullptr;
M.AllocatedSize = 0;
Expand Down Expand Up @@ -186,7 +186,7 @@ std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
if (InvalidateCache && !(Protect & PROT_READ)) {
int Result = ::mprotect((void *)Start, End - Start, Protect | PROT_READ);
if (Result != 0)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize);
InvalidateCache = false;
Expand All @@ -196,7 +196,7 @@ std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
int Result = ::mprotect((void *)Start, End - Start, Protect);

if (Result != 0)
return std::error_code(errno, std::generic_category());
return errnoAsErrorCode();

if (InvalidateCache)
Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize);
Expand Down
Loading