Skip to content

Commit 29c07b0

Browse files
committed
ast-exporter: Skip over parameter expansions in VisitExpr
1 parent eb9a975 commit 29c07b0

3 files changed

Lines changed: 43 additions & 25 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,38 +1421,56 @@ class TranslateASTVisitor final
14211421
}
14221422

14231423
curMacroExpansionSource =
1424-
Lexer::getSourceText(ExpansionStack[0], Mgr, Context->getLangOpts());
1424+
Lexer::getSourceText(ExpansionStack[0].range, Mgr, Context->getLangOpts());
1425+
1426+
for (auto &elem : ExpansionStack) {
1427+
if (elem.isParameter) {
1428+
continue;
1429+
}
14251430

1426-
for (auto &ExpansionRange : ExpansionStack) {
14271431
StringRef name;
1428-
MacroInfo *mac = getMacroInfo(ExpansionRange.getBegin(), name);
1432+
MacroInfo *mac = getMacroInfo(elem.range.getBegin(), name);
14291433

14301434
if (!mac || mac->getNumTokens() == 0) {
14311435
return true;
14321436
}
14331437

1434-
if (VisitMacro(name, ExpansionRange.getBegin(), mac, E)) {
1438+
if (VisitMacro(name, elem.range.getBegin(), mac, E)) {
14351439
curMacroExpansionStack.push_back(mac);
14361440
}
14371441
}
14381442

14391443
return true;
14401444
}
14411445

1442-
std::vector<CharSourceRange> getMacroExpansionStack(SourceRange Range) const {
1446+
struct ExpansionRange {
1447+
CharSourceRange range;
1448+
bool isParameter;
1449+
1450+
bool operator== (const ExpansionRange &rhs) const {
1451+
return this->range.getAsRange() == rhs.range.getAsRange()
1452+
&& this->range.isTokenRange() == rhs.range.isTokenRange()
1453+
&& this->isParameter == rhs.isParameter;
1454+
}
1455+
};
1456+
1457+
std::vector<ExpansionRange> getMacroExpansionStack(SourceRange Range) const {
14431458
auto &Mgr = Context->getSourceManager();
14441459
auto Begin = Range.getBegin();
14451460
auto End = Range.getEnd();
1446-
std::vector<CharSourceRange> ExpansionStack;
1461+
std::vector<ExpansionRange> ExpansionStack;
14471462

14481463
do {
14491464
if (!isAtStartOfImmediateMacroExpansion(Begin)) {
14501465
break;
14511466
}
14521467

1453-
auto ExpansionRange = Mgr.getImmediateExpansionRange(Begin);
1454-
ExpansionStack.push_back(ExpansionRange);
1455-
Begin = ExpansionRange.getBegin();
1468+
ExpansionRange elem = {
1469+
Mgr.getImmediateExpansionRange(Begin),
1470+
Mgr.isMacroArgExpansion(Begin)
1471+
};
1472+
Begin = elem.range.getBegin();
1473+
ExpansionStack.push_back(elem);
14561474
} while (Begin.isMacroID());
14571475

14581476
// Find the point at which `Begin` and `End` converge on the same expansion range.
@@ -1464,16 +1482,12 @@ class TranslateASTVisitor final
14641482
break;
14651483
}
14661484

1467-
auto ExpansionRange = Mgr.getImmediateExpansionRange(End);
1468-
ConvergencePoint = std::find_if(
1469-
ExpansionStack.begin(),
1470-
ExpansionStack.end(),
1471-
[ExpansionRange](auto &R) {
1472-
return R.getAsRange() == ExpansionRange.getAsRange() &&
1473-
R.isTokenRange() == ExpansionRange.isTokenRange();
1474-
}
1475-
);
1476-
End = ExpansionRange.getEnd();
1485+
ExpansionRange elem = {
1486+
Mgr.getImmediateExpansionRange(End),
1487+
Mgr.isMacroArgExpansion(End)
1488+
};
1489+
End = elem.range.getEnd();
1490+
ConvergencePoint = std::find(ExpansionStack.begin(), ExpansionStack.end(), elem);
14771491
} while (End.isMacroID() && ConvergencePoint == ExpansionStack.end());
14781492

14791493
// Remove all elements before the convergence point.
@@ -1483,8 +1497,8 @@ class TranslateASTVisitor final
14831497
auto EraseAfter = std::find_if(
14841498
ExpansionStack.begin(),
14851499
ExpansionStack.end(),
1486-
[this](auto &R) {
1487-
auto End = R.getEnd();
1500+
[this](auto &elem) {
1501+
auto End = elem.range.getEnd();
14881502
return End.isMacroID() && !isAtEndOfImmediateMacroExpansion(End);
14891503
}
14901504
);

c2rust-transpile/tests/snapshots/snapshots__transpile@macro_fns.c.2021.clang15.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ expression: cat tests/snapshots/macro_fns.2021.clang15.rs
1111
unused_assignments,
1212
unused_mut
1313
)]
14+
pub const LIT_ID: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1415
pub const LIT_PAREN: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
16+
pub const CONST_ID: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1517
pub const CONST_PAREN: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1618
#[no_mangle]
1719
pub unsafe extern "C" fn basic() {
1820
let mut id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1921
let mut paren: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
20-
let mut lit_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
22+
let mut lit_id: ::core::ffi::c_int = LIT_ID;
2123
let mut lit_paren: ::core::ffi::c_int = LIT_PAREN;
22-
let mut const_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
24+
let mut const_id: ::core::ffi::c_int = CONST_ID;
2325
let mut const_paren: ::core::ffi::c_int = CONST_PAREN;
2426
let mut nested_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
2527
let mut nested_paren: ::core::ffi::c_int = 42 as ::core::ffi::c_int;

c2rust-transpile/tests/snapshots/snapshots__transpile@macro_fns.c.2024.clang15.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ expression: cat tests/snapshots/macro_fns.2024.clang15.rs
1212
unused_assignments,
1313
unused_mut
1414
)]
15+
pub const LIT_ID: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1516
pub const LIT_PAREN: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
17+
pub const CONST_ID: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1618
pub const CONST_PAREN: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
1719
#[unsafe(no_mangle)]
1820
pub unsafe extern "C" fn basic() {
1921
let mut id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
2022
let mut paren: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
21-
let mut lit_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
23+
let mut lit_id: ::core::ffi::c_int = LIT_ID;
2224
let mut lit_paren: ::core::ffi::c_int = LIT_PAREN;
23-
let mut const_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
25+
let mut const_id: ::core::ffi::c_int = CONST_ID;
2426
let mut const_paren: ::core::ffi::c_int = CONST_PAREN;
2527
let mut nested_id: ::core::ffi::c_int = 42 as ::core::ffi::c_int;
2628
let mut nested_paren: ::core::ffi::c_int = 42 as ::core::ffi::c_int;

0 commit comments

Comments
 (0)