1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
22 // mingw-w64 tends to define it as 0x0502 in its headers.
26 // Require at least Windows XP(5.1) API.
27 #define _WIN32_WINNT 0x0501
28 #define _WIN32_IE 0x0600 // MinGW at it again.
29 #define WIN32_LEAN_AND_MEAN
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Config/config.h" // Get build system configuration settings
34 #include "llvm/Support/Compiler.h"
35 #include <system_error>
42 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
46 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
47 FORMAT_MESSAGE_FROM_SYSTEM,
48 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
50 *ErrMsg = prefix + buffer;
52 *ErrMsg = prefix + "Unknown error";
58 template <typename HandleTraits>
60 typedef typename HandleTraits::handle_type handle_type;
63 ScopedHandle(const ScopedHandle &other); // = delete;
64 void operator=(const ScopedHandle &other); // = delete;
67 : Handle(HandleTraits::GetInvalid()) {}
69 explicit ScopedHandle(handle_type h)
73 if (HandleTraits::IsValid(Handle))
74 HandleTraits::Close(Handle);
78 handle_type t = Handle;
79 Handle = HandleTraits::GetInvalid();
83 ScopedHandle &operator=(handle_type h) {
84 if (HandleTraits::IsValid(Handle))
85 HandleTraits::Close(Handle);
90 // True if Handle is valid.
91 LLVM_EXPLICIT operator bool() const {
92 return HandleTraits::IsValid(Handle) ? true : false;
95 operator handle_type() const {
100 struct CommonHandleTraits {
101 typedef HANDLE handle_type;
103 static handle_type GetInvalid() {
104 return INVALID_HANDLE_VALUE;
107 static void Close(handle_type h) {
111 static bool IsValid(handle_type h) {
112 return h != GetInvalid();
116 struct JobHandleTraits : CommonHandleTraits {
117 static handle_type GetInvalid() {
122 struct CryptContextTraits : CommonHandleTraits {
123 typedef HCRYPTPROV handle_type;
125 static handle_type GetInvalid() {
129 static void Close(handle_type h) {
130 ::CryptReleaseContext(h, 0);
133 static bool IsValid(handle_type h) {
134 return h != GetInvalid();
138 struct FindHandleTraits : CommonHandleTraits {
139 static void Close(handle_type h) {
144 struct FileHandleTraits : CommonHandleTraits {};
146 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
147 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
148 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
149 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
150 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
154 class SmallVectorImpl;
157 typename SmallVectorImpl<T>::const_pointer
158 c_str(SmallVectorImpl<T> &str) {
166 error_code UTF8ToUTF16(StringRef utf8,
167 SmallVectorImpl<wchar_t> &utf16);
168 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
169 SmallVectorImpl<char> &utf8);
170 } // end namespace windows
171 } // end namespace sys
172 } // end namespace llvm.