Skip to content

Commit d5d99ef

Browse files
committed
Recognize macros in Mapper
1 parent 97227e1 commit d5d99ef

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,11 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
16261626
}
16271627

16281628
bool Converter::VisitIntegerLiteral(clang::IntegerLiteral *expr) {
1629+
if (auto str = GetMappedAsString(expr); !str.empty()) {
1630+
StrCat(str);
1631+
computed_expr_type_ = ComputedExprType::FreshValue;
1632+
return false;
1633+
}
16291634
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()) != "i32"));
16301635
computed_expr_type_ = ComputedExprType::FreshValue;
16311636
return false;

cpp2rust/converter/mapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <clang/AST/ExprCXX.h>
77
#include <clang/Basic/SourceManager.h>
8+
#include <clang/Lex/Lexer.h>
89
#include <llvm/Support/ThreadPool.h>
910

1011
#include <format>
@@ -804,6 +805,16 @@ std::string ToString(const clang::Expr *expr) {
804805

805806
expr = expr->IgnoreParenImpCasts();
806807

808+
if (llvm::isa<clang::IntegerLiteral>(expr) &&
809+
expr->getBeginLoc().isMacroID()) {
810+
auto &sm = ctx_->getSourceManager();
811+
auto name = clang::Lexer::getImmediateMacroName(expr->getBeginLoc(), sm,
812+
ctx_->getLangOpts());
813+
if (!name.empty()) {
814+
return name.str();
815+
}
816+
}
817+
807818
if (const auto *CE = llvm::dyn_cast<clang::CallExpr>(expr)) {
808819
if (const auto *decl = CE->getDirectCallee()) {
809820
return ToString(decl);

cpp2rust/converter/rule_src_parser.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
188188
add(Mapper::ToString(decl));
189189
return;
190190
}
191+
if (const auto *lit = R.Nodes.getNodeAs<clang::Expr>("lit")) {
192+
auto src = Mapper::ToString(lit);
193+
if (!src.empty()) {
194+
add(std::move(src));
195+
}
196+
return;
197+
}
191198
}
192199
}
193200

@@ -689,7 +696,8 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
689696
declRefExpr(to(decl(unless(parmVarDecl()))))))
690697
.bind("udeclref"),
691698
cxxDependentScopeMemberExpr().bind("dsme"),
692-
cxxUnresolvedConstructExpr().bind("uctor"))))),
699+
cxxUnresolvedConstructExpr().bind("uctor"),
700+
integerLiteral().bind("lit"))))),
693701
hasAncestor(functionDecl(isDefinition(),
694702
matchesName("(^|::)f[0-9]+$"),
695703
isExpansionInMainFile())
@@ -701,7 +709,6 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
701709
.bind("tvar"),
702710
&cb_);
703711

704-
// Collect every f<n> for the post-run diff against bound bodies.
705712
finder_.addMatcher(functionDecl(isDefinition(),
706713
matchesName("(^|::)f[0-9]+$"),
707714
isExpansionInMainFile())
@@ -767,10 +774,7 @@ bool Extract(const std::filesystem::path &src_path, llvm::json::Object &out) {
767774
if (out.find(name) == out.end()) {
768775
llvm::errs() << src_path.string() << ": " << name
769776
<< ": matcher did not bind to the function body.\n"
770-
<< " body: " << body_src << '\n'
771-
<< " Likely cause: return expression shape not handled by "
772-
"rule_src_parser.cpp's ActionFactory matcher (e.g. a "
773-
"literal from a macro expansion).\n";
777+
<< " body: " << body_src << '\n';
774778
ok = false;
775779
}
776780
}

0 commit comments

Comments
 (0)