Skip to content

Commit 77b4f75

Browse files
committed
[SourceKit] Reorgantize code completion options
* Abolish 'reuseastcontext' per-request option * Add 'MaxASTContextReuseCount' global configuration
1 parent bec6a8f commit 77b4f75

30 files changed

+144
-154
lines changed

include/swift/IDE/CompletionInstance.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
3737

3838
/// Manages \c CompilerInstance for completion like operations.
3939
class CompletionInstance {
40-
unsigned MaxASTReuseCount = 100;
41-
unsigned DependencyCheckIntervalSecond = 5;
40+
struct Options {
41+
unsigned MaxASTReuseCount = 100;
42+
unsigned DependencyCheckIntervalSecond = 5;
43+
} Opts;
4244

4345
std::mutex mtx;
4446

@@ -59,7 +61,7 @@ class CompletionInstance {
5961
/// argument has changed, primary file is not the same, the \c Offset is not
6062
/// in function bodies, or the interface hash of the file has changed.
6163
bool performCachedOperationIfPossible(
62-
const swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
64+
llvm::hash_code ArgsHash,
6365
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
6466
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
6567
DiagnosticConsumer *DiagC,
@@ -78,7 +80,9 @@ class CompletionInstance {
7880
llvm::function_ref<void(CompilerInstance &, bool)> Callback);
7981

8082
public:
81-
void setDependencyCheckIntervalSecond(unsigned Value);
83+
CompletionInstance() {}
84+
85+
void setOptions(Options NewOpts);
8286

8387
/// Calls \p Callback with a \c CompilerInstance which is prepared for the
8488
/// second pass. \p Callback is resposible to perform the second pass on it.
@@ -94,7 +98,7 @@ class CompletionInstance {
9498
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
9599
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
96100
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
97-
bool EnableASTCaching, std::string &Error, DiagnosticConsumer *DiagC,
101+
std::string &Error, DiagnosticConsumer *DiagC,
98102
llvm::function_ref<void(CompilerInstance &, bool)> Callback);
99103
};
100104

lib/IDE/CompletionInstance.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static bool areAnyDependentFilesInvalidated(
275275
} // namespace
276276

277277
bool CompletionInstance::performCachedOperationIfPossible(
278-
const swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
278+
llvm::hash_code ArgsHash,
279279
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
280280
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
281281
DiagnosticConsumer *DiagC,
@@ -285,7 +285,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
285285

286286
if (!CachedCI)
287287
return false;
288-
if (CachedReuseCount >= MaxASTReuseCount)
288+
if (CachedReuseCount >= Opts.MaxASTReuseCount)
289289
return false;
290290
if (CachedArgHash != ArgsHash)
291291
return false;
@@ -570,20 +570,21 @@ bool CompletionInstance::shouldCheckDependencies() const {
570570
assert(CachedCI);
571571
using namespace std::chrono;
572572
auto now = system_clock::now();
573-
return DependencyCheckedTimestamp + seconds(DependencyCheckIntervalSecond) <
574-
now;
573+
auto threshold = DependencyCheckedTimestamp +
574+
seconds(Opts.DependencyCheckIntervalSecond);
575+
return threshold < now;
575576
}
576577

577-
void CompletionInstance::setDependencyCheckIntervalSecond(unsigned Value) {
578+
void CompletionInstance::setOptions(CompletionInstance::Options NewOpts) {
578579
std::lock_guard<std::mutex> lock(mtx);
579-
DependencyCheckIntervalSecond = Value;
580+
Opts = NewOpts;
580581
}
581582

582583
bool swift::ide::CompletionInstance::performOperation(
583584
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
584585
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
585586
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
586-
bool EnableASTCaching, std::string &Error, DiagnosticConsumer *DiagC,
587+
std::string &Error, DiagnosticConsumer *DiagC,
587588
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
588589

589590
// Always disable source location resolutions from .swiftsourceinfo file
@@ -601,29 +602,23 @@ bool swift::ide::CompletionInstance::performOperation(
601602
// We don't need token list.
602603
Invocation.getLangOptions().CollectParsedToken = false;
603604

604-
if (EnableASTCaching) {
605-
// Compute the signature of the invocation.
606-
llvm::hash_code ArgsHash(0);
607-
for (auto arg : Args)
608-
ArgsHash = llvm::hash_combine(ArgsHash, StringRef(arg));
605+
// Compute the signature of the invocation.
606+
llvm::hash_code ArgsHash(0);
607+
for (auto arg : Args)
608+
ArgsHash = llvm::hash_combine(ArgsHash, StringRef(arg));
609609

610-
// Concurrent completions will block so that they have higher chance to use
611-
// the cached completion instance.
612-
std::lock_guard<std::mutex> lock(mtx);
610+
// Concurrent completions will block so that they have higher chance to use
611+
// the cached completion instance.
612+
std::lock_guard<std::mutex> lock(mtx);
613613

614-
if (performCachedOperationIfPossible(Invocation, ArgsHash, FileSystem,
615-
completionBuffer, Offset, DiagC,
616-
Callback))
617-
return true;
614+
if (performCachedOperationIfPossible(ArgsHash, FileSystem, completionBuffer,
615+
Offset, DiagC, Callback)) {
616+
return true;
617+
}
618618

619-
if (performNewOperation(ArgsHash, Invocation, FileSystem, completionBuffer,
620-
Offset, Error, DiagC, Callback))
621-
return true;
622-
} else {
623-
// Concurrent completions may happen in parallel when caching is disabled.
624-
if (performNewOperation(None, Invocation, FileSystem, completionBuffer,
625-
Offset, Error, DiagC, Callback))
626-
return true;
619+
if(performNewOperation(ArgsHash, Invocation, FileSystem, completionBuffer,
620+
Offset, Error, DiagC, Callback)) {
621+
return true;
627622
}
628623

629624
assert(!Error.empty());

test/SourceKit/CodeComplete/complete_checkdeps_avoid_check.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func foo() {
4040
// RUN: -shell -- cp -R $INPUT_DIR/ClangFW.framework_mod/* %t/Frameworks/ClangFW.framework/ == \
4141
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} == \
4242

43-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
43+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
4444
// RUN: -shell -- echo '### Checking dependencies' == \
4545
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} \
4646

test/SourceKit/CodeComplete/complete_checkdeps_bridged.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func foo() {
2929
// RUN: %target-swift-frontend -emit-module -module-name SwiftFW -o %t/Frameworks/SwiftFW.framework/Modules/SwiftFW.swiftmodule/%target-swiftmodule-name $INPUT_DIR/SwiftFW_src/Funcs.swift
3030

3131
// RUN: %sourcekitd-test \
32-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
32+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
3333

3434
// RUN: -shell -- echo "### Initial" == \
3535
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} == \

test/SourceKit/CodeComplete/complete_checkdeps_clangmodule.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func foo() {
2929
// RUN: %target-swift-frontend -emit-module -module-name SwiftFW -o %t/Frameworks/SwiftFW.framework/Modules/SwiftFW.swiftmodule/%target-swiftmodule-name $INPUT_DIR/SwiftFW_src/Funcs.swift
3030

3131
// RUN: %sourcekitd-test \
32-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
32+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
3333

3434
// RUN: -shell -- echo "### Initial" == \
3535
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} == \

test/SourceKit/CodeComplete/complete_checkdeps_otherfile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func foo() {
2929
// RUN: %target-swift-frontend -emit-module -module-name SwiftFW -o %t/Frameworks/SwiftFW.framework/Modules/SwiftFW.swiftmodule/%target-swiftmodule-name $INPUT_DIR/SwiftFW_src/Funcs.swift
3030

3131
// RUN: %sourcekitd-test \
32-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
32+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
3333

3434
// RUN: -shell -- echo "### Initial" == \
3535
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} == \

test/SourceKit/CodeComplete/complete_checkdeps_ownfile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func foo(val: MyStruct) {
4646
// RUN: cp %t/State1.swift %t/test.swift
4747

4848
// RUN: %sourcekitd-test \
49-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
49+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
5050

5151
// RUN: -shell -- echo "### Initial" == \
5252
// RUN: -req=complete -pos=5:4 %t/test.swift -- ${COMPILER_ARGS[@]} == \

test/SourceKit/CodeComplete/complete_checkdeps_swiftmodule.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func foo() {
2929
// RUN: %target-swift-frontend -emit-module -module-name SwiftFW -o %t/Frameworks/SwiftFW.framework/Modules/SwiftFW.swiftmodule/%target-swiftmodule-name $INPUT_DIR/SwiftFW_src/Funcs.swift
3030

3131
// RUN: %sourcekitd-test \
32-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
32+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
3333

3434
// RUN: -shell -- echo "### Initial" == \
3535
// RUN: -req=complete -pos=5:3 %s -- ${COMPILER_ARGS[@]} == \

test/SourceKit/CodeComplete/complete_checkdeps_vfs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func foo(value: MyStruct) {
1212
// RUN: cp %S/Inputs/checkdeps/MyProject/LibraryExt.swift %t/VFS/
1313

1414
// RUN: %sourcekitd-test \
15-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
15+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
1616

1717
// RUN: -shell -- echo "### Initial" == \
1818
// RUN: -req=complete -pos=2:9 -pass-as-sourcetext -vfs-files=%t/VFS/Main.swift=@%s,%t/VFS/Library.swift=@%S/Inputs/checkdeps/MyProject/Library.swift %t/VFS/Main.swift -- -target %target-triple %t/VFS/Main.swift %t/VFS/LibraryExt.swift %t/VFS/Library.swift == \

test/SourceKit/CodeComplete/complete_checkdeps_vfs_open.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func foo(value: MyStruct) {
1212
// RUN: cp %S/Inputs/checkdeps/MyProject/LibraryExt.swift %t/VFS/
1313

1414
// RUN: %sourcekitd-test \
15-
// RUN: -req=global-config -completion-check-dependency-interval ${DEPCHECK_INTERVAL} == \
15+
// RUN: -req=global-config -req-opts=completion_check_dependency_interval=${DEPCHECK_INTERVAL} == \
1616

1717
// RUN: -shell -- echo "### Initial" == \
1818
// RUN: -req=complete.open -pos=2:9 -pass-as-sourcetext -vfs-files=%t/VFS/Main.swift=@%s,%t/VFS/Library.swift=@%S/Inputs/checkdeps/MyProject/Library.swift %t/VFS/Main.swift -- -target %target-triple %t/VFS/Main.swift %t/VFS/LibraryExt.swift %t/VFS/Library.swift == \

0 commit comments

Comments
 (0)