Home | History | Annotate | Download | only in Windows
      1 //===- llvm/Support/Win32/system_error.inc - Windows error_code --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file provides the Windows specific implementation of the error_code
     11 // and error_condition classes.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 //===----------------------------------------------------------------------===//
     16 //=== WARNING: Implementation here must contain only generic Windows code that
     17 //===          is guaranteed to work on *all* Windows variants.
     18 //===----------------------------------------------------------------------===//
     19 
     20 #include <windows.h>
     21 #include <winerror.h>
     22 
     23 using namespace llvm;
     24 
     25 std::string
     26 _system_error_category::message(int ev) const {
     27   LPVOID lpMsgBuf = 0;
     28   DWORD retval = ::FormatMessageA(
     29     FORMAT_MESSAGE_ALLOCATE_BUFFER |
     30     FORMAT_MESSAGE_FROM_SYSTEM |
     31     FORMAT_MESSAGE_IGNORE_INSERTS,
     32     NULL,
     33     ev,
     34     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
     35     (LPSTR) &lpMsgBuf,
     36     0,
     37     NULL);
     38   if (retval == 0) {
     39     ::LocalFree(lpMsgBuf);
     40     return std::string("Unknown error");
     41   }
     42 
     43   std::string str( static_cast<LPCSTR>(lpMsgBuf) );
     44   ::LocalFree(lpMsgBuf);
     45 
     46   while (str.size()
     47      && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r'))
     48     str.erase( str.size()-1 );
     49   if (str.size() && str[str.size()-1] == '.')
     50     str.erase( str.size()-1 );
     51   return str;
     52 }
     53 
     54 // I'd rather not double the line count of the following.
     55 #define MAP_ERR_TO_COND(x, y) case x: return make_error_condition(errc::y)
     56 
     57 error_condition
     58 _system_error_category::default_error_condition(int ev) const {
     59   switch (ev) {
     60   MAP_ERR_TO_COND(0, success);
     61   // Windows system -> posix_errno decode table  ---------------------------//
     62   // see WinError.h comments for descriptions of errors
     63   MAP_ERR_TO_COND(ERROR_ACCESS_DENIED,       permission_denied);
     64   MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS,      file_exists);
     65   MAP_ERR_TO_COND(ERROR_BAD_UNIT,            no_such_device);
     66   MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW,     filename_too_long);
     67   MAP_ERR_TO_COND(ERROR_BUSY,                device_or_resource_busy);
     68   MAP_ERR_TO_COND(ERROR_BUSY_DRIVE,          device_or_resource_busy);
     69   MAP_ERR_TO_COND(ERROR_CANNOT_MAKE,         permission_denied);
     70   MAP_ERR_TO_COND(ERROR_CANTOPEN,            io_error);
     71   MAP_ERR_TO_COND(ERROR_CANTREAD,            io_error);
     72   MAP_ERR_TO_COND(ERROR_CANTWRITE,           io_error);
     73   MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY,   permission_denied);
     74   MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST,       no_such_device);
     75   MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE,       device_or_resource_busy);
     76   MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY,       directory_not_empty);
     77   MAP_ERR_TO_COND(ERROR_DIRECTORY,           invalid_argument);
     78   MAP_ERR_TO_COND(ERROR_DISK_FULL,           no_space_on_device);
     79   MAP_ERR_TO_COND(ERROR_FILE_EXISTS,         file_exists);
     80   MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND,      no_such_file_or_directory);
     81   MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL,    no_space_on_device);
     82   MAP_ERR_TO_COND(ERROR_HANDLE_EOF,          value_too_large);
     83   MAP_ERR_TO_COND(ERROR_INVALID_ACCESS,      permission_denied);
     84   MAP_ERR_TO_COND(ERROR_INVALID_DRIVE,       no_such_device);
     85   MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION,    function_not_supported);
     86   MAP_ERR_TO_COND(ERROR_INVALID_HANDLE,      invalid_argument);
     87   MAP_ERR_TO_COND(ERROR_INVALID_NAME,        invalid_argument);
     88   MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION,      no_lock_available);
     89   MAP_ERR_TO_COND(ERROR_LOCKED,              no_lock_available);
     90   MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK,       invalid_argument);
     91   MAP_ERR_TO_COND(ERROR_NOACCESS,            permission_denied);
     92   MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY,   not_enough_memory);
     93   MAP_ERR_TO_COND(ERROR_NOT_READY,           resource_unavailable_try_again);
     94   MAP_ERR_TO_COND(ERROR_NOT_SAME_DEVICE,     cross_device_link);
     95   MAP_ERR_TO_COND(ERROR_OPEN_FAILED,         io_error);
     96   MAP_ERR_TO_COND(ERROR_OPEN_FILES,          device_or_resource_busy);
     97   MAP_ERR_TO_COND(ERROR_OPERATION_ABORTED,   operation_canceled);
     98   MAP_ERR_TO_COND(ERROR_OUTOFMEMORY,         not_enough_memory);
     99   MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND,      no_such_file_or_directory);
    100   MAP_ERR_TO_COND(ERROR_BAD_NETPATH,         no_such_file_or_directory);
    101   MAP_ERR_TO_COND(ERROR_READ_FAULT,          io_error);
    102   MAP_ERR_TO_COND(ERROR_RETRY,               resource_unavailable_try_again);
    103   MAP_ERR_TO_COND(ERROR_SEEK,                io_error);
    104   MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION,   permission_denied);
    105   MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
    106   MAP_ERR_TO_COND(ERROR_WRITE_FAULT,         io_error);
    107   MAP_ERR_TO_COND(ERROR_WRITE_PROTECT,       permission_denied);
    108   MAP_ERR_TO_COND(ERROR_SEM_TIMEOUT,         timed_out);
    109   MAP_ERR_TO_COND(WSAEACCES,                 permission_denied);
    110   MAP_ERR_TO_COND(WSAEADDRINUSE,             address_in_use);
    111   MAP_ERR_TO_COND(WSAEADDRNOTAVAIL,          address_not_available);
    112   MAP_ERR_TO_COND(WSAEAFNOSUPPORT,           address_family_not_supported);
    113   MAP_ERR_TO_COND(WSAEALREADY,               connection_already_in_progress);
    114   MAP_ERR_TO_COND(WSAEBADF,                  bad_file_descriptor);
    115   MAP_ERR_TO_COND(WSAECONNABORTED,           connection_aborted);
    116   MAP_ERR_TO_COND(WSAECONNREFUSED,           connection_refused);
    117   MAP_ERR_TO_COND(WSAECONNRESET,             connection_reset);
    118   MAP_ERR_TO_COND(WSAEDESTADDRREQ,           destination_address_required);
    119   MAP_ERR_TO_COND(WSAEFAULT,                 bad_address);
    120   MAP_ERR_TO_COND(WSAEHOSTUNREACH,           host_unreachable);
    121   MAP_ERR_TO_COND(WSAEINPROGRESS,            operation_in_progress);
    122   MAP_ERR_TO_COND(WSAEINTR,                  interrupted);
    123   MAP_ERR_TO_COND(WSAEINVAL,                 invalid_argument);
    124   MAP_ERR_TO_COND(WSAEISCONN,                already_connected);
    125   MAP_ERR_TO_COND(WSAEMFILE,                 too_many_files_open);
    126   MAP_ERR_TO_COND(WSAEMSGSIZE,               message_size);
    127   MAP_ERR_TO_COND(WSAENAMETOOLONG,           filename_too_long);
    128   MAP_ERR_TO_COND(WSAENETDOWN,               network_down);
    129   MAP_ERR_TO_COND(WSAENETRESET,              network_reset);
    130   MAP_ERR_TO_COND(WSAENETUNREACH,            network_unreachable);
    131   MAP_ERR_TO_COND(WSAENOBUFS,                no_buffer_space);
    132   MAP_ERR_TO_COND(WSAENOPROTOOPT,            no_protocol_option);
    133   MAP_ERR_TO_COND(WSAENOTCONN,               not_connected);
    134   MAP_ERR_TO_COND(WSAENOTSOCK,               not_a_socket);
    135   MAP_ERR_TO_COND(WSAEOPNOTSUPP,             operation_not_supported);
    136   MAP_ERR_TO_COND(WSAEPROTONOSUPPORT,        protocol_not_supported);
    137   MAP_ERR_TO_COND(WSAEPROTOTYPE,             wrong_protocol_type);
    138   MAP_ERR_TO_COND(WSAETIMEDOUT,              timed_out);
    139   MAP_ERR_TO_COND(WSAEWOULDBLOCK,            operation_would_block);
    140   default: return error_condition(ev, system_category());
    141   }
    142 }
    143