2
0

WindowsSupport.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. //=== WARNING: Implementation here must contain only generic Win32 code that
  18. //=== is guaranteed to work on *all* Win32 variants.
  19. // //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
  22. #define LLVM_SUPPORT_WINDOWSSUPPORT_H
  23. // mingw-w64 tends to define it as 0x0502 in its headers.
  24. #undef _WIN32_WINNT
  25. #undef _WIN32_IE
  26. // Require at least Windows XP(5.1) API.
  27. #define _WIN32_WINNT 0x0501
  28. #define _WIN32_IE 0x0600 // MinGW at it again.
  29. #define WIN32_LEAN_AND_MEAN
  30. #include "llvm/ADT/SmallVector.h"
  31. #include "llvm/ADT/StringRef.h"
  32. #include "llvm/ADT/Twine.h"
  33. #include "llvm/Config/config.h" // Get build system configuration settings
  34. #include "llvm/Support/Compiler.h"
  35. #include <system_error>
  36. #include <windows.h>
  37. #include <wincrypt.h>
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include "llvm/Support/FileSystem.h"
  42. #include "llvm/Support/MSFileSystem.h"
  43. inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
  44. if (!ErrMsg)
  45. return true;
  46. char *buffer = NULL;
  47. DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  48. FORMAT_MESSAGE_FROM_SYSTEM,
  49. NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
  50. if (R)
  51. *ErrMsg = prefix + buffer;
  52. else
  53. *ErrMsg = prefix + "Unknown error";
  54. LocalFree(buffer);
  55. return R != 0;
  56. }
  57. template <typename HandleTraits>
  58. class ScopedHandle {
  59. typedef typename HandleTraits::handle_type handle_type;
  60. handle_type Handle;
  61. ScopedHandle(const ScopedHandle &other); // = delete;
  62. void operator=(const ScopedHandle &other); // = delete;
  63. public:
  64. ScopedHandle()
  65. : Handle(HandleTraits::GetInvalid()) {}
  66. explicit ScopedHandle(handle_type h)
  67. : Handle(h) {}
  68. ~ScopedHandle() {
  69. if (HandleTraits::IsValid(Handle))
  70. HandleTraits::Close(Handle);
  71. }
  72. handle_type take() {
  73. handle_type t = Handle;
  74. Handle = HandleTraits::GetInvalid();
  75. return t;
  76. }
  77. ScopedHandle &operator=(handle_type h) {
  78. if (HandleTraits::IsValid(Handle))
  79. HandleTraits::Close(Handle);
  80. Handle = h;
  81. return *this;
  82. }
  83. // True if Handle is valid.
  84. explicit operator bool() const {
  85. return HandleTraits::IsValid(Handle) ? true : false;
  86. }
  87. operator handle_type() const {
  88. return Handle;
  89. }
  90. };
  91. struct CommonHandleTraits {
  92. typedef HANDLE handle_type;
  93. static handle_type GetInvalid() {
  94. return INVALID_HANDLE_VALUE;
  95. }
  96. static void Close(handle_type h) {
  97. ::llvm::sys::fs::GetCurrentThreadFileSystem()->CloseHandle(h);
  98. }
  99. static bool IsValid(handle_type h) {
  100. return h != GetInvalid();
  101. }
  102. };
  103. struct JobHandleTraits : CommonHandleTraits {
  104. static handle_type GetInvalid() {
  105. return NULL;
  106. }
  107. };
  108. struct CryptContextTraits : CommonHandleTraits {
  109. typedef HCRYPTPROV handle_type;
  110. static handle_type GetInvalid() {
  111. return 0;
  112. }
  113. static void Close(handle_type h) {
  114. ::CryptReleaseContext(h, 0);
  115. }
  116. static bool IsValid(handle_type h) {
  117. return h != GetInvalid();
  118. }
  119. };
  120. struct FindHandleTraits : CommonHandleTraits {
  121. static void Close(handle_type h) {
  122. ::llvm::sys::fs::GetCurrentThreadFileSystem()->FindClose(h);
  123. }
  124. };
  125. struct FileHandleTraits : CommonHandleTraits {};
  126. typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
  127. typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
  128. typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
  129. typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
  130. typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
  131. namespace llvm {
  132. template <class T>
  133. class SmallVectorImpl;
  134. template <class T>
  135. typename SmallVectorImpl<T>::const_pointer
  136. c_str(SmallVectorImpl<T> &str) {
  137. str.push_back(0);
  138. str.pop_back();
  139. return str.data();
  140. }
  141. namespace sys {
  142. namespace path {
  143. std::error_code widenPath(const Twine &Path8,
  144. SmallVectorImpl<wchar_t> &Path16);
  145. } // end namespace path
  146. namespace windows {
  147. std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
  148. std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
  149. SmallVectorImpl<char> &utf8);
  150. /// Convert from UTF16 to the current code page used in the system
  151. std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
  152. SmallVectorImpl<char> &utf8);
  153. std::error_code ACPToUTF8(const char *acp, size_t acp_len,
  154. SmallVectorImpl<char> &utf8);
  155. } // end namespace windows
  156. } // end namespace sys
  157. } // end namespace llvm.
  158. #endif