@@ -1933,9 +1933,19 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
19331933 if (!braceCharRange.contains (Loc))
19341934 return Action::SkipChildren (S);
19351935
1936- // Reset the node found in a parent context.
1937- if (!brace->isImplicit ())
1938- FoundNode = nullptr ;
1936+ // Reset the node found in a parent context if it's not part of this
1937+ // brace statement.
1938+ // We must not reset FoundNode if it's inside thei BraceStmt's source
1939+ // range because the found node could be inside a capture list, which is
1940+ // syntactically part of the brace stmt's range but won't be walked as
1941+ // a child of the brace stmt.
1942+ if (!brace->isImplicit () && FoundNode) {
1943+ auto foundNodeCharRange = Lexer::getCharSourceRangeFromSourceRange (
1944+ SM, FoundNode->getSourceRange ());
1945+ if (!braceCharRange.contains (foundNodeCharRange)) {
1946+ FoundNode = nullptr ;
1947+ }
1948+ }
19391949
19401950 for (ASTNode &node : brace->getElements ()) {
19411951 if (SM.isBeforeInBuffer (Loc, node.getStartLoc ()))
@@ -1992,6 +2002,18 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
19922002 PreWalkAction walkToDeclPre (Decl *D) override {
19932003 if (auto *newDC = dyn_cast<DeclContext>(D))
19942004 DC = newDC;
2005+
2006+ if (!SM.isBeforeInBuffer (Loc, D->getStartLoc ())) {
2007+ // NOTE: We need to check the character loc here because the target
2008+ // loc can be inside the last token of the node. i.e. interpolated
2009+ // string.
2010+ SourceLoc endLoc = Lexer::getLocForEndOfToken (SM, D->getEndLoc ());
2011+ if (!(SM.isBeforeInBuffer (endLoc, Loc) || endLoc == Loc)) {
2012+ if (!isa<TopLevelCodeDecl>(D)) {
2013+ FoundNode = new ASTNode (D);
2014+ }
2015+ }
2016+ }
19952017 return Action::Continue ();
19962018 }
19972019
@@ -2005,7 +2027,7 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
20052027 }
20062028
20072029 // Nothing found at the location, or the decl context does not own the 'Loc'.
2008- if (finder.isNull ())
2030+ if (finder.isNull () || !finder. getDeclContext () )
20092031 return true ;
20102032
20112033 DeclContext *DC = finder.getDeclContext ();
0 commit comments