Remove windows_error.

MSVC doesn't seem to provide any is_error_code_enum enumeration for the
windows errors.

Fortunately very few places in llvm have to handle raw windows errors, so
we can just construct the corresponding error_code directly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210631 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-06-11 03:58:34 +00:00
parent 7456ad1240
commit e5e77d1cd8
5 changed files with 50 additions and 101 deletions

View File

@ -791,75 +791,6 @@ inline bool operator!=(const error_condition& _x, const error_code& _y) {
inline bool operator!=(const error_condition& _x, const error_condition& _y) {
return !(_x == _y);
}
// Windows errors.
// To construct an error_code after an API error:
//
// error_code( ::GetLastError(), system_category() )
enum class windows_error {
// These names and values are based on Windows WinError.h
// This is not a complete list. Add to this list if you need to explicitly
// check for it.
invalid_function = 1, // ERROR_INVALID_FUNCTION,
file_not_found = 2, // ERROR_FILE_NOT_FOUND,
path_not_found = 3, // ERROR_PATH_NOT_FOUND,
too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
access_denied = 5, // ERROR_ACCESS_DENIED,
invalid_handle = 6, // ERROR_INVALID_HANDLE,
arena_trashed = 7, // ERROR_ARENA_TRASHED,
not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
invalid_block = 9, // ERROR_INVALID_BLOCK,
bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
bad_format = 11, // ERROR_BAD_FORMAT,
invalid_access = 12, // ERROR_INVALID_ACCESS,
outofmemory = 14, // ERROR_OUTOFMEMORY,
invalid_drive = 15, // ERROR_INVALID_DRIVE,
current_directory = 16, // ERROR_CURRENT_DIRECTORY,
not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
no_more_files = 18, // ERROR_NO_MORE_FILES,
write_protect = 19, // ERROR_WRITE_PROTECT,
bad_unit = 20, // ERROR_BAD_UNIT,
not_ready = 21, // ERROR_NOT_READY,
bad_command = 22, // ERROR_BAD_COMMAND,
crc = 23, // ERROR_CRC,
bad_length = 24, // ERROR_BAD_LENGTH,
seek = 25, // ERROR_SEEK,
not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
out_of_paper = 28, // ERROR_OUT_OF_PAPER,
write_fault = 29, // ERROR_WRITE_FAULT,
read_fault = 30, // ERROR_READ_FAULT,
gen_failure = 31, // ERROR_GEN_FAILURE,
sharing_violation = 32, // ERROR_SHARING_VIOLATION,
lock_violation = 33, // ERROR_LOCK_VIOLATION,
wrong_disk = 34, // ERROR_WRONG_DISK,
sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
handle_eof = 38, // ERROR_HANDLE_EOF,
handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
rem_not_list = 51, // ERROR_REM_NOT_LIST,
dup_name = 52, // ERROR_DUP_NAME,
bad_net_path = 53, // ERROR_BAD_NETPATH,
network_busy = 54, // ERROR_NETWORK_BUSY,
file_exists = 80, // ERROR_FILE_EXISTS,
cannot_make = 82, // ERROR_CANNOT_MAKE,
broken_pipe = 109, // ERROR_BROKEN_PIPE,
open_failed = 110, // ERROR_OPEN_FAILED,
buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
disk_full = 112, // ERROR_DISK_FULL,
insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
lock_failed = 167, // ERROR_LOCK_FAILED,
busy = 170, // ERROR_BUSY,
cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
already_exists = 183 // ERROR_ALREADY_EXISTS
};
template <> struct is_error_code_enum<windows_error> : std::true_type { };
inline error_code make_error_code(windows_error e) {
return error_code(static_cast<int>(e), system_category());
}
} // end namespace llvm
#endif

View File

@ -44,6 +44,10 @@ using namespace llvm;
using llvm::sys::windows::UTF8ToUTF16;
using llvm::sys::windows::UTF16ToUTF8;
static error_code windows_error(DWORD E) {
return error_code(E, system_category());
}
static error_code TempDir(SmallVectorImpl<char> &Result) {
SmallVector<wchar_t, 64> Res;
retry_temp_dir:
@ -150,9 +154,9 @@ error_code create_directory(const Twine &path, bool IgnoreExisting) {
return ec;
if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
error_code ec = windows_error(::GetLastError());
if (ec != windows_error::already_exists || !IgnoreExisting)
return ec;
DWORD LastError = ::GetLastError();
if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting)
return windows_error(LastError);
}
return error_code();
@ -232,8 +236,8 @@ error_code rename(const Twine &from, const Twine &to) {
if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
return error_code();
ec = windows_error(::GetLastError());
if (ec != windows_error::access_denied)
DWORD LastError = ::GetLastError();
if (LastError != ERROR_ACCESS_DENIED)
break;
// Retry MoveFile() at ACCESS_DENIED.
// System scanners (eg. indexer) might open the source file when
@ -276,10 +280,10 @@ error_code exists(const Twine &path, bool &result) {
if (attributes == INVALID_FILE_ATTRIBUTES) {
// See if the file didn't actually exist.
error_code ec = make_error_code(windows_error(::GetLastError()));
if (ec != windows_error::file_not_found &&
ec != windows_error::path_not_found)
return ec;
DWORD LastError = ::GetLastError();
if (LastError != ERROR_FILE_NOT_FOUND &&
LastError != ERROR_PATH_NOT_FOUND)
return windows_error(LastError);
result = false;
} else
result = true;
@ -392,15 +396,15 @@ static error_code getStatus(HANDLE FileHandle, file_status &Result) {
}
handle_status_error:
error_code EC = windows_error(::GetLastError());
if (EC == windows_error::file_not_found ||
EC == windows_error::path_not_found)
DWORD LastError = ::GetLastError();
if (LastError == ERROR_FILE_NOT_FOUND ||
LastError == ERROR_PATH_NOT_FOUND)
Result = file_status(file_type::file_not_found);
else if (EC == windows_error::sharing_violation)
else if (LastError == ERROR_SHARING_VIOLATION)
Result = file_status(file_type::type_unknown);
else
Result = file_status(file_type::status_error);
return EC;
return windows_error(LastError);
}
error_code status(const Twine &path, file_status &result) {
@ -733,11 +737,11 @@ error_code detail::directory_iterator_construct(detail::DirIterState &it,
(FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
FirstFind.cFileName[1] == L'.'))
if (!::FindNextFileW(FindHandle, &FirstFind)) {
error_code ec = windows_error(::GetLastError());
DWORD LastError = ::GetLastError();
// Check for end.
if (ec == windows_error::no_more_files)
if (LastError == ERROR_NO_MORE_FILES)
return detail::directory_iterator_destruct(it);
return ec;
return windows_error(LastError);
} else
FilenameLen = ::wcslen(FirstFind.cFileName);
@ -768,11 +772,11 @@ error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
error_code detail::directory_iterator_increment(detail::DirIterState &it) {
WIN32_FIND_DATAW FindData;
if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
error_code ec = windows_error(::GetLastError());
DWORD LastError = ::GetLastError();
// Check for end.
if (ec == windows_error::no_more_files)
if (LastError == ERROR_NO_MORE_FILES)
return detail::directory_iterator_destruct(it);
return ec;
return windows_error(LastError);
}
size_t FilenameLen = ::wcslen(FindData.cFileName);
@ -803,11 +807,12 @@ error_code openFileForRead(const Twine &Name, int &ResultFD) {
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (H == INVALID_HANDLE_VALUE) {
error_code EC = windows_error(::GetLastError());
DWORD LastError = ::GetLastError();
error_code EC = windows_error(LastError);
// Provide a better error message when trying to open directories.
// This only runs if we failed to open the file, so there is probably
// no performances issues.
if (EC != windows_error::access_denied)
if (LastError != ERROR_ACCESS_DENIED)
return EC;
if (is_directory(Name))
return make_error_code(errc::is_a_directory);
@ -817,7 +822,7 @@ error_code openFileForRead(const Twine &Name, int &ResultFD) {
int FD = ::_open_osfhandle(intptr_t(H), 0);
if (FD == -1) {
::CloseHandle(H);
return windows_error::invalid_handle;
return windows_error(ERROR_INVALID_HANDLE);
}
ResultFD = FD;
@ -854,11 +859,12 @@ error_code openFileForWrite(const Twine &Name, int &ResultFD,
CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (H == INVALID_HANDLE_VALUE) {
error_code EC = windows_error(::GetLastError());
DWORD LastError = ::GetLastError();
error_code EC = windows_error(LastError);
// Provide a better error message when trying to open directories.
// This only runs if we failed to open the file, so there is probably
// no performances issues.
if (EC != windows_error::access_denied)
if (LastError != ERROR_ACCESS_DENIED)
return EC;
if (is_directory(Name))
return make_error_code(errc::is_a_directory);
@ -875,7 +881,7 @@ error_code openFileForWrite(const Twine &Name, int &ResultFD,
int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
if (FD == -1) {
::CloseHandle(H);
return windows_error::invalid_handle;
return windows_error(ERROR_INVALID_HANDLE);
}
ResultFD = FD;
@ -907,7 +913,7 @@ llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
utf8.size(), utf16.begin(), 0);
if (len == 0)
return llvm::windows_error(::GetLastError());
return windows_error(::GetLastError());
utf16.reserve(len + 1);
utf16.set_size(len);
@ -916,7 +922,7 @@ llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
utf8.size(), utf16.begin(), utf16.size());
if (len == 0)
return llvm::windows_error(::GetLastError());
return windows_error(::GetLastError());
}
// Make utf16 null terminated.
@ -934,7 +940,7 @@ llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
0, NULL, NULL);
if (len == 0)
return llvm::windows_error(::GetLastError());
return windows_error(::GetLastError());
utf8.reserve(len);
utf8.set_size(len);
@ -944,7 +950,7 @@ llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
utf8.size(), NULL, NULL);
if (len == 0)
return llvm::windows_error(::GetLastError());
return windows_error(::GetLastError());
}
// Make utf8 null terminated.

View File

@ -179,6 +179,10 @@ Optional<std::string> Process::GetEnv(StringRef Name) {
return std::string(Res.data());
}
static error_code windows_error(DWORD E) {
return error_code(E, system_category());
}
error_code
Process::GetArgumentVector(SmallVectorImpl<const char *> &Args,
ArrayRef<const char *>,

View File

@ -14,6 +14,10 @@
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#ifdef LLVM_ON_WIN32
#include <winerror.h>
#endif
using namespace llvm;
using namespace llvm::sys;
@ -393,7 +397,7 @@ TEST_F(FileSystemTest, TempFiles) {
"abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
"abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
windows_error::path_not_found);
error_code(ERROR_PATH_NOT_FOUND, system_category()));
#endif
}

View File

@ -169,6 +169,10 @@ namespace {
typedef ScopedHandle<FileHandle> FileScopedHandle;
}
static error_code windows_error(unsigned E) {
return error_code(E, system_category());
}
static error_code GetFileNameFromHandle(HANDLE FileHandle,
std::string& Name) {
char Filename[MAX_PATH+1];
@ -245,7 +249,7 @@ static std::string FindProgram(const std::string &Program, error_code &ec) {
ec = windows_error(::GetLastError());
else if (length > array_lengthof(PathName)) {
// This may have been the file, return with error.
ec = windows_error::buffer_overflow;
ec = windows_error(ERROR_BUFFER_OVERFLOW);
break;
} else {
// We found the path! Return it.