1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
//===--- SwiftCompile.cpp -------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "SwiftEditorDiagConsumer.h"
#include "SwiftLangSupport.h"
#include "SourceKit/Support/FileSystemProvider.h"
#include "swift/IDETool/CompileInstance.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace SourceKit;
using namespace swift;
std::shared_ptr<compile::Session>
compile::SessionManager::getSession(StringRef name) {
llvm::sys::ScopedLock lock(mtx);
auto i = sessions.find(name);
if (i != sessions.end()) {
return i->second;
}
bool inserted = false;
std::tie(i, inserted) =
sessions.try_emplace(name, std::make_shared<compile::Session>(
SwiftExecutablePath, RuntimeResourcePath,
DiagnosticDocumentationPath, Plugins));
assert(inserted);
return i->second;
}
void compile::SessionManager::clearSession(StringRef name) {
llvm::sys::ScopedLock lock(mtx);
sessions.erase(name);
}
namespace {
class InvocationRequest final
: public llvm::TrailingObjects<InvocationRequest, char *, char> {
friend class llvm::TrailingObjects<InvocationRequest, char *, char>;
size_t numArgs;
size_t numTrailingObjects(OverloadToken<char *>) const { return numArgs; }
MutableArrayRef<char *> getMutableArgs() {
return {getTrailingObjects<char *>(), numArgs};
}
InvocationRequest(ArrayRef<const char *> Args) : numArgs(Args.size()) {
// Copy the arguments to the buffer.
char *ptr = getTrailingObjects<char>();
auto thisArgs = getMutableArgs();
size_t i = 0;
for (const char *arg : Args) {
thisArgs[i++] = ptr;
auto size = strlen(arg) + 1;
strncpy(ptr, arg, size);
ptr += size;
}
}
public:
static InvocationRequest *create(ArrayRef<const char *> Args) {
size_t charBufSize = 0;
for (auto arg : Args) {
charBufSize += strlen(arg) + 1;
}
auto size = InvocationRequest::totalSizeToAlloc<char *, char>(Args.size(),
charBufSize);
auto *data = malloc(size);
return new (data) InvocationRequest(Args);
}
ArrayRef<const char *> getArgs() const {
return {getTrailingObjects<char *>(), numArgs};
}
};
} // namespace
void compile::SessionManager::performCompileAsync(
StringRef Name, ArrayRef<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
std::shared_ptr<std::atomic<bool>> CancellationFlag,
std::function<void(const RequestResult<CompilationResult> &)> Receiver) {
auto session = getSession(Name);
auto *request = InvocationRequest::create(Args);
compileQueue.dispatch(
[session, request, fileSystem, Receiver, CancellationFlag]() {
SWIFT_DEFER {
delete request;
};
// Cancelled during async dispatching?
if (CancellationFlag->load(std::memory_order_relaxed)) {
Receiver(RequestResult<CompilationResult>::cancelled());
return;
}
EditorDiagConsumer diagC;
auto stat =
session->performCompile(request->getArgs(), fileSystem, &diagC,
CancellationFlag);
// Cancelled during the compilation?
if (CancellationFlag->load(std::memory_order_relaxed)) {
Receiver(RequestResult<CompilationResult>::cancelled());
return;
}
SmallVector<DiagnosticEntryInfo, 0> diagEntryInfos;
diagC.getAllDiagnostics(diagEntryInfos);
Receiver(RequestResult<CompilationResult>::fromResult(
{stat, diagEntryInfos}));
},
/*isStackDeep=*/true);
}
void SwiftLangSupport::performCompile(
StringRef Name, ArrayRef<const char *> Args,
std::optional<VFSOptions> vfsOptions,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CompilationResult> &)> Receiver) {
std::string error;
auto fileSystem =
getFileSystem(vfsOptions, /*primaryFile=*/std::nullopt, error);
if (!fileSystem) {
Receiver(RequestResult<CompilationResult>::fromError(error));
return;
}
std::shared_ptr<std::atomic<bool>> CancellationFlag = std::make_shared<std::atomic<bool>>(false);
ReqTracker->setCancellationHandler(CancellationToken, [CancellationFlag]() {
CancellationFlag->store(true, std::memory_order_relaxed);
});
CompileManager->performCompileAsync(Name, Args, std::move(fileSystem),
CancellationFlag, Receiver);
}
void SwiftLangSupport::closeCompile(StringRef Name) {
CompileManager->clearSession(Name);
}
|