Skip to content

Commit 697f71a

Browse files
committed
Propagate ExprKind::Void downstream on void cast
1 parent 3957f30 commit 697f71a

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,10 +1865,21 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18651865
return false;
18661866
}
18671867

1868+
void Converter::ConvertVoidCastExpr(clang::ExplicitCastExpr *expr) {
1869+
PushExprKind push(*this, ExprKind::Void);
1870+
StrCat("let _ = ");
1871+
Convert(expr->getSubExpr());
1872+
if (expr->getSubExpr()->isLValue()) {
1873+
StrCat(".clone()");
1874+
}
1875+
StrCat(token::kSemiColon);
1876+
}
1877+
18681878
bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
18691879
auto type = expr->getTypeAsWritten();
18701880
auto *sub_expr = expr->getSubExpr();
18711881
if (type->isVoidType()) {
1882+
ConvertVoidCastExpr(expr);
18721883
return false;
18731884
}
18741885
switch (expr->getStmtClass()) {
@@ -1950,7 +1961,10 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
19501961
ConvertCast(lhs_type);
19511962
}
19521963
} else if (expr->isCommaOp()) {
1953-
Convert(lhs);
1964+
{
1965+
PushExprKind push(*this, ExprKind::Void);
1966+
Convert(lhs);
1967+
}
19541968
StrCat(token::kSemiColon);
19551969
Convert(rhs);
19561970
} else if (IsUnsignedArithOp(expr)) {
@@ -2248,11 +2262,22 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
22482262
}
22492263

22502264
bool Converter::VisitParenExpr(clang::ParenExpr *expr) {
2265+
// Comma operator becomes (A, B, C) -> { A; B; C }
2266+
auto *bin = clang::dyn_cast<clang::BinaryOperator>(expr->getSubExpr());
2267+
// Void cast becomes ((void) A) -> { let _ = A; }
2268+
auto *cast = clang::dyn_cast<clang::ExplicitCastExpr>(expr->getSubExpr());
2269+
if ((bin && bin->isCommaOp()) ||
2270+
(cast && cast->getTypeAsWritten()->isVoidType())) {
2271+
PushBrace push(*this);
2272+
Convert(expr->getSubExpr());
2273+
return false;
2274+
}
2275+
22512276
// Add cast to avoid ambigous integers. Don't add cast if sub expression is a
22522277
// pointer dereference because we might want to mutate the dereferenced value.
22532278
bool should_add_integral_cast =
22542279
expr->getType()->isIntegralOrEnumerationType() && !isAddrOf() &&
2255-
!clang::isa<clang::UnaryOperator>(expr->getSubExpr());
2280+
!isVoid() && !clang::isa<clang::UnaryOperator>(expr->getSubExpr());
22562281
PushParen outer(*this, should_add_integral_cast);
22572282

22582283
{
@@ -3399,7 +3424,7 @@ void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op,
33993424

34003425
void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) {
34013426
assert(pointer_type->isPointerType());
3402-
if (IsReferenceType(expr)) {
3427+
if (IsReferenceType(expr) || pointer_type->isFunctionPointerType()) {
34033428
PushExprKind push(*this, ExprKind::AddrOf);
34043429
Convert(expr);
34053430
} else {

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
240240

241241
virtual bool VisitImplicitCastExpr(clang::ImplicitCastExpr *expr);
242242

243+
void ConvertVoidCastExpr(clang::ExplicitCastExpr *expr);
244+
243245
virtual bool VisitExplicitCastExpr(clang::ExplicitCastExpr *expr);
244246

245247
virtual bool VisitBinaryOperator(clang::BinaryOperator *expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ bool ConverterRefCount::VisitFunctionPointerCast(
11281128

11291129
bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
11301130
if (expr->getTypeAsWritten()->isVoidType()) {
1131+
ConvertVoidCastExpr(expr);
11311132
return false;
11321133
}
11331134
switch (expr->getStmtClass()) {

0 commit comments

Comments
 (0)