Skip to content

Commit b0c7d3b

Browse files
committed
ast-exporter: Add parameter information to macro invocations
1 parent 1051323 commit b0c7d3b

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,16 @@ class TranslateASTVisitor final
725725
std::set<std::pair<void *, ASTEntryTag>> exportedTags;
726726
std::unordered_map<MacroInfo*, MacroDeclInfo> macros;
727727

728+
struct MacroInvocationInfo {
729+
MacroInfo* macro;
730+
StringRef parameter;
731+
};
732+
728733
// This stores a raw encoding of the macro call site SourceLocation, since
729734
// SourceLocation isn't hashable.
730-
std::unordered_set<unsigned> macroCallSites;
731-
SmallVector<MacroInfo*, 1> curMacroInvocationStack;
735+
// TODO: Can this be made to use `unordered_set` for speed?
736+
std::set<std::pair<unsigned, const StringRef>> macroCallSites;
737+
SmallVector<MacroInvocationInfo, 1> curMacroInvocationStack;
732738
StringRef curMacroInvocationSource;
733739

734740
// Returns true when a new entry is added to exportedTags
@@ -815,7 +821,7 @@ class TranslateASTVisitor final
815821
if (encodeMacroInvocations) {
816822
for (auto I = curMacroInvocationStack.rbegin(), E = curMacroInvocationStack.rend();
817823
I != E; ++I) {
818-
cbor_encode_uint(&childEnc, uintptr_t(*I));
824+
cbor_encode_uint(&childEnc, uintptr_t(I->macro));
819825
}
820826
}
821827
cbor_encoder_close_container(&local, &childEnc);
@@ -903,14 +909,15 @@ class TranslateASTVisitor final
903909
isVaList(ast, T), encodeMacroInvocations, childIds, extra);
904910
}
905911

906-
bool VisitMacro(StringRef name, SourceLocation loc, MacroInfo *mac, Expr *E) {
912+
bool VisitMacro(StringRef name, StringRef parameter, SourceLocation loc, MacroInfo *mac, Expr *E) {
907913
// TODO: handle builtin macros
908914
if (mac->isBuiltinMacro())
909915
return false;
910916
// If this isn't the first time we've seen this macro call site, we
911917
// shouldn't associate this expression with the macro as it is a subexpr
912918
// of a previously seen expression.
913-
if (!macroCallSites.insert(loc.getRawEncoding()).second)
919+
std::pair<unsigned, StringRef> key { loc.getRawEncoding(), parameter };
920+
if (!macroCallSites.insert(key).second)
914921
return false;
915922
auto &info = macros[mac];
916923
if (info.Name.empty())
@@ -1400,11 +1407,20 @@ class TranslateASTVisitor final
14001407
Lexer::getSourceText(ExpansionStack[0].range, Mgr, Context->getLangOpts());
14011408

14021409
for (auto &elem : ExpansionStack) {
1410+
auto loc = elem.range.getBegin();
1411+
StringRef parameter;
1412+
14031413
if (elem.isParameter) {
1404-
continue;
1414+
auto IdentifierInfo = getIdentifierInfo(loc);
1415+
1416+
if (!IdentifierInfo) {
1417+
return true;
1418+
}
1419+
1420+
parameter = IdentifierInfo->getName();
1421+
loc = Mgr.getImmediateExpansionRange(loc).getBegin();
14051422
}
14061423

1407-
auto loc = elem.range.getBegin();
14081424
auto IdentifierInfo = getIdentifierInfo(loc);
14091425

14101426
if (!IdentifierInfo) {
@@ -1417,8 +1433,9 @@ class TranslateASTVisitor final
14171433
return true;
14181434
}
14191435

1420-
if (VisitMacro(IdentifierInfo->getName(), loc, MacroInfo, E)) {
1421-
curMacroInvocationStack.push_back(MacroInfo);
1436+
if (VisitMacro(IdentifierInfo->getName(), parameter, loc, MacroInfo, E)) {
1437+
MacroInvocationInfo info = { MacroInfo, parameter };
1438+
curMacroInvocationStack.push_back(info);
14221439
}
14231440
}
14241441

0 commit comments

Comments
 (0)