blob: 1f3417d160a24cedbb038a35ee083f6a6dd9e784 [file] [log] [blame]
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00001//===- Win32/Win32.h - Common Win32 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//
Jeff Cohen3800a282005-07-13 02:15:18 +000010// This file defines things specific to Win32 implementations.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000011//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
Jeff Cohen3800a282005-07-13 02:15:18 +000015//=== WARNING: Implementation here must contain only generic Win32 code that
16//=== is guaranteed to work on *all* Win32 variants.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000017//===----------------------------------------------------------------------===//
18
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000019// mingw-w64 tends to define it as 0x0502 in its headers.
20#undef _WIN32_WINNT
21
NAKAMURA Takumi613655f2011-08-20 06:35:31 +000022// Require at least Windows XP(5.1) API.
23#define _WIN32_WINNT 0x0501
NAKAMURA Takumifd6cb642011-08-23 03:49:11 +000024#define _WIN32_IE 0x0600 // MinGW at it again.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000025#define WIN32_LEAN_AND_MEAN
Jeff Cohen3800a282005-07-13 02:15:18 +000026
Rui Ueyama471d0c52013-09-10 19:45:51 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000029#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000030#include "llvm/Support/Compiler.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000031#include "llvm/Support/system_error.h"
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000032#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000033#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000034#include <cassert>
35#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000036#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000037
Reid Spencer879ed5a2006-08-23 07:30:48 +000038inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000039 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000040 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000041 char *buffer = NULL;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000042 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43 FORMAT_MESSAGE_FROM_SYSTEM,
44 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
45 if (R)
46 *ErrMsg = prefix + buffer;
47 else
48 *ErrMsg = prefix + "Unknown error";
49
Reid Spencer42bcf6e2006-08-21 06:02:44 +000050 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000051 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000052}
Jeff Cohena531d042007-03-05 05:22:08 +000053
Michael J. Spencer513f1b62011-12-12 06:03:33 +000054template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000055class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000056 typedef typename HandleTraits::handle_type handle_type;
57 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000058
Michael J. Spencer513f1b62011-12-12 06:03:33 +000059 ScopedHandle(const ScopedHandle &other); // = delete;
60 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000061public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000062 ScopedHandle()
63 : Handle(HandleTraits::GetInvalid()) {}
64
65 explicit ScopedHandle(handle_type h)
66 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000067
68 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000069 if (HandleTraits::IsValid(Handle))
70 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000071 }
72
Michael J. Spencer513f1b62011-12-12 06:03:33 +000073 handle_type take() {
74 handle_type t = Handle;
75 Handle = HandleTraits::GetInvalid();
76 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000077 }
78
Michael J. Spencer513f1b62011-12-12 06:03:33 +000079 ScopedHandle &operator=(handle_type h) {
80 if (HandleTraits::IsValid(Handle))
81 HandleTraits::Close(Handle);
82 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000083 return *this;
84 }
85
Michael J. Spencer39c46212010-12-06 04:28:13 +000086 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000087 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000088 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000089 }
90
Michael J. Spencer513f1b62011-12-12 06:03:33 +000091 operator handle_type() const {
92 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000093 }
Michael J. Spencer39c46212010-12-06 04:28:13 +000094};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000095
Michael J. Spencer513f1b62011-12-12 06:03:33 +000096struct CommonHandleTraits {
97 typedef HANDLE handle_type;
98
99 static handle_type GetInvalid() {
100 return INVALID_HANDLE_VALUE;
101 }
102
103 static void Close(handle_type h) {
104 ::CloseHandle(h);
105 }
106
107 static bool IsValid(handle_type h) {
108 return h != GetInvalid();
109 }
110};
111
112struct JobHandleTraits : CommonHandleTraits {
113 static handle_type GetInvalid() {
114 return NULL;
115 }
116};
117
118struct CryptContextTraits : CommonHandleTraits {
119 typedef HCRYPTPROV handle_type;
120
121 static handle_type GetInvalid() {
122 return 0;
123 }
124
125 static void Close(handle_type h) {
126 ::CryptReleaseContext(h, 0);
127 }
128
129 static bool IsValid(handle_type h) {
130 return h != GetInvalid();
131 }
132};
133
134struct FindHandleTraits : CommonHandleTraits {
135 static void Close(handle_type h) {
136 ::FindClose(h);
137 }
138};
139
140struct FileHandleTraits : CommonHandleTraits {};
141
142typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
143typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
144typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
145typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
146typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000147
148namespace llvm {
149template <class T>
150class SmallVectorImpl;
151
152template <class T>
153typename SmallVectorImpl<T>::const_pointer
154c_str(SmallVectorImpl<T> &str) {
155 str.push_back(0);
156 str.pop_back();
157 return str.data();
158}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000159
160namespace sys {
161namespace windows {
162error_code UTF8ToUTF16(StringRef utf8,
163 SmallVectorImpl<wchar_t> &utf16);
164error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
165 SmallVectorImpl<char> &utf8);
166} // end namespace windows
167} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000168} // end namespace llvm.