blob: 5c1da0d617aa776132943a8042571c346eb8569e [file] [log] [blame]
Reid Spencer0b22ba42004-09-15 05:48:11 +00001//===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer0b22ba42004-09-15 05:48:11 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer0b22ba42004-09-15 05:48:11 +00008//===----------------------------------------------------------------------===//
9//
Jeff Cohen6d235222005-07-13 02:15:18 +000010// This file defines things specific to Win32 implementations.
Reid Spencer0b22ba42004-09-15 05:48:11 +000011//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
Jeff Cohen6d235222005-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 Spencer0b22ba42004-09-15 05:48:11 +000017//===----------------------------------------------------------------------===//
18
NAKAMURA Takumifd289492011-02-09 04:18:30 +000019// mingw-w64 tends to define it as 0x0502 in its headers.
20#undef _WIN32_WINNT
21
NAKAMURA Takumiaa629662011-08-20 06:35:31 +000022// Require at least Windows XP(5.1) API.
23#define _WIN32_WINNT 0x0501
NAKAMURA Takumi07097dd2011-08-23 03:49:11 +000024#define _WIN32_IE 0x0600 // MinGW at it again.
Michael J. Spencer966023b2010-11-09 15:11:07 +000025#define WIN32_LEAN_AND_MEAN
Jeff Cohen6d235222005-07-13 02:15:18 +000026
Michael J. Spencer64cf7522010-11-09 15:10:29 +000027#include "llvm/Config/config.h" // Get build system configuration settings
NAKAMURA Takumi6d600da2011-02-04 12:53:04 +000028#include <windows.h>
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000029#include <wincrypt.h>
NAKAMURA Takumi6d600da2011-02-04 12:53:04 +000030#include <shlobj.h>
Reid Spencer0b22ba42004-09-15 05:48:11 +000031#include <cassert>
32#include <string>
33
Reid Spencer5a060772006-08-23 07:30:48 +000034inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +000035 if (!ErrMsg)
Reid Spencer5a060772006-08-23 07:30:48 +000036 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000037 char *buffer = NULL;
38 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
39 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
Anton Korobeynikov7d515442006-09-01 20:35:17 +000040 *ErrMsg = prefix + buffer;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000041 LocalFree(buffer);
Reid Spencer5a060772006-08-23 07:30:48 +000042 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000043}
Jeff Cohen0a182672007-03-05 05:22:08 +000044
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000045template <typename HandleTraits>
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000046class ScopedHandle {
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000047 typedef typename HandleTraits::handle_type handle_type;
48 handle_type Handle;
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000049
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000050 ScopedHandle(const ScopedHandle &other); // = delete;
51 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000052public:
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000053 ScopedHandle()
54 : Handle(HandleTraits::GetInvalid()) {}
55
56 explicit ScopedHandle(handle_type h)
57 : Handle(h) {}
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000058
59 ~ScopedHandle() {
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000060 if (HandleTraits::IsValid(Handle))
61 HandleTraits::Close(Handle);
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000062 }
63
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000064 handle_type take() {
65 handle_type t = Handle;
66 Handle = HandleTraits::GetInvalid();
67 return t;
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000068 }
69
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000070 ScopedHandle &operator=(handle_type h) {
71 if (HandleTraits::IsValid(Handle))
72 HandleTraits::Close(Handle);
73 Handle = h;
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000074 return *this;
75 }
76
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000077 // True if Handle is valid.
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000078 operator bool() const {
79 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000080 }
81
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000082 operator handle_type() const {
83 return Handle;
Michael J. Spencer753cbbb2010-12-06 04:28:42 +000084 }
Michael J. Spencer7eec50b2010-12-06 04:28:13 +000085};
Michael J. Spencer753cbbb2010-12-06 04:28:42 +000086
Michael J. Spencer1dd2ee72011-12-12 06:03:33 +000087struct CommonHandleTraits {
88 typedef HANDLE handle_type;
89
90 static handle_type GetInvalid() {
91 return INVALID_HANDLE_VALUE;
92 }
93
94 static void Close(handle_type h) {
95 ::CloseHandle(h);
96 }
97
98 static bool IsValid(handle_type h) {
99 return h != GetInvalid();
100 }
101};
102
103struct JobHandleTraits : CommonHandleTraits {
104 static handle_type GetInvalid() {
105 return NULL;
106 }
107};
108
109struct CryptContextTraits : CommonHandleTraits {
110 typedef HCRYPTPROV handle_type;
111
112 static handle_type GetInvalid() {
113 return 0;
114 }
115
116 static void Close(handle_type h) {
117 ::CryptReleaseContext(h, 0);
118 }
119
120 static bool IsValid(handle_type h) {
121 return h != GetInvalid();
122 }
123};
124
125struct FindHandleTraits : CommonHandleTraits {
126 static void Close(handle_type h) {
127 ::FindClose(h);
128 }
129};
130
131struct FileHandleTraits : CommonHandleTraits {};
132
133typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
134typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
135typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
136typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
137typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer58fe86d2010-12-09 17:37:18 +0000138
139namespace llvm {
140template <class T>
141class SmallVectorImpl;
142
143template <class T>
144typename SmallVectorImpl<T>::const_pointer
145c_str(SmallVectorImpl<T> &str) {
146 str.push_back(0);
147 str.pop_back();
148 return str.data();
149}
150} // end namespace llvm.