[clang] [clang-format] Respect ColumnLimit while aligning multiline expressions (PR #163863)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 17 14:42:57 PDT 2025


================
@@ -636,37 +637,76 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
     // If there is more than one matching token per line, or if the number of
     // preceding commas, do not match anymore, end the sequence.
     if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch) {
-      MatchedIndices.push_back(i);
+      MatchedIndices.push_back(I);
       AlignCurrentSequence();
     }
 
     CommasBeforeLastMatch = CommasBeforeMatch;
     FoundMatchOnLine = true;
 
     if (StartOfSequence == 0)
-      StartOfSequence = i;
+      StartOfSequence = I;
 
     unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
     unsigned ChangeWidthAnchor = 0;
     unsigned ChangeWidthRight = 0;
+    unsigned CurrentChangeWidthRight = 0;
     if (RightJustify)
       if (ACS.PadOperators)
         ChangeWidthAnchor = CurrentChange.TokenLength;
       else
         ChangeWidthLeft += CurrentChange.TokenLength;
     else
-      ChangeWidthRight = CurrentChange.TokenLength;
-    for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
-      ChangeWidthRight += Changes[j].Spaces;
+      CurrentChangeWidthRight = CurrentChange.TokenLength;
+    llvm::SmallPtrSet<const FormatToken *, 8> MatchingParensToEncounter;
+    for (unsigned J = I + 1; J != E && (Changes[J].NewlinesBefore == 0 ||
+                                        !MatchingParensToEncounter.empty());
+         ++J) {
+      const auto &Change = Changes[J];
+      const auto *Tok = Change.Tok;
+
+      if (Tok->MatchingParen) {
+        if (Tok->isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
+                         TT_TemplateOpener)) {
+          MatchingParensToEncounter.insert(Tok->MatchingParen);
+        } else {
+          MatchingParensToEncounter.erase(Tok);
+        }
+      }
+
+      if (Change.NewlinesBefore != 0) {
+        ChangeWidthRight = std::max(ChangeWidthRight, CurrentChangeWidthRight);
+        // if (static_cast<unsigned>(Change.Spaces) >= ChangeWidthLeft)
----------------
HazardyKnusperkeks wrote:

Yeah.

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


More information about the cfe-commits mailing list