Skip to content

Commit 0c3a442

Browse files
committed
Add Mapper::IsLibcPassthrough
1 parent 680424e commit 0c3a442

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,11 +1524,9 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
15241524
}
15251525

15261526
if (Mapper::Contains(expr->getCallee())) {
1527-
if (auto tgt_ir = Mapper::GetExprRule(GetCalleeOrExpr(expr))) {
1528-
if (tgt_ir->body.empty() && tgt_ir->is_variadic) {
1529-
ConvertGenericCallExpr(expr);
1530-
return false;
1531-
}
1527+
if (Mapper::IsLibcPassthrough(GetCalleeOrExpr(expr))) {
1528+
ConvertGenericCallExpr(expr);
1529+
return false;
15321530
}
15331531

15341532
auto **args = expr->getArgs();
@@ -1631,12 +1629,7 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) {
16311629
function ? function->getNumParams() : proto->getNumParams();
16321630
info.is_variadic = function ? function->isVariadic() : proto->isVariadic();
16331631
info.is_fn_ptr_call = !function;
1634-
info.is_libc_passthrough = false;
1635-
if (auto tgt_ir = Mapper::GetExprRule(GetCalleeOrExpr(expr))) {
1636-
if (tgt_ir->body.empty() && tgt_ir->is_variadic) {
1637-
info.is_libc_passthrough = true;
1638-
}
1639-
}
1632+
info.is_libc_passthrough = Mapper::IsLibcPassthrough(GetCalleeOrExpr(expr));
16401633

16411634
for (unsigned i = 0; i < num_named_params && i < num_args; ++i) {
16421635
auto *arg = expr->getArg(i + arg_begin);

cpp2rust/converter/mapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,19 @@ const TranslationRule::ExprRule *GetExprRule(const clang::Expr *expr) {
588588
return search(expr);
589589
}
590590

591+
bool IsLibcPassthrough(const clang::Expr *expr) {
592+
const auto *tgt_ir = GetExprRule(expr);
593+
if (tgt_ir == nullptr || !tgt_ir->body.empty() || !tgt_ir->is_extern) {
594+
return false;
595+
}
596+
const auto *ref =
597+
clang::dyn_cast<clang::DeclRefExpr>(expr->IgnoreParenImpCasts());
598+
const auto *decl = ref != nullptr ? ref->getDecl() : nullptr;
599+
return decl != nullptr &&
600+
decl->getASTContext().getSourceManager().isInSystemHeader(
601+
decl->getLocation());
602+
}
603+
591604
std::string MapFunctionName(const clang::FunctionDecl *decl) {
592605
assert(decl);
593606
if (!IsUserDefinedDecl(decl) &&

cpp2rust/converter/mapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bool Contains(const clang::Expr *expr);
3030
std::string Map(clang::QualType qual_type);
3131
std::string MapInitializer(clang::QualType qual_type);
3232
const TranslationRule::ExprRule *GetExprRule(const clang::Expr *expr);
33+
bool IsLibcPassthrough(const clang::Expr *expr);
3334
std::string MapFunctionName(const clang::FunctionDecl *decl);
3435
std::string InstantiateTemplate(const clang::Expr *expr, unsigned n);
3536
bool ReturnsPointer(const clang::Expr *expr);

0 commit comments

Comments
 (0)