Skip to content

Commit 84626a2

Browse files
committed
Skip if rust type is the same as cast-to type
1 parent bca62cd commit 84626a2

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,19 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
17791779
}
17801780
}
17811781

1782+
bool Converter::IsSameRustType(clang::Expr *a, clang::Expr *b) {
1783+
auto get_converted_type_or_mapped_type = [&](clang::Expr *expr) {
1784+
if (!clang::isa<clang::ImplicitCastExpr>(expr)) {
1785+
if (const auto *rule = Mapper::GetExprRule(expr)) {
1786+
return rule->return_type.type;
1787+
}
1788+
}
1789+
return GetUnsafeTypeAsString(expr->getType());
1790+
};
1791+
return get_converted_type_or_mapped_type(a) ==
1792+
get_converted_type_or_mapped_type(b);
1793+
}
1794+
17821795
bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17831796
auto *sub_expr = expr->getSubExpr();
17841797
auto type = expr->getType();
@@ -1878,8 +1891,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18781891
break;
18791892
}
18801893
// Skip cast if source and target map to the same Rust type.
1881-
if (GetUnsafeTypeAsString(sub_expr->getType()) ==
1882-
GetUnsafeTypeAsString(type)) {
1894+
if (IsSameRustType(sub_expr, expr)) {
18831895
Convert(sub_expr);
18841896
break;
18851897
}
@@ -3134,7 +3146,7 @@ std::string Converter::GetUnsafeTypeAsString(clang::QualType qual_type) const {
31343146
std::string type_as_string;
31353147
Converter converter(type_as_string, ctx_);
31363148
converter.Convert(qual_type);
3137-
return type_as_string;
3149+
return std::string(Trim(type_as_string));
31383150
}
31393151

31403152
void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
@@ -3572,7 +3584,8 @@ void Converter::ConvertDeref(clang::Expr *expr) {
35723584

35733585
void Converter::ConvertArrow(clang::Expr *expr) { ConvertDeref(expr); }
35743586

3575-
void Converter::ConvertCast(clang::QualType qual_type) {
3587+
void Converter::ConvertCast(clang::QualType qual_type, int line) {
3588+
log() << "[ConvertCast] Called from line " << line << "\n";
35763589
StrCat(keyword::kAs, GetUnsafeTypeAsString(qual_type));
35773590
}
35783591

cpp2rust/converter/converter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
480480

481481
virtual void ConvertArrow(clang::Expr *expr);
482482

483-
virtual void ConvertCast(clang::QualType qual_type);
483+
virtual void ConvertCast(clang::QualType qual_type,
484+
int line = __builtin_LINE());
484485

485486
virtual void ConvertLoopVariable(clang::VarDecl *decl,
486487
clang::Expr *range_init);
@@ -705,6 +706,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
705706

706707
TempMaterializationCtx CollectPrvalueToLRefArgs(clang::CallExpr *expr);
707708

709+
bool IsSameRustType(clang::Expr *a, clang::Expr *b);
710+
708711
private:
709712
void materializeTemplateSpecialization(clang::CXXRecordDecl *decl);
710713

cpp2rust/converter/converter_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ bool SwitchHasFallthrough(clang::SwitchStmt *stmt) {
803803
return false;
804804
}
805805

806-
static std::string_view Trim(std::string_view s) {
806+
std::string_view Trim(std::string_view s) {
807807
auto is_space = [](unsigned char c) { return std::isspace(c); };
808808
auto b = std::find_if_not(s.begin(), s.end(), is_space);
809809
auto e = std::find_if_not(s.rbegin(), s.rend(), is_space).base();

cpp2rust/converter/converter_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,
170170

171171
bool SwitchHasFallthrough(clang::SwitchStmt *stmt);
172172

173+
std::string_view Trim(std::string_view s);
174+
173175
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
174176

175177
std::string ReplaceAll(std::string str, std::string_view from,

0 commit comments

Comments
 (0)