[clang] Support variable empty lines between include categories (PR #183960)

Tobias Erbsland via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 1 14:00:36 PST 2026


================
@@ -3551,18 +3568,21 @@ static void sortCppIncludes(const FormatStyle &Style,
   const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
+    const auto NewCategory = Includes[Index].Category;
     if (!result.empty()) {
       result += "\n";
       if (Style.IncludeStyle.IncludeBlocks ==
               tooling::IncludeStyle::IBS_Regroup &&
-          CurrentCategory != Includes[Index].Category) {
-        result += "\n";
+          CurrentCategory != NewCategory) {
+        const int EmptyLineCount = GetEmptyLines(CurrentCategory, NewCategory);
----------------
erbsland-dev wrote:

(I hope I understood your comment correctly.)

Using just `EmptyLines` of the new category only works when all categories are present. If some of the include categories are omitted, the largest separation should be inserted to properly separate the blocks.

Assuming category `B` should be separated by 2 empty lines.

If all categories are present, the formatting will be correct:
```
A


B

C
```

If category `B` is omitted, `C` still needs to be separated by 2 empty lines:
```
A


C
```

Just using `EmptyLines` of `C` would lead to the wrong result.

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


More information about the cfe-commits mailing list