Skip to content

Commit 31d567e

Browse files
authored
Merge pull request swiftlang#28452 from nathawes/add-configuration-request-and-option-to-ignore-swiftsourceinfo
[SourceKit] Add global-configuration request to control SourceKit's behavior around .swiftsourceinfo files
2 parents 7485716 + b9d5672 commit 31d567e

File tree

23 files changed

+225
-54
lines changed

23 files changed

+225
-54
lines changed

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,18 +764,21 @@ void swift::ide::getLocationInfo(const ValueDecl *VD,
764764
auto ClangNode = VD->getClangNode();
765765

766766
if (VD->getLoc().isValid()) {
767+
auto getSignatureRange = [&](const ValueDecl *VD) -> Optional<unsigned> {
768+
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD)) {
769+
SourceRange R = FD->getSignatureSourceRange();
770+
if (R.isValid())
771+
return getCharLength(SM, R);
772+
}
773+
return None;
774+
};
767775
unsigned NameLen;
768-
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD)) {
769-
SourceRange R = FD->getSignatureSourceRange();
770-
if (R.isInvalid())
771-
return;
772-
NameLen = getCharLength(SM, R);
776+
if (auto SigLen = getSignatureRange(VD)) {
777+
NameLen = SigLen.getValue();
778+
} else if (VD->hasName()) {
779+
NameLen = VD->getBaseName().userFacingName().size();
773780
} else {
774-
if (VD->hasName()) {
775-
NameLen = VD->getBaseName().userFacingName().size();
776-
} else {
777-
NameLen = getCharLength(SM, VD->getLoc());
778-
}
781+
NameLen = getCharLength(SM, VD->getLoc());
779782
}
780783

781784
unsigned DeclBufID = SM.findBufferContainingLoc(VD->getLoc());

test/Serialization/comments-batch-mode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -enable-batch-mode -emit-module -emit-module-doc -emit-module-path %t/Foo.swiftmodule %S/Inputs/comments-batch/File1.swift %S/Inputs/comments-batch/File2.swift %S/Inputs/comments-batch/File3.swift %S/Inputs/comments-batch/File4.swift %S/Inputs/comments-batch/File5.swift -module-name Foo -emit-module-source-info-path %t/Foo.swiftsourceinfo -emit-module-doc-path %t/Foo.swiftdoc
3-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=Foo -source-filename %s -I %t | %FileCheck %s
3+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=Foo -enable-swiftsourceinfo -source-filename %s -I %t | %FileCheck %s
44

55
// RUN: %empty-directory(%t)
66
// RUN: %target-swift-frontend -wmo -emit-module -emit-module-doc -emit-module-path %t/Foo.swiftmodule %S/Inputs/comments-batch/File1.swift %S/Inputs/comments-batch/File2.swift %S/Inputs/comments-batch/File3.swift %S/Inputs/comments-batch/File4.swift %S/Inputs/comments-batch/File5.swift -module-name Foo -emit-module-source-info-path %t/Foo.swiftsourceinfo -emit-module-doc-path %t/Foo.swiftdoc
7-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=Foo -source-filename %s -I %t | %FileCheck %s
7+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=Foo -enable-swiftsourceinfo -source-filename %s -I %t | %FileCheck %s
88

99
// CHECK: Inputs/comments-batch/File1.swift:2:13: Func/FuncFromFile1 RawComment=[/// Comment in File1\n]
1010
// CHECK: Inputs/comments-batch/File2.swift:2:13: Func/FuncFromFile2 RawComment=[/// Comment in File2\n]

test/Serialization/comments-framework.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// RUN: %empty-directory(%t/comments.framework/Modules/comments.swiftmodule/Project)
44

55
// RUN: %target-swift-frontend -module-name comments -emit-module -emit-module-path %t/comments.framework/Modules/comments.swiftmodule/%target-swiftmodule-name -emit-module-doc-path %t/comments.framework/Modules/comments.swiftmodule/%target-swiftdoc-name -emit-module-source-info-path %t/comments.framework/Modules/comments.swiftmodule/Project/%target-swiftsourceinfo-name %s
6-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -source-filename %s -F %t | %FileCheck %s
6+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -enable-swiftsourceinfo -source-filename %s -F %t | %FileCheck %s
77

88
// RUN: cp -r %t/comments.framework/Modules/comments.swiftmodule %t/comments.swiftmodule
9-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -source-filename %s -I %t | %FileCheck %s
9+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -enable-swiftsourceinfo -source-filename %s -I %t | %FileCheck %s
1010

1111
/// first_decl_class_1 Aaa.
1212
public class first_decl_class_1 {

test/Serialization/comments-hidden.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//
1919
// RUN: %empty-directory(%t)
2020
// RUN: %target-swift-frontend -enable-testing -module-name comments -emit-module -emit-module-path %t/comments.swiftmodule -emit-module-doc -emit-module-doc-path %t/comments.swiftdoc -emit-module-source-info-path %t/comments.swiftsourceinfo %s
21-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -source-filename %s -I %t > %t.testing.txt
21+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -enable-swiftsourceinfo -source-filename %s -I %t > %t.testing.txt
2222
// RUN: %FileCheck %s -check-prefix=SOURCE-LOC < %t.testing.txt
2323

2424
/// PublicClass Documentation

test/Serialization/comments.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: llvm-bcanalyzer %t/comments.swiftmodule | %FileCheck %s -check-prefix=BCANALYZER
66
// RUN: llvm-bcanalyzer %t/comments.swiftdoc | %FileCheck %s -check-prefix=BCANALYZER
77
// RUN: llvm-bcanalyzer %t/comments.swiftsourceinfo | %FileCheck %s -check-prefix=BCANALYZER
8-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -source-filename %s -I %t | %FileCheck %s -check-prefix=FIRST
8+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -enable-swiftsourceinfo -source-filename %s -I %t | %FileCheck %s -check-prefix=FIRST
99

1010
// Test the case when we have a multiple files in a module.
1111
//
@@ -16,7 +16,7 @@
1616
// RUN: llvm-bcanalyzer %t/comments.swiftmodule | %FileCheck %s -check-prefix=BCANALYZER
1717
// RUN: llvm-bcanalyzer %t/comments.swiftdoc | %FileCheck %s -check-prefix=BCANALYZER
1818
// RUN: llvm-bcanalyzer %t/comments.swiftsourceinfo | %FileCheck %s -check-prefix=BCANALYZER
19-
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -source-filename %s -I %t > %t.printed.txt
19+
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=comments -enable-swiftsourceinfo -source-filename %s -I %t > %t.printed.txt
2020
// RUN: %FileCheck %s -check-prefix=FIRST < %t.printed.txt
2121
// RUN: %FileCheck %s -check-prefix=SECOND < %t.printed.txt
2222

test/SourceKit/CompileNotifications/arg-parsing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// ARG_PARSE_0: {
44
// ARG_PARSE_0: key.notification: source.notification.compile-will-start
55
// ARG_PARSE_0: key.compileid: [[CID1:".*"]]
6-
// ARG_PARSE_0: key.compilerargs-string: "{{.*}}.swift -no-such-arg -Xfrontend -ignore-module-source-info"
6+
// ARG_PARSE_0: key.compilerargs-string: "{{.*}}.swift -no-such-arg"
77
// ARG_PARSE_0: }
88
// ARG_PARSE_0: {
99
// ARG_PARSE_0: key.notification: source.notification.compile-did-finish
@@ -24,7 +24,7 @@
2424
// ARG_PARSE_1: {
2525
// ARG_PARSE_1: key.notification: source.notification.compile-will-start
2626
// ARG_PARSE_1: key.compileid: [[CID1:".*"]]
27-
// ARG_PARSE_1: key.compilerargs-string: "{{.*}}.swift -no-such-arg -Xfrontend -ignore-module-source-info"
27+
// ARG_PARSE_1: key.compilerargs-string: "{{.*}}.swift -no-such-arg"
2828
// ARG_PARSE_1: }
2929
// ARG_PARSE_1: {
3030
// ARG_PARSE_1: key.notification: source.notification.compile-did-finish
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Foo
2+
func bar() {
3+
foo()
4+
}
5+
6+
// RUN: %empty-directory(%t)
7+
// RUN: echo "/// Some doc" >> %t/Foo.swift
8+
// RUN: echo "public func foo() { }" >> %t/Foo.swift
9+
// RUN: %target-swift-frontend -enable-batch-mode -emit-module -emit-module-doc -emit-module-path %t/Foo.swiftmodule %t/Foo.swift -module-name Foo -emit-module-source-info-path %t/Foo.swiftsourceinfo -emit-module-doc-path %t/Foo.swiftdoc
10+
//
11+
// Test setting optimize for ide to false
12+
// RUN: %sourcekitd-test -req=global-config -for-ide=0 == -req=cursor -pos=3:3 %s -- -I %t -target %target-triple %s | %FileCheck --check-prefixes=BOTH,WITH %s
13+
//
14+
// Test setting optimize for ide to true
15+
// RUN: %sourcekitd-test -req=global-config -for-ide=1 == -req=cursor -pos=3:3 %s -- -I %t -target %target-triple %s | %FileCheck --check-prefixes=BOTH,WITHOUT %s
16+
//
17+
// Test sourcekitd-test's default global configuration request (optimize for ide is true)
18+
// RUN: %sourcekitd-test -req=cursor -pos=3:3 %s -- -I %t -target %target-triple %s | %FileCheck --check-prefixes=BOTH,WITHOUT %s
19+
//
20+
// Test without sending any global configuration request to check the sevice's default settings (optimize for ide is false)
21+
// RUN: %sourcekitd-test -suppress-config-request -req=cursor -pos=3:3 %s -- -I %t -target %target-triple %s | %FileCheck --check-prefixes=BOTH,WITH %s
22+
23+
// WITH: source.lang.swift.ref.function.free ({{.*}}/Foo.swift:2:13-2:16)
24+
// WITHOUT: source.lang.swift.ref.function.free ()
25+
// BOTH: foo()
26+
// BOTH: s:3Foo3fooyyF
27+
// BOTH: () -> ()
28+
// BOTH: $syycD
29+
// BOTH: Foo

test/SourceKit/Misc/stats.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ func foo() {}
22

33
// RUN: %sourcekitd-test -req=syntax-map %s == -req=stats | %FileCheck %s -check-prefix=SYNTAX_1
44

5-
// SYNTAX_1: 2 {{.*}} source.statistic.num-requests
5+
// SYNTAX_1: 3 {{.*}} source.statistic.num-requests
66
// SYNTAX_1: 0 {{.*}} source.statistic.num-semantic-requests
77
// SYNTAX_1: 0 {{.*}} source.statistic.num-ast-builds
88
// SYNTAX_1: 1 {{.*}} source.statistic.num-open-documents
99
// SYNTAX_1: 1 {{.*}} source.statistic.max-open-documents
1010

1111
// RUN: %sourcekitd-test -req=syntax-map %s == -req=close %s == -req=stats | %FileCheck %s -check-prefix=SYNTAX_2
1212

13-
// SYNTAX_2: 3 {{.*}} source.statistic.num-requests
13+
// SYNTAX_2: 4 {{.*}} source.statistic.num-requests
1414
// SYNTAX_2: 0 {{.*}} source.statistic.num-semantic-requests
1515
// SYNTAX_2: 0 {{.*}} source.statistic.num-ast-builds
1616
// SYNTAX_2: 0 {{.*}} source.statistic.num-open-documents
1717
// SYNTAX_2: 1 {{.*}} source.statistic.max-open-documents
1818

1919
// RUN: %sourcekitd-test -req=sema %s -- %s == -req=stats | %FileCheck %s -check-prefix=SEMA_1
2020

21-
// SEMA_1: 3 {{.*}} source.statistic.num-requests
21+
// SEMA_1: 4 {{.*}} source.statistic.num-requests
2222
// SEMA_1: 0 {{.*}} source.statistic.num-semantic-requests
2323
// SEMA_1: 1 {{.*}} source.statistic.num-ast-builds
2424
// SEMA_1: 1 {{.*}} source.statistic.num-asts-in-memory
@@ -28,7 +28,7 @@ func foo() {}
2828

2929
// RUN: %sourcekitd-test -req=sema %s -- %s == -req=edit -pos=1:1 -replace=" " %s == -req=stats | %FileCheck %s -check-prefix=SEMA_2
3030

31-
// SEMA_2: 5 {{.*}} source.statistic.num-requests
31+
// SEMA_2: 6 {{.*}} source.statistic.num-requests
3232
// SEMA_2: 0 {{.*}} source.statistic.num-semantic-requests
3333
// SEMA_2: 2 {{.*}} source.statistic.num-ast-builds
3434
// NOTE: we cannot match num-asts-in-memory, or num-ast-cache-hits reliably when
@@ -40,7 +40,7 @@ func foo() {}
4040

4141
// RUN: %sourcekitd-test -req=sema %s -- %s == -req=cursor -pos=1:6 %s -- %s == -req=stats | %FileCheck %s -check-prefix=SEMA_3
4242

43-
// SEMA_3: 4 {{.*}} source.statistic.num-requests
43+
// SEMA_3: 5 {{.*}} source.statistic.num-requests
4444
// SEMA_3: 1 {{.*}} source.statistic.num-semantic-requests
4545
// SEMA_3: 1 {{.*}} source.statistic.num-ast-builds
4646
// SEMA_3: 1 {{.*}} source.statistic.num-asts-in-memory
@@ -50,7 +50,7 @@ func foo() {}
5050

5151
// RUN: %sourcekitd-test -req=sema %s -- %s == -req=related-idents -pos=1:6 %s -- %s == -req=stats | %FileCheck %s -check-prefix=SEMA_4
5252

53-
// SEMA_4: 4 {{.*}} source.statistic.num-requests
53+
// SEMA_4: 5 {{.*}} source.statistic.num-requests
5454
// SEMA_4: 1 {{.*}} source.statistic.num-semantic-requests
5555
// SEMA_4: 1 {{.*}} source.statistic.num-ast-builds
5656
// SEMA_4: 1 {{.*}} source.statistic.num-asts-in-memory

tools/SourceKit/include/SourceKit/Core/Context.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SourceKit/Core/LLVM.h"
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/ADT/STLExtras.h"
19+
#include "llvm/Support/Mutex.h"
1920
#include <memory>
2021
#include <string>
2122

@@ -27,10 +28,30 @@ namespace SourceKit {
2728
class LangSupport;
2829
class NotificationCenter;
2930

31+
class GlobalConfig {
32+
public:
33+
struct Settings {
34+
/// When true, the default compiler options and other configuration flags will be chosen to optimize for
35+
/// usage from an IDE.
36+
///
37+
/// At the time of writing this just means ignoring .swiftsourceinfo files.
38+
bool OptimizeForIDE = false;
39+
};
40+
41+
private:
42+
Settings State;
43+
mutable llvm::sys::Mutex Mtx;
44+
45+
public:
46+
Settings update(Optional<bool> OptimizeForIDE);
47+
bool shouldOptimizeForIDE() const;
48+
};
49+
3050
class Context {
3151
std::string RuntimeLibPath;
3252
std::unique_ptr<LangSupport> SwiftLang;
3353
std::shared_ptr<NotificationCenter> NotificationCtr;
54+
std::shared_ptr<GlobalConfig> Config;
3455

3556
public:
3657
Context(StringRef RuntimeLibPath,
@@ -44,6 +65,8 @@ class Context {
4465
LangSupport &getSwiftLangSupport() { return *SwiftLang; }
4566

4667
std::shared_ptr<NotificationCenter> getNotificationCenter() { return NotificationCtr; }
68+
69+
std::shared_ptr<GlobalConfig> getGlobalConfiguration() { return Config; }
4770
};
4871

4972
} // namespace SourceKit

tools/SourceKit/lib/Core/Context.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@
1616

1717
using namespace SourceKit;
1818

19+
GlobalConfig::Settings
20+
GlobalConfig::update(Optional<bool> OptimizeForIDE) {
21+
llvm::sys::ScopedLock L(Mtx);
22+
if (OptimizeForIDE.hasValue())
23+
State.OptimizeForIDE = *OptimizeForIDE;
24+
return State;
25+
};
26+
27+
bool GlobalConfig::shouldOptimizeForIDE() const {
28+
llvm::sys::ScopedLock L(Mtx);
29+
return State.OptimizeForIDE;
30+
}
31+
1932
SourceKit::Context::Context(StringRef RuntimeLibPath,
2033
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
2134
LangSupportFactoryFn,
2235
bool shouldDispatchNotificationsOnMain) : RuntimeLibPath(RuntimeLibPath),
23-
NotificationCtr(new NotificationCenter(shouldDispatchNotificationsOnMain)) {
36+
NotificationCtr(new NotificationCenter(shouldDispatchNotificationsOnMain)),
37+
Config(new GlobalConfig()) {
2438
// Should be called last after everything is initialized.
2539
SwiftLang = LangSupportFactoryFn(*this);
2640
}

0 commit comments

Comments
 (0)