1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
21
22 // mingw-w64 tends to define it as 0x0502 in its headers.
23 #undef _WIN32_WINNT
24 #undef _WIN32_IE
25
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
30
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/ADT/Twine.h"
34 #include "llvm/Config/config.h" // Get build system configuration settings
35 #include "llvm/Support/Compiler.h"
36 #include <system_error>
37 #include <windows.h>
38 #include <wincrypt.h>
39 #include <cassert>
40 #include <string>
41 #include <vector>
42
MakeErrMsg(std::string * ErrMsg,const std::string & prefix)43 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
44 if (!ErrMsg)
45 return true;
46 char *buffer = NULL;
47 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48 FORMAT_MESSAGE_FROM_SYSTEM,
49 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
50 if (R)
51 *ErrMsg = prefix + buffer;
52 else
53 *ErrMsg = prefix + "Unknown error";
54
55 LocalFree(buffer);
56 return R != 0;
57 }
58
59 template <typename HandleTraits>
60 class ScopedHandle {
61 typedef typename HandleTraits::handle_type handle_type;
62 handle_type Handle;
63
64 ScopedHandle(const ScopedHandle &other); // = delete;
65 void operator=(const ScopedHandle &other); // = delete;
66 public:
ScopedHandle()67 ScopedHandle()
68 : Handle(HandleTraits::GetInvalid()) {}
69
ScopedHandle(handle_type h)70 explicit ScopedHandle(handle_type h)
71 : Handle(h) {}
72
~ScopedHandle()73 ~ScopedHandle() {
74 if (HandleTraits::IsValid(Handle))
75 HandleTraits::Close(Handle);
76 }
77
take()78 handle_type take() {
79 handle_type t = Handle;
80 Handle = HandleTraits::GetInvalid();
81 return t;
82 }
83
84 ScopedHandle &operator=(handle_type h) {
85 if (HandleTraits::IsValid(Handle))
86 HandleTraits::Close(Handle);
87 Handle = h;
88 return *this;
89 }
90
91 // True if Handle is valid.
92 LLVM_EXPLICIT operator bool() const {
93 return HandleTraits::IsValid(Handle) ? true : false;
94 }
95
handle_type()96 operator handle_type() const {
97 return Handle;
98 }
99 };
100
101 struct CommonHandleTraits {
102 typedef HANDLE handle_type;
103
GetInvalidCommonHandleTraits104 static handle_type GetInvalid() {
105 return INVALID_HANDLE_VALUE;
106 }
107
CloseCommonHandleTraits108 static void Close(handle_type h) {
109 ::CloseHandle(h);
110 }
111
IsValidCommonHandleTraits112 static bool IsValid(handle_type h) {
113 return h != GetInvalid();
114 }
115 };
116
117 struct JobHandleTraits : CommonHandleTraits {
GetInvalidJobHandleTraits118 static handle_type GetInvalid() {
119 return NULL;
120 }
121 };
122
123 struct CryptContextTraits : CommonHandleTraits {
124 typedef HCRYPTPROV handle_type;
125
GetInvalidCryptContextTraits126 static handle_type GetInvalid() {
127 return 0;
128 }
129
CloseCryptContextTraits130 static void Close(handle_type h) {
131 ::CryptReleaseContext(h, 0);
132 }
133
IsValidCryptContextTraits134 static bool IsValid(handle_type h) {
135 return h != GetInvalid();
136 }
137 };
138
139 struct FindHandleTraits : CommonHandleTraits {
CloseFindHandleTraits140 static void Close(handle_type h) {
141 ::FindClose(h);
142 }
143 };
144
145 struct FileHandleTraits : CommonHandleTraits {};
146
147 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
148 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
149 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
150 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
151 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
152
153 namespace llvm {
154 template <class T>
155 class SmallVectorImpl;
156
157 template <class T>
158 typename SmallVectorImpl<T>::const_pointer
c_str(SmallVectorImpl<T> & str)159 c_str(SmallVectorImpl<T> &str) {
160 str.push_back(0);
161 str.pop_back();
162 return str.data();
163 }
164
165 namespace sys {
166 namespace path {
167 std::error_code widenPath(const Twine &Path8,
168 SmallVectorImpl<wchar_t> &Path16);
169 } // end namespace path
170
171 namespace windows {
172 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
173 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
174 SmallVectorImpl<char> &utf8);
175 /// Convert from UTF16 to the current code page used in the system
176 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
177 SmallVectorImpl<char> &utf8);
178 } // end namespace windows
179 } // end namespace sys
180 } // end namespace llvm.
181