[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
================
@@ -146,6 +147,33 @@ static bool startsNextParameter(const FormatToken &Current,
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
}
+// Returns \c true if \c Token in an alignable binary operator
+static bool isAlignableBinaryOperator(const FormatToken &Token) {
+ // No need to align binary operators that only have two operands.
+ bool HasTwoOperands = Token.OperatorIndex == 0 && !Token.NextOperator &&
+ Token.isNot(TT_ConditionalExpr);
+ return Token.is(TT_BinaryOperator) && !HasTwoOperands &&
+ Token.getPrecedence() > prec::Conditional &&
+ Token.getPrecedence() < prec::PointerToMember;
+}
+
+// Returns \c true if \c Current starts the next operand in a binary operation.
+static bool startsNextOperand(const FormatToken &Current) {
+ assert(Current.Previous);
+ const auto &Previous = *Current.Previous;
+ return isAlignableBinaryOperator(Previous) && !Current.isTrailingComment();
+}
+
+// Returns \c true if \c Current is a binary operation that must break.
+static bool mustBreakBinaryOperation(const FormatToken &Current,
+ const FormatStyle &Style) {
+ return Style.BreakBinaryOperations != FormatStyle::BBO_Never &&
+ ((isAlignableBinaryOperator(Current) &&
+ Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
+ (startsNextOperand(Current) &&
+ Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None));
----------------
owenca wrote:
```suggestion
(Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None
? startsNextOperand
: isAlignableBinaryOperator)(Current);
```
https://github.com/llvm/llvm-project/pull/95013
More information about the cfe-commits
mailing list