blob: 4cdac788a0ed4ff872b7145a503a1582ad203ee3 [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
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000027#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000028#include "llvm/Support/Compiler.h"
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000029#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000030#include <wincrypt.h>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000031#include <shlobj.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000032#include <cassert>
33#include <string>
34
Reid Spencer879ed5a2006-08-23 07:30:48 +000035inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000036 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000037 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000038 char *buffer = NULL;
39 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
40 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +000041 *ErrMsg = prefix + buffer;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000042 LocalFree(buffer);
Reid Spencer879ed5a2006-08-23 07:30:48 +000043 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000044}
Jeff Cohena531d042007-03-05 05:22:08 +000045
Michael J. Spencer513f1b62011-12-12 06:03:33 +000046template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000047class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000048 typedef typename HandleTraits::handle_type handle_type;
49 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000050
Michael J. Spencer513f1b62011-12-12 06:03:33 +000051 ScopedHandle(const ScopedHandle &other); // = delete;
52 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000053public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000054 ScopedHandle()
55 : Handle(HandleTraits::GetInvalid()) {}
56
57 explicit ScopedHandle(handle_type h)
58 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000059
60 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000061 if (HandleTraits::IsValid(Handle))
62 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000063 }
64
Michael J. Spencer513f1b62011-12-12 06:03:33 +000065 handle_type take() {
66 handle_type t = Handle;
67 Handle = HandleTraits::GetInvalid();
68 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000069 }
70
Michael J. Spencer513f1b62011-12-12 06:03:33 +000071 ScopedHandle &operator=(handle_type h) {
72 if (HandleTraits::IsValid(Handle))
73 HandleTraits::Close(Handle);
74 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000075 return *this;
76 }
77
Michael J. Spencer39c46212010-12-06 04:28:13 +000078 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000079 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000080 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000081 }
82
Michael J. Spencer513f1b62011-12-12 06:03:33 +000083 operator handle_type() const {
84 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000085 }
Michael J. Spencer39c46212010-12-06 04:28:13 +000086};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000087
Michael J. Spencer513f1b62011-12-12 06:03:33 +000088struct CommonHandleTraits {
89 typedef HANDLE handle_type;
90
91 static handle_type GetInvalid() {
92 return INVALID_HANDLE_VALUE;
93 }
94
95 static void Close(handle_type h) {
96 ::CloseHandle(h);
97 }
98
99 static bool IsValid(handle_type h) {
100 return h != GetInvalid();
101 }
102};
103
104struct JobHandleTraits : CommonHandleTraits {
105 static handle_type GetInvalid() {
106 return NULL;
107 }
108};
109
110struct CryptContextTraits : CommonHandleTraits {
111 typedef HCRYPTPROV handle_type;
112
113 static handle_type GetInvalid() {
114 return 0;
115 }
116
117 static void Close(handle_type h) {
118 ::CryptReleaseContext(h, 0);
119 }
120
121 static bool IsValid(handle_type h) {
122 return h != GetInvalid();
123 }
124};
125
126struct FindHandleTraits : CommonHandleTraits {
127 static void Close(handle_type h) {
128 ::FindClose(h);
129 }
130};
131
132struct FileHandleTraits : CommonHandleTraits {};
133
134typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
135typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
136typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
137typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
138typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000139
140namespace llvm {
141template <class T>
142class SmallVectorImpl;
143
144template <class T>
145typename SmallVectorImpl<T>::const_pointer
146c_str(SmallVectorImpl<T> &str) {
147 str.push_back(0);
148 str.pop_back();
149 return str.data();
150}
151} // end namespace llvm.