Skip to content

[CodeCompletion] Parse #if block containing CC token as active #33475

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
2 changes: 0 additions & 2 deletions include/swift/AST/NameLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,6 @@ class ASTScope {
return Mem;
}

static bool areInactiveIfConfigClausesSupported();

private:
static ast_scope::ASTSourceFileScope *createScopeTree(SourceFile *);

Expand Down
40 changes: 1 addition & 39 deletions lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,23 +479,6 @@ class ScopeCreator final {
}

public:
/// When ASTScopes are enabled for code completion,
/// IfConfigs will pose a challenge because we may need to field lookups into
/// the inactive clauses, but the AST contains redundancy: the active clause's
/// elements are present in the members or elements of an IterableTypeDecl or
/// BraceStmt alongside of the IfConfigDecl. In addition there are two more
/// complications:
///
/// 1. The active clause's elements may be nested inside an init self
/// rebinding decl (as in StringObject.self).
///
/// 2. The active clause may be before or after the inactive ones
///
/// So, when encountering an IfConfigDecl, we will expand the inactive
/// elements. Also, always sort members or elements so that the child scopes
/// are in source order (Just one of several reasons we need to sort.)
///
static const bool includeInactiveIfConfigClauses = false;

private:
static std::vector<ASTNode> expandIfConfigClauses(ArrayRef<ASTNode> input) {
Expand Down Expand Up @@ -526,9 +509,6 @@ class ScopeCreator final {
if (isa<IfConfigDecl>(d))
expandIfConfigClausesInto(expansion, {d}, true);
}
} else if (includeInactiveIfConfigClauses) {
expandIfConfigClausesInto(expansion, clause.Elements,
/*isInAnActiveNode=*/false);
}
}
}
Expand Down Expand Up @@ -762,10 +742,6 @@ void ASTScope::
impl->buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals();
}

bool ASTScope::areInactiveIfConfigClausesSupported() {
return ScopeCreator::includeInactiveIfConfigClauses;
}

void ASTScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
auto *const SF = AFD->getParentSourceFile();
SF->getScope().expandFunctionBodyImpl(AFD);
Expand Down Expand Up @@ -2083,10 +2059,8 @@ class LocalizableDeclContextCollector : public ASTWalker {
// catchForDebugging(D, "DictionaryBridging.swift", 694);
if (const auto *dc = dyn_cast<DeclContext>(D))
record(dc);
if (auto *icd = dyn_cast<IfConfigDecl>(D)) {
walkToClauses(icd);
if (isa<IfConfigDecl>(D))
return false;
}
if (auto *pd = dyn_cast<ParamDecl>(D))
record(pd->getDefaultArgumentInitContext());
else if (auto *pbd = dyn_cast<PatternBindingDecl>(D))
Expand All @@ -2104,18 +2078,6 @@ class LocalizableDeclContextCollector : public ASTWalker {
}

private:
void walkToClauses(IfConfigDecl *icd) {
for (auto &clause : icd->getClauses()) {
// Generate scopes for any closures in the condition
if (ScopeCreator::includeInactiveIfConfigClauses && clause.isActive) {
if (clause.Cond)
clause.Cond->walk(*this);
for (auto n : clause.Elements)
n.walk(*this);
}
}
}

void recordInitializers(PatternBindingDecl *pbd) {
for (auto idx : range(pbd->getNumPatternEntries()))
record(pbd->getInitContext(idx));
Expand Down
3 changes: 0 additions & 3 deletions lib/IDE/CompletionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ template <typename Range> Decl *getElementAt(const Range &Decls, unsigned N) {
/// Find the equivalent \c DeclContext with \p DC from \p SF AST.
/// This assumes the AST which contains \p DC has exact the same structure with
/// \p SF.
/// FIXME: This doesn't support IfConfigDecl blocks. If \p DC is in an inactive
/// config block, this function returns \c false.
static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
SourceFile *SF) {
PrettyStackTraceDeclContext trace("getting equivalent decl context for", DC);
Expand Down Expand Up @@ -129,7 +127,6 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
}

// Not found in the decl context tree.
// FIXME: Probably DC is in an inactive #if block.
if (N == ~0U) {
return nullptr;
}
Expand Down
31 changes: 30 additions & 1 deletion lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,17 +603,42 @@ static Expr *findAnyLikelySimulatorEnvironmentTest(Expr *Condition) {
/// Delegate callback function to parse elements in the blocks.
ParserResult<IfConfigDecl> Parser::parseIfConfig(
llvm::function_ref<void(SmallVectorImpl<ASTNode> &, bool)> parseElements) {
assert(Tok.is(tok::pound_if));
SyntaxParsingContext IfConfigCtx(SyntaxContext, SyntaxKind::IfConfigDecl);

SmallVector<IfConfigClause, 4> Clauses;
Parser::StructureMarkerRAII ParsingDecl(
*this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig);

// Find the region containing code completion token.
SourceLoc codeCompletionClauseLoc;
if (SourceMgr.hasCodeCompletionBuffer() &&
SourceMgr.getCodeCompletionBufferID() == L->getBufferID() &&
SourceMgr.isBeforeInBuffer(Tok.getLoc(),
SourceMgr.getCodeCompletionLoc())) {
llvm::SaveAndRestore<Optional<llvm::MD5>> H(CurrentTokenHash, None);
BacktrackingScope backtrack(*this);
do {
auto startLoc = Tok.getLoc();
consumeToken();
skipUntilConditionalBlockClose();
auto endLoc = PreviousLoc;
if (SourceMgr.rangeContainsTokenLoc(SourceRange(startLoc, endLoc),
SourceMgr.getCodeCompletionLoc())){
codeCompletionClauseLoc = startLoc;
break;
}
} while (Tok.isNot(tok::pound_endif, tok::eof));
}

bool shouldEvaluate =
// Don't evaluate if it's in '-parse' mode, etc.
shouldEvaluatePoundIfDecls() &&
// If it's in inactive #if ... #endif block, there's no point to do it.
!getScopeInfo().isInactiveConfigBlock();
!getScopeInfo().isInactiveConfigBlock() &&
// If this directive contains code completion location, 'isActive' is
// determined solely by which block has the completion token.
!codeCompletionClauseLoc.isValid();

bool foundActive = false;
bool isVersionCondition = false;
Expand Down Expand Up @@ -658,6 +683,10 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
}
}

// Treat the region containing code completion token as "active".
if (codeCompletionClauseLoc.isValid() && !foundActive)
isActive = (ClauseLoc == codeCompletionClauseLoc);

foundActive |= isActive;

if (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,9 @@ void Parser::skipSingle() {
void Parser::skipUntil(tok T1, tok T2) {
// tok::NUM_TOKENS is a sentinel that means "don't skip".
if (T1 == tok::NUM_TOKENS && T2 == tok::NUM_TOKENS) return;

while (Tok.isNot(T1, T2, tok::eof, tok::pound_endif, tok::code_complete))

while (Tok.isNot(T1, T2, tok::eof, tok::pound_endif, tok::pound_else,
tok::pound_elseif))
Comment on lines -699 to +700
Copy link
Member Author

@rintaro rintaro Aug 14, 2020

Choose a reason for hiding this comment

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

tok::code_complete was here to prevent excessive skipping beyond the completion point. But we sometimes want to skip code_complete token, for example canParse*** kind of functions. This doesn't cause any visible regression because skipUntilDeclRBrace() family skipping functions still stop at tok::code_complete.

As for tok::pound_else and tok::pound_elseif, they should be treated as the same as tok::pound_endif in terms of "skipping" tokens.

skipSingle();
}

Expand Down
75 changes: 75 additions & 0 deletions test/IDE/complete_in_ifconfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

struct MyStruct {
init() {}
var value: Int
}

// MEMBER_MyStruct: Begin completions, 2 items
// MEMBER_MyStruct-DAG: Keyword[self]/CurrNominal: self[#MyStruct#];
// MEMBER_MyStruct-DAG: Decl[InstanceVar]/CurrNominal: value[#Int#];
// MEMBER_MyStruct: End completions

#if true
let toplevelActive = MyStruct()
_ = toplevelActive.#^MEMBER_TOPLEVEL_ACTIVE?check=MEMBER_MyStruct^#
#else
let toplevelInactive = MyStruct()
_ = toplevelInactive.#^MEMBER_TOPLEVEL_INACTIVE?check=MEMBER_MyStruct^#
#endif

func foo() {
#if true
let infuncActive = MyStruct()
_ = infuncActive.#^MEMBER_INFUNC_ACTIVE?check=MEMBER_MyStruct^#
#else
let infuncInactive = MyStruct()
_ = infuncInactive.#^MEMBER_INFUNC_INACTIVE?check=MEMBER_MyStruct^#
#endif
}

protocol TestP {
func foo()
func bar()
}
struct TestStruct: TestP {
#if true
func foo() {}
func #^OVERRIDE_ACTIVE^#
// OVERRIDE_ACTIVE: Begin completions, 1 items
// OVERRIDE_ACTIVE-DAG: Decl[InstanceMethod]/Super: bar() {|};
// OVERRIDE_ACTIVE: End completions
#else
func bar() {}
func #^OVERRIDE_INACTIVE^#
// OVERRIDE_INACTIVE: Begin completions, 1 items
// OVERRIDE_INACTIVE-DAG: Decl[InstanceMethod]/Super: foo() {|};
// OVERRIDE_INACTIVE: End completions
#endif
}

struct TestStruct2 {
#if true
func activeFunc() {}
func test() {
self.#^SELF_ACTIVE^#
}
// SELF_ACTIVE: Begin completions, 3 items
// SELF_ACTIVE-DAG: Keyword[self]/CurrNominal: self[#TestStruct2#];
// SELF_ACTIVE-DAG: Decl[InstanceMethod]/CurrNominal: activeFunc()[#Void#];
// SELF_ACTIVE-DAG: Decl[InstanceMethod]/CurrNominal: test()[#Void#];
// SELF_ACTIVE: End completions
#else
func inactiveFunc() {}
func test() {
self.#^SELF_INACTIVE^#
}
// SELF_INACTIVE: Begin completions, 3 items
// SELF_INACTIVE-DAG: Keyword[self]/CurrNominal: self[#TestStruct2#];
// SELF_INACTIVE-DAG: Decl[InstanceMethod]/CurrNominal: inactiveFunc()[#Void#];
// SELF_INACTIVE-DAG: Decl[InstanceMethod]/CurrNominal: test()[#Void#];
// SELF_INACTIVE: End completions
#endif
}

54 changes: 54 additions & 0 deletions test/SourceKit/CodeComplete/complete_sequence_in_ifconfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
struct MyStruct {
init() {}
var value: Int = 1
}

func foo(arg: MyStruct) {
#if true
_ = arg./*8:11*/
#else
_ = arg./*10:11*/
#endif
}

struct TestStruct {
#if true
func testActive(arg: MyStruct) {
_ = arg./*17:13*/
}
#else
func testInactive(arg: MyStruct) {
_ = arg./*21:13*/
}
#endif
}

// Test that (1) fast completion happens even in inactive #if blocks, and
// (2) #if in toplevel decls invalidate cached ASTContext

// RUN: %sourcekitd-test \
// RUN: -req=complete -pos=8:11 %s -- %s -parse-as-library == \
// RUN: -req=complete -pos=10:11 %s -- %s -parse-as-library == \
// RUN: -req=complete -pos=17:13 %s -- %s -parse-as-library == \
// RUN: -req=complete -pos=21:13 %s -- %s -parse-as-library \
// RUN: | %FileCheck %s --check-prefix=RESULT

// RESULT-LABEL: key.results: [
// RESULT-DAG: key.description: "value"
// RESULT: ]
// RESULT-NOT: key.reusingastcontext: 1

// RESULT-LABEL: key.results: [
// RESULT-DAG: key.description: "value"
// RESULT: ]
// RESULT: key.reusingastcontext: 1

// RESULT-LABEL: key.results: [
// RESULT-DAG: key.description: "value"
// RESULT: ]
// RESULT: key.reusingastcontext: 1

// RESULT-LABEL: key.results: [
// RESULT-DAG: key.description: "value"
// RESULT: ]
// RESULT-NOT: key.reusingastcontext: 1