@@ -2462,12 +2462,29 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2462
2462
return MacroWalking::ArgumentsAndExpansion;
2463
2463
}
2464
2464
2465
+ // / Checks whether the given range, when treated as a character range,
2466
+ // / contains the searched location.
2467
+ bool charRangeContainsLoc (SourceRange range) {
2468
+ if (!range)
2469
+ return false ;
2470
+
2471
+ if (SM.isBefore (Loc, range.Start ))
2472
+ return false ;
2473
+
2474
+ // NOTE: We need to check the character loc here because the target
2475
+ // loc can be inside the last token of the node. i.e. interpolated
2476
+ // string.
2477
+ return SM.isBefore (Loc, Lexer::getLocForEndOfToken (SM, range.End ));
2478
+ }
2479
+
2465
2480
PreWalkResult<Stmt *> walkToStmtPre (Stmt *S) override {
2466
2481
if (auto *brace = dyn_cast<BraceStmt>(S)) {
2467
- auto braceCharRange = Lexer::getCharSourceRangeFromSourceRange (
2468
- SM, brace->getSourceRange ());
2482
+ auto braceRange = brace->getSourceRange ();
2483
+ auto braceCharRange = SourceRange (
2484
+ braceRange.Start , Lexer::getLocForEndOfToken (SM, braceRange.End ));
2485
+
2469
2486
// Unless this brace contains the loc, there's nothing to do.
2470
- if (!braceCharRange. contains ( Loc))
2487
+ if (!SM. containsLoc (braceCharRange, Loc))
2471
2488
return Action::SkipNode (S);
2472
2489
2473
2490
// Reset the node found in a parent context if it's not part of this
@@ -2477,22 +2494,22 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2477
2494
// syntactically part of the brace stmt's range but won't be walked as
2478
2495
// a child of the brace stmt.
2479
2496
if (!brace->isImplicit () && FoundNode) {
2480
- auto foundNodeCharRange = Lexer::getCharSourceRangeFromSourceRange (
2481
- SM, FoundNode->getSourceRange ());
2482
- if (!braceCharRange.contains (foundNodeCharRange)) {
2497
+ auto foundRange = FoundNode->getSourceRange ();
2498
+ auto foundCharRange = SourceRange (
2499
+ foundRange.Start , Lexer::getLocForEndOfToken (SM, foundRange.End ));
2500
+ if (!SM.encloses (braceCharRange, foundCharRange))
2483
2501
FoundNode = nullptr ;
2484
- }
2485
2502
}
2486
2503
2487
2504
for (ASTNode &node : brace->getElements ()) {
2488
- if (SM.isBeforeInBuffer (Loc, node.getStartLoc ()))
2505
+ auto range = node.getSourceRange ();
2506
+ if (SM.isBefore (Loc, range.Start ))
2489
2507
break ;
2490
2508
2491
2509
// NOTE: We need to check the character loc here because the target
2492
2510
// loc can be inside the last token of the node. i.e. interpolated
2493
2511
// string.
2494
- SourceLoc endLoc = Lexer::getLocForEndOfToken (SM, node.getEndLoc ());
2495
- if (SM.isBeforeInBuffer (endLoc, Loc) || endLoc == Loc)
2512
+ if (!SM.isBefore (Loc, Lexer::getLocForEndOfToken (SM, range.End )))
2496
2513
continue ;
2497
2514
2498
2515
// 'node' may be the target node, except 'CaseStmt' which cannot be
@@ -2509,13 +2526,11 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2509
2526
return Action::Stop ();
2510
2527
} else if (auto Conditional = dyn_cast<LabeledConditionalStmt>(S)) {
2511
2528
for (StmtConditionElement &Cond : Conditional->getCond ()) {
2512
- if (SM.isBeforeInBuffer (Loc, Cond.getStartLoc ())) {
2529
+ auto range = Cond.getSourceRange ();
2530
+ if (SM.isBefore (Loc, range.Start ))
2513
2531
break ;
2514
- }
2515
- SourceLoc endLoc = Lexer::getLocForEndOfToken (SM, Cond.getEndLoc ());
2516
- if (SM.isBeforeInBuffer (endLoc, Loc) || endLoc == Loc) {
2532
+ if (!SM.isBefore (Loc, Lexer::getLocForEndOfToken (SM, range.End )))
2517
2533
continue ;
2518
- }
2519
2534
2520
2535
FoundNodeStorage = ASTNode (&Cond);
2521
2536
FoundNode = &FoundNodeStorage;
@@ -2527,11 +2542,7 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2527
2542
}
2528
2543
2529
2544
PreWalkResult<Expr *> walkToExprPre (Expr *E) override {
2530
- if (SM.isBeforeInBuffer (Loc, E->getStartLoc ()))
2531
- return Action::SkipNode (E);
2532
-
2533
- SourceLoc endLoc = Lexer::getLocForEndOfToken (SM, E->getEndLoc ());
2534
- if (SM.isBeforeInBuffer (endLoc, Loc))
2545
+ if (!charRangeContainsLoc (E->getSourceRange ()))
2535
2546
return Action::SkipNode (E);
2536
2547
2537
2548
// Don't walk into 'TapExpr'. They should be type checked with parent
@@ -2546,9 +2557,7 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2546
2557
if (auto *SVE = dyn_cast<SingleValueStmtExpr>(E)) {
2547
2558
SmallVector<Expr *> scratch;
2548
2559
for (auto *result : SVE->getResultExprs (scratch)) {
2549
- auto resultCharRange = Lexer::getCharSourceRangeFromSourceRange (
2550
- SM, result->getSourceRange ());
2551
- if (resultCharRange.contains (Loc)) {
2560
+ if (charRangeContainsLoc (result->getSourceRange ())) {
2552
2561
if (!result->walk (*this ))
2553
2562
return Action::Stop ();
2554
2563
@@ -2570,20 +2579,15 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2570
2579
}
2571
2580
2572
2581
PreWalkAction walkToDeclPre (Decl *D) override {
2582
+ if (!charRangeContainsLoc (D->getSourceRange ()))
2583
+ return Action::SkipNode ();
2584
+
2573
2585
if (auto *newDC = dyn_cast<DeclContext>(D))
2574
2586
DC = newDC;
2575
2587
2576
- if (!SM.isBeforeInBuffer (Loc, D->getStartLoc ())) {
2577
- // NOTE: We need to check the character loc here because the target
2578
- // loc can be inside the last token of the node. i.e. interpolated
2579
- // string.
2580
- SourceLoc endLoc = Lexer::getLocForEndOfToken (SM, D->getEndLoc ());
2581
- if (!(SM.isBeforeInBuffer (endLoc, Loc) || endLoc == Loc)) {
2582
- if (!isa<TopLevelCodeDecl>(D)) {
2583
- FoundNodeStorage = ASTNode (D);
2584
- FoundNode = &FoundNodeStorage;
2585
- }
2586
- }
2588
+ if (!isa<TopLevelCodeDecl>(D)) {
2589
+ FoundNodeStorage = ASTNode (D);
2590
+ FoundNode = &FoundNodeStorage;
2587
2591
}
2588
2592
return Action::Continue ();
2589
2593
}
0 commit comments