[clang] [clang-format] Add BreakBinaryOperations configuration (PR #95013)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 4 23:36:54 PDT 2024
================
@@ -27630,6 +27630,245 @@ TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
verifyFormat("return sizeof \"5\";");
}
+TEST_F(FormatTest, BreakBinaryOperations) {
+ auto Style = getLLVMStyleWithColumns(60);
+ // Logical operations
+ verifyFormat("if (condition1 && condition2) {\n"
+ "}",
+ Style);
+
+ verifyFormat("if (condition1 && condition2 &&\n"
+ " (condition3 || condition4) && condition5 &&\n"
+ " condition6) {\n"
+ "}",
+ Style);
+
+ verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
+ " loooooooooooooooooooooongcondition2) {\n"
+ "}",
+ Style);
+
+ // Arithmetic
+ verifyFormat("const int result = lhs + rhs;", Style);
+
+ verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
+ " loooooooooooooooooooooongop3;",
+ Style);
+
+ verifyFormat("result = longOperand1 + longOperand2 -\n"
+ " (longOperand3 + longOperand4) -\n"
+ " longOperand5 * longOperand6;",
+ Style);
+
+ verifyFormat("const int result =\n"
+ " operand1 + operand2 - (operand3 + operand4);",
+ Style);
+
+ Style.BreakBinaryOperations = FormatStyle::BBO_BreakAll;
+
+ // Logical operations
+ verifyFormat("if (condition1 && condition2) {\n"
+ "}",
+ Style);
+
+ verifyFormat("if (condition1 && // comment\n"
+ " condition2 &&\n"
+ " (condition3 || condition4) && // comment\n"
+ " condition5 &&\n"
+ " condition6) {\n"
+ "}",
+ Style);
+
+ verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
+ " loooooooooooooooooooooongcondition2) {\n"
+ "}",
+ Style);
+
+ // Arithmetic
+ verifyFormat("const int result = lhs + rhs;", Style);
+
+ verifyFormat("result = loooooooooooooooooooooongop1 +\n"
+ " loooooooooooooooooooooongop2 +\n"
+ " loooooooooooooooooooooongop3;",
+ Style);
+
+ verifyFormat("const int result =\n"
+ " operand1 + operand2 - (operand3 + operand4);",
+ Style);
+
+ verifyFormat("result = longOperand1 +\n"
+ " longOperand2 -\n"
+ " (longOperand3 + longOperand4) -\n"
+ " longOperand5 +\n"
+ " longOperand6;",
+ Style);
+
+ verifyFormat("result = operand1 +\n"
+ " operand2 -\n"
+ " operand3 +\n"
+ " operand4 -\n"
+ " operand5 +\n"
+ " operand6;",
+ Style);
+
+ // Ensure mixed precedence operations are handled properly
+ Style.BreakBinaryOperations = FormatStyle::BBO_BreakAll;
+ verifyFormat("result = op1 + op2 * op3 - op4;", Style);
+
+ verifyFormat("result = operand1 +\n"
+ " operand2 /\n"
+ " operand3 +\n"
+ " operand4 /\n"
+ " operand5 *\n"
+ " operand6;",
+ Style);
+
+ verifyFormat("result = operand1 *\n"
+ " operand2 -\n"
+ " operand3 *\n"
+ " operand4 -\n"
+ " operand5 +\n"
+ " operand6;",
+ Style);
+
+ verifyFormat("result = operand1 *\n"
+ " (operand2 - operand3 * operand4) -\n"
+ " operand5 +\n"
+ " operand6;",
+ Style);
+
+ verifyFormat("result = operand1.member *\n"
+ " (operand2.member() - operand3->mem * operand4) -\n"
+ " operand5.member() +\n"
+ " operand6->member;",
+ Style);
+
+ Style.BreakBinaryOperations = FormatStyle::BBO_BreakRespectPrecedence;
+ verifyFormat("result = op1 + op2 * op3 - op4;", Style);
+
+ verifyFormat("result = operand1 +\n"
+ " operand2 / operand3 +\n"
+ " operand4 / operand5 * operand6;",
+ Style);
+
+ verifyFormat("result = operand1 * operand2 -\n"
+ " operand3 * operand4 -\n"
+ " operand5 +\n"
+ " operand6;",
+ Style);
+
+ verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
+ " operand5 +\n"
+ " operand6;",
+ Style);
+
+ verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
+ " byte_buffer[1] << 8 |\n"
+ " byte_buffer[2] << 16 |\n"
+ " byte_buffer[3] << 24;",
+ Style);
+
+ Style.BreakBinaryOperations = FormatStyle::BBO_BreakAll;
+ Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
+
+ // Logical operations
+ verifyFormat("if (condition1 && condition2) {\n"
+ "}",
+ Style);
+
+ verifyFormat("if (loooooooooooooooooooooongcondition1\n"
+ " && loooooooooooooooooooooongcondition2) {\n"
+ "}",
+ Style);
+
+ // Arithmetic
+ verifyFormat("const int result = lhs + rhs;", Style);
+
+ verifyFormat("result = loooooooooooooooooooooongop1\n"
+ " + loooooooooooooooooooooongop2\n"
+ " + loooooooooooooooooooooongop3;",
+ Style);
+
+ verifyFormat("const int result =\n"
+ " operand1 + operand2 - (operand3 + operand4);",
+ Style);
+
+ verifyFormat("result = longOperand1\n"
+ " + longOperand2\n"
+ " - (longOperand3 + longOperand4)\n"
+ " - longOperand5\n"
+ " + longOperand6;",
+ Style);
+
+ verifyFormat("result = operand1\n"
+ " + operand2\n"
+ " - operand3\n"
+ " + operand4\n"
+ " - operand5\n"
+ " + operand6;",
+ Style);
+
+ // Ensure mixed precedence operations are handled properly
+ Style.BreakBinaryOperations = FormatStyle::BBO_BreakAll;
----------------
owenca wrote:
Redundant.
https://github.com/llvm/llvm-project/pull/95013
More information about the cfe-commits
mailing list