Add Windows error code and tidy formatting for system errors.
[oota-llvm.git] / lib / Support / Windows / WindowsSupport.h
1 //===- WindowsSupport.h - Common Windows Include File -----------*- 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 defines things specific to Windows implementations.  In addition to
11 // providing some helpers for working with win32 APIs, this header wraps
12 // <windows.h> with some portability macros.  Always include WindowsSupport.h
13 // instead of including <windows.h> directly.
14 //
15 //===----------------------------------------------------------------------===//
16
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //===          is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
28
29 // Require at least Windows 7 API.
30 #define _WIN32_WINNT 0x0601
31 #define _WIN32_IE    0x0800 // MinGW at it again. FIXME: verify if still needed.
32 #define WIN32_LEAN_AND_MEAN
33
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/ADT/StringRef.h"
37 #include "llvm/ADT/Twine.h"
38 #include "llvm/Config/config.h" // Get build system configuration settings
39 #include "llvm/Support/Compiler.h"
40 #include <system_error>
41 #include <windows.h>
42 #include <wincrypt.h>
43 #include <cassert>
44 #include <string>
45 #include <vector>
46
47 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
48   if (!ErrMsg)
49     return true;
50   char *buffer = NULL;
51   DWORD LastError = GetLastError();
52   DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
53                           FORMAT_MESSAGE_FROM_SYSTEM |
54                           FORMAT_MESSAGE_MAX_WIDTH_MASK,
55                           NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
56   if (R)
57     *ErrMsg = prefix + ": " + buffer;
58   else
59     *ErrMsg = prefix + ": Unknown error";
60   *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
61
62   LocalFree(buffer);
63   return R != 0;
64 }
65
66 template <typename HandleTraits>
67 class ScopedHandle {
68   typedef typename HandleTraits::handle_type handle_type;
69   handle_type Handle;
70
71   ScopedHandle(const ScopedHandle &other); // = delete;
72   void operator=(const ScopedHandle &other); // = delete;
73 public:
74   ScopedHandle()
75     : Handle(HandleTraits::GetInvalid()) {}
76
77   explicit ScopedHandle(handle_type h)
78     : Handle(h) {}
79
80   ~ScopedHandle() {
81     if (HandleTraits::IsValid(Handle))
82       HandleTraits::Close(Handle);
83   }
84
85   handle_type take() {
86     handle_type t = Handle;
87     Handle = HandleTraits::GetInvalid();
88     return t;
89   }
90
91   ScopedHandle &operator=(handle_type h) {
92     if (HandleTraits::IsValid(Handle))
93       HandleTraits::Close(Handle);
94     Handle = h;
95     return *this;
96   }
97
98   // True if Handle is valid.
99   explicit operator bool() const {
100     return HandleTraits::IsValid(Handle) ? true : false;
101   }
102
103   operator handle_type() const {
104     return Handle;
105   }
106 };
107
108 struct CommonHandleTraits {
109   typedef HANDLE handle_type;
110
111   static handle_type GetInvalid() {
112     return INVALID_HANDLE_VALUE;
113   }
114
115   static void Close(handle_type h) {
116     ::CloseHandle(h);
117   }
118
119   static bool IsValid(handle_type h) {
120     return h != GetInvalid();
121   }
122 };
123
124 struct JobHandleTraits : CommonHandleTraits {
125   static handle_type GetInvalid() {
126     return NULL;
127   }
128 };
129
130 struct CryptContextTraits : CommonHandleTraits {
131   typedef HCRYPTPROV handle_type;
132
133   static handle_type GetInvalid() {
134     return 0;
135   }
136
137   static void Close(handle_type h) {
138     ::CryptReleaseContext(h, 0);
139   }
140
141   static bool IsValid(handle_type h) {
142     return h != GetInvalid();
143   }
144 };
145
146 struct FindHandleTraits : CommonHandleTraits {
147   static void Close(handle_type h) {
148     ::FindClose(h);
149   }
150 };
151
152 struct FileHandleTraits : CommonHandleTraits {};
153
154 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
155 typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
156 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
157 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
158 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
159
160 namespace llvm {
161 template <class T>
162 class SmallVectorImpl;
163
164 template <class T>
165 typename SmallVectorImpl<T>::const_pointer
166 c_str(SmallVectorImpl<T> &str) {
167   str.push_back(0);
168   str.pop_back();
169   return str.data();
170 }
171
172 namespace sys {
173 namespace path {
174 std::error_code widenPath(const Twine &Path8,
175                           SmallVectorImpl<wchar_t> &Path16);
176 } // end namespace path
177
178 namespace windows {
179 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
180 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
181                             SmallVectorImpl<char> &utf8);
182 /// Convert from UTF16 to the current code page used in the system
183 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
184                              SmallVectorImpl<char> &utf8);
185 } // end namespace windows
186 } // end namespace sys
187 } // end namespace llvm.
188
189 #endif