LLVM  3.7.0
WindowsSupport.h
Go to the documentation of this file.
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 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24 
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
28 
29 // Require at least Windows XP(5.1) API.
30 #define _WIN32_WINNT 0x0501
31 #define _WIN32_IE 0x0600 // MinGW at it again.
32 #define WIN32_LEAN_AND_MEAN
33 
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/ADT/Twine.h"
37 #include "llvm/Config/config.h" // Get build system configuration settings
38 #include "llvm/Support/Compiler.h"
39 #include <system_error>
40 #include <windows.h>
41 #include <wincrypt.h>
42 #include <cassert>
43 #include <string>
44 #include <vector>
45 
46 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
47  if (!ErrMsg)
48  return true;
49  char *buffer = NULL;
50  DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
51  FORMAT_MESSAGE_FROM_SYSTEM,
52  NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
53  if (R)
54  *ErrMsg = prefix + buffer;
55  else
56  *ErrMsg = prefix + "Unknown error";
57 
58  LocalFree(buffer);
59  return R != 0;
60 }
61 
62 template <typename HandleTraits>
63 class ScopedHandle {
64  typedef typename HandleTraits::handle_type handle_type;
65  handle_type Handle;
66 
67  ScopedHandle(const ScopedHandle &other); // = delete;
68  void operator=(const ScopedHandle &other); // = delete;
69 public:
71  : Handle(HandleTraits::GetInvalid()) {}
72 
73  explicit ScopedHandle(handle_type h)
74  : Handle(h) {}
75 
77  if (HandleTraits::IsValid(Handle))
78  HandleTraits::Close(Handle);
79  }
80 
81  handle_type take() {
82  handle_type t = Handle;
83  Handle = HandleTraits::GetInvalid();
84  return t;
85  }
86 
87  ScopedHandle &operator=(handle_type h) {
88  if (HandleTraits::IsValid(Handle))
89  HandleTraits::Close(Handle);
90  Handle = h;
91  return *this;
92  }
93 
94  // True if Handle is valid.
95  explicit operator bool() const {
96  return HandleTraits::IsValid(Handle) ? true : false;
97  }
98 
99  operator handle_type() const {
100  return Handle;
101  }
102 };
103 
105  typedef HANDLE handle_type;
106 
108  return INVALID_HANDLE_VALUE;
109  }
110 
111  static void Close(handle_type h) {
112  ::CloseHandle(h);
113  }
114 
115  static bool IsValid(handle_type h) {
116  return h != GetInvalid();
117  }
118 };
119 
122  return NULL;
123  }
124 };
125 
127  typedef HCRYPTPROV handle_type;
128 
130  return 0;
131  }
132 
133  static void Close(handle_type h) {
134  ::CryptReleaseContext(h, 0);
135  }
136 
137  static bool IsValid(handle_type h) {
138  return h != GetInvalid();
139  }
140 };
141 
143  static void Close(handle_type h) {
144  ::FindClose(h);
145  }
146 };
147 
149 
155 
156 namespace llvm {
157 template <class T>
158 class SmallVectorImpl;
159 
160 template <class T>
161 typename SmallVectorImpl<T>::const_pointer
163  str.push_back(0);
164  str.pop_back();
165  return str.data();
166 }
167 
168 namespace sys {
169 namespace path {
170 std::error_code widenPath(const Twine &Path8,
171  SmallVectorImpl<wchar_t> &Path16);
172 } // end namespace path
173 
174 namespace windows {
175 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
176 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
177  SmallVectorImpl<char> &utf8);
178 /// Convert from UTF16 to the current code page used in the system
179 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
180  SmallVectorImpl<char> &utf8);
181 } // end namespace windows
182 } // end namespace sys
183 } // end namespace llvm.
184 
185 #endif
static handle_type GetInvalid()
static void Close(handle_type h)
static bool IsValid(handle_type h)
static bool IsValid(handle_type h)
ScopedHandle< JobHandleTraits > ScopedJobHandle
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
static void Close(handle_type h)
static handle_type GetInvalid()
ScopedHandle(handle_type h)
HCRYPTPROV handle_type
std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl< wchar_t > &utf16)
ScopedHandle< FindHandleTraits > ScopedFindHandle
std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
Convert from UTF16 to the current code page used in the system.
handle_type take()
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
ScopedHandle & operator=(handle_type h)
ScopedHandle< FileHandleTraits > ScopedFileHandle
static handle_type GetInvalid()
static void Close(handle_type h)
ScopedHandle< CryptContextTraits > ScopedCryptContext
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
std::error_code widenPath(const Twine &Path8, SmallVectorImpl< wchar_t > &Path16)