Skip to content

Commit b65dfbb

Browse files
committed
Wrap BinaryOperator in integer cast in C mode
1 parent fb1e8ea commit b65dfbb

4 files changed

Lines changed: 41 additions & 27 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,24 +1742,12 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
17421742
auto *stripped = sub_expr->IgnoreParenImpCasts();
17431743

17441744
if (auto binop = clang::dyn_cast<clang::BinaryOperator>(stripped)) {
1745-
// Comparison already produces bool, no wrap needed.
1746-
if (binop->isComparisonOp()) {
1745+
// Comparisons and logical ops already produces bool, no wrap needed.
1746+
if ((binop->isComparisonOp() || binop->isLogicalOp()) &&
1747+
binop->getType()->isBooleanType()) {
17471748
Convert(sub_expr);
17481749
return;
17491750
}
1750-
// Distribute bool conversion to each argument of the logical op.
1751-
if (binop->isLogicalOp()) {
1752-
{
1753-
PushParen paren(*this);
1754-
ConvertCondition(binop->getLHS());
1755-
}
1756-
StrCat(binop->getOpcodeStr());
1757-
{
1758-
PushParen paren(*this);
1759-
ConvertCondition(binop->getRHS());
1760-
}
1761-
return;
1762-
}
17631751
}
17641752

17651753
PushParen paren(*this);
@@ -1945,6 +1933,21 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
19451933
}
19461934

19471935
bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
1936+
bool needs_cast = (expr->isComparisonOp() || expr->isLogicalOp()) &&
1937+
expr->getType()->isIntegerType() &&
1938+
!expr->getType()->isBooleanType();
1939+
PushParen outer(*this, needs_cast);
1940+
{
1941+
PushParen inner(*this, needs_cast);
1942+
ConvertBinaryOperator(expr);
1943+
}
1944+
if (needs_cast) {
1945+
ConvertCast(expr->getType());
1946+
}
1947+
return false;
1948+
}
1949+
1950+
void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
19481951
auto type = expr->getType();
19491952
auto *lhs = expr->getLHS();
19501953
auto *rhs = expr->getRHS();
@@ -2048,23 +2051,33 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
20482051
}
20492052
ConvertCast(expr->getType());
20502053
computed_expr_type_ = ComputedExprType::FreshValue;
2054+
} else if (expr->isLogicalOp()) {
2055+
{
2056+
PushParen paren(*this);
2057+
ConvertCondition(expr->getLHS());
2058+
}
2059+
StrCat(expr->getOpcodeStr());
2060+
{
2061+
PushParen paren(*this);
2062+
ConvertCondition(expr->getRHS());
2063+
}
20512064
} else {
20522065
ConvertGenericBinaryOperator(expr);
20532066
}
2054-
return false;
20552067
}
20562068

20572069
void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
2058-
PushParen outer(*this);
20592070
{
20602071
PushParen lhs_paren(*this);
20612072
Convert(expr->getLHS());
20622073
}
20632074

20642075
StrCat(expr->getOpcodeStr());
20652076

2066-
PushParen rhs_paren(*this);
2067-
Convert(expr->getRHS());
2077+
{
2078+
PushParen rhs_paren(*this);
2079+
Convert(expr->getRHS());
2080+
}
20682081
}
20692082

20702083
bool Converter::IsReferenceType(const clang::Expr *expr) const {

cpp2rust/converter/converter.h

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

253253
virtual bool VisitBinaryOperator(clang::BinaryOperator *expr);
254254

255+
virtual void ConvertBinaryOperator(clang::BinaryOperator *expr);
256+
255257
virtual bool ConvertIncAndDec(clang::UnaryOperator *expr);
256258

257259
virtual bool VisitUnaryOperator(clang::UnaryOperator *expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ void ConverterRefCount::EmitStmtExprTail(clang::Expr *tail) {
11911191
StrCat("__result");
11921192
}
11931193

1194-
bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
1194+
void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
11951195
auto *lhs = expr->getLHS();
11961196
auto *rhs = expr->getRHS();
11971197
auto lhs_type = lhs->getType();
@@ -1229,7 +1229,7 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
12291229
}
12301230
StrCat(token::kSemiColon);
12311231
EmitSetOrAssign(lhs, "rhs_0");
1232-
return false;
1232+
return;
12331233
}
12341234

12351235
if (IsUnsignedArithOp(expr)) {
@@ -1249,7 +1249,7 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
12491249
StrCat(token::kSemiColon);
12501250
EmitSetOrAssign(lhs, "rhs_0");
12511251
}
1252-
return false;
1252+
return;
12531253
}
12541254

12551255
// pointer subtraction. The Sub trait gets elements by Value, so we need
@@ -1263,15 +1263,15 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
12631263
}
12641264
ConvertCast(expr->getType());
12651265
computed_expr_type_ = ComputedExprType::FreshValue;
1266-
return false;
1266+
return;
12671267
}
12681268

12691269
if (expr->isAssignmentOp()) {
12701270
ConvertAssignment(lhs, rhs, opcode_as_string);
1271-
return false;
1271+
return;
12721272
}
12731273

1274-
return Converter::VisitBinaryOperator(expr);
1274+
Converter::ConvertBinaryOperator(expr);
12751275
}
12761276

12771277
bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
@@ -1840,7 +1840,6 @@ void ConverterRefCount::ConvertGenericBinaryOperator(
18401840
return;
18411841
}
18421842

1843-
PushParen paren(*this);
18441843
Convert(lhs);
18451844
StrCat(opcode);
18461845
Convert(rhs);

cpp2rust/converter/models/converter_refcount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ConverterRefCount final : public Converter {
8282

8383
bool VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) override;
8484

85-
bool VisitBinaryOperator(clang::BinaryOperator *expr) override;
85+
void ConvertBinaryOperator(clang::BinaryOperator *expr) override;
8686

8787
bool VisitStmtExpr(clang::StmtExpr *expr) override;
8888

0 commit comments

Comments
 (0)