[clang] [clang-format] Fix ColumnLimit violation while aligning (PR #165627)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 29 14:36:00 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Björn Schäpers (HazardyKnusperkeks)

<details>
<summary>Changes</summary>

It did compute the length only on the first line, and thus the following lines could be (and in the test example were) moved over the column limit, when the = was aligned.

---
Full diff: https://github.com/llvm/llvm-project/pull/165627.diff


2 Files Affected:

- (modified) clang/lib/Format/WhitespaceManager.cpp (+2-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+7) 


``````````diff
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index f24b8ab14bdce..406c77cb3ae8f 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -591,7 +591,8 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
       CurrentChangeWidthRight = CurrentChange.TokenLength;
     const FormatToken *MatchingParenToEncounter = nullptr;
     for (unsigned J = I + 1;
-         J != E && (Changes[J].NewlinesBefore == 0 || MatchingParenToEncounter);
+         J != E && (Changes[J].NewlinesBefore == 0 ||
+                    MatchingParenToEncounter || Changes[J].IsAligned);
          ++J) {
       const auto &Change = Changes[J];
       const auto *Tok = Change.Tok;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d45babe1b82ad..84a50f76c5902 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20820,6 +20820,13 @@ TEST_F(FormatTest, AlignWithLineBreaks) {
                "    argument1,\n"
                "    argument2);",
                Style);
+
+  Style.ColumnLimit = 45;
+  verifyFormat("auto xxxxxxxx = foo;\n"
+               "auto x = whatever ? some / long -\n"
+               "                        computition / stuff\n"
+               "                  : random;",
+               Style);
 }
 
 TEST_F(FormatTest, AlignWithInitializerPeriods) {

``````````

</details>


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


More information about the cfe-commits mailing list