@@ -59,19 +59,28 @@ class ContextFinder : public SourceEntityWalker {
59
59
}
60
60
return Result;
61
61
}
62
+
62
63
public:
63
64
ContextFinder (SourceFile &SF, ASTNode TargetNode,
64
65
std::function<bool (ASTNode)> IsContext =
65
66
[](ASTNode N) { return true ; }) :
66
67
SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
67
68
Target (TargetNode.getSourceRange()), IsContext(IsContext) {}
69
+
68
70
ContextFinder (SourceFile &SF, SourceLoc TargetLoc,
69
71
std::function<bool (ASTNode)> IsContext =
70
72
[](ASTNode N) { return true ; }) :
71
73
SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
72
74
Target (TargetLoc), IsContext(IsContext) {
73
75
assert (TargetLoc.isValid () && " Invalid loc to find" );
74
- }
76
+ }
77
+
78
+ // Only need expansions for the expands refactoring, but we
79
+ // skip nodes that don't contain the passed location anyway.
80
+ virtual MacroWalking getMacroWalkingBehavior () const override {
81
+ return MacroWalking::ArgumentsAndExpansion;
82
+ }
83
+
75
84
bool walkToDeclPre (Decl *D, CharSourceRange Range) override { return contains (D); }
76
85
bool walkToStmtPre (Stmt *S) override { return contains (S); }
77
86
bool walkToExprPre (Expr *E) override { return contains (E); }
@@ -8497,6 +8506,15 @@ static Optional<unsigned> getMacroExpansionBuffer(
8497
8506
return None;
8498
8507
}
8499
8508
8509
+ // / Retrieve the macro expansion buffer for the given macro expansion
8510
+ // / declaration.
8511
+ static Optional<unsigned >
8512
+ getMacroExpansionBuffer (SourceManager &sourceMgr,
8513
+ MacroExpansionDecl *expansion) {
8514
+ return evaluateOrDefault (expansion->getASTContext ().evaluator ,
8515
+ ExpandMacroExpansionDeclRequest{expansion}, {});
8516
+ }
8517
+
8500
8518
// / Retrieve the macro expansion buffers for the given attached macro reference.
8501
8519
static llvm::SmallVector<unsigned , 2 >
8502
8520
getMacroExpansionBuffers (MacroDecl *macro, const CustomAttr *attr, Decl *decl) {
@@ -8584,13 +8602,14 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
8584
8602
// FIXME: A resolved cursor should contain a slice up to its reference.
8585
8603
// We shouldn't need to find it again.
8586
8604
ContextFinder Finder (*Info->getSourceFile (), Info->getLoc (), [&](ASTNode N) {
8587
- if (N.getStartLoc () == Info->getLoc ())
8588
- return true ;
8589
-
8590
- // TODO: Handle MacroExpansionDecl
8591
8605
if (auto *expr =
8592
8606
dyn_cast_or_null<MacroExpansionExpr>(N.dyn_cast <Expr *>())) {
8593
- return expr->getMacroNameLoc ().getBaseNameLoc () == Info->getLoc ();
8607
+ return expr->getStartLoc () == Info->getLoc () ||
8608
+ expr->getMacroNameLoc ().getBaseNameLoc () == Info->getLoc ();
8609
+ } else if (auto *decl =
8610
+ dyn_cast_or_null<MacroExpansionDecl>(N.dyn_cast <Decl *>())) {
8611
+ return decl->getStartLoc () == Info->getLoc () ||
8612
+ decl->getMacroNameLoc ().getBaseNameLoc () == Info->getLoc ();
8594
8613
}
8595
8614
8596
8615
return false ;
@@ -8599,8 +8618,11 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
8599
8618
8600
8619
if (!Finder.getContexts ().empty ()) {
8601
8620
Optional<unsigned > bufferID;
8602
- if (auto *target = dyn_cast<MacroExpansionExpr>(
8603
- Finder.getContexts ()[0 ].get <Expr *>())) {
8621
+ if (auto *target = dyn_cast_or_null<MacroExpansionExpr>(
8622
+ Finder.getContexts ()[0 ].dyn_cast <Expr *>())) {
8623
+ bufferID = getMacroExpansionBuffer (sourceMgr, target);
8624
+ } else if (auto *target = dyn_cast_or_null<MacroExpansionDecl>(
8625
+ Finder.getContexts ()[0 ].dyn_cast <Decl *>())) {
8604
8626
bufferID = getMacroExpansionBuffer (sourceMgr, target);
8605
8627
}
8606
8628
0 commit comments