@@ -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
19471935bool 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,10 +2051,19 @@ 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
20572069void Converter::ConvertGenericBinaryOperator (clang::BinaryOperator *expr) {
@@ -2063,8 +2075,10 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
20632075
20642076 StrCat (expr->getOpcodeStr ());
20652077
2066- PushParen rhs_paren (*this );
2067- Convert (expr->getRHS ());
2078+ {
2079+ PushParen rhs_paren (*this );
2080+ Convert (expr->getRHS ());
2081+ }
20682082}
20692083
20702084bool Converter::IsReferenceType (const clang::Expr *expr) const {
0 commit comments