LLVM  10.0.0svn
WindowsSupport.h
Go to the documentation of this file.
1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines things specific to Windows implementations. In addition to
10 // providing some helpers for working with win32 APIs, this header wraps
11 // <windows.h> with some portability macros. Always include WindowsSupport.h
12 // instead of including <windows.h> directly.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 //===----------------------------------------------------------------------===//
17 //=== WARNING: Implementation here must contain only generic Win32 code that
18 //=== is guaranteed to work on *all* Win32 variants.
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
22 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
23 
24 // mingw-w64 tends to define it as 0x0502 in its headers.
25 #undef _WIN32_WINNT
26 #undef _WIN32_IE
27 
28 // Require at least Windows 7 API.
29 #define _WIN32_WINNT 0x0601
30 #define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
31 #define WIN32_LEAN_AND_MEAN
32 #ifndef NOMINMAX
33 #define NOMINMAX
34 #endif
35 
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/ADT/Twine.h"
40 #include "llvm/Config/config.h" // Get build system configuration settings
41 #include "llvm/Support/Chrono.h"
42 #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 namespace llvm {
53 
54 /// Determines if the program is running on Windows 8 or newer. This
55 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
56 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
57 /// yet have VersionHelpers.h, so we have our own helper.
59 
60 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
61 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
62 /// GetVersionEx is deprecated, but this API exposes the build number which can
63 /// be useful for working around certain kernel bugs.
65 
66 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
67 
68 template <typename HandleTraits>
69 class ScopedHandle {
70  typedef typename HandleTraits::handle_type handle_type;
71  handle_type Handle;
72 
73  ScopedHandle(const ScopedHandle &other) = delete;
74  void operator=(const ScopedHandle &other) = delete;
75 public:
77  : Handle(HandleTraits::GetInvalid()) {}
78 
79  explicit ScopedHandle(handle_type h)
80  : Handle(h) {}
81 
83  if (HandleTraits::IsValid(Handle))
84  HandleTraits::Close(Handle);
85  }
86 
87  handle_type take() {
88  handle_type t = Handle;
89  Handle = HandleTraits::GetInvalid();
90  return t;
91  }
92 
93  ScopedHandle &operator=(handle_type h) {
94  if (HandleTraits::IsValid(Handle))
95  HandleTraits::Close(Handle);
96  Handle = h;
97  return *this;
98  }
99 
100  // True if Handle is valid.
101  explicit operator bool() const {
102  return HandleTraits::IsValid(Handle) ? true : false;
103  }
104 
105  operator handle_type() const {
106  return Handle;
107  }
108 };
109 
111  typedef HANDLE handle_type;
112 
113  static handle_type GetInvalid() {
114  return INVALID_HANDLE_VALUE;
115  }
116 
117  static void Close(handle_type h) {
118  ::CloseHandle(h);
119  }
120 
121  static bool IsValid(handle_type h) {
122  return h != GetInvalid();
123  }
124 };
125 
128  return NULL;
129  }
130 };
131 
133  typedef HCRYPTPROV handle_type;
134 
135  static handle_type GetInvalid() {
136  return 0;
137  }
138 
139  static void Close(handle_type h) {
140  ::CryptReleaseContext(h, 0);
141  }
142 
143  static bool IsValid(handle_type h) {
144  return h != GetInvalid();
145  }
146 };
147 
149  typedef HKEY handle_type;
150 
151  static handle_type GetInvalid() {
152  return NULL;
153  }
154 
155  static void Close(handle_type h) {
156  ::RegCloseKey(h);
157  }
158 
159  static bool IsValid(handle_type h) {
160  return h != GetInvalid();
161  }
162 };
163 
165  static void Close(handle_type h) {
166  ::FindClose(h);
167  }
168 };
169 
171 
178 
179 template <class T>
180 class SmallVectorImpl;
181 
182 template <class T>
185  str.push_back(0);
186  str.pop_back();
187  return str.data();
188 }
189 
190 namespace sys {
191 
192 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
193  ULARGE_INTEGER TimeInteger;
194  TimeInteger.LowPart = Time.dwLowDateTime;
195  TimeInteger.HighPart = Time.dwHighDateTime;
196 
197  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
198  return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
199 }
200 
201 inline TimePoint<> toTimePoint(FILETIME Time) {
202  ULARGE_INTEGER TimeInteger;
203  TimeInteger.LowPart = Time.dwLowDateTime;
204  TimeInteger.HighPart = Time.dwHighDateTime;
205 
206  // Adjust for different epoch
207  TimeInteger.QuadPart -= 11644473600ll * 10000000;
208 
209  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
210  return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
211 }
212 
213 inline FILETIME toFILETIME(TimePoint<> TP) {
214  ULARGE_INTEGER TimeInteger;
215  TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
216  TimeInteger.QuadPart += 11644473600ll * 10000000;
217 
218  FILETIME Time;
219  Time.dwLowDateTime = TimeInteger.LowPart;
220  Time.dwHighDateTime = TimeInteger.HighPart;
221  return Time;
222 }
223 
224 namespace windows {
225 // Returns command line arguments. Unlike arguments given to main(),
226 // this function guarantees that the returned arguments are encoded in
227 // UTF-8 regardless of the current code page setting.
229  BumpPtrAllocator &Alloc);
230 } // end namespace windows
231 } // end namespace sys
232 } // end namespace llvm.
233 
234 #endif
ScopedHandle< JobHandleTraits > ScopedJobHandle
std::error_code GetCommandLineArguments(SmallVectorImpl< const char *> &Args, BumpPtrAllocator &Alloc)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:41
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:140
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:26
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
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:33