Skip to content

Commit 013ee33

Browse files
committed
Drop printer depdendncies in converter
1 parent 21e51a4 commit 013ee33

4 files changed

Lines changed: 58 additions & 28 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,6 @@ const clang::Expr *Converter::GetParentExpr(const clang::Expr *expr) {
17201720
bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
17211721
std::string &fmt_args, const char *&fmt_trait,
17221722
std::string &fmt_width) {
1723-
std::string arg_str = Printer::ToString(ctx_, arg);
17241723
if (auto *str_lit =
17251724
clang::dyn_cast<clang::StringLiteral>(arg->IgnoreImplicit())) {
17261725
if (!IsAsciiStringLiteral(str_lit)) {
@@ -1734,20 +1733,20 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
17341733
fmt += trim;
17351734
} else if (auto ch = GetEscapedUTF8CharLiteral(arg); !ch.empty()) {
17361735
fmt += std::move(ch);
1737-
} else if (arg_str.contains("std::endl")) {
1736+
} else if (IsStdFunction(arg, "endl")) {
17381737
fmt += "\\n";
1739-
} else if (arg_str.contains("std::hex")) {
1738+
} else if (IsStdFunction(arg, "hex")) {
17401739
fmt_trait = "x";
1741-
} else if (arg_str.contains("std::dec")) {
1740+
} else if (IsStdFunction(arg, "dec")) {
17421741
fmt_trait = "";
1743-
} else if (arg_str.contains("Setw")) {
1742+
} else if (IsStdFunction(arg, "setw")) {
17441743
fmt_width = Trim(ToString(arg));
17451744
} else if (!arg->getType()->isCharType() &&
17461745
Mapper::Map(*this, arg->getType()) !=
17471746
std::format("Vec<{}>", CharRustType())) {
17481747
fmt += ("{:" + fmt_width + fmt_trait + "}");
17491748
fmt_width.clear(); // Reset setw after first usage
1750-
arg_str = ToString(arg);
1749+
auto arg_str = ToString(arg);
17511750
if (arg->getType()->isBooleanType()) {
17521751
arg_str = std::format("({} as u8)", std::move(arg_str));
17531752
}
@@ -1767,7 +1766,7 @@ bool Converter::GetRawArg(clang::Expr *arg, std::string &raw_args) {
17671766
std::string str = ToString(arg);
17681767
raw_args += "(&(" + str + ").iter().take((" + str +
17691768
").len() - 1).map(|&c| c as u8).collect::<Vec<u8>>()[..]";
1770-
} else if (Printer::ToString(ctx_, arg).contains("std::endl")) {
1769+
} else if (IsStdFunction(arg, "endl")) {
17711770
raw_args += "(&[b'\\n']";
17721771
} else if (clang::isa<clang::StringLiteral>(arg->IgnoreImplicit())) {
17731772
raw_args += "(b" + GetEscapedStringLiteral(arg);
@@ -1853,8 +1852,7 @@ void Converter::ConvertCallToOstream(clang::CallExpr *expr) {
18531852
}
18541853

18551854
void Converter::ConvertPrintf(clang::CallExpr *expr) {
1856-
bool is_fprintf =
1857-
Printer::ToString(ctx_, expr->getCallee()).starts_with("int fprintf");
1855+
bool is_fprintf = IsCDecl(expr->getDirectCallee(), "fprintf");
18581856

18591857
StrCat("printf(");
18601858
for (unsigned i = is_fprintf; i < expr->getNumArgs(); ++i) {
@@ -2338,9 +2336,8 @@ std::optional<Converter::TempMaterializationCtx>
23382336
Converter::ConvertCallExpr(clang::CallExpr *expr) {
23392337
auto *callee = expr->getCallee();
23402338

2341-
auto fn = Printer::ToString(ctx_, callee);
2342-
bool is_printf = fn.starts_with("int printf");
2343-
bool is_fprintf = fn.starts_with("int fprintf");
2339+
bool is_printf = IsCDecl(expr->getDirectCallee(), "printf");
2340+
bool is_fprintf = IsCDecl(expr->getDirectCallee(), "fprintf");
23442341
unsigned format_arg = is_fprintf ? 1 : 0;
23452342
if ((is_printf || is_fprintf) &&
23462343
clang::isa<clang::StringLiteral>(
@@ -4295,7 +4292,7 @@ std::string Converter::GetArrayDefaultAsString(clang::QualType qual_type) {
42954292
if (clang::isa<clang::IncompleteArrayType>(qual_type)) {
42964293
return "[]";
42974294
}
4298-
if (Printer::ToString(ctx_, qual_type).contains("std::array")) {
4295+
if (IsStdDecl(qual_type->getAsCXXRecordDecl(), "array")) {
42994296
assert(GetTemplateArgs(qual_type).has_value());
43004297
auto template_args = *GetTemplateArgs(qual_type);
43014298
assert(template_args.size() == 2);

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,42 @@ bool IsConvertibleFunctionDecl(const clang::FunctionDecl *decl) {
668668
return decl->hasBody() && decl->isThisDeclarationADefinition();
669669
}
670670

671+
bool IsStdDecl(const clang::NamedDecl *decl, llvm::StringRef name) {
672+
return decl && decl->isInStdNamespace() && decl->getIdentifier() &&
673+
decl->getName() == name;
674+
}
675+
671676
bool IsUniquePtr(clang::QualType type) {
672-
auto *record_decl = type.getNonReferenceType()->getAsCXXRecordDecl();
673-
return record_decl && record_decl->getNameAsString() == "unique_ptr";
677+
return IsStdDecl(type.getNonReferenceType()->getAsCXXRecordDecl(),
678+
"unique_ptr");
674679
}
675680

676681
bool IsCallToOstream(clang::CallExpr *expr) {
677-
if (auto *op = clang::dyn_cast<clang::CXXOperatorCallExpr>(expr)) {
678-
if (auto *record_decl = expr->getType()->getAsCXXRecordDecl()) {
679-
return record_decl->getNameAsString() == "basic_ostream" &&
680-
op->getOperator() == clang::OverloadedOperatorKind::OO_LessLess;
681-
}
682+
auto *op = clang::dyn_cast<clang::CXXOperatorCallExpr>(expr);
683+
return op &&
684+
op->getOperator() == clang::OverloadedOperatorKind::OO_LessLess &&
685+
IsStdDecl(expr->getType()->getAsCXXRecordDecl(), "basic_ostream");
686+
}
687+
688+
bool IsCDecl(const clang::NamedDecl *decl, llvm::StringRef name) {
689+
bool is_extern_c = false;
690+
if (auto *function = clang::dyn_cast_or_null<clang::FunctionDecl>(decl)) {
691+
is_extern_c = function->isExternC();
692+
} else if (auto *var = clang::dyn_cast_or_null<clang::VarDecl>(decl)) {
693+
is_extern_c = var->isExternC();
682694
}
683-
return false;
695+
return is_extern_c && decl->getIdentifier() && decl->getName() == name;
696+
}
697+
698+
bool IsStdFunction(const clang::Expr *expr, llvm::StringRef name) {
699+
expr = expr->IgnoreUnlessSpelledInSource();
700+
const clang::NamedDecl *decl = nullptr;
701+
if (auto *call = clang::dyn_cast<clang::CallExpr>(expr)) {
702+
decl = call->getDirectCallee();
703+
} else if (auto *ref = clang::dyn_cast<clang::DeclRefExpr>(expr)) {
704+
decl = clang::dyn_cast<clang::FunctionDecl>(ref->getDecl());
705+
}
706+
return IsStdDecl(decl, name);
684707
}
685708

686709
bool IsAsciiStringLiteral(const clang::StringLiteral *str) {

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,16 @@ bool IsMethodOnPtr(const clang::CXXMethodDecl *method);
147147

148148
bool IsConvertibleFunctionDecl(const clang::FunctionDecl *decl);
149149

150+
bool IsStdDecl(const clang::NamedDecl *decl, llvm::StringRef name);
151+
150152
bool IsUniquePtr(clang::QualType type);
151153

152154
bool IsCallToOstream(clang::CallExpr *expr);
153155

156+
bool IsCDecl(const clang::NamedDecl *decl, llvm::StringRef name);
157+
158+
bool IsStdFunction(const clang::Expr *expr, llvm::StringRef name);
159+
154160
bool IsAsciiStringLiteral(const clang::StringLiteral *str);
155161

156162
bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "converter/converter_lib.h"
1616
#include "converter/lex.h"
1717
#include "converter/mapper.h"
18-
#include "converter/printer.h"
1918
#include "converter/rules/registry.h"
2019

2120
namespace cpp2rust {
@@ -1234,19 +1233,24 @@ static std::vector<const char *> printf2fmt(std::string &format) {
12341233
}
12351234

12361235
void ConverterRefCount::ConvertPrintf(clang::CallExpr *expr) {
1237-
bool is_fprintf =
1238-
Printer::ToString(ctx_, expr->getCallee()).starts_with("int fprintf");
1236+
bool is_fprintf = IsCDecl(expr->getDirectCallee(), "fprintf");
12391237
auto format = GetEscapedStringLiteral(clang::cast<clang::StringLiteral>(
12401238
expr->getArg(is_fprintf)->IgnoreImplicit()));
12411239
bool ends_newline = format.ends_with("\\n\"");
12421240

1243-
auto fd = is_fprintf ? Printer::ToString(ctx_, expr->getArg(0)) : "stdout";
1244-
if (fd == "stdout" || fd == "__stdoutp") {
1241+
const clang::NamedDecl *stream = nullptr;
1242+
if (auto *ref = clang::dyn_cast<clang::DeclRefExpr>(
1243+
expr->getArg(0)->IgnoreParenImpCasts())) {
1244+
stream = ref->getDecl();
1245+
}
1246+
if (!is_fprintf || IsCDecl(stream, "stdout") ||
1247+
IsCDecl(stream, "__stdoutp")) {
12451248
StrCat(ends_newline ? "println!(" : "print!(");
1246-
} else if (fd == "stderr" || fd == "__stderrp") {
1249+
} else if (IsCDecl(stream, "stderr") || IsCDecl(stream, "__stderrp")) {
12471250
StrCat(ends_newline ? "eprintln!(" : "eprint!(");
12481251
} else {
1249-
llvm::errs() << "Unknown fprintf fd: " << fd << '\n';
1252+
llvm::errs() << "Unknown fprintf fd:\n";
1253+
expr->getArg(0)->dump(llvm::errs(), ctx_);
12501254
exit(1);
12511255
}
12521256
if (ends_newline) {

0 commit comments

Comments
 (0)