Skip to content

[SourceKit] Remove dependency of SwiftLang on Core #3101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tools/SourceKit/include/SourceKit/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "SourceKit/Core/LLVM.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/STLExtras.h"
#include <memory>
#include <string>

Expand All @@ -32,7 +33,9 @@ class Context {
std::unique_ptr<NotificationCenter> NotificationCtr;

public:
explicit Context(StringRef RuntimeLibPath);
Context(StringRef RuntimeLibPath,
llvm::function_ref<
std::unique_ptr<LangSupport>(Context &)> LangSupportFactoryFn);
~Context();

StringRef getRuntimeLibPath() const { return RuntimeLibPath; }
Expand Down
4 changes: 0 additions & 4 deletions tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace llvm {
class MemoryBuffer;
}
namespace SourceKit {
class Context;

struct EntityInfo {
UIdent Kind;
Expand Down Expand Up @@ -472,9 +471,6 @@ class LangSupport {
StringRef ModuleName,
ArrayRef<const char *> Args,
DocInfoConsumer &Consumer) = 0;

static std::unique_ptr<LangSupport> createSwiftLangSupport(
SourceKit::Context &SKCtx);
};

} // namespace SourceKit
Expand Down
26 changes: 26 additions & 0 deletions tools/SourceKit/include/SourceKit/SwiftLang/Factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===--- Factory.h - --------------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SOURCEKIT_SWIFTLANG_FACTORY_H
#define LLVM_SOURCEKIT_SWIFTLANG_FACTORY_H

#include <memory>

namespace SourceKit {
class LangSupport;
class Context;

std::unique_ptr<LangSupport> createSwiftLangSupport(Context &SKCtx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers should generally include the headers that they depend on, in this case it should get a #include <memory> (because of std::unique_ptr)


}

#endif
7 changes: 5 additions & 2 deletions tools/SourceKit/lib/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

using namespace SourceKit;

SourceKit::Context::Context(StringRef RuntimeLibPath)
SourceKit::Context::Context(StringRef RuntimeLibPath,
llvm::function_ref<
std::unique_ptr<LangSupport>(Context &)> LangSupportFactoryFn)
: RuntimeLibPath(RuntimeLibPath),
SwiftLang(LangSupport::createSwiftLangSupport(*this)),
NotificationCtr(new NotificationCenter()) {
// Should be called last after everything is initialized.
SwiftLang = LangSupportFactoryFn(*this);
}

SourceKit::Context::~Context() {
Expand Down
3 changes: 2 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "SwiftLangSupport.h"
#include "SwiftASTManager.h"
#include "SourceKit/Core/Context.h"
#include "SourceKit/SwiftLang/Factory.h"
#include "SourceKit/Support/UIdent.h"

#include "swift/AST/AST.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ static UIdent KindStructureElemTypeRef("source.lang.swift.structure.elem.typeref


std::unique_ptr<LangSupport>
LangSupport::createSwiftLangSupport(SourceKit::Context &SKCtx) {
SourceKit::createSwiftLangSupport(SourceKit::Context &SKCtx) {
return std::unique_ptr<LangSupport>(new SwiftLangSupport(SKCtx));
}

Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace SourceKit {
typedef RefPtr<ImmutableTextSnapshot> ImmutableTextSnapshotRef;
class SwiftASTManager;
class SwiftLangSupport;
class Context;

class SwiftEditorDocument :
public ThreadSafeRefCountedBase<SwiftEditorDocument> {
Expand Down
4 changes: 3 additions & 1 deletion tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "SourceKit/Support/Concurrency.h"
#include "SourceKit/Support/Logging.h"
#include "SourceKit/Support/UIdent.h"
#include "SourceKit/SwiftLang/Factory.h"

#include "swift/Basic/DemangleWrappers.h"

Expand Down Expand Up @@ -132,7 +133,8 @@ static void onDocumentUpdateNotification(StringRef DocumentName) {
static SourceKit::Context *GlobalCtx = nullptr;

void sourcekitd::initialize() {
GlobalCtx = new SourceKit::Context(sourcekitd::getRuntimeLibPath());
GlobalCtx = new SourceKit::Context(sourcekitd::getRuntimeLibPath(),
SourceKit::createSwiftLangSupport);
GlobalCtx->getNotificationCenter().addDocumentUpdateNotificationReceiver(
onDocumentUpdateNotification);
}
Expand Down
3 changes: 2 additions & 1 deletion unittests/SourceKit/SwiftLang/CursorInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "SourceKit/Core/LangSupport.h"
#include "SourceKit/Core/NotificationCenter.h"
#include "SourceKit/Support/Concurrency.h"
#include "SourceKit/SwiftLang/Factory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -94,7 +95,7 @@ struct TestCursorInfo {
};

class CursorInfoTest : public ::testing::Test {
SourceKit::Context Ctx{ getRuntimeLibPath() };
SourceKit::Context Ctx{ getRuntimeLibPath(), SourceKit::createSwiftLangSupport };
std::atomic<int> NumTasks;

public:
Expand Down