@@ -49,27 +49,33 @@ class ContextFinder : public SourceEntityWalker {
49
49
ASTContext &Ctx;
50
50
SourceManager &SM;
51
51
SourceRange Target;
52
- std::function< bool (ASTNode)> IsContext ;
52
+ SourceRange TargetInOriginalBuffer ;
53
53
SmallVector<ASTNode, 4 > AllContexts;
54
54
bool contains (ASTNode Enclosing) {
55
- auto Result = SM.rangeContains (Enclosing.getSourceRange (), Target);
56
- if (Result && IsContext (Enclosing))
55
+ auto Result =
56
+ SM.rangeContains (Enclosing.getSourceRange (), Target) ||
57
+ SM.rangeContains (Enclosing.getSourceRange (), TargetInOriginalBuffer);
58
+ if (Result && IsContext (Enclosing)) {
57
59
AllContexts.push_back (Enclosing);
60
+ }
58
61
return Result;
59
62
}
60
63
public:
61
- ContextFinder (SourceFile &SF, ASTNode TargetNode,
62
- std::function<bool (ASTNode)> IsContext =
63
- [](ASTNode N) { return true ; }) :
64
- SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
65
- Target (TargetNode.getSourceRange()), IsContext(IsContext) {}
66
- ContextFinder (SourceFile &SF, SourceLoc TargetLoc,
67
- std::function<bool (ASTNode)> IsContext =
68
- [](ASTNode N) { return true ; }) :
69
- SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
70
- Target (TargetLoc), IsContext(IsContext) {
71
- assert (TargetLoc.isValid () && " Invalid loc to find" );
72
- }
64
+ ContextFinder (
65
+ SourceFile &SF, ASTNode TargetNode,
66
+ std::function<bool (ASTNode)> IsContext = [](ASTNode N) { return true ; })
67
+ : SF(SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
68
+ Target (TargetNode.getSourceRange()),
69
+ TargetInOriginalBuffer(TargetNode.getSourceRange()),
70
+ IsContext(IsContext) {}
71
+ ContextFinder (
72
+ SourceFile &SF, SourceLoc TargetLoc, SourceLoc TargetLocInOriginalBuffer,
73
+ std::function<bool (ASTNode)> IsContext = [](ASTNode N) { return true ; })
74
+ : SF(SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr), Target(TargetLoc),
75
+ TargetInOriginalBuffer (TargetLocInOriginalBuffer),
76
+ IsContext(IsContext) {
77
+ assert (TargetLoc.isValid () && " Invalid loc to find" );
78
+ }
73
79
bool walkToDeclPre (Decl *D, CharSourceRange Range) override { return contains (D); }
74
80
bool walkToStmtPre (Stmt *S) override { return contains (S); }
75
81
bool walkToExprPre (Expr *E) override { return contains (E); }
@@ -3928,10 +3934,11 @@ static CallExpr *findTrailingClosureTarget(
3928
3934
return nullptr ;
3929
3935
3930
3936
// Find inner most CallExpr
3931
- ContextFinder Finder (
3932
- *CursorInfo.getSourceFile (), CursorInfo.getLoc (), [](ASTNode N) {
3933
- return N.isStmt (StmtKind::Brace) || N.isExpr (ExprKind::Call);
3934
- });
3937
+ ContextFinder Finder (*CursorInfo.getSourceFile (), CursorInfo.getLoc (),
3938
+ CursorInfo.getLocInOriginalBuffer (), [](ASTNode N) {
3939
+ return N.isStmt (StmtKind::Brace) ||
3940
+ N.isExpr (ExprKind::Call);
3941
+ });
3935
3942
Finder.resolve ();
3936
3943
auto contexts = Finder.getContexts ();
3937
3944
if (contexts.empty ())
@@ -4165,7 +4172,7 @@ CallExpr *findOuterCall(const ResolvedCursorInfo &CursorInfo) {
4165
4172
// already generated a slice of the AST for anything that contains
4166
4173
// the cursor location
4167
4174
ContextFinder Finder (*CursorInfo.getSourceFile (), CursorInfo.getLoc (),
4168
- IncludeInContext);
4175
+ CursorInfo. getLocInOriginalBuffer (), IncludeInContext);
4169
4176
Finder.resolve ();
4170
4177
auto Contexts = Finder.getContexts ();
4171
4178
if (Contexts.empty ())
@@ -4191,7 +4198,7 @@ FuncDecl *findFunction(const ResolvedCursorInfo &CursorInfo) {
4191
4198
};
4192
4199
4193
4200
ContextFinder Finder (*CursorInfo.getSourceFile (), CursorInfo.getLoc (),
4194
- IncludeInContext);
4201
+ CursorInfo. getLocInOriginalBuffer (), IncludeInContext);
4195
4202
Finder.resolve ();
4196
4203
4197
4204
auto Contexts = Finder.getContexts ();
@@ -8282,9 +8289,10 @@ bool RefactoringActionConvertCallToAsyncAlternative::performChange() {
8282
8289
" Should not run performChange when refactoring is not applicable" );
8283
8290
8284
8291
// Find the scope this call is in
8285
- ContextFinder Finder (
8286
- *CursorInfo.getSourceFile (), CursorInfo.getLoc (),
8287
- [](ASTNode N) { return N.isStmt (StmtKind::Brace) && !N.isImplicit (); });
8292
+ ContextFinder Finder (*CursorInfo.getSourceFile (), CursorInfo.getLoc (),
8293
+ CursorInfo.getLocInOriginalBuffer (), [](ASTNode N) {
8294
+ return N.isStmt (StmtKind::Brace) && !N.isImplicit ();
8295
+ });
8288
8296
Finder.resolve ();
8289
8297
auto Scopes = Finder.getContexts ();
8290
8298
BraceStmt *Scope = nullptr ;
@@ -8437,7 +8445,8 @@ findMacroExpansionTargetExpr(const ResolvedCursorInfo &Info) {
8437
8445
if (auto refInfo = dyn_cast<ResolvedValueRefCursorInfo>(&Info)) {
8438
8446
if (refInfo->isRef () && isa_and_nonnull<MacroDecl>(refInfo->getValueD ())) {
8439
8447
ContextFinder Finder (
8440
- *Info.getSourceFile (), Info.getLoc (), [&](ASTNode N) {
8448
+ *Info.getSourceFile (), Info.getLoc (), Info.getLocInOriginalBuffer (),
8449
+ [&](ASTNode N) {
8441
8450
auto *expr =
8442
8451
dyn_cast_or_null<MacroExpansionExpr>(N.dyn_cast <Expr *>());
8443
8452
return expr &&
0 commit comments