[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 3 12:58:24 PDT 2024


================
@@ -32,6 +32,26 @@ bool isRecordLBrace(const FormatToken &Tok) {
                      TT_StructLBrace, TT_UnionLBrace);
 }
 
+bool LineStartsNamespaceScope(const AnnotatedLine *Line,
+                              const AnnotatedLine *PreviousLine,
+                              const AnnotatedLine *PrevPrevLine) {
+  return PreviousLine &&
+         ((PreviousLine->Last->is(tok::l_brace) &&
----------------
dmasloff wrote:

If it could then we have just found a bug from main branch since [this line](https://github.com/llvm/llvm-project/blob/451a3135a7afece0b6e7605376ce208435605934/clang/lib/Format/UnwrappedLineFormatter.cpp#L1488) goes before my function call and does basically the same:
```
// Remove empty lines after "{".
if (!Style.KeepEmptyLines.AtStartOfBlock && PreviousLine &&
    PreviousLine->Last->is(tok::l_brace) &&
    !PreviousLine->startsWithNamespace() &&
    !(PrevPrevLine && PrevPrevLine->startsWithNamespace() &&
      PreviousLine->startsWith(tok::l_brace)) &&
    !startsExternCBlock(*PreviousLine)) {
  Newlines = 1;
}
```
To be serious, seems like empty lines are removed and their number is collected into `RootToken.NewlinesBefore` ([here](https://github.com/llvm/llvm-project/blob/451a3135a7afece0b6e7605376ce208435605934/clang/lib/Format/UnwrappedLineFormatter.cpp#L1466-L1467)) and, yes, `RootToken` always exists as it is used in `formatFirstToken` function ([here](https://github.com/llvm/llvm-project/blob/451a3135a7afece0b6e7605376ce208435605934/clang/lib/Format/UnwrappedLineFormatter.cpp#L1557))

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


More information about the cfe-commits mailing list