[clang] [clang-format] Align different categories together (PR #172242)

via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 14 18:52:35 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: None (sstwcw)

<details>
<summary>Changes</summary>

How the program figured out which lines to move along the aligned lines in 75c85bafb830e5.  Aligning the lines caused the information to be out of date for aligning different categories later on.  It caused a regression.

Fixes #<!-- -->171021.

new, with the options `AlignConsecutiveAssignments` and `AlignConsecutiveDeclarations` enabled

```C++
const char *const MatHtoolCompressorTypes[] = {"sympartialACA", "fullACA",
                                               "SVD"};
const char        HtoolCitation[]           = "@<!-- -->article{marchand2020two,\n"
                                              "  "
                                              "  "
                                              "  "
                                              "}\n";
```

old

```C++
const char *const MatHtoolCompressorTypes[] = {"sympartialACA", "fullACA"};
const char        HtoolCitation[]           = "@<!-- -->article{marchand2020two,\n"
                                    "  "
                                    "  "
                                    "  "
                                    "}\n";
```

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


2 Files Affected:

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


``````````diff
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 805bb78f5c90e..6bfcebe73a165 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -372,6 +372,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
         (ScopeStack.size() == 1u && CurrentChange.NewlinesBefore > 0 &&
          InsideNestedScope)) {
       LineShifted = true;
+      CurrentChange.IndentedFromColumn += Shift;
       CurrentChange.Spaces += Shift;
     }
 
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3ff784035dd44..bf69d0a56ebfc 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20879,6 +20879,14 @@ TEST_F(FormatTest, AlignWithLineBreaks) {
                "                   };",
                Style);
 
+  verifyFormat("const char *const MatHtool[] = {};\n"
+               "const char        Htool[]    = \"@article{m,\\n\"\n"
+               "                               \" \"\n"
+               "                               \" \"\n"
+               "                               \" \"\n"
+               "                               \"}\\n\";",
+               Style);
+
   Style.ColumnLimit = 15;
   verifyFormat("int i1 = 1;\n"
                "k      = bar(\n"

``````````

</details>


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


More information about the cfe-commits mailing list