LLVM  14.0.0git
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/llvm-config.h" // Get build system configuration settings
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
46 #include <cassert>
47 #include <string>
48 #include <system_error>
49 #include <windows.h>
50 
51 // Must be included after windows.h
52 #include <wincrypt.h>
53 
54 namespace llvm {
55 
56 /// Determines if the program is running on Windows 8 or newer. This
57 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
58 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
59 /// yet have VersionHelpers.h, so we have our own helper.
61 
62 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
63 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
64 /// GetVersionEx is deprecated, but this API exposes the build number which can
65 /// be useful for working around certain kernel bugs.
67 
68 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
69 
70 // Include GetLastError() in a fatal error message.
71 [[noreturn]] inline void ReportLastErrorFatal(const char *Msg) {
72  std::string ErrMsg;
73  MakeErrMsg(&ErrMsg, Msg);
75 }
76 
77 template <typename HandleTraits>
78 class ScopedHandle {
79  typedef typename HandleTraits::handle_type handle_type;
80  handle_type Handle;
81 
82  ScopedHandle(const ScopedHandle &other) = delete;
83  void operator=(const ScopedHandle &other) = delete;
84 public:
86  : Handle(HandleTraits::GetInvalid()) {}
87 
88  explicit ScopedHandle(handle_type h)
89  : Handle(h) {}
90 
92  if (HandleTraits::IsValid(Handle))
93  HandleTraits::Close(Handle);
94  }
95 
96  handle_type take() {
97  handle_type t = Handle;
98  Handle = HandleTraits::GetInvalid();
99  return t;
100  }
101 
102  ScopedHandle &operator=(handle_type h) {
103  if (HandleTraits::IsValid(Handle))
104  HandleTraits::Close(Handle);
105  Handle = h;
106  return *this;
107  }
108 
109  // True if Handle is valid.
110  explicit operator bool() const {
111  return HandleTraits::IsValid(Handle) ? true : false;
112  }
113 
114  operator handle_type() const {
115  return Handle;
116  }
117 };
118 
120  typedef HANDLE handle_type;
121 
123  return INVALID_HANDLE_VALUE;
124  }
125 
126  static void Close(handle_type h) {
127  ::CloseHandle(h);
128  }
129 
130  static bool IsValid(handle_type h) {
131  return h != GetInvalid();
132  }
133 };
134 
137  return NULL;
138  }
139 };
140 
142  typedef HCRYPTPROV handle_type;
143 
145  return 0;
146  }
147 
148  static void Close(handle_type h) {
149  ::CryptReleaseContext(h, 0);
150  }
151 
152  static bool IsValid(handle_type h) {
153  return h != GetInvalid();
154  }
155 };
156 
158  typedef HKEY handle_type;
159 
161  return NULL;
162  }
163 
164  static void Close(handle_type h) {
165  ::RegCloseKey(h);
166  }
167 
168  static bool IsValid(handle_type h) {
169  return h != GetInvalid();
170  }
171 };
172 
174  static void Close(handle_type h) {
175  ::FindClose(h);
176  }
177 };
178 
180 
187 
188 template <class T>
189 class SmallVectorImpl;
190 
191 template <class T>
194  str.push_back(0);
195  str.pop_back();
196  return str.data();
197 }
198 
199 namespace sys {
200 
201 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
202  ULARGE_INTEGER TimeInteger;
203  TimeInteger.LowPart = Time.dwLowDateTime;
204  TimeInteger.HighPart = Time.dwHighDateTime;
205 
206  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
207  return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
208 }
209 
210 inline TimePoint<> toTimePoint(FILETIME Time) {
211  ULARGE_INTEGER TimeInteger;
212  TimeInteger.LowPart = Time.dwLowDateTime;
213  TimeInteger.HighPart = Time.dwHighDateTime;
214 
215  // Adjust for different epoch
216  TimeInteger.QuadPart -= 11644473600ll * 10000000;
217 
218  // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
219  return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
220 }
221 
222 inline FILETIME toFILETIME(TimePoint<> TP) {
223  ULARGE_INTEGER TimeInteger;
224  TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
225  TimeInteger.QuadPart += 11644473600ll * 10000000;
226 
227  FILETIME Time;
228  Time.dwLowDateTime = TimeInteger.LowPart;
229  Time.dwHighDateTime = TimeInteger.HighPart;
230  return Time;
231 }
232 
233 namespace windows {
234 // Returns command line arguments. Unlike arguments given to main(),
235 // this function guarantees that the returned arguments are encoded in
236 // UTF-8 regardless of the current code page setting.
238  BumpPtrAllocator &Alloc);
239 
240 /// Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode
241 /// File API.
242 std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
243  size_t MaxPathLen = MAX_PATH);
244 
245 } // end namespace windows
246 } // end namespace sys
247 } // end namespace llvm.
248 
249 #endif
llvm::RunningWindows8OrGreater
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::GetWindowsOSVersion
llvm::VersionTuple GetWindowsOSVersion()
Returns the Windows version as Major.Minor.0.BuildNumber.
StringRef.h
ErrorHandling.h
VersionTuple.h
Allocator.h
llvm::ScopedHandle::ScopedHandle
ScopedHandle()
Definition: WindowsSupport.h:85
llvm::FindHandleTraits::Close
static void Close(handle_type h)
Definition: WindowsSupport.h:174
llvm::CommonHandleTraits::handle_type
HANDLE handle_type
Definition: WindowsSupport.h:120
llvm::RegTraits::Close
static void Close(handle_type h)
Definition: WindowsSupport.h:164
llvm::JobHandleTraits::GetInvalid
static handle_type GetInvalid()
Definition: WindowsSupport.h:136
llvm::ScopedFindHandle
ScopedHandle< FindHandleTraits > ScopedFindHandle
Definition: WindowsSupport.h:185
llvm::sys::TimePoint
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33
Chrono.h
llvm::ScopedRegHandle
ScopedHandle< RegTraits > ScopedRegHandle
Definition: WindowsSupport.h:184
Twine.h
t
bitcast float %x to i32 %s=and i32 %t, 2147483647 %d=bitcast i32 %s to float ret float %d } declare float @fabsf(float %n) define float @bar(float %x) nounwind { %d=call float @fabsf(float %x) ret float %d } This IR(from PR6194):target datalayout="e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple="x86_64-apple-darwin10.0.0" %0=type { double, double } %struct.float3=type { float, float, float } define void @test(%0, %struct.float3 *nocapture %res) nounwind noinline ssp { entry:%tmp18=extractvalue %0 %0, 0 t
Definition: README-SSE.txt:788
llvm::RegTraits::IsValid
static bool IsValid(handle_type h)
Definition: WindowsSupport.h:168
ll
Analysis the ScalarEvolution expression for r is< loop > Outside the this could be evaluated simply however ScalarEvolution currently evaluates it it involves i65 which is very inefficient when expanded into code In formatValue in test CodeGen X86 lsr delayed fold ll
Definition: README.txt:20
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::RegTraits::GetInvalid
static handle_type GetInvalid()
Definition: WindowsSupport.h:160
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
llvm::ScopedHandle
Definition: WindowsSupport.h:78
llvm::RegTraits::handle_type
HKEY handle_type
Definition: WindowsSupport.h:158
llvm::JobHandleTraits
Definition: WindowsSupport.h:135
llvm::RegTraits
Definition: WindowsSupport.h:157
llvm::sys::windows::GetCommandLineArguments
std::error_code GetCommandLineArguments(SmallVectorImpl< const char * > &Args, BumpPtrAllocator &Alloc)
llvm::ScopedHandle::take
handle_type take()
Definition: WindowsSupport.h:96
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
StringExtras.h
llvm::MakeErrMsg
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
llvm::sys::windows::widenPath
std::error_code widenPath(const Twine &Path8, SmallVectorImpl< wchar_t > &Path16, size_t MaxPathLen=MAX_PATH)
Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode File API.
llvm::ScopedJobHandle
ScopedHandle< JobHandleTraits > ScopedJobHandle
Definition: WindowsSupport.h:186
llvm::CryptContextTraits::GetInvalid
static handle_type GetInvalid()
Definition: WindowsSupport.h:144
Compiler.h
llvm::FileHandleTraits
Definition: WindowsSupport.h:179
llvm::CommonHandleTraits::Close
static void Close(handle_type h)
Definition: WindowsSupport.h:126
llvm::ScopedCryptContext
ScopedHandle< CryptContextTraits > ScopedCryptContext
Definition: WindowsSupport.h:183
llvm::ScopedHandle::ScopedHandle
ScopedHandle(handle_type h)
Definition: WindowsSupport.h:88
llvm::CryptContextTraits::IsValid
static bool IsValid(handle_type h)
Definition: WindowsSupport.h:152
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::ScopedFileHandle
ScopedHandle< FileHandleTraits > ScopedFileHandle
Definition: WindowsSupport.h:182
llvm::CryptContextTraits::handle_type
HCRYPTPROV handle_type
Definition: WindowsSupport.h:142
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: WindowsSupport.h:193
llvm::sys::toDuration
std::chrono::nanoseconds toDuration(FILETIME Time)
Definition: WindowsSupport.h:201
llvm::sys::toFILETIME
FILETIME toFILETIME(TimePoint<> TP)
Definition: WindowsSupport.h:222
llvm::ScopedHandle::operator=
ScopedHandle & operator=(handle_type h)
Definition: WindowsSupport.h:102
llvm::CryptContextTraits
Definition: WindowsSupport.h:141
llvm::ScopedCommonHandle
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
Definition: WindowsSupport.h:181
llvm::FindHandleTraits
Definition: WindowsSupport.h:173
llvm::CommonHandleTraits::IsValid
static bool IsValid(handle_type h)
Definition: WindowsSupport.h:130
SmallVector.h
llvm::sys::toTimePoint
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
llvm::CommonHandleTraits::GetInvalid
static handle_type GetInvalid()
Definition: WindowsSupport.h:122
llvm::CommonHandleTraits
Definition: WindowsSupport.h:119
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
h
the multiplication has a latency of four as opposed to two cycles for the movl lea variant It appears gcc place string data with linkonce linkage in section coalesced instead of section coalesced Take a look at darwin h
Definition: README.txt:261
llvm::CryptContextTraits::Close
static void Close(handle_type h)
Definition: WindowsSupport.h:148
llvm::ScopedHandle::~ScopedHandle
~ScopedHandle()
Definition: WindowsSupport.h:91
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::ReportLastErrorFatal
void ReportLastErrorFatal(const char *Msg)
Definition: WindowsSupport.h:71
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1883