blob: f68835b1a71a7463a89eaff1b580e75bb904d4fd [file] [log] [blame]
Reid Klecknerd59e2fa2014-02-12 21:26:20 +00001//===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00008//===----------------------------------------------------------------------===//
9//
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000010// 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.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000014//
15//===----------------------------------------------------------------------===//
16
17//===----------------------------------------------------------------------===//
Jeff Cohen3800a282005-07-13 02:15:18 +000018//=== WARNING: Implementation here must contain only generic Win32 code that
19//=== is guaranteed to work on *all* Win32 variants.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000020//===----------------------------------------------------------------------===//
21
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000022// mingw-w64 tends to define it as 0x0502 in its headers.
23#undef _WIN32_WINNT
Aaron Ballman42f66222014-02-03 17:20:26 +000024#undef _WIN32_IE
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000025
NAKAMURA Takumi613655f2011-08-20 06:35:31 +000026// Require at least Windows XP(5.1) API.
27#define _WIN32_WINNT 0x0501
NAKAMURA Takumifd6cb642011-08-23 03:49:11 +000028#define _WIN32_IE 0x0600 // MinGW at it again.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000029#define WIN32_LEAN_AND_MEAN
Jeff Cohen3800a282005-07-13 02:15:18 +000030
Rui Ueyama471d0c52013-09-10 19:45:51 +000031#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringRef.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000033#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000034#include "llvm/Support/Compiler.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000035#include <system_error>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000036#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000037#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000038#include <cassert>
39#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000040#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000041
Reid Spencer879ed5a2006-08-23 07:30:48 +000042inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000043 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000044 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000045 char *buffer = NULL;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000046 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
47 FORMAT_MESSAGE_FROM_SYSTEM,
48 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
49 if (R)
50 *ErrMsg = prefix + buffer;
51 else
52 *ErrMsg = prefix + "Unknown error";
53
Reid Spencer42bcf6e2006-08-21 06:02:44 +000054 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000055 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000056}
Jeff Cohena531d042007-03-05 05:22:08 +000057
Michael J. Spencer513f1b62011-12-12 06:03:33 +000058template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000059class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000060 typedef typename HandleTraits::handle_type handle_type;
61 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000062
Michael J. Spencer513f1b62011-12-12 06:03:33 +000063 ScopedHandle(const ScopedHandle &other); // = delete;
64 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000065public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000066 ScopedHandle()
67 : Handle(HandleTraits::GetInvalid()) {}
68
69 explicit ScopedHandle(handle_type h)
70 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000071
72 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000073 if (HandleTraits::IsValid(Handle))
74 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000075 }
76
Michael J. Spencer513f1b62011-12-12 06:03:33 +000077 handle_type take() {
78 handle_type t = Handle;
79 Handle = HandleTraits::GetInvalid();
80 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000081 }
82
Michael J. Spencer513f1b62011-12-12 06:03:33 +000083 ScopedHandle &operator=(handle_type h) {
84 if (HandleTraits::IsValid(Handle))
85 HandleTraits::Close(Handle);
86 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000087 return *this;
88 }
89
Michael J. Spencer39c46212010-12-06 04:28:13 +000090 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000091 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000092 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000093 }
94
Michael J. Spencer513f1b62011-12-12 06:03:33 +000095 operator handle_type() const {
96 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000097 }
Michael J. Spencer39c46212010-12-06 04:28:13 +000098};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000099
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000100struct CommonHandleTraits {
101 typedef HANDLE handle_type;
102
103 static handle_type GetInvalid() {
104 return INVALID_HANDLE_VALUE;
105 }
106
107 static void Close(handle_type h) {
108 ::CloseHandle(h);
109 }
110
111 static bool IsValid(handle_type h) {
112 return h != GetInvalid();
113 }
114};
115
116struct JobHandleTraits : CommonHandleTraits {
117 static handle_type GetInvalid() {
118 return NULL;
119 }
120};
121
122struct CryptContextTraits : CommonHandleTraits {
123 typedef HCRYPTPROV handle_type;
124
125 static handle_type GetInvalid() {
126 return 0;
127 }
128
129 static void Close(handle_type h) {
130 ::CryptReleaseContext(h, 0);
131 }
132
133 static bool IsValid(handle_type h) {
134 return h != GetInvalid();
135 }
136};
137
138struct FindHandleTraits : CommonHandleTraits {
139 static void Close(handle_type h) {
140 ::FindClose(h);
141 }
142};
143
144struct FileHandleTraits : CommonHandleTraits {};
145
146typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
147typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
148typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
149typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
150typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000151
152namespace llvm {
153template <class T>
154class SmallVectorImpl;
155
156template <class T>
157typename SmallVectorImpl<T>::const_pointer
158c_str(SmallVectorImpl<T> &str) {
159 str.push_back(0);
160 str.pop_back();
161 return str.data();
162}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000163
164namespace sys {
165namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000166std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
167std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
168 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000169} // end namespace windows
170} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000171} // end namespace llvm.