@@ -610,27 +610,35 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
610
610
Parser::StructureMarkerRAII ParsingDecl (
611
611
*this , Tok.getLoc (), Parser::StructureMarkerKind::IfConfig);
612
612
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 ;
615
615
if (SourceMgr.hasCodeCompletionBuffer () &&
616
- SourceMgr.getCodeCompletionBufferID () == L->getBufferID ()) {
616
+ SourceMgr.getCodeCompletionBufferID () == L->getBufferID () &&
617
+ SourceMgr.isBeforeInBuffer (Tok.getLoc (),
618
+ SourceMgr.getCodeCompletionLoc ())) {
617
619
llvm::SaveAndRestore<Optional<llvm::MD5>> H (CurrentTokenHash, None);
618
620
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));
624
632
}
625
633
626
634
bool shouldEvaluate =
627
635
// Don't evaluate if it's in '-parse' mode, etc.
628
636
shouldEvaluatePoundIfDecls () &&
629
637
// If it's in inactive #if ... #endif block, there's no point to do it.
630
638
!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 () ;
634
642
635
643
bool foundActive = false ;
636
644
bool isVersionCondition = false ;
@@ -676,15 +684,8 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
676
684
}
677
685
678
686
// 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);
688
689
689
690
foundActive |= isActive;
690
691
0 commit comments