[PATCH] D104044: [clang-format] Fix the issue of no empty line after namespace

Darwin Xu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 10 22:07:26 PDT 2021


darwin added a comment.

In D104044#2811268 <https://reviews.llvm.org/D104044#2811268>, @HazardyKnusperkeks wrote:

> In D104044#2810909 <https://reviews.llvm.org/D104044#2810909>, @darwin wrote:
>
>> Sorry, I may need some help here. It shows "Context not available.", how do I correct it?
>
> There are multiple ways: https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface
> I use `git diff HEAD~1 -U999999 > mypatch.patch`
>
> For your tests: You want to keep an empty line after the now wrapped `{`, did I understand that correctly?
> Is that bound to the google style, i.e. does it not happen with LLVM style?

Oh, now I remember, I forget to use this command. Thank you.

About the issue, let me explain it. It isn't bound to the google style or LLVM style either, since both of them keep the first brace at the same line of the namespace.

Let's see this example:

  darwin at darwin-ubuntu-04:~/temp$ cat b.cpp
  namespace A{ int i; }
  
  namespace B{
  
  
  int j;
  
  
  }
  
  darwin at darwin-ubuntu-04:~/temp$ /home/darwin/projects/llvm-project/build/bin/clang-format  b.cpp -style="{BasedOnStyle: google}"
  namespace A {
  int i;
  }
  
  namespace B {
  
  int j;
  
  }

You can see, if there isn't an empty line, clang-format doesn't add one. And if there are some extra lines, clang-format will keep just one line. This is expected.

But if I use set `BraceWrapping.AfterNamespace` to true, the thing is different, look this:

  darwin at darwin-ubuntu-04:~/temp$ cat b.cpp
  namespace A{ int i; }
  
  namespace B{
  
  
  int j;
  
  
  }
  
  darwin at darwin-ubuntu-04:~/temp$ /home/darwin/projects/llvm-project/build/bin/clang-format  b.cpp -style="{BasedOnStyle: google, BreakBeforeBraces: Custom, BraceWrapping: {AfterNamespace: true}}"
  namespace A
  {
  int i;
  }
  
  namespace B
  {
  int j;
  
  }

There isn't an empty line between the `{` and the `int j;`, but in the previous example, there is an empty line.

But later I found out that if I set `KeepEmptyLinesAtTheStartOfBlocks` to true, I will get the expect result:

  darwin at darwin-ubuntu-04:~/temp$ cat b.cpp
  namespace A{ int i; }
  
  namespace B{
  
  
  int j;
  
  
  }
  
  darwin at darwin-ubuntu-04:~/temp$ /home/darwin/projects/llvm-project/build/bin/clang-format  b.cpp -style="{BasedOnStyle: google, BreakBeforeBraces: Custom, BraceWrapping: {AfterNamespace: true}, KeepEmptyLinesAtTheStartOfBlocks: true}"
  namespace A
  {
  int i;
  }
  
  namespace B
  {
  
  int j;
  
  }

So, this might not be a very critical issue. But still, I think clang-format isn't working correctly and should be fixed.


Repository:
  rZORG LLVM Github Zorg

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104044/new/

https://reviews.llvm.org/D104044



More information about the cfe-commits mailing list