Skip to content

Commit 6cfdaf6

Browse files
committed
[CodeCompletion] Reduce backtracking for searching CC token in IfConfig
1 parent 7198b3b commit 6cfdaf6

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

lib/Parse/ParseIfConfig.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -610,27 +610,35 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
610610
Parser::StructureMarkerRAII ParsingDecl(
611611
*this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig);
612612

613-
// See if this '#if ... #endif' directive contains code completion token.
614-
bool hasCCToken = false;
613+
// Find the region containing code completion token.
614+
SourceLoc codeCompletionClauseLoc;
615615
if (SourceMgr.hasCodeCompletionBuffer() &&
616-
SourceMgr.getCodeCompletionBufferID() == L->getBufferID()) {
616+
SourceMgr.getCodeCompletionBufferID() == L->getBufferID() &&
617+
SourceMgr.isBeforeInBuffer(Tok.getLoc(),
618+
SourceMgr.getCodeCompletionLoc())) {
617619
llvm::SaveAndRestore<Optional<llvm::MD5>> H(CurrentTokenHash, None);
618620
BacktrackingScope backtrack(*this);
619-
auto startLoc = Tok.getLoc();
620-
skipSingle();
621-
auto endLoc = PreviousLoc;
622-
hasCCToken = SourceMgr.rangeContainsTokenLoc(
623-
SourceRange(startLoc, endLoc), SourceMgr.getCodeCompletionLoc());
621+
do {
622+
auto startLoc = Tok.getLoc();
623+
consumeToken();
624+
skipUntilConditionalBlockClose();
625+
auto endLoc = PreviousLoc;
626+
if (SourceMgr.rangeContainsTokenLoc(SourceRange(startLoc, endLoc),
627+
SourceMgr.getCodeCompletionLoc())){
628+
codeCompletionClauseLoc = startLoc;
629+
break;
630+
}
631+
} while (Tok.isNot(tok::pound_endif, tok::eof));
624632
}
625633

626634
bool shouldEvaluate =
627635
// Don't evaluate if it's in '-parse' mode, etc.
628636
shouldEvaluatePoundIfDecls() &&
629637
// If it's in inactive #if ... #endif block, there's no point to do it.
630638
!getScopeInfo().isInactiveConfigBlock() &&
631-
// If this directive contains code completion, 'isActive' is determined
632-
// solely by which block has the completion token.
633-
!hasCCToken;
639+
// If this directive contains code completion location, 'isActive' is
640+
// determined solely by which block has the completion token.
641+
!codeCompletionClauseLoc.isValid();
634642

635643
bool foundActive = false;
636644
bool isVersionCondition = false;
@@ -676,15 +684,8 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
676684
}
677685

678686
// Treat the region containing code completion token as "active".
679-
if (hasCCToken && !foundActive) {
680-
llvm::SaveAndRestore<Optional<llvm::MD5>> H(CurrentTokenHash, None);
681-
BacktrackingScope backtrack(*this);
682-
auto startLoc = Tok.getLoc();
683-
skipUntilConditionalBlockClose();
684-
auto endLoc = PreviousLoc;
685-
isActive = SourceMgr.rangeContainsTokenLoc(
686-
SourceRange(startLoc, endLoc), SourceMgr.getCodeCompletionLoc());
687-
}
687+
if (codeCompletionClauseLoc.isValid() && !foundActive)
688+
isActive = (ClauseLoc == codeCompletionClauseLoc);
688689

689690
foundActive |= isActive;
690691

0 commit comments

Comments
 (0)