Skip to content

Commit 54df7c2

Browse files
committed
Handle string literalt to char* conversion
1 parent 56dc655 commit 54df7c2

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,9 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17521752
}
17531753
case clang::CastKind::CK_NoOp: {
17541754
Convert(sub_expr);
1755+
if (IsConversionFromStringLiteralToCharPtr(expr)) {
1756+
StrCat(".cast_mut()");
1757+
}
17551758
break;
17561759
}
17571760
case clang::CastKind::CK_FunctionToPointerDecay:

cpp2rust/converter/converter_lib.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,10 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) {
763763
}
764764
}
765765

766+
bool IsConversionFromStringLiteralToCharPtr(clang::Expr *expr) {
767+
return clang::isa<clang::StringLiteral>(expr->IgnoreImplicit()) &&
768+
expr->getType()->isPointerType() &&
769+
expr->getType()->getPointeeType()->isCharType();
770+
}
771+
766772
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

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

168168
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
169169

170+
bool IsConversionFromStringLiteralToCharPtr(clang::Expr *expr);
171+
170172
} // namespace cpp2rust

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,11 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
10331033
return false;
10341034
}
10351035

1036+
if (expr->getCastKind() == clang::CastKind::CK_NoOp) {
1037+
Convert(sub_expr);
1038+
return false;
1039+
}
1040+
10361041
return Converter::VisitImplicitCastExpr(expr);
10371042
}
10381043

0 commit comments

Comments
 (0)