[clang] [clang-format] Add BinPackBinaryOperations configuration (PR #95013)

Ameer J via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 12 09:12:25 PDT 2024


================
@@ -27492,6 +27492,86 @@ TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
   verifyFormat("return sizeof \"5\";");
 }
 
+TEST_F(FormatTest, BinPackBinaryOperations) {
+  auto Style = getLLVMStyle();
+  Style.BinPackBinaryOperations = false;
+
+  // 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;\n", Style);
+  verifyFormat("const int result = loooooooooooooooooooooongop1 +\n"
+               "                   loooooooooooooooooooooongop2 +\n"
+               "                   loooooooooooooooooooooongop3;\n",
+               Style);
+
+  // clang-format off
+  verifyFormat("const int result =\n"
+               "    operand1 + operand2 - (operand3 + operand4) - operand5 * operand6;\n",
+               Style);
+  // clang-format on
+
+  verifyFormat("const int result = longOperand1 +\n"
+               "                   longOperand2 -\n"
+               "                   (longOperand3 + longOperand4) -\n"
+               "                   longOperand5 * longOperand6;\n",
----------------
ameerj wrote:

I'm admittedly not too familiar with the codebase. There seems to be some logic to differentiate a sequence of operations based on their precedence, and consider them to overflow the column limit or not accordingly. 

i.e. the operations inside the parens, or the multiplication do not overflow the column limit, so they don't break. But the surrounding operations that are of the addition precedence overflow the column limit, and are subject to break when BinPackBinaryOperations is false.

My attempts to fix this and have all ops on a newline are breaking inside parentheses as well, which we wouldn't want, and adding indentation where there shouldn't be. I will need some guidance if this is legitimately a deal-breaker 😅 

https://github.com/llvm/llvm-project/pull/95013


More information about the cfe-commits mailing list