@@ -1417,19 +1417,20 @@ class TranslateASTVisitor final
14171417 return true ;
14181418 }
14191419
1420- auto ExpansionRange = Mgr.getImmediateExpansionRange (Begin);
1420+ // Holds the stack of ranges of macro expansions that expand to this expression.
1421+ // The last element is the top-level macro call.
1422+ auto ExpansionStack = getMacroExpansionStack (Range);
1423+
1424+ if (ExpansionStack.empty ()) {
1425+ return true ;
1426+ }
1427+
14211428 curMacroExpansionSource =
1422- Lexer::getSourceText (ExpansionRange, Mgr, Context->getLangOpts ());
1423-
1424- // The macro stack unwound by getImmediateMacroCallerLoc and friends
1425- // starts with literal replacement and works it's way to the macro call
1426- // that was replaced.
1427- while (Begin.isMacroID ()) {
1428- auto ExpansionRange = Mgr.getImmediateExpansionRange (Begin).getAsRange ();
1429- auto ExpansionBegin = ExpansionRange.getBegin ();
1430- auto ExpansionEnd = ExpansionRange.getEnd ();
1429+ Lexer::getSourceText (ExpansionStack[0 ], Mgr, Context->getLangOpts ());
1430+
1431+ for (auto &ExpansionRange : ExpansionStack) {
14311432 StringRef name;
1432- MacroInfo *mac = getMacroInfo (ExpansionBegin , name);
1433+ MacroInfo *mac = getMacroInfo (ExpansionRange. getBegin () , name);
14331434
14341435 if (!mac || mac->getNumTokens () == 0 ) {
14351436 return true ;
@@ -1440,22 +1441,39 @@ class TranslateASTVisitor final
14401441 // Verify that this expansion covers the entire macro replacement
14411442 // definition, i.e. E is not a subexpression of the macro
14421443 // replacement.
1443- if (Mgr.getSpellingLoc (Begin ) != ReplacementBegin ||
1444- Mgr.getSpellingLoc (End ) != ReplacementEnd) {
1444+ if (Mgr.getSpellingLoc (Range. getBegin () ) != ReplacementBegin ||
1445+ Mgr.getSpellingLoc (Range. getEnd () ) != ReplacementEnd) {
14451446 return true ;
14461447 }
14471448
1448- Begin = ExpansionBegin;
1449- End = ExpansionEnd;
1450-
1451- if (VisitMacro (name, Begin, mac, E)) {
1449+ if (VisitMacro (name, ExpansionRange.getBegin (), mac, E)) {
14521450 curMacroExpansionStack.push_back (mac);
14531451 }
1452+
1453+ Range = ExpansionRange.getAsRange ();
14541454 }
14551455
14561456 return true ;
14571457 }
14581458
1459+ std::vector<CharSourceRange> getMacroExpansionStack (SourceRange Range) const {
1460+ auto &Mgr = Context->getSourceManager ();
1461+ auto Begin = Range.getBegin ();
1462+ auto End = Range.getEnd ();
1463+
1464+ // Holds the stack of ranges of macro expansions.
1465+ // The last element is the top-level macro call.
1466+ std::vector<CharSourceRange> ExpansionStack;
1467+
1468+ do {
1469+ auto ExpansionRange = Mgr.getImmediateExpansionRange (Begin);
1470+ ExpansionStack.push_back (ExpansionRange);
1471+ Begin = ExpansionRange.getBegin ();
1472+ } while (Begin.isMacroID ());
1473+
1474+ return ExpansionStack;
1475+ }
1476+
14591477 bool VisitVAArgExpr (VAArgExpr *E) {
14601478 std::vector<void *> childIds{E->getSubExpr ()};
14611479 encode_entry (E, TagVAArgExpr, childIds);
0 commit comments