[clang] [clang-format] Add per-operator granularity for BreakBinaryOperations (PR #181051)

Sergey Subbotin via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 15 14:22:52 PST 2026


================
@@ -2450,9 +2450,75 @@ struct FormatStyle {
     BBO_RespectPrecedence
   };
 
+  /// A rule that specifies how to break a specific set of binary operators.
+  /// \version 23
+  struct BinaryOperationBreakRule {
+    /// The list of operator tokens this rule applies to,
+    /// e.g. ``["&&", "||"]``.
+    std::vector<std::string> Operators;
+    /// The break style for these operators (defaults to ``OnePerLine``).
+    BreakBinaryOperationsStyle Style;
+    /// Minimum number of chained operators before the rule triggers.
+    /// ``0`` means always break (when the line is too long).
+    unsigned MinChainLength;
+    bool operator==(const BinaryOperationBreakRule &R) const {
+      return Operators == R.Operators && Style == R.Style &&
+             MinChainLength == R.MinChainLength;
+    }
+    bool operator!=(const BinaryOperationBreakRule &R) const {
+      return !(*this == R);
+    }
+  };
+
+  /// Options for ``BreakBinaryOperations``.
+  ///
+  /// If specified as a simple string (e.g. ``OnePerLine``), it behaves like
+  /// the original enum and applies to all binary operators.
+  ///
+  /// If specified as a struct, allows per-operator configuration:
+  /// \code{.yaml}
+  ///   BreakBinaryOperations:
+  ///     Default: Never
+  ///     PerOperator:
+  ///       - Operators: ['&&', '||']
+  ///         Style: OnePerLine
+  ///         MinChainLength: 3
+  /// \endcode
+  /// \version 23
+  struct BreakBinaryOperationsOptions {
+    /// The default break style for operators not covered by ``PerOperator``.
+    BreakBinaryOperationsStyle Default;
+    /// Per-operator override rules.
+    std::vector<BinaryOperationBreakRule> PerOperator;
+    const BinaryOperationBreakRule *findRuleForOperator(StringRef Op) const {
+      for (const auto &Rule : PerOperator) {
+        for (const auto &O : Rule.Operators)
+          if (O == Op)
----------------
ssubbotin wrote:

Done — switched to `llvm::find` in commit 258f4f3. Also changed `Operators` storage from `std::vector<std::string>` to `std::vector<tok::TokenKind>` (per your other comment), so the lookup is now:

```cpp
if (llvm::find(Rule.Operators, Kind) != Rule.Operators.end())
  return &Rule;
```

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


More information about the cfe-commits mailing list