LLVM  4.0.0
WindowsSupport.h
Go to the documentation of this file.
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 #ifndef NOMINMAX
34 #define NOMINMAX
35 #endif
36 
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Config/config.h" // Get build system configuration settings
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
44 #include <cassert>
45 #include <string>
46 #include <system_error>
47 #include <windows.h>
48 #include <wincrypt.h> // Must be included after windows.h
49 
50 /// Determines if the program is running on Windows 8 or newer. This
51 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
52 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
53 /// yet have VersionHelpers.h, so we have our own helper.
54 inline bool RunningWindows8OrGreater() {
55  // Windows 8 is version 6.2, service pack 0.
56  OSVERSIONINFOEXW osvi = {};
57  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
58  osvi.dwMajorVersion = 6;
59  osvi.dwMinorVersion = 2;
60  osvi.wServicePackMajor = 0;
61 
62  DWORDLONG Mask = 0;
63  Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
64  Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
65  Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
66 
67  return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
68  VER_SERVICEPACKMAJOR,
69  Mask) != FALSE;
70 }
71 
72 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
73  if (!ErrMsg)
74  return true;
75  char *buffer = NULL;
76  DWORD LastError = GetLastError();
77  DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
78  FORMAT_MESSAGE_FROM_SYSTEM |
79  FORMAT_MESSAGE_MAX_WIDTH_MASK,
80  NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
81  if (R)
82  *ErrMsg = prefix + ": " + buffer;
83  else
84  *ErrMsg = prefix + ": Unknown error";
85  *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
86 
87  LocalFree(buffer);
88  return R != 0;
89 }
90 
91 template <typename HandleTraits>
92 class ScopedHandle {
93  typedef typename HandleTraits::handle_type handle_type;
94  handle_type Handle;
95 
96  ScopedHandle(const ScopedHandle &other); // = delete;
97  void operator=(const ScopedHandle &other); // = delete;
98 public:
100  : Handle(HandleTraits::GetInvalid()) {}
101 
102  explicit ScopedHandle(handle_type h)
103  : Handle(h) {}
104 
106  if (HandleTraits::IsValid(Handle))
107  HandleTraits::Close(Handle);
108  }
109 
110  handle_type take() {
111  handle_type t = Handle;
112  Handle = HandleTraits::GetInvalid();
113  return t;
114  }
115 
116  ScopedHandle &operator=(handle_type h) {
117  if (HandleTraits::IsValid(Handle))
118  HandleTraits::Close(Handle);
119  Handle = h;
120  return *this;
121  }
122 
123  // True if Handle is valid.
124  explicit operator bool() const {
125  return HandleTraits::IsValid(Handle) ? true : false;
126  }
127 
128  operator handle_type() const {
129  return Handle;
130  }
131 };
132 
134  typedef HANDLE handle_type;
135 
137  return INVALID_HANDLE_VALUE;
138  }
139 
140  static void Close(handle_type h) {
141  ::CloseHandle(h);
142  }
143 
144  static bool IsValid(handle_type h) {
145  return h != GetInvalid();
146  }
147 };
148 
151  return NULL;
152  }
153 };
154 
156  typedef HCRYPTPROV handle_type;
157 
159  return 0;
160  }
161 
162  static void Close(handle_type h) {
163  ::CryptReleaseContext(h, 0);
164  }
165 
166  static bool IsValid(handle_type h) {
167  return h != GetInvalid();
168  }
169 };
170 
172  typedef HKEY handle_type;
173 
175  return NULL;
176  }
177 
178  static void Close(handle_type h) {
179  ::RegCloseKey(h);
180  }
181 
182  static bool IsValid(handle_type h) {
183  return h != GetInvalid();
184  }
185 };
186 
188  static void Close(handle_type h) {
189  ::FindClose(h);
190  }
191 };
192 
194 
201 
202 namespace llvm {
203 template <class T>
204 class SmallVectorImpl;
205 
206 template <class T>
207 typename SmallVectorImpl<T>::const_pointer
209  str.push_back(0);
210  str.pop_back();
211  return str.data();
212 }
213 
214 namespace sys {
215 
216 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
217  ULARGE_INTEGER TimeInteger;
218  TimeInteger.LowPart = Time.dwLowDateTime;
219  TimeInteger.HighPart = Time.dwHighDateTime;
220 
221  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
222  return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
223 }
224 
225 inline TimePoint<> toTimePoint(FILETIME Time) {
226  ULARGE_INTEGER TimeInteger;
227  TimeInteger.LowPart = Time.dwLowDateTime;
228  TimeInteger.HighPart = Time.dwHighDateTime;
229 
230  // Adjust for different epoch
231  TimeInteger.QuadPart -= 11644473600ll * 10000000;
232 
233  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
234  return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
235 }
236 
237 inline FILETIME toFILETIME(TimePoint<> TP) {
238  ULARGE_INTEGER TimeInteger;
239  TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
240  TimeInteger.QuadPart += 11644473600ll * 10000000;
241 
242  FILETIME Time;
243  Time.dwLowDateTime = TimeInteger.LowPart;
244  Time.dwHighDateTime = TimeInteger.HighPart;
245  return Time;
246 }
247 
248 namespace path {
249 std::error_code widenPath(const Twine &Path8,
250  SmallVectorImpl<wchar_t> &Path16);
251 } // end namespace path
252 
253 namespace windows {
254 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
255 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
256  SmallVectorImpl<char> &utf8);
257 /// Convert from UTF16 to the current code page used in the system
258 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
259  SmallVectorImpl<char> &utf8);
260 } // end namespace windows
261 } // end namespace sys
262 } // end namespace llvm.
263 
264 #endif
FILETIME toFILETIME(TimePoint<> TP)
static handle_type GetInvalid()
static void Close(handle_type h)
static void Close(handle_type h)
static bool IsValid(handle_type h)
LLVM_ATTRIBUTE_ALWAYS_INLINE TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
static bool IsValid(handle_type h)
ScopedHandle< JobHandleTraits > ScopedJobHandle
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static F t[256]
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
static void Close(handle_type h)
static handle_type GetInvalid()
ScopedHandle(handle_type h)
HCRYPTPROV handle_type
std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl< wchar_t > &utf16)
static handle_type GetInvalid()
ScopedHandle< FindHandleTraits > ScopedFindHandle
std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
Convert from UTF16 to the current code page used in the system.
handle_type take()
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
HKEY handle_type
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:142
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
Definition: Unix.h:78
ScopedHandle & operator=(handle_type h)
ScopedHandle< FileHandleTraits > ScopedFileHandle
static std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:48
static handle_type GetInvalid()
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
static void Close(handle_type h)
ScopedHandle< RegTraits > ScopedRegHandle
ScopedHandle< CryptContextTraits > ScopedCryptContext
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static bool IsValid(handle_type h)
std::error_code widenPath(const Twine &Path8, SmallVectorImpl< wchar_t > &Path16)
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33