Skip to content

[CodeCompletion] Inherit options when parsing new buffer #31741

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 1 commit into from
May 13, 2020
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
17 changes: 12 additions & 5 deletions lib/IDE/CompletionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ bool CompletionInstance::performCachedOperationIfPossible(
auto tmpBufferID = tmpSM.addMemBufferCopy(completionBuffer);
tmpSM.setCodeCompletionPoint(tmpBufferID, Offset);

LangOptions langOpts;
LangOptions langOpts = CI.getASTContext().LangOpts;
langOpts.DisableParserLookup = true;
TypeCheckerOptions typeckOpts;
SearchPathOptions searchPathOpts;
TypeCheckerOptions typeckOpts = CI.getASTContext().TypeCheckerOpts;
SearchPathOptions searchPathOpts = CI.getASTContext().SearchPathOpts;
DiagnosticEngine tmpDiags(tmpSM);
std::unique_ptr<ASTContext> tmpCtx(
ASTContext::get(langOpts, typeckOpts, searchPathOpts, tmpSM, tmpDiags));
Expand All @@ -327,13 +327,20 @@ bool CompletionInstance::performCachedOperationIfPossible(
registerTypeCheckerRequestFunctions(tmpCtx->evaluator);
registerSILGenRequestFunctions(tmpCtx->evaluator);
ModuleDecl *tmpM = ModuleDecl::create(Identifier(), *tmpCtx);
SourceFile *tmpSF = new (*tmpCtx) SourceFile(*tmpM, oldSF->Kind, tmpBufferID);
SourceFile *tmpSF = new (*tmpCtx)
SourceFile(*tmpM, oldSF->Kind, tmpBufferID, /*KeepParsedTokens=*/false,
/*BuildSyntaxTree=*/false, oldSF->getParsingOptions());
tmpSF->enableInterfaceHash();
// Ensure all non-function-body tokens are hashed into the interface hash
tmpCtx->LangOpts.EnableTypeFingerprints = false;

// Couldn't find any completion token?
// FIXME: Since we don't setup module loaders on the temporary AST context,
// 'canImport()' conditional compilation directive always fails. That causes
// interface hash change and prevents fast-completion.

// Parse and get the completion context.
auto *newState = tmpSF->getDelayedParserState();
// Couldn't find any completion token?
if (!newState->hasCodeCompletionDelayedDeclState())
return false;

Expand Down
4 changes: 4 additions & 0 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
SmallVector<ASTNode, 16> Elements;
llvm::SaveAndRestore<bool> S(InInactiveClauseEnvironment,
InInactiveClauseEnvironment || !isActive);
// Disable updating the interface hash inside inactive blocks.
llvm::SaveAndRestore<NullablePtr<llvm::MD5>> T(
CurrentTokenHash, isActive ? CurrentTokenHash : nullptr);

if (isActive || !isVersionCondition) {
parseElements(Elements, isActive);
} else if (SyntaxContext->isEnabled()) {
Expand Down
37 changes: 37 additions & 0 deletions test/SourceKit/CodeComplete/complete_sequence_ifconfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
struct MyStruct { func foo() {} }

#if DEBUG
import Swift
#endif

#if arch(x86_64)
operator ++++++
#endif

#if !targetEnvironment(simulator)
precedencegroup MyPrecedence
#endif

func foo(value: MyStruct) {
value.
}

// Ensure that compilation directives are equally evaluated and doesn't affect the fast completions.

// RUN: %sourcekitd-test \
// RUN: -req=complete -pos=16:9 -repeat-request=2 %s -- %s -target %target-triple \
// RUN: | %FileCheck %s

// CHECK-LABEL: key.results: [
// CHECK-NOT: ]
// CHECK-DAG: key.description: "foo()",
// CHECK-DAG: key.description: "self",
// CHECK: ]
// CHECK-NOT: key.reusingastcontext

// CHECK-LABEL: key.results: [
// CHECK-NOT: ]
// CHECK-DAG: key.description: "foo()",
// CHECK-DAG: key.description: "self",
// CHECK: ],
// CHECK: key.reusingastcontext: 1