[PATCH] D107961: [clang-format] Distinguish K&R C function definition and attribute

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 13 01:55:15 PDT 2021


MyDeveloperDay added a comment.

In D107961#2943266 <https://reviews.llvm.org/D107961#2943266>, @owenpan wrote:

> In D107961#2943223 <https://reviews.llvm.org/D107961#2943223>, @MyDeveloperDay wrote:
>
>> Did the original change make it into the 13 branch?
>
> ~~What's //the 13 branch//?~~
>
>> I'm seeing some unexpected behavior.
>>
>>   bool foo(int a, Bar) override;
>>   bool foo(int a, Bar) override; // comment
>>
>> becomes
>>
>>   bool foo(int a, Bar)
>>   override;
>>   bool foo(int a, Bar)
>>   override; // comment
>
> How can I reproduce it? The above C++ declarations should be inside a class definition, no?
>
> Anyway, here is what I got:
>
>   $ cat .clang-format
>   BasedOnStyle: LLVM
>   AlwaysBreakAfterReturnType: TopLevelDefinitions
>   $ cat foo.cpp
>   typedef float Bar;
>   class A {
>     virtual bool foo(int, Bar);
>     virtual bool bar(int, Bar);
>   };
>   class B : A {
>     bool foo(int a, Bar) override;
>     bool bar(int a, Bar) override; // comment
>   };
>   $ clang -Wall -std=c++11 -c foo.cpp
>   $ clang-format foo.cpp > bar.cpp
>   $ diff foo.cpp bar.cpp
>   $ 



> The above C++ declarations should be inside a class definition, no?

Not really, we don't have to have any context of where we are (in class or struct). if you had  for example

  class A
  {
  #include "myfunctions.h"
  }

you still would want the code in myfunctions.h to be formatted just the same (less indentation)

So the following code:

  bool foo(int a, Bar)
  override;
  bool foo(int a, Bar)
  override; // comment

Failed on the 13 branch, but is fixed with @krasimir fix, but that isn't backported (we need to do that as a minimum I think)

With this revision that is also fixed, the following occurs if I change the return type

  Bar foo(int a, Bar)
  override;
  Bar foo(int a, Bar)
  override; // comment

And that doesn't feel correct to me. Well not as by default at least. From my perspective there seems to be a propensity to think code is K&R, but that should be the exception not the rule.

If that is to be the case then we need some sort of setting that means they need to expect it, (like the Language: XXXX) that was proposed before

I just ran this on my 4.5 million line and ended up with way more areas that needed clang-formatting than I think should need to (in c++ code not c code)


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

https://reviews.llvm.org/D107961



More information about the cfe-commits mailing list