LLVM 20.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
27// Require at least Windows 7 API.
28#define _WIN32_WINNT 0x0601
29#define WIN32_LEAN_AND_MEAN
30#ifndef NOMINMAX
31#define NOMINMAX
32#endif
33
36#include "llvm/ADT/StringRef.h"
37#include "llvm/ADT/Twine.h"
38#include "llvm/Config/llvm-config.h" // Get build system configuration settings
40#include "llvm/Support/Chrono.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
52namespace 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/// Determines if the program is running on Windows 11 or Windows Server 2022.
62
63/// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
64/// RtlGetVersion or GetVersionEx under the hood depending on what is available.
65/// GetVersionEx is deprecated, but this API exposes the build number which can
66/// be useful for working around certain kernel bugs.
68
69bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
70
71// Include GetLastError() in a fatal error message.
72[[noreturn]] inline void ReportLastErrorFatal(const char *Msg) {
73 std::string ErrMsg;
74 MakeErrMsg(&ErrMsg, Msg);
76}
77
78template <typename HandleTraits>
80 typedef typename HandleTraits::handle_type handle_type;
81 handle_type Handle;
82
83 ScopedHandle(const ScopedHandle &other) = delete;
84 void operator=(const ScopedHandle &other) = delete;
85public:
87 : Handle(HandleTraits::GetInvalid()) {}
88
89 explicit ScopedHandle(handle_type h)
90 : Handle(h) {}
91
93 if (HandleTraits::IsValid(Handle))
94 HandleTraits::Close(Handle);
95 }
96
97 handle_type take() {
98 handle_type t = Handle;
99 Handle = HandleTraits::GetInvalid();
100 return t;
101 }
102
103 ScopedHandle &operator=(handle_type h) {
104 if (HandleTraits::IsValid(Handle))
105 HandleTraits::Close(Handle);
106 Handle = h;
107 return *this;
108 }
109
110 // True if Handle is valid.
111 explicit operator bool() const {
112 return HandleTraits::IsValid(Handle) ? true : false;
113 }
114
115 operator handle_type() const {
116 return Handle;
117 }
118};
119
121 typedef HANDLE handle_type;
122
124 return INVALID_HANDLE_VALUE;
125 }
126
127 static void Close(handle_type h) {
128 ::CloseHandle(h);
129 }
130
131 static bool IsValid(handle_type h) {
132 return h != GetInvalid();
133 }
134};
135
138 return NULL;
139 }
140};
141
143 typedef HCRYPTPROV handle_type;
144
146 return 0;
147 }
148
149 static void Close(handle_type h) {
150 ::CryptReleaseContext(h, 0);
151 }
152
153 static bool IsValid(handle_type h) {
154 return h != GetInvalid();
155 }
156};
157
159 typedef HKEY handle_type;
160
162 return NULL;
163 }
164
165 static void Close(handle_type h) {
166 ::RegCloseKey(h);
167 }
168
169 static bool IsValid(handle_type h) {
170 return h != GetInvalid();
171 }
172};
173
175 static void Close(handle_type h) {
176 ::FindClose(h);
177 }
178};
179
181
188
189template <class T>
190class SmallVectorImpl;
191
192template <class T>
195 str.push_back(0);
196 str.pop_back();
197 return str.data();
198}
199
200namespace sys {
201
202inline std::chrono::nanoseconds toDuration(FILETIME Time) {
203 ULARGE_INTEGER TimeInteger;
204 TimeInteger.LowPart = Time.dwLowDateTime;
205 TimeInteger.HighPart = Time.dwHighDateTime;
206
207 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
208 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
209}
210
211inline TimePoint<> toTimePoint(FILETIME Time) {
212 ULARGE_INTEGER TimeInteger;
213 TimeInteger.LowPart = Time.dwLowDateTime;
214 TimeInteger.HighPart = Time.dwHighDateTime;
215
216 // Adjust for different epoch
217 TimeInteger.QuadPart -= 11644473600ll * 10000000;
218
219 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
220 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
221}
222
223inline FILETIME toFILETIME(TimePoint<> TP) {
224 ULARGE_INTEGER TimeInteger;
225 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
226 TimeInteger.QuadPart += 11644473600ll * 10000000;
227
228 FILETIME Time;
229 Time.dwLowDateTime = TimeInteger.LowPart;
230 Time.dwHighDateTime = TimeInteger.HighPart;
231 return Time;
232}
233
234namespace windows {
235// Returns command line arguments. Unlike arguments given to main(),
236// this function guarantees that the returned arguments are encoded in
237// UTF-8 regardless of the current code page setting.
240
241/// Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode
242/// File API.
243std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
244 size_t MaxPathLen = MAX_PATH);
245
246} // end namespace windows
247} // end namespace sys
248} // end namespace llvm.
249
250#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:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:286
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:167
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)