2004-09-15 05:48:11 +00:00
|
|
|
//===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-09-15 05:48:11 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-09-15 05:48:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2005-07-13 02:15:18 +00:00
|
|
|
// This file defines things specific to Win32 implementations.
|
2004-09-15 05:48:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2005-07-13 02:15:18 +00:00
|
|
|
//=== WARNING: Implementation here must contain only generic Win32 code that
|
|
|
|
//=== is guaranteed to work on *all* Win32 variants.
|
2004-09-15 05:48:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-09 04:18:30 +00:00
|
|
|
// mingw-w64 tends to define it as 0x0502 in its headers.
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
|
2011-08-20 06:35:31 +00:00
|
|
|
// Require at least Windows XP(5.1) API.
|
|
|
|
#define _WIN32_WINNT 0x0501
|
2011-08-23 03:49:11 +00:00
|
|
|
#define _WIN32_IE 0x0600 // MinGW at it again.
|
2010-11-09 15:11:07 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2005-07-13 02:15:18 +00:00
|
|
|
|
2010-11-09 15:10:29 +00:00
|
|
|
#include "llvm/Config/config.h" // Get build system configuration settings
|
2013-05-15 09:00:30 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2011-02-04 12:53:04 +00:00
|
|
|
#include <windows.h>
|
2011-12-12 06:03:33 +00:00
|
|
|
#include <wincrypt.h>
|
2011-02-04 12:53:04 +00:00
|
|
|
#include <shlobj.h>
|
2004-09-15 05:48:11 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
|
2006-08-23 07:30:48 +00:00
|
|
|
inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (!ErrMsg)
|
2006-08-23 07:30:48 +00:00
|
|
|
return true;
|
2006-08-21 06:02:44 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
|
2006-09-01 20:35:17 +00:00
|
|
|
*ErrMsg = prefix + buffer;
|
2006-08-21 06:02:44 +00:00
|
|
|
LocalFree(buffer);
|
2006-08-23 07:30:48 +00:00
|
|
|
return true;
|
2006-08-21 06:02:44 +00:00
|
|
|
}
|
2007-03-05 05:22:08 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
template <typename HandleTraits>
|
|
|
|
class ScopedHandle {
|
|
|
|
typedef typename HandleTraits::handle_type handle_type;
|
|
|
|
handle_type Handle;
|
2007-03-05 05:22:08 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
ScopedHandle(const ScopedHandle &other); // = delete;
|
|
|
|
void operator=(const ScopedHandle &other); // = delete;
|
2007-03-05 05:22:08 +00:00
|
|
|
public:
|
2011-12-12 06:03:33 +00:00
|
|
|
ScopedHandle()
|
|
|
|
: Handle(HandleTraits::GetInvalid()) {}
|
|
|
|
|
|
|
|
explicit ScopedHandle(handle_type h)
|
|
|
|
: Handle(h) {}
|
2007-03-05 05:22:08 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
~ScopedHandle() {
|
|
|
|
if (HandleTraits::IsValid(Handle))
|
|
|
|
HandleTraits::Close(Handle);
|
2007-03-05 05:22:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
handle_type take() {
|
|
|
|
handle_type t = Handle;
|
|
|
|
Handle = HandleTraits::GetInvalid();
|
|
|
|
return t;
|
2007-03-05 05:22:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
ScopedHandle &operator=(handle_type h) {
|
|
|
|
if (HandleTraits::IsValid(Handle))
|
|
|
|
HandleTraits::Close(Handle);
|
|
|
|
Handle = h;
|
2007-03-05 05:22:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2011-12-12 06:03:33 +00:00
|
|
|
|
|
|
|
// True if Handle is valid.
|
2013-05-15 07:36:59 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const {
|
2011-12-12 06:03:33 +00:00
|
|
|
return HandleTraits::IsValid(Handle) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator handle_type() const {
|
|
|
|
return Handle;
|
|
|
|
}
|
2007-03-05 05:22:08 +00:00
|
|
|
};
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
struct CommonHandleTraits {
|
|
|
|
typedef HANDLE handle_type;
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
static handle_type GetInvalid() {
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
static void Close(handle_type h) {
|
|
|
|
::CloseHandle(h);
|
2010-12-06 04:28:13 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
static bool IsValid(handle_type h) {
|
|
|
|
return h != GetInvalid();
|
2010-12-06 04:28:13 +00:00
|
|
|
}
|
2011-12-12 06:03:33 +00:00
|
|
|
};
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
struct JobHandleTraits : CommonHandleTraits {
|
|
|
|
static handle_type GetInvalid() {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
struct CryptContextTraits : CommonHandleTraits {
|
|
|
|
typedef HCRYPTPROV handle_type;
|
|
|
|
|
|
|
|
static handle_type GetInvalid() {
|
|
|
|
return 0;
|
2010-12-06 04:28:13 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
static void Close(handle_type h) {
|
|
|
|
::CryptReleaseContext(h, 0);
|
|
|
|
}
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
static bool IsValid(handle_type h) {
|
|
|
|
return h != GetInvalid();
|
2010-12-06 04:28:13 +00:00
|
|
|
}
|
2011-12-12 06:03:33 +00:00
|
|
|
};
|
2010-12-06 04:28:13 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
struct FindHandleTraits : CommonHandleTraits {
|
|
|
|
static void Close(handle_type h) {
|
|
|
|
::FindClose(h);
|
2010-12-06 04:28:42 +00:00
|
|
|
}
|
2010-12-06 04:28:13 +00:00
|
|
|
};
|
2010-12-06 04:28:42 +00:00
|
|
|
|
2011-12-12 06:03:33 +00:00
|
|
|
struct FileHandleTraits : CommonHandleTraits {};
|
|
|
|
|
|
|
|
typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
|
|
|
|
typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
|
|
|
|
typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
|
|
|
|
typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
|
|
|
|
typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
|
2010-12-09 17:37:18 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template <class T>
|
|
|
|
class SmallVectorImpl;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename SmallVectorImpl<T>::const_pointer
|
|
|
|
c_str(SmallVectorImpl<T> &str) {
|
|
|
|
str.push_back(0);
|
|
|
|
str.pop_back();
|
|
|
|
return str.data();
|
|
|
|
}
|
|
|
|
} // end namespace llvm.
|