@@ -59,19 +59,28 @@ class ContextFinder : public SourceEntityWalker {
5959 }
6060 return Result;
6161 }
62+
6263public:
6364 ContextFinder (SourceFile &SF, ASTNode TargetNode,
6465 std::function<bool (ASTNode)> IsContext =
6566 [](ASTNode N) { return true ; }) :
6667 SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
6768 Target (TargetNode.getSourceRange()), IsContext(IsContext) {}
69+
6870 ContextFinder (SourceFile &SF, SourceLoc TargetLoc,
6971 std::function<bool (ASTNode)> IsContext =
7072 [](ASTNode N) { return true ; }) :
7173 SF (SF), Ctx(SF.getASTContext()), SM(Ctx.SourceMgr),
7274 Target (TargetLoc), IsContext(IsContext) {
7375 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+
7584 bool walkToDeclPre (Decl *D, CharSourceRange Range) override { return contains (D); }
7685 bool walkToStmtPre (Stmt *S) override { return contains (S); }
7786 bool walkToExprPre (Expr *E) override { return contains (E); }
@@ -8497,6 +8506,15 @@ static Optional<unsigned> getMacroExpansionBuffer(
84978506 return None;
84988507}
84998508
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+
85008518// / Retrieve the macro expansion buffers for the given attached macro reference.
85018519static llvm::SmallVector<unsigned , 2 >
85028520getMacroExpansionBuffers (MacroDecl *macro, const CustomAttr *attr, Decl *decl) {
@@ -8584,13 +8602,14 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
85848602 // FIXME: A resolved cursor should contain a slice up to its reference.
85858603 // We shouldn't need to find it again.
85868604 ContextFinder Finder (*Info->getSourceFile (), Info->getLoc (), [&](ASTNode N) {
8587- if (N.getStartLoc () == Info->getLoc ())
8588- return true ;
8589-
8590- // TODO: Handle MacroExpansionDecl
85918605 if (auto *expr =
85928606 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 ();
85948613 }
85958614
85968615 return false ;
@@ -8599,8 +8618,11 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
85998618
86008619 if (!Finder.getContexts ().empty ()) {
86018620 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 *>())) {
86048626 bufferID = getMacroExpansionBuffer (sourceMgr, target);
86058627 }
86068628
0 commit comments