@@ -1933,9 +1933,19 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
1933
1933
if (!braceCharRange.contains (Loc))
1934
1934
return Action::SkipChildren (S);
1935
1935
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
+ }
1939
1949
1940
1950
for (ASTNode &node : brace->getElements ()) {
1941
1951
if (SM.isBeforeInBuffer (Loc, node.getStartLoc ()))
@@ -1992,6 +2002,18 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
1992
2002
PreWalkAction walkToDeclPre (Decl *D) override {
1993
2003
if (auto *newDC = dyn_cast<DeclContext>(D))
1994
2004
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
+ }
1995
2017
return Action::Continue ();
1996
2018
}
1997
2019
@@ -2005,7 +2027,7 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2005
2027
}
2006
2028
2007
2029
// Nothing found at the location, or the decl context does not own the 'Loc'.
2008
- if (finder.isNull ())
2030
+ if (finder.isNull () || !finder. getDeclContext () )
2009
2031
return true ;
2010
2032
2011
2033
DeclContext *DC = finder.getDeclContext ();
0 commit comments