Skip to content

Commit 9a6b897

Browse files
committed
ast-exporter: In VisitExpr, pre-compute the macro expansion stack
1 parent aa01ca4 commit 9a6b897

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,34 +1381,38 @@ class TranslateASTVisitor final
13811381
// return true;
13821382

13831383
auto &Mgr = Context->getSourceManager();
1384-
auto Range = E->getSourceRange();
1384+
auto ExprRange = E->getSourceRange();
13851385
LLVM_DEBUG(dbgs() << "Checking expr for macro expansion: ");
13861386
LLVM_DEBUG(E->dump());
1387-
LLVM_DEBUG(Range.getBegin().dump(Mgr));
1388-
LLVM_DEBUG(Range.getEnd().dump(Mgr));
1387+
LLVM_DEBUG(ExprRange.getBegin().dump(Mgr));
1388+
LLVM_DEBUG(ExprRange.getEnd().dump(Mgr));
13891389

1390-
auto Begin = Range.getBegin();
1391-
auto End = Range.getEnd();
1390+
auto Begin = ExprRange.getBegin();
1391+
auto End = ExprRange.getEnd();
13921392

13931393
// Check that we are only expanding a single macro call.
13941394
if (!Begin.isMacroID() || !End.isMacroID() ||
13951395
Mgr.getImmediateMacroCallerLoc(Begin) != Mgr.getImmediateMacroCallerLoc(End)) {
13961396
return true;
13971397
}
13981398

1399-
auto ExpansionRange = Mgr.getImmediateExpansionRange(Begin);
1399+
// Holds the stack of ranges of macro expansions.
1400+
// The last element is the top-level macro call.
1401+
std::vector<CharSourceRange> ExpansionStack;
1402+
1403+
do {
1404+
auto ExpansionRange = Mgr.getImmediateExpansionRange(Begin);
1405+
ExpansionStack.push_back(ExpansionRange);
1406+
Begin = ExpansionRange.getBegin();
1407+
} while (Begin.isMacroID());
1408+
14001409
curMacroExpansionSource =
1401-
Lexer::getSourceText(ExpansionRange, Mgr, Context->getLangOpts());
1402-
1403-
// The macro stack unwound by getImmediateMacroCallerLoc and friends
1404-
// starts with literal replacement and works it's way to the macro call
1405-
// that was replaced.
1406-
while (Begin.isMacroID()) {
1407-
auto ExpansionRange = Mgr.getImmediateExpansionRange(Begin).getAsRange();
1408-
auto ExpansionBegin = ExpansionRange.getBegin();
1409-
auto ExpansionEnd = ExpansionRange.getEnd();
1410+
Lexer::getSourceText(ExpansionStack[0], Mgr, Context->getLangOpts());
1411+
1412+
auto Range = ExprRange;
1413+
for (auto ExpansionRange : ExpansionStack) {
14101414
StringRef name;
1411-
MacroInfo *mac = getMacroInfo(ExpansionBegin, name);
1415+
MacroInfo *mac = getMacroInfo(ExpansionRange.getBegin(), name);
14121416

14131417
if (!mac || mac->getNumTokens() == 0) {
14141418
return true;
@@ -1419,17 +1423,16 @@ class TranslateASTVisitor final
14191423
// Verify that this expansion covers the entire macro replacement
14201424
// definition, i.e. E is not a subexpression of the macro
14211425
// replacement.
1422-
if (Mgr.getSpellingLoc(Begin) != ReplacementBegin ||
1423-
Mgr.getSpellingLoc(End) != ReplacementEnd) {
1426+
if (Mgr.getSpellingLoc(Range.getBegin()) != ReplacementBegin ||
1427+
Mgr.getSpellingLoc(Range.getEnd()) != ReplacementEnd) {
14241428
return true;
14251429
}
14261430

1427-
Begin = ExpansionBegin;
1428-
End = ExpansionEnd;
1429-
1430-
if (VisitMacro(name, Begin, mac, E)) {
1431+
if (VisitMacro(name, ExpansionRange.getBegin(), mac, E)) {
14311432
curMacroExpansionStack.push_back(mac);
14321433
}
1434+
1435+
Range = ExpansionRange.getAsRange();
14331436
}
14341437

14351438
return true;

0 commit comments

Comments
 (0)