llvm.org GIT mirror llvm / e5e77d1
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 Rafael Espindola 6 years ago
5 changed file(s) with 50 addition(s) and 101 deletion(s). Raw diff Collapse all Expand all
790790 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
791791 return !(_x == _y);
792792 }
793
794 // Windows errors.
795
796 // To construct an error_code after an API error:
797 //
798 // error_code( ::GetLastError(), system_category() )
799 enum class windows_error {
800 // These names and values are based on Windows WinError.h
801 // This is not a complete list. Add to this list if you need to explicitly
802 // check for it.
803 invalid_function = 1, // ERROR_INVALID_FUNCTION,
804 file_not_found = 2, // ERROR_FILE_NOT_FOUND,
805 path_not_found = 3, // ERROR_PATH_NOT_FOUND,
806 too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
807 access_denied = 5, // ERROR_ACCESS_DENIED,
808 invalid_handle = 6, // ERROR_INVALID_HANDLE,
809 arena_trashed = 7, // ERROR_ARENA_TRASHED,
810 not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
811 invalid_block = 9, // ERROR_INVALID_BLOCK,
812 bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
813 bad_format = 11, // ERROR_BAD_FORMAT,
814 invalid_access = 12, // ERROR_INVALID_ACCESS,
815 outofmemory = 14, // ERROR_OUTOFMEMORY,
816 invalid_drive = 15, // ERROR_INVALID_DRIVE,
817 current_directory = 16, // ERROR_CURRENT_DIRECTORY,
818 not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
819 no_more_files = 18, // ERROR_NO_MORE_FILES,
820 write_protect = 19, // ERROR_WRITE_PROTECT,
821 bad_unit = 20, // ERROR_BAD_UNIT,
822 not_ready = 21, // ERROR_NOT_READY,
823 bad_command = 22, // ERROR_BAD_COMMAND,
824 crc = 23, // ERROR_CRC,
825 bad_length = 24, // ERROR_BAD_LENGTH,
826 seek = 25, // ERROR_SEEK,
827 not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
828 sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
829 out_of_paper = 28, // ERROR_OUT_OF_PAPER,
830 write_fault = 29, // ERROR_WRITE_FAULT,
831 read_fault = 30, // ERROR_READ_FAULT,
832 gen_failure = 31, // ERROR_GEN_FAILURE,
833 sharing_violation = 32, // ERROR_SHARING_VIOLATION,
834 lock_violation = 33, // ERROR_LOCK_VIOLATION,
835 wrong_disk = 34, // ERROR_WRONG_DISK,
836 sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
837 handle_eof = 38, // ERROR_HANDLE_EOF,
838 handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
839 rem_not_list = 51, // ERROR_REM_NOT_LIST,
840 dup_name = 52, // ERROR_DUP_NAME,
841 bad_net_path = 53, // ERROR_BAD_NETPATH,
842 network_busy = 54, // ERROR_NETWORK_BUSY,
843 file_exists = 80, // ERROR_FILE_EXISTS,
844 cannot_make = 82, // ERROR_CANNOT_MAKE,
845 broken_pipe = 109, // ERROR_BROKEN_PIPE,
846 open_failed = 110, // ERROR_OPEN_FAILED,
847 buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
848 disk_full = 112, // ERROR_DISK_FULL,
849 insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
850 lock_failed = 167, // ERROR_LOCK_FAILED,
851 busy = 170, // ERROR_BUSY,
852 cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
853 already_exists = 183 // ERROR_ALREADY_EXISTS
854 };
855
856 template <> struct is_error_code_enum : std::true_type { };
857
858 inline error_code make_error_code(windows_error e) {
859 return error_code(static_cast(e), system_category());
860 }
861
862793 } // end namespace llvm
863794
864795 #endif
4343 using llvm::sys::windows::UTF8ToUTF16;
4444 using llvm::sys::windows::UTF16ToUTF8;
4545
46 static error_code windows_error(DWORD E) {
47 return error_code(E, system_category());
48 }
49
4650 static error_code TempDir(SmallVectorImpl &Result) {
4751 SmallVector Res;
4852 retry_temp_dir:
149153 return ec;
150154
151155 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
152 error_code ec = windows_error(::GetLastError());
153 if (ec != windows_error::already_exists || !IgnoreExisting)
154 return ec;
156 DWORD LastError = ::GetLastError();
157 if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting)
158 return windows_error(LastError);
155159 }
156160
157161 return error_code();
231235 if (::MoveFileExW(wide_from.begin(), wide_to.begin(),
232236 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
233237 return error_code();
234 ec = windows_error(::GetLastError());
235 if (ec != windows_error::access_denied)
238 DWORD LastError = ::GetLastError();
239 if (LastError != ERROR_ACCESS_DENIED)
236240 break;
237241 // Retry MoveFile() at ACCESS_DENIED.
238242 // System scanners (eg. indexer) might open the source file when
275279
276280 if (attributes == INVALID_FILE_ATTRIBUTES) {
277281 // See if the file didn't actually exist.
278 error_code ec = make_error_code(windows_error(::GetLastError()));
279 if (ec != windows_error::file_not_found &&
280 ec != windows_error::path_not_found)
281 return ec;
282 DWORD LastError = ::GetLastError();
283 if (LastError != ERROR_FILE_NOT_FOUND &&
284 LastError != ERROR_PATH_NOT_FOUND)
285 return windows_error(LastError);
282286 result = false;
283287 } else
284288 result = true;
391395 }
392396
393397 handle_status_error:
394 error_code EC = windows_error(::GetLastError());
395 if (EC == windows_error::file_not_found ||
396 EC == windows_error::path_not_found)
398 DWORD LastError = ::GetLastError();
399 if (LastError == ERROR_FILE_NOT_FOUND ||
400 LastError == ERROR_PATH_NOT_FOUND)
397401 Result = file_status(file_type::file_not_found);
398 else if (EC == windows_error::sharing_violation)
402 else if (LastError == ERROR_SHARING_VIOLATION)
399403 Result = file_status(file_type::type_unknown);
400404 else
401405 Result = file_status(file_type::status_error);
402 return EC;
406 return windows_error(LastError);
403407 }
404408
405409 error_code status(const Twine &path, file_status &result) {
732736 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' &&
733737 FirstFind.cFileName[1] == L'.'))
734738 if (!::FindNextFileW(FindHandle, &FirstFind)) {
735 error_code ec = windows_error(::GetLastError());
739 DWORD LastError = ::GetLastError();
736740 // Check for end.
737 if (ec == windows_error::no_more_files)
741 if (LastError == ERROR_NO_MORE_FILES)
738742 return detail::directory_iterator_destruct(it);
739 return ec;
743 return windows_error(LastError);
740744 } else
741745 FilenameLen = ::wcslen(FirstFind.cFileName);
742746
767771 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
768772 WIN32_FIND_DATAW FindData;
769773 if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
770 error_code ec = windows_error(::GetLastError());
774 DWORD LastError = ::GetLastError();
771775 // Check for end.
772 if (ec == windows_error::no_more_files)
776 if (LastError == ERROR_NO_MORE_FILES)
773777 return detail::directory_iterator_destruct(it);
774 return ec;
778 return windows_error(LastError);
775779 }
776780
777781 size_t FilenameLen = ::wcslen(FindData.cFileName);
802806 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
803807 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
804808 if (H == INVALID_HANDLE_VALUE) {
805 error_code EC = windows_error(::GetLastError());
809 DWORD LastError = ::GetLastError();
810 error_code EC = windows_error(LastError);
806811 // Provide a better error message when trying to open directories.
807812 // This only runs if we failed to open the file, so there is probably
808813 // no performances issues.
809 if (EC != windows_error::access_denied)
814 if (LastError != ERROR_ACCESS_DENIED)
810815 return EC;
811816 if (is_directory(Name))
812817 return make_error_code(errc::is_a_directory);
816821 int FD = ::_open_osfhandle(intptr_t(H), 0);
817822 if (FD == -1) {
818823 ::CloseHandle(H);
819 return windows_error::invalid_handle;
824 return windows_error(ERROR_INVALID_HANDLE);
820825 }
821826
822827 ResultFD = FD;
853858 CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
854859
855860 if (H == INVALID_HANDLE_VALUE) {
856 error_code EC = windows_error(::GetLastError());
861 DWORD LastError = ::GetLastError();
862 error_code EC = windows_error(LastError);
857863 // Provide a better error message when trying to open directories.
858864 // This only runs if we failed to open the file, so there is probably
859865 // no performances issues.
860 if (EC != windows_error::access_denied)
866 if (LastError != ERROR_ACCESS_DENIED)
861867 return EC;
862868 if (is_directory(Name))
863869 return make_error_code(errc::is_a_directory);
874880 int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
875881 if (FD == -1) {
876882 ::CloseHandle(H);
877 return windows_error::invalid_handle;
883 return windows_error(ERROR_INVALID_HANDLE);
878884 }
879885
880886 ResultFD = FD;
906912 utf8.size(), utf16.begin(), 0);
907913
908914 if (len == 0)
909 return llvm::windows_error(::GetLastError());
915 return windows_error(::GetLastError());
910916
911917 utf16.reserve(len + 1);
912918 utf16.set_size(len);
915921 utf8.size(), utf16.begin(), utf16.size());
916922
917923 if (len == 0)
918 return llvm::windows_error(::GetLastError());
924 return windows_error(::GetLastError());
919925 }
920926
921927 // Make utf16 null terminated.
933939 0, NULL, NULL);
934940
935941 if (len == 0)
936 return llvm::windows_error(::GetLastError());
942 return windows_error(::GetLastError());
937943
938944 utf8.reserve(len);
939945 utf8.set_size(len);
943949 utf8.size(), NULL, NULL);
944950
945951 if (len == 0)
946 return llvm::windows_error(::GetLastError());
952 return windows_error(::GetLastError());
947953 }
948954
949955 // Make utf8 null terminated.
178178 return std::string(Res.data());
179179 }
180180
181 static error_code windows_error(DWORD E) {
182 return error_code(E, system_category());
183 }
184
181185 error_code
182186 Process::GetArgumentVector(SmallVectorImpl &Args,
183187 ArrayRef,
1212 #include "llvm/Support/MemoryBuffer.h"
1313 #include "llvm/Support/raw_ostream.h"
1414 #include "gtest/gtest.h"
15
16 #ifdef LLVM_ON_WIN32
17 #include
18 #endif
1519
1620 using namespace llvm;
1721 using namespace llvm::sys;
392396 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
393397 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
394398 EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
395 windows_error::path_not_found);
399 error_code(ERROR_PATH_NOT_FOUND, system_category()));
396400 #endif
397401 }
398402
168168 typedef ScopedHandle FileScopedHandle;
169169 }
170170
171 static error_code windows_error(unsigned E) {
172 return error_code(E, system_category());
173 }
174
171175 static error_code GetFileNameFromHandle(HANDLE FileHandle,
172176 std::string& Name) {
173177 char Filename[MAX_PATH+1];
244248 ec = windows_error(::GetLastError());
245249 else if (length > array_lengthof(PathName)) {
246250 // This may have been the file, return with error.
247 ec = windows_error::buffer_overflow;
251 ec = windows_error(ERROR_BUFFER_OVERFLOW);
248252 break;
249253 } else {
250254 // We found the path! Return it.