Skip to content

Commit a184057

Browse files
authored
Merge pull request #5949 from hughbe/basic-win-msvc
Port swift/basic to Windows using MSVC
2 parents 63db004 + 50e94af commit a184057

File tree

7 files changed

+104
-33
lines changed

7 files changed

+104
-33
lines changed

include/swift/Basic/EncodedSequence.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,20 @@ class EncodedSequence : public EncodedSequenceBase {
337337
/// see the documentation there for information about how to use this
338338
/// data structure.
339339
template <class ValueType> class Map {
340+
// Hack: MSVC isn't able to resolve the InlineKeyCapacity part of the
341+
// template of PrefixMap, so we have to split it up and pass it manually.
342+
#if defined(_MSC_VER)
343+
static const size_t Size = (sizeof(void*) - 1) / sizeof(Chunk);
344+
static const size_t ActualSize = max<size_t>(Size, 1);
345+
346+
using MapBase = PrefixMap<Chunk, ValueType, ActualSize>;
347+
#else
340348
using MapBase = PrefixMap<Chunk, ValueType>;
349+
#endif
341350
MapBase TheMap;
342351

343352
public:
344-
using SequenceIterator = EncodedSequence::iterator;
353+
using SequenceIterator = typename EncodedSequence::iterator;
345354
using KeyType = typename MapBase::KeyType;
346355
using Handle = typename MapBase::Handle;
347356

include/swift/Basic/ImmutablePointerSet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ template <typename T> class ImmutablePointerSet : public llvm::FoldingSetNode {
112112
return *LowerBound == Ptr;
113113
}
114114

115-
using iterator = typename decltype(Data)::iterator;
115+
using iterator = typename ArrayRef<PtrTy>::iterator;
116116
iterator begin() const { return Data.begin(); }
117117
iterator end() const { return Data.end(); }
118118

include/swift/Basic/type_traits.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace swift {
2929
/// is not intended to be specialized.
3030
template<typename T>
3131
struct IsTriviallyCopyable {
32-
#if _LIBCPP_VERSION
32+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
3333
// libc++ implements it.
3434
static const bool value = std::is_trivially_copyable<T>::value;
3535
#elif __has_feature(is_trivially_copyable)
@@ -41,7 +41,7 @@ struct IsTriviallyCopyable {
4141

4242
template<typename T>
4343
struct IsTriviallyConstructible {
44-
#if _LIBCPP_VERSION
44+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
4545
// libc++ implements it.
4646
static const bool value = std::is_trivially_constructible<T>::value;
4747
#elif __has_feature(has_trivial_constructor)
@@ -53,7 +53,7 @@ struct IsTriviallyConstructible {
5353

5454
template<typename T>
5555
struct IsTriviallyDestructible {
56-
#if _LIBCPP_VERSION
56+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
5757
// libc++ implements it.
5858
static const bool value = std::is_trivially_destructible<T>::value;
5959
#elif __has_feature(has_trivial_destructor)

lib/Basic/CMakeLists.txt

+12-9
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,27 @@ set(get_svn_script "${LLVM_MAIN_SRC_DIR}/cmake/modules/GetSVN.cmake")
3131

3232
function(generate_revision_inc revision_inc_var name dir)
3333
find_first_existing_vc_file(dep_file "${dir}")
34+
# Create custom target to generate the VC revision include.
35+
set(revision_inc "${CMAKE_CURRENT_BINARY_DIR}/${name}Revision.inc")
36+
string(TOUPPER ${name} upper_name)
3437
if(DEFINED dep_file)
35-
# Create custom target to generate the VC revision include.
36-
set(revision_inc "${CMAKE_CURRENT_BINARY_DIR}/${name}Revision.inc")
37-
string(TOUPPER ${name} upper_name)
3838
add_custom_command(OUTPUT "${revision_inc}"
3939
DEPENDS "${dep_file}" "${get_svn_script}"
4040
COMMAND
4141
${CMAKE_COMMAND} "-DFIRST_SOURCE_DIR=${dir}"
4242
"-DFIRST_NAME=${upper_name}"
4343
"-DHEADER_FILE=${revision_inc}"
4444
-P "${get_svn_script}")
45-
46-
# Mark the generated header as being generated.
47-
set_source_files_properties("${revision_inc}"
48-
PROPERTIES GENERATED TRUE
49-
HEADER_FILE_ONLY TRUE)
50-
set(${revision_inc_var} ${revision_inc} PARENT_SCOPE)
45+
else()
46+
# Generate an empty Revision.inc file if we are not using git or SVN.
47+
file(WRITE "${revision_inc}" "")
5148
endif()
49+
50+
# Mark the generated header as being generated.
51+
set_source_files_properties("${revision_inc}"
52+
PROPERTIES GENERATED TRUE
53+
HEADER_FILE_ONLY TRUE)
54+
set(${revision_inc_var} ${revision_inc} PARENT_SCOPE)
5255
endfunction()
5356

5457
generate_revision_inc(llvm_revision_inc LLVM "${LLVM_MAIN_SRC_DIR}")

lib/Basic/SourceLoc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void SourceManager::verifyAllBuffers() const {
2424
};
2525

2626
// FIXME: This depends on the buffer IDs chosen by llvm::SourceMgr.
27-
__attribute__((used)) static char arbitraryTotal = 0;
27+
LLVM_ATTRIBUTE_USED static char arbitraryTotal = 0;
2828
for (unsigned i = 1, e = LLVMSourceMgr.getNumBuffers(); i <= e; ++i) {
2929
auto *buffer = LLVMSourceMgr.getMemoryBuffer(i);
3030
if (buffer->getBufferSize() == 0)

lib/Basic/UUID.cpp

+73-8
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,105 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18-
#include <uuid/uuid.h>
1918
#include "swift/Basic/UUID.h"
2019

20+
// WIN32 doesn't natively support <uuid/uuid.h>. Instead, we use Win32 APIs.
21+
#if defined(_WIN32)
22+
#define WIN32_LEAN_AND_MEAN
23+
#include <rpc.h>
24+
#include <string>
25+
#else
26+
#include <uuid/uuid.h>
27+
#endif
28+
2129
using namespace swift;
2230

23-
UUID::UUID(FromRandom_t) {
31+
swift::UUID::UUID(FromRandom_t) {
32+
#if defined(_WIN32)
33+
::UUID uuid;
34+
UuidCreate(&uuid);
35+
36+
memcpy(Value, &uuid, Size);
37+
#else
2438
uuid_generate_random(Value);
39+
#endif
2540
}
2641

27-
UUID::UUID(FromTime_t) {
42+
swift::UUID::UUID(FromTime_t) {
43+
#if defined(_WIN32)
44+
::UUID uuid;
45+
UuidCreate(&uuid);
46+
47+
memcpy(Value, &uuid, Size);
48+
#else
2849
uuid_generate_time(Value);
50+
#endif
2951
}
3052

31-
UUID::UUID() {
53+
swift::UUID::UUID() {
54+
#if defined(_WIN32)
55+
::UUID uuid = *((::UUID *)&Value);
56+
UuidCreateNil(&uuid);
57+
58+
memcpy(Value, &uuid, Size);
59+
#else
3260
uuid_clear(Value);
61+
#endif
3362
}
3463

35-
Optional<UUID> UUID::fromString(const char *s) {
36-
UUID result;
64+
Optional<swift::UUID> swift::UUID::fromString(const char *s) {
65+
#if defined(_WIN32)
66+
RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s));
67+
68+
::UUID uuid;
69+
RPC_STATUS status = UuidFromStringA(t, &uuid);
70+
if (status == RPC_S_INVALID_STRING_UUID) {
71+
return None;
72+
}
73+
74+
swift::UUID result = UUID();
75+
memcpy(result.Value, &uuid, Size);
76+
return result;
77+
#else
78+
swift::UUID result;
3779
if (uuid_parse(s, result.Value))
3880
return None;
3981
return result;
82+
#endif
4083
}
4184

42-
void UUID::toString(llvm::SmallVectorImpl<char> &out) const {
85+
void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
4386
out.resize(UUID::StringBufferSize);
87+
#if defined(_WIN32)
88+
::UUID uuid;
89+
memcpy(&uuid, Value, Size);
90+
91+
RPC_CSTR str;
92+
UuidToStringA(&uuid, &str);
93+
94+
char* signedStr = reinterpret_cast<char*>(str);
95+
memcpy(out.data(), signedStr, StringBufferSize);
96+
#else
4497
uuid_unparse_upper(Value, out.data());
98+
#endif
4599
// Pop off the null terminator.
46100
assert(out.back() == '\0' && "did not null-terminate?!");
47101
out.pop_back();
48102
}
49103

50-
int UUID::compare(UUID y) const {
104+
int swift::UUID::compare(UUID y) const {
105+
#if defined(_WIN32)
106+
RPC_STATUS s;
107+
::UUID uuid1;
108+
memcpy(&uuid1, Value, Size);
109+
110+
::UUID uuid2;
111+
memcpy(&uuid2, y.Value, Size);
112+
113+
return UuidCompare(&uuid1, &uuid2, &s);
114+
#else
51115
return uuid_compare(Value, y.Value);
116+
#endif
52117
}
53118

54119
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os, UUID uuid) {

lib/Basic/Version.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,9 @@
4343
SWIFT_MAKE_VERSION_STRING(SWIFT_VERSION_MAJOR, SWIFT_VERSION_MINOR)
4444
#endif
4545

46-
#if __has_include("LLVMRevision.inc")
47-
# include "LLVMRevision.inc"
48-
#endif
49-
#if __has_include("ClangRevision.inc")
50-
# include "ClangRevision.inc"
51-
#endif
52-
#if __has_include("SwiftRevision.inc")
53-
# include "SwiftRevision.inc"
54-
#endif
46+
#include "LLVMRevision.inc"
47+
#include "ClangRevision.inc"
48+
#include "SwiftRevision.inc"
5549

5650
namespace swift {
5751
namespace version {
@@ -401,7 +395,7 @@ std::string getSwiftFullVersion(Version effectiveVersion) {
401395
#endif
402396

403397
// Suppress unused function warning
404-
(void) printFullRevisionString;
398+
(void)&printFullRevisionString;
405399

406400
return OS.str();
407401
}

0 commit comments

Comments
 (0)