LLVM  8.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"
45 #include <cassert>
46 #include <string>
47 #include <system_error>
48 #include <windows.h>
49 
50 // Must be included after windows.h
51 #include <wincrypt.h>
52 
53 namespace llvm {
54 
55 /// Determines if the program is running on Windows 8 or newer. This
56 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
57 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
58 /// yet have VersionHelpers.h, so we have our own helper.
60 
61 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
62 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
63 /// GetVersionEx is deprecated, but this API exposes the build number which can
64 /// be useful for working around certain kernel bugs.
66 
67 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
68 
69 template <typename HandleTraits>
70 class ScopedHandle {
71  typedef typename HandleTraits::handle_type handle_type;
72  handle_type Handle;
73 
74  ScopedHandle(const ScopedHandle &other) = delete;
75  void operator=(const ScopedHandle &other) = delete;
76 public:
78  : Handle(HandleTraits::GetInvalid()) {}
79 
80  explicit ScopedHandle(handle_type h)
81  : Handle(h) {}
82 
84  if (HandleTraits::IsValid(Handle))
85  HandleTraits::Close(Handle);
86  }
87 
88  handle_type take() {
89  handle_type t = Handle;
90  Handle = HandleTraits::GetInvalid();
91  return t;
92  }
93 
94  ScopedHandle &operator=(handle_type h) {
95  if (HandleTraits::IsValid(Handle))
96  HandleTraits::Close(Handle);
97  Handle = h;
98  return *this;
99  }
100 
101  // True if Handle is valid.
102  explicit operator bool() const {
103  return HandleTraits::IsValid(Handle) ? true : false;
104  }
105 
106  operator handle_type() const {
107  return Handle;
108  }
109 };
110 
112  typedef HANDLE handle_type;
113 
114  static handle_type GetInvalid() {
115  return INVALID_HANDLE_VALUE;
116  }
117 
118  static void Close(handle_type h) {
119  ::CloseHandle(h);
120  }
121 
122  static bool IsValid(handle_type h) {
123  return h != GetInvalid();
124  }
125 };
126 
129  return NULL;
130  }
131 };
132 
134  typedef HCRYPTPROV handle_type;
135 
136  static handle_type GetInvalid() {
137  return 0;
138  }
139 
140  static void Close(handle_type h) {
141  ::CryptReleaseContext(h, 0);
142  }
143 
144  static bool IsValid(handle_type h) {
145  return h != GetInvalid();
146  }
147 };
148 
150  typedef HKEY handle_type;
151 
152  static handle_type GetInvalid() {
153  return NULL;
154  }
155 
156  static void Close(handle_type h) {
157  ::RegCloseKey(h);
158  }
159 
160  static bool IsValid(handle_type h) {
161  return h != GetInvalid();
162  }
163 };
164 
166  static void Close(handle_type h) {
167  ::FindClose(h);
168  }
169 };
170 
172 
179 
180 template <class T>
181 class SmallVectorImpl;
182 
183 template <class T>
186  str.push_back(0);
187  str.pop_back();
188  return str.data();
189 }
190 
191 namespace sys {
192 
193 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
194  ULARGE_INTEGER TimeInteger;
195  TimeInteger.LowPart = Time.dwLowDateTime;
196  TimeInteger.HighPart = Time.dwHighDateTime;
197 
198  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
199  return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
200 }
201 
202 inline TimePoint<> toTimePoint(FILETIME Time) {
203  ULARGE_INTEGER TimeInteger;
204  TimeInteger.LowPart = Time.dwLowDateTime;
205  TimeInteger.HighPart = Time.dwHighDateTime;
206 
207  // Adjust for different epoch
208  TimeInteger.QuadPart -= 11644473600ll * 10000000;
209 
210  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
211  return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
212 }
213 
214 inline FILETIME toFILETIME(TimePoint<> TP) {
215  ULARGE_INTEGER TimeInteger;
216  TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
217  TimeInteger.QuadPart += 11644473600ll * 10000000;
218 
219  FILETIME Time;
220  Time.dwLowDateTime = TimeInteger.LowPart;
221  Time.dwHighDateTime = TimeInteger.HighPart;
222  return Time;
223 }
224 
225 namespace windows {
226 // Returns command line arguments. Unlike arguments given to main(),
227 // this function guarantees that the returned arguments are encoded in
228 // UTF-8 regardless of the current code page setting.
230  BumpPtrAllocator &Alloc);
231 } // end namespace windows
232 } // end namespace sys
233 } // end namespace llvm.
234 
235 #endif
ScopedHandle< JobHandleTraits > ScopedJobHandle
std::error_code GetCommandLineArguments(SmallVectorImpl< const char *> &Args, BumpPtrAllocator &Alloc)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
FILETIME toFILETIME(TimePoint<> TP)
ScopedHandle< FileHandleTraits > ScopedFileHandle
static void Close(handle_type h)
ScopedHandle< FindHandleTraits > ScopedFindHandle
block Block Frequency true
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
static bool IsValid(handle_type h)
static bool IsValid(handle_type h)
static void Close(handle_type h)
static bool IsValid(handle_type h)
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
static handle_type GetInvalid()
TimePoint toTimePoint(FILETIME Time)
ScopedHandle< CryptContextTraits > ScopedCryptContext
static handle_type GetInvalid()
ScopedHandle< RegTraits > ScopedRegHandle
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
llvm::VersionTuple GetWindowsOSVersion()
Returns the Windows version as Major.Minor.0.BuildNumber.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
ScopedHandle & operator=(handle_type h)
static void Close(handle_type h)
handle_type take()
static void Close(handle_type h)
static handle_type GetInvalid()
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
ScopedHandle(handle_type h)
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:27
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
static handle_type GetInvalid()
Defines the llvm::VersionTuple class, which represents a version in the form major[.minor[.subminor]].
std::chrono::nanoseconds toDuration(FILETIME Time)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34