LLVM 19.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
38#include "llvm/ADT/StringRef.h"
39#include "llvm/ADT/Twine.h"
40#include "llvm/Config/llvm-config.h" // Get build system configuration settings
42#include "llvm/Support/Chrono.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
54namespace 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/// Determines if the program is running on Windows 11 or Windows Server 2022.
64
65/// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
66/// RtlGetVersion or GetVersionEx under the hood depending on what is available.
67/// GetVersionEx is deprecated, but this API exposes the build number which can
68/// be useful for working around certain kernel bugs.
70
71bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
72
73// Include GetLastError() in a fatal error message.
74[[noreturn]] inline void ReportLastErrorFatal(const char *Msg) {
75 std::string ErrMsg;
76 MakeErrMsg(&ErrMsg, Msg);
78}
79
80template <typename HandleTraits>
82 typedef typename HandleTraits::handle_type handle_type;
83 handle_type Handle;
84
85 ScopedHandle(const ScopedHandle &other) = delete;
86 void operator=(const ScopedHandle &other) = delete;
87public:
89 : Handle(HandleTraits::GetInvalid()) {}
90
91 explicit ScopedHandle(handle_type h)
92 : Handle(h) {}
93
95 if (HandleTraits::IsValid(Handle))
96 HandleTraits::Close(Handle);
97 }
98
99 handle_type take() {
100 handle_type t = Handle;
101 Handle = HandleTraits::GetInvalid();
102 return t;
103 }
104
105 ScopedHandle &operator=(handle_type h) {
106 if (HandleTraits::IsValid(Handle))
107 HandleTraits::Close(Handle);
108 Handle = h;
109 return *this;
110 }
111
112 // True if Handle is valid.
113 explicit operator bool() const {
114 return HandleTraits::IsValid(Handle) ? true : false;
115 }
116
117 operator handle_type() const {
118 return Handle;
119 }
120};
121
123 typedef HANDLE handle_type;
124
126 return INVALID_HANDLE_VALUE;
127 }
128
129 static void Close(handle_type h) {
130 ::CloseHandle(h);
131 }
132
133 static bool IsValid(handle_type h) {
134 return h != GetInvalid();
135 }
136};
137
140 return NULL;
141 }
142};
143
145 typedef HCRYPTPROV handle_type;
146
148 return 0;
149 }
150
151 static void Close(handle_type h) {
152 ::CryptReleaseContext(h, 0);
153 }
154
155 static bool IsValid(handle_type h) {
156 return h != GetInvalid();
157 }
158};
159
161 typedef HKEY handle_type;
162
164 return NULL;
165 }
166
167 static void Close(handle_type h) {
168 ::RegCloseKey(h);
169 }
170
171 static bool IsValid(handle_type h) {
172 return h != GetInvalid();
173 }
174};
175
177 static void Close(handle_type h) {
178 ::FindClose(h);
179 }
180};
181
183
190
191template <class T>
192class SmallVectorImpl;
193
194template <class T>
197 str.push_back(0);
198 str.pop_back();
199 return str.data();
200}
201
202namespace sys {
203
204inline std::chrono::nanoseconds toDuration(FILETIME Time) {
205 ULARGE_INTEGER TimeInteger;
206 TimeInteger.LowPart = Time.dwLowDateTime;
207 TimeInteger.HighPart = Time.dwHighDateTime;
208
209 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
210 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
211}
212
213inline TimePoint<> toTimePoint(FILETIME Time) {
214 ULARGE_INTEGER TimeInteger;
215 TimeInteger.LowPart = Time.dwLowDateTime;
216 TimeInteger.HighPart = Time.dwHighDateTime;
217
218 // Adjust for different epoch
219 TimeInteger.QuadPart -= 11644473600ll * 10000000;
220
221 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
222 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
223}
224
225inline FILETIME toFILETIME(TimePoint<> TP) {
226 ULARGE_INTEGER TimeInteger;
227 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
228 TimeInteger.QuadPart += 11644473600ll * 10000000;
229
230 FILETIME Time;
231 Time.dwLowDateTime = TimeInteger.LowPart;
232 Time.dwHighDateTime = TimeInteger.HighPart;
233 return Time;
234}
235
236namespace windows {
237// Returns command line arguments. Unlike arguments given to main(),
238// this function guarantees that the returned arguments are encoded in
239// UTF-8 regardless of the current code page setting.
242
243/// Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode
244/// File API.
245std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
246 size_t MaxPathLen = MAX_PATH);
247
248} // end namespace windows
249} // end namespace sys
250} // end namespace llvm.
251
252#endif
This file defines the BumpPtrAllocator interface.
basic Basic Alias true
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Defines the llvm::VersionTuple class, which represents a version in the form major[....
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
ScopedHandle(handle_type h)
ScopedHandle & operator=(handle_type h)
handle_type take()
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
std::error_code GetCommandLineArguments(SmallVectorImpl< const char * > &Args, BumpPtrAllocator &Alloc)
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.
std::chrono::nanoseconds toDuration(FILETIME Time)
FILETIME toFILETIME(TimePoint<> TP)
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:65
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void ReportLastErrorFatal(const char *Msg)
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
ScopedHandle< CryptContextTraits > ScopedCryptContext
llvm::VersionTuple GetWindowsOSVersion()
Returns the Windows version as Major.Minor.0.BuildNumber.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
ScopedHandle< FindHandleTraits > ScopedFindHandle
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
ScopedHandle< JobHandleTraits > ScopedJobHandle
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
ScopedHandle< RegTraits > ScopedRegHandle
bool RunningWindows11OrGreater()
Determines if the program is running on Windows 11 or Windows Server 2022.
ScopedHandle< FileHandleTraits > ScopedFileHandle
static handle_type GetInvalid()
static void Close(handle_type h)
static bool IsValid(handle_type h)
static void Close(handle_type h)
static handle_type GetInvalid()
static bool IsValid(handle_type h)
static void Close(handle_type h)
static handle_type GetInvalid()
static handle_type GetInvalid()
static bool IsValid(handle_type h)
static void Close(handle_type h)