Skip to content

Commit cccfbf4

Browse files
tinysun212jrose-apple
authored andcommitted
[swiftc/msvc] Compiling with MSVC (#1516)
Some modifications for the ms-extension option of the clang.exe in the Visual Studio 2015 development environment This patch is only for swiftc.exe. I used the library set of Visual Studio 2015 Update 1 and recent version of swift-clang as the compiler. If you are using the real MSVC compiler, more patch might be required.
1 parent d43e5e3 commit cccfbf4

File tree

8 files changed

+67
-18
lines changed

8 files changed

+67
-18
lines changed

include/swift/Basic/JSONSerialization.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,16 @@ struct ScalarTraits<uint32_t> {
481481
static bool mustQuote(StringRef) { return false; }
482482
};
483483

484+
#if defined(_MSC_VER)
485+
// In MSVC, 'unsigned long' is 32bit size and different from uint32_t,
486+
// and it is used to define swift::sys::ProcessId.
487+
template<>
488+
struct ScalarTraits<unsigned long> {
489+
static void output(const unsigned long &, llvm::raw_ostream &);
490+
static bool mustQuote(StringRef) { return false; }
491+
};
492+
#endif
493+
484494
template<>
485495
struct ScalarTraits<uint64_t> {
486496
static void output(const uint64_t &, llvm::raw_ostream &);

include/swift/Basic/Malloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
#define SWIFT_BASIC_MALLOC_H
2020

2121
#include <cassert>
22+
#if defined(_MSC_VER)
23+
#include <malloc.h>
24+
#else
2225
#include <cstdlib>
26+
#endif
2327

2428
namespace swift {
2529

include/swift/IDE/CommentConversion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Basic/LLVM.h"
1717
#include <memory>
18+
#include <string>
1819

1920
namespace swift {
2021
class Decl;

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,23 @@ class CastOptimizer {
485485

486486
public:
487487
CastOptimizer(std::function<void (SILInstruction *I, ValueBase *V)> ReplaceInstUsesAction,
488-
std::function<void (SILInstruction *)> EraseAction = [](SILInstruction*){},
489-
std::function<void ()> WillSucceedAction = [](){},
488+
std::function<void (SILInstruction *)> EraseAction,
489+
std::function<void ()> WillSucceedAction,
490490
std::function<void ()> WillFailAction = [](){})
491491
: ReplaceInstUsesAction(ReplaceInstUsesAction),
492492
EraseInstAction(EraseAction),
493493
WillSucceedAction(WillSucceedAction),
494494
WillFailAction(WillFailAction) {}
495495

496+
// This constructor is used in
497+
// 'SILOptimizer/Mandatory/ConstantPropagation.cpp'. MSVC2015 compiler
498+
// couldn't use the single constructor version which has three default
499+
// arguments. It seems the number of the default argument with lambda is
500+
// limited.
501+
CastOptimizer(std::function<void (SILInstruction *I, ValueBase *V)> ReplaceInstUsesAction,
502+
std::function<void (SILInstruction *)> EraseAction = [](SILInstruction*){})
503+
: CastOptimizer(ReplaceInstUsesAction, EraseAction, [](){}, [](){}) {}
504+
496505
/// Simplify checked_cast_br. It may change the control flow.
497506
SILInstruction *
498507
simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst);

lib/Basic/JSONSerialization.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ void ScalarTraits<uint32_t>::output(const uint32_t &Val,
261261
Out << Val;
262262
}
263263

264+
#if defined(_MSC_VER)
265+
void ScalarTraits<unsigned long>::output(const unsigned long &Val,
266+
raw_ostream &Out) {
267+
Out << Val;
268+
}
269+
#endif
270+
264271
void ScalarTraits<uint64_t>::output(const uint64_t &Val,
265272
raw_ostream &Out) {
266273
Out << Val;

lib/Driver/Compilation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,16 @@ int Compilation::performSingleCommand(const Job *Cmd) {
631631
const char *ExecPath = Cmd->getExecutable();
632632
const char **argv = Argv.data();
633633

634-
for (auto &envPair : Cmd->getExtraEnvironment())
634+
for (auto &envPair : Cmd->getExtraEnvironment()) {
635+
#if defined(_MSC_VER)
636+
llvm::SmallString<256> envStr = StringRef(envPair.first);
637+
envStr.append(StringRef("="));
638+
envStr.append(StringRef(envPair.second));
639+
_putenv(envStr.c_str());
640+
#else
635641
setenv(envPair.first, envPair.second, /*replacing=*/true);
642+
#endif
643+
}
636644

637645
return ExecuteInPlace(ExecPath, argv);
638646
}

lib/Immediate/Immediate.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,27 @@
3737
#include "llvm/Transforms/IPO.h"
3838
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
3939
#include "llvm/Support/Path.h"
40-
40+
#if defined(_MSC_VER)
41+
#include "Windows.h"
42+
#else
4143
#include <dlfcn.h>
42-
44+
#endif
4345
using namespace swift;
4446
using namespace swift::immediate;
4547

48+
static bool loadRuntimeLib(StringRef runtimeLibPathWithName) {
49+
#if defined(_MSC_VER)
50+
return LoadLibrary(runtimeLibPathWithName.str().c_str());
51+
#else
52+
return dlopen(runtimeLibPathWithName.str().c_str(), RTLD_LAZY | RTLD_GLOBAL);
53+
#endif
54+
}
55+
4656
static bool loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
4757
// FIXME: Need error-checking.
4858
llvm::SmallString<128> Path = runtimeLibPath;
4959
llvm::sys::path::append(Path, sharedLibName);
50-
return dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
60+
return loadRuntimeLib(Path);
5161
}
5262

5363
bool swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath) {
@@ -60,7 +70,7 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
6070

6171
// If we have an absolute or relative path, just try to load it now.
6272
if (llvm::sys::path::has_parent_path(path.str())) {
63-
return dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
73+
return loadRuntimeLib(path);
6474
}
6575

6676
bool success = false;
@@ -80,14 +90,14 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
8090
for (auto &libDir : searchPathOpts.LibrarySearchPaths) {
8191
path = libDir;
8292
llvm::sys::path::append(path, stem.str());
83-
success = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
93+
success = loadRuntimeLib(path);
8494
if (success)
8595
break;
8696
}
8797

88-
// Let dlopen determine the best search paths.
98+
// Let loadRuntimeLib determine the best search paths.
8999
if (!success)
90-
success = dlopen(stem.c_str(), RTLD_LAZY | RTLD_GLOBAL);
100+
success = loadRuntimeLib(stem);
91101

92102
// If that fails, try our runtime library path.
93103
if (!success)
@@ -106,14 +116,14 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
106116
for (auto &frameworkDir : searchPathOpts.FrameworkSearchPaths) {
107117
path = frameworkDir;
108118
llvm::sys::path::append(path, frameworkPart.str());
109-
success = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
119+
success = loadRuntimeLib(path);
110120
if (success)
111121
break;
112122
}
113123

114-
// If that fails, let dlopen search for system frameworks.
124+
// If that fails, let loadRuntimeLib search for system frameworks.
115125
if (!success)
116-
success = dlopen(frameworkPart.c_str(), RTLD_LAZY | RTLD_GLOBAL);
126+
success = loadRuntimeLib(frameworkPart);
117127
break;
118128
}
119129
}

lib/Sema/CSDiag.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,9 @@ namespace {
879879
unsigned level,
880880
ArrayRef<CallArgParam> actualArgs);
881881

882-
void filterList(ArrayRef<CallArgParam> actualArgs);
882+
void filterListArgs(ArrayRef<CallArgParam> actualArgs);
883883
void filterList(Type actualArgsType) {
884-
return filterList(decomposeArgType(actualArgsType));
884+
return filterListArgs(decomposeArgType(actualArgsType));
885885
}
886886
void filterList(ClosenessPredicate predicate);
887887
void filterContextualMemberList(Expr *argExpr);
@@ -1483,7 +1483,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn) {
14831483
/// After the candidate list is formed, it can be filtered down to discard
14841484
/// obviously mismatching candidates and compute a "closeness" for the
14851485
/// resultant set.
1486-
void CalleeCandidateInfo::filterList(ArrayRef<CallArgParam> actualArgs) {
1486+
void CalleeCandidateInfo::filterListArgs(ArrayRef<CallArgParam> actualArgs) {
14871487
// Now that we have the candidate list, figure out what the best matches from
14881488
// the candidate list are, and remove all the ones that aren't at that level.
14891489
filterList([&](UncurriedCandidate candidate) -> ClosenessResultTy {
@@ -1530,7 +1530,7 @@ void CalleeCandidateInfo::filterContextualMemberList(Expr *argExpr) {
15301530

15311531
CallArgParam param;
15321532
param.Ty = argType;
1533-
return filterList(param);
1533+
return filterListArgs(param);
15341534
}
15351535

15361536
// If we have a tuple expression, form a tuple type.
@@ -1547,7 +1547,7 @@ void CalleeCandidateInfo::filterContextualMemberList(Expr *argExpr) {
15471547
ArgElts.push_back(param);
15481548
}
15491549

1550-
return filterList(ArgElts);
1550+
return filterListArgs(ArgElts);
15511551
}
15521552

15531553
CalleeCandidateInfo::CalleeCandidateInfo(Type baseType,

0 commit comments

Comments
 (0)