LLVM  6.0.0svn
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 
49 // Must be included after windows.h
50 #include <wincrypt.h>
51 
52 /// Determines if the program is running on Windows 8 or newer. This
53 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
54 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
55 /// yet have VersionHelpers.h, so we have our own helper.
56 inline bool RunningWindows8OrGreater() {
57  // Windows 8 is version 6.2, service pack 0.
58  OSVERSIONINFOEXW osvi = {};
59  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
60  osvi.dwMajorVersion = 6;
61  osvi.dwMinorVersion = 2;
62  osvi.wServicePackMajor = 0;
63 
64  DWORDLONG Mask = 0;
65  Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
66  Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
67  Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
68 
69  return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
70  VER_SERVICEPACKMAJOR,
71  Mask) != FALSE;
72 }
73 
74 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
75  if (!ErrMsg)
76  return true;
77  char *buffer = NULL;
78  DWORD LastError = GetLastError();
79  DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
80  FORMAT_MESSAGE_FROM_SYSTEM |
81  FORMAT_MESSAGE_MAX_WIDTH_MASK,
82  NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
83  if (R)
84  *ErrMsg = prefix + ": " + buffer;
85  else
86  *ErrMsg = prefix + ": Unknown error";
87  *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
88 
89  LocalFree(buffer);
90  return R != 0;
91 }
92 
93 template <typename HandleTraits>
94 class ScopedHandle {
95  typedef typename HandleTraits::handle_type handle_type;
96  handle_type Handle;
97 
98  ScopedHandle(const ScopedHandle &other); // = delete;
99  void operator=(const ScopedHandle &other); // = delete;
100 public:
102  : Handle(HandleTraits::GetInvalid()) {}
103 
104  explicit ScopedHandle(handle_type h)
105  : Handle(h) {}
106 
108  if (HandleTraits::IsValid(Handle))
109  HandleTraits::Close(Handle);
110  }
111 
112  handle_type take() {
113  handle_type t = Handle;
114  Handle = HandleTraits::GetInvalid();
115  return t;
116  }
117 
118  ScopedHandle &operator=(handle_type h) {
119  if (HandleTraits::IsValid(Handle))
120  HandleTraits::Close(Handle);
121  Handle = h;
122  return *this;
123  }
124 
125  // True if Handle is valid.
126  explicit operator bool() const {
127  return HandleTraits::IsValid(Handle) ? true : false;
128  }
129 
130  operator handle_type() const {
131  return Handle;
132  }
133 };
134 
136  typedef HANDLE handle_type;
137 
138  static handle_type GetInvalid() {
139  return INVALID_HANDLE_VALUE;
140  }
141 
142  static void Close(handle_type h) {
143  ::CloseHandle(h);
144  }
145 
146  static bool IsValid(handle_type h) {
147  return h != GetInvalid();
148  }
149 };
150 
153  return NULL;
154  }
155 };
156 
158  typedef HCRYPTPROV handle_type;
159 
160  static handle_type GetInvalid() {
161  return 0;
162  }
163 
164  static void Close(handle_type h) {
165  ::CryptReleaseContext(h, 0);
166  }
167 
168  static bool IsValid(handle_type h) {
169  return h != GetInvalid();
170  }
171 };
172 
174  typedef HKEY handle_type;
175 
176  static handle_type GetInvalid() {
177  return NULL;
178  }
179 
180  static void Close(handle_type h) {
181  ::RegCloseKey(h);
182  }
183 
184  static bool IsValid(handle_type h) {
185  return h != GetInvalid();
186  }
187 };
188 
190  static void Close(handle_type h) {
191  ::FindClose(h);
192  }
193 };
194 
196 
203 
204 namespace llvm {
205 template <class T>
206 class SmallVectorImpl;
207 
208 template <class T>
209 typename SmallVectorImpl<T>::const_pointer
211  str.push_back(0);
212  str.pop_back();
213  return str.data();
214 }
215 
216 namespace sys {
217 
218 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
219  ULARGE_INTEGER TimeInteger;
220  TimeInteger.LowPart = Time.dwLowDateTime;
221  TimeInteger.HighPart = Time.dwHighDateTime;
222 
223  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
224  return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
225 }
226 
227 inline TimePoint<> toTimePoint(FILETIME Time) {
228  ULARGE_INTEGER TimeInteger;
229  TimeInteger.LowPart = Time.dwLowDateTime;
230  TimeInteger.HighPart = Time.dwHighDateTime;
231 
232  // Adjust for different epoch
233  TimeInteger.QuadPart -= 11644473600ll * 10000000;
234 
235  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
236  return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
237 }
238 
239 inline FILETIME toFILETIME(TimePoint<> TP) {
240  ULARGE_INTEGER TimeInteger;
241  TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
242  TimeInteger.QuadPart += 11644473600ll * 10000000;
243 
244  FILETIME Time;
245  Time.dwLowDateTime = TimeInteger.LowPart;
246  Time.dwHighDateTime = TimeInteger.HighPart;
247  return Time;
248 }
249 
250 namespace path {
251 std::error_code widenPath(const Twine &Path8,
252  SmallVectorImpl<wchar_t> &Path16);
253 } // end namespace path
254 
255 namespace windows {
256 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
257 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
258  SmallVectorImpl<char> &utf8);
259 /// Convert from UTF16 to the current code page used in the system
260 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
261  SmallVectorImpl<char> &utf8);
262 } // end namespace windows
263 } // end namespace sys
264 } // end namespace llvm.
265 
266 #endif
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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)
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
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
TimePoint toTimePoint(FILETIME Time)
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)
Basic Alias true
HKEY handle_type
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
ScopedHandle & operator=(handle_type h)
ScopedHandle< FileHandleTraits > ScopedFileHandle
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&#39;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:49
static bool IsValid(handle_type h)
std::chrono::nanoseconds toDuration(FILETIME Time)
std::error_code widenPath(const Twine &Path8, SmallVectorImpl< wchar_t > &Path16)
std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:76
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34