Finishing touch for the std::error_code transition.

While std::error_code itself seems to work OK in all platforms, there
are few annoying differences with regards to the std::errc enumeration.

This patch adds a simple llvm enumeration, which will hopefully avoid build
breakages in other platforms and surprises as we get more uses of
std::error_code.

llvm-svn: 210920
This commit is contained in:
Rafael Espindola 2014-06-13 17:20:48 +00:00
parent 372bc70c63
commit 2a826e40fa
16 changed files with 134 additions and 38 deletions

View File

@ -0,0 +1,86 @@
//===- llvm/Support/Errc.h - Defines the llvm::errc enum --------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// While std::error_code works OK on all platforms we use, there are some
// some problems with std::errc that can be avoided by using our own
// enumeration:
//
// * std::errc is a namespace in some implementations. That meas that ADL
// doesn't work and it is sometimes necessary to write std::make_error_code
// or in templates:
// using std::make_error_code;
// make_error_code(...);
//
// with this enum it is safe to always just use make_error_code.
//
// * Some implementations define fewer names than others. This header has
// the intersection of all the ones we support.
//
// * std::errc is just marked with is_error_condition_enum. This means that
// common patters like AnErrorCode == errc::no_such_file_or_directory take
// 4 virtual calls instead of two comparisons.
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_ERRC_H
#define LLVM_SUPPORT_ERRC_H
#include <system_error>
namespace llvm {
enum class errc {
argument_list_too_long = int(std::errc::argument_list_too_long),
argument_out_of_domain = int(std::errc::argument_out_of_domain),
bad_address = int(std::errc::bad_address),
bad_file_descriptor = int(std::errc::bad_file_descriptor),
broken_pipe = int(std::errc::broken_pipe),
device_or_resource_busy = int(std::errc::device_or_resource_busy),
directory_not_empty = int(std::errc::directory_not_empty),
executable_format_error = int(std::errc::executable_format_error),
file_exists = int(std::errc::file_exists),
file_too_large = int(std::errc::file_too_large),
filename_too_long = int(std::errc::filename_too_long),
function_not_supported = int(std::errc::function_not_supported),
illegal_byte_sequence = int(std::errc::illegal_byte_sequence),
inappropriate_io_control_operation =
int(std::errc::inappropriate_io_control_operation),
interrupted = int(std::errc::interrupted),
invalid_argument = int(std::errc::invalid_argument),
invalid_seek = int(std::errc::invalid_seek),
io_error = int(std::errc::io_error),
is_a_directory = int(std::errc::is_a_directory),
no_child_process = int(std::errc::no_child_process),
no_lock_available = int(std::errc::no_lock_available),
no_space_on_device = int(std::errc::no_space_on_device),
no_such_device_or_address = int(std::errc::no_such_device_or_address),
no_such_device = int(std::errc::no_such_device),
no_such_file_or_directory = int(std::errc::no_such_file_or_directory),
no_such_process = int(std::errc::no_such_process),
not_a_directory = int(std::errc::not_a_directory),
not_enough_memory = int(std::errc::not_enough_memory),
operation_not_permitted = int(std::errc::operation_not_permitted),
permission_denied = int(std::errc::permission_denied),
read_only_file_system = int(std::errc::read_only_file_system),
resource_deadlock_would_occur = int(std::errc::resource_deadlock_would_occur),
resource_unavailable_try_again =
int(std::errc::resource_unavailable_try_again),
result_out_of_range = int(std::errc::result_out_of_range),
too_many_files_open_in_system = int(std::errc::too_many_files_open_in_system),
too_many_files_open = int(std::errc::too_many_files_open),
too_many_links = int(std::errc::too_many_links)
};
inline std::error_code make_error_code(errc E) {
return std::error_code(static_cast<int>(E), std::generic_category());
}
}
namespace std {
template <> struct is_error_code_enum<llvm::errc> : std::true_type {};
}
#endif

View File

@ -99,7 +99,6 @@ public:
std::is_error_condition_enum<E>::value,
void *>::type = 0)
: HasError(true) {
using std::make_error_code;
new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
}

View File

@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
@ -51,7 +52,7 @@ FileOutputBuffer::create(StringRef FilePath, size_t Size,
if (EC)
return EC;
else
return std::make_error_code(std::errc::operation_not_permitted);
return make_error_code(errc::operation_not_permitted);
}
// Delete target file.

View File

@ -9,6 +9,7 @@
#include "llvm/Support/LockFileManager.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@ -112,7 +113,7 @@ LockFileManager::LockFileManager(StringRef FileName)
if (Out.has_error()) {
// We failed to write out PID, so make up an excuse, remove the
// unique lock file, and fail.
Error = std::make_error_code(std::errc::no_space_on_device);
Error = make_error_code(errc::no_space_on_device);
sys::fs::remove(UniqueLockFileName.c_str());
return;
}
@ -125,7 +126,7 @@ LockFileManager::LockFileManager(StringRef FileName)
if (!EC)
return;
if (EC != std::errc::file_exists) {
if (EC != errc::file_exists) {
Error = EC;
return;
}

View File

@ -14,6 +14,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MathExtras.h"
@ -364,7 +365,7 @@ static std::error_code getOpenFileImpl(int FD, const char *Filename,
if (!Buf) {
// Failed to create a buffer. The only way it can fail is if
// new(std::nothrow) returns 0.
return std::make_error_code(std::errc::not_enough_memory);
return make_error_code(errc::not_enough_memory);
}
std::unique_ptr<MemoryBuffer> SB(Buf);

View File

@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errc.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
@ -204,7 +205,7 @@ retry_random_path:
if (std::error_code EC =
sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
if (EC == std::errc::file_exists)
if (EC == errc::file_exists)
goto retry_random_path;
return EC;
}
@ -225,7 +226,7 @@ retry_random_path:
case FS_Dir: {
if (std::error_code EC =
sys::fs::create_directory(ResultPath.begin(), false)) {
if (EC == std::errc::file_exists)
if (EC == errc::file_exists)
goto retry_random_path;
return EC;
}
@ -830,7 +831,7 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting) {
std::error_code EC = create_directory(P, IgnoreExisting);
// If we succeeded, or had any error other than the parent not existing, just
// return it.
if (EC != std::errc::no_such_file_or_directory)
if (EC != errc::no_such_file_or_directory)
return EC;
// We failed because of a no_such_file_or_directory, try to create the

View File

@ -317,7 +317,7 @@ std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
// effectively prevents LLVM from erasing things like /dev/null, any block
// special file, or other things that aren't "regular" files.
if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode))
return make_error_code(std::errc::operation_not_permitted);
return make_error_code(errc::operation_not_permitted);
if (::remove(p.begin()) == -1) {
if (errno != ENOENT || !IgnoreNonExisting)
@ -404,7 +404,7 @@ static std::error_code fillStatus(int StatRet, const struct stat &Status,
file_status &Result) {
if (StatRet != 0) {
std::error_code ec(errno, std::generic_category());
if (ec == std::errc::no_such_file_or_directory)
if (ec == errc::no_such_file_or_directory)
Result = file_status(file_type::file_not_found);
else
Result = file_status(file_type::status_error);
@ -468,7 +468,7 @@ std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
return std::error_code();
#else
#warning Missing futimes() and futimens()
return make_error_code(std::errc::not_supported);
return make_error_code(errc::not_supported);
#endif
}
@ -512,7 +512,7 @@ mapped_file_region::mapped_file_region(const Twine &path,
, Mapping() {
// Make sure that the requested size fits within SIZE_T.
if (length > std::numeric_limits<size_t>::max()) {
ec = make_error_code(std::errc::invalid_argument);
ec = make_error_code(errc::invalid_argument);
return;
}
@ -541,7 +541,7 @@ mapped_file_region::mapped_file_region(int fd,
, Mapping() {
// Make sure that the requested size fits within SIZE_T.
if (length > std::numeric_limits<size_t>::max()) {
ec = make_error_code(std::errc::invalid_argument);
ec = make_error_code(errc::invalid_argument);
return;
}

View File

@ -196,7 +196,7 @@ std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
file_status ST;
if (std::error_code EC = status(path, ST)) {
if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
return EC;
return std::error_code();
}
@ -208,14 +208,14 @@ std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
if (ST.type() == file_type::directory_file) {
if (!::RemoveDirectoryW(c_str(path_utf16))) {
std::error_code EC = windows_error(::GetLastError());
if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
return EC;
}
return std::error_code();
}
if (!::DeleteFileW(c_str(path_utf16))) {
std::error_code EC = windows_error(::GetLastError());
if (EC != std::errc::no_such_file_or_directory || !IgnoreNonExisting)
if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
return EC;
}
return std::error_code();
@ -481,7 +481,7 @@ std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset)
_close(FileDescriptor);
} else
::CloseHandle(FileHandle);
return std::make_error_code(std::errc::invalid_argument);
return make_error_code(errc::invalid_argument);
}
DWORD flprotect;
@ -617,7 +617,7 @@ mapped_file_region::mapped_file_region(int fd,
if (closefd)
_close(FileDescriptor);
FileDescriptor = 0;
ec = std::make_error_code(std::errc::bad_file_descriptor);
ec = make_error_code(errc::bad_file_descriptor);
return;
}
@ -779,7 +779,7 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD) {
if (LastError != ERROR_ACCESS_DENIED)
return EC;
if (is_directory(Name))
return std::make_error_code(std::errc::is_a_directory);
return make_error_code(errc::is_a_directory);
return EC;
}
@ -831,7 +831,7 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
if (LastError != ERROR_ACCESS_DENIED)
return EC;
if (is_directory(Name))
return std::make_error_code(std::errc::is_a_directory);
return make_error_code(errc::is_a_directory);
return EC;
}

View File

@ -12,12 +12,12 @@
// errors to generic ones. The one implemented in msvc is too conservative
// for llvm, so we do an extra mapping when constructing an error_code
// from an windows error. This allows the rest of llvm to simple checks
// like "EC == std::errc::file_exists" and have it work on both posix and
// like "EC == errc::file_exists" and have it work on both posix and
// windows.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errc.h"
#include "llvm/Support/WindowsError.h"
#include "llvm/Config/llvm-config.h"
@ -30,7 +30,7 @@
// I'd rather not double the line count of the following.
#define MAP_ERR_TO_COND(x, y) \
case x: \
return std::make_error_code(std::errc::y)
return make_error_code(errc::y)
std::error_code llvm::mapWindowsError(unsigned EV) {
switch (EV) {

View File

@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Errc.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Casting.h"
@ -72,7 +73,7 @@ bool Input::setCurrentDocument() {
Node *N = DocIterator->getRoot();
if (!N) {
assert(Strm->failed() && "Root is NULL iff parsing failed");
EC = std::make_error_code(std::errc::invalid_argument);
EC = make_error_code(errc::invalid_argument);
return false;
}
@ -122,7 +123,7 @@ bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
// nodes are present.
if (!CurrentNode) {
if (Required)
EC = std::make_error_code(std::errc::invalid_argument);
EC = make_error_code(errc::invalid_argument);
return false;
}
@ -298,7 +299,7 @@ void Input::setError(HNode *hnode, const Twine &message) {
void Input::setError(Node *node, const Twine &message) {
Strm->printError(node, message);
EC = std::make_error_code(std::errc::invalid_argument);
EC = make_error_code(errc::invalid_argument);
}
Input::HNode *Input::createHNodes(Node *N) {

View File

@ -17,6 +17,7 @@
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
@ -453,7 +454,7 @@ int NewArchiveIterator::getFD() const {
// Linux cannot open directories with open(2), although
// cygwin and *bsd can.
if (NewStatus.type() == sys::fs::file_type::directory_file)
failIfError(std::make_error_code(std::errc::is_a_directory), NewFilename);
failIfError(make_error_code(errc::is_a_directory), NewFilename);
return NewFD;
}
@ -939,7 +940,7 @@ static int performOperation(ArchiveOperation Operation) {
// Create or open the archive object.
std::unique_ptr<MemoryBuffer> Buf;
std::error_code EC = MemoryBuffer::getFile(ArchiveName, Buf, -1, false);
if (EC && EC != std::errc::no_such_file_or_directory) {
if (EC && EC != errc::no_such_file_or_directory) {
errs() << ToolName << ": error opening '" << ArchiveName
<< "': " << EC.message() << "!\n";
return 1;
@ -957,7 +958,7 @@ static int performOperation(ArchiveOperation Operation) {
return 0;
}
assert(EC == std::errc::no_such_file_or_directory);
assert(EC == errc::no_such_file_or_directory);
if (!shouldCreateArchive(Operation)) {
failIfError(EC, Twine("error loading '") + ArchiveName + "'");

View File

@ -13,6 +13,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/GCOV.h"
#include "llvm/Support/ManagedStatic.h"
@ -116,7 +117,7 @@ int main(int argc, char **argv) {
std::unique_ptr<MemoryBuffer> GCDA_Buff;
if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
if (ec != std::errc::no_such_file_or_directory) {
if (ec != errc::no_such_file_or_directory) {
errs() << InputGCDA << ": " << ec.message() << "\n";
return 1;
}

View File

@ -19,6 +19,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@ -311,7 +312,7 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
getDarwinDWARFResourceForPath(Path);
BinaryOrErr = createBinary(ResourcePath);
std::error_code EC = BinaryOrErr.getError();
if (EC != std::errc::no_such_file_or_directory && !error(EC)) {
if (EC != errc::no_such_file_or_directory && !error(EC)) {
DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin));
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/Errc.h"
#include "gtest/gtest.h"
#include <memory>
@ -16,7 +17,7 @@ using namespace llvm;
namespace {
ErrorOr<int> t1() {return 1;}
ErrorOr<int> t2() { return std::errc::invalid_argument; }
ErrorOr<int> t2() { return errc::invalid_argument; }
TEST(ErrorOr, SimpleValue) {
ErrorOr<int> a = t1();
@ -30,7 +31,7 @@ TEST(ErrorOr, SimpleValue) {
a = t2();
EXPECT_FALSE(a);
EXPECT_EQ(std::errc::invalid_argument, a.getError());
EXPECT_EQ(a.getError(), errc::invalid_argument);
#ifdef EXPECT_DEBUG_DEATH
EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
#endif

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/Path.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
@ -355,10 +356,10 @@ TEST_F(FileSystemTest, TempFiles) {
ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
ASSERT_EQ(fs::remove(Twine(TempPath2), false),
std::errc::no_such_file_or_directory);
errc::no_such_file_or_directory);
std::error_code EC = fs::status(TempPath2.c_str(), B);
EXPECT_EQ(EC, std::errc::no_such_file_or_directory);
EXPECT_EQ(EC, errc::no_such_file_or_directory);
EXPECT_EQ(B.type(), fs::file_type::file_not_found);
// Make sure Temp2 doesn't exist.
@ -398,7 +399,7 @@ TEST_F(FileSystemTest, TempFiles) {
"abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
"abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
std::errc::no_such_file_or_directory);
errc::no_such_file_or_directory);
#endif
}
@ -406,7 +407,7 @@ TEST_F(FileSystemTest, CreateDir) {
ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
std::errc::file_exists);
errc::file_exists);
ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
}

View File

@ -18,6 +18,7 @@
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Path.h"
@ -58,7 +59,7 @@ bool removeIfExists(StringRef Path) {
// This is an approximation, on error we don't know in general if the file
// existed or not.
std::error_code EC = sys::fs::remove(Path, false);
return EC != std::errc::no_such_file_or_directory;
return EC != llvm::errc::no_such_file_or_directory;
}
char * current_dir() {