Skip to content

Commit 6c3a5be

Browse files
committed
Skip if rust type is the same as cast-to type
1 parent dd26518 commit 6c3a5be

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
@@ -1769,6 +1769,19 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
17691769
}
17701770
}
17711771

1772+
bool Converter::IsSameRustType(clang::Expr *a, clang::Expr *b) {
1773+
auto get_converted_type_or_mapped_type = [&](clang::Expr *expr) {
1774+
if (!clang::isa<clang::ImplicitCastExpr>(expr)) {
1775+
if (const auto *rule = Mapper::GetExprRule(expr)) {
1776+
return rule->return_type.type;
1777+
}
1778+
}
1779+
return GetUnsafeTypeAsString(expr->getType());
1780+
};
1781+
return get_converted_type_or_mapped_type(a) ==
1782+
get_converted_type_or_mapped_type(b);
1783+
}
1784+
17721785
bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17731786
auto *sub_expr = expr->getSubExpr();
17741787
auto type = expr->getType();
@@ -1866,8 +1879,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18661879
break;
18671880
}
18681881
// Skip cast if source and target map to the same Rust type.
1869-
if (GetUnsafeTypeAsString(sub_expr->getType()) ==
1870-
GetUnsafeTypeAsString(type)) {
1882+
if (IsSameRustType(sub_expr, expr)) {
18711883
Convert(sub_expr);
18721884
break;
18731885
}
@@ -3109,7 +3121,7 @@ std::string Converter::GetUnsafeTypeAsString(clang::QualType qual_type) const {
31093121
std::string type_as_string;
31103122
Converter converter(type_as_string, ctx_);
31113123
converter.Convert(qual_type);
3112-
return type_as_string;
3124+
return std::string(Trim(type_as_string));
31133125
}
31143126

31153127
void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
@@ -3545,7 +3557,8 @@ void Converter::ConvertDeref(clang::Expr *expr) {
35453557

35463558
void Converter::ConvertArrow(clang::Expr *expr) { ConvertDeref(expr); }
35473559

3548-
void Converter::ConvertCast(clang::QualType qual_type) {
3560+
void Converter::ConvertCast(clang::QualType qual_type, int line) {
3561+
log() << "[ConvertCast] Called from line " << line << "\n";
35493562
StrCat(keyword::kAs, GetUnsafeTypeAsString(qual_type));
35503563
}
35513564

cpp2rust/converter/converter.h

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

479479
virtual void ConvertArrow(clang::Expr *expr);
480480

481-
virtual void ConvertCast(clang::QualType qual_type);
481+
virtual void ConvertCast(clang::QualType qual_type,
482+
int line = __builtin_LINE());
482483

483484
virtual void ConvertLoopVariable(clang::VarDecl *decl,
484485
clang::Expr *range_init);
@@ -685,6 +686,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
685686

686687
TempMaterializationCtx CollectPrvalueToLRefArgs(clang::CallExpr *expr);
687688

689+
bool IsSameRustType(clang::Expr *a, clang::Expr *b);
690+
688691
private:
689692
void materializeTemplateSpecialization(clang::CXXRecordDecl *decl);
690693

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)