Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: always break all parameters, one per line

I would like my function prototypes / definitions to always separate out parameters into separate lines, regardless of total length. An otherwise default .clang-format with options

BasedOnStyle: Chromium
AlignAfterOpenBracket: 'AlwaysBreak'
BinPackArguments: 'false'
BinPackParameters: 'false'
ColumnLimit: '80'

gives the following formatting

  void foo(float a, float b);

  void foo(float a, float b, float c, float d, float e, float f, float g);

  void
  foo(float a, float b, float c, float d, float e, float f, float g, float h);

  void foo(
      float a,
      float b,
      float c,
      float d,
      float e,
      float f,
      float g,
      float h,
      float i);

I would like for them to all be broken consistently, one parameter per line, like so:

  void foo(
      float a,
      float b);

  void foo(
      float a,
      float b,
      float c,
      float d,
      float e,
      float f,
      float g);

  void foo(
      float a,
      float b,
      float c,
      float d,
      float e,
      float f,
      float g,
      float h);

  void foo(
      float a,
      float b,
      float c,
      float d,
      float e,
      float f,
      float g,
      float h,
      float i);

Is there any mechanism to do this?

Bonus for the bounty: I would additionally like my member function implementations to split on the return type, class name, and function name, so e.g. instead of this:

float A::foo(float a, float b)
{
// ...
}

I'd like this:

float
A::
foo(float a,
    float b)
{
// ...
}
like image 853
Aurelius Avatar asked Jun 02 '18 19:06

Aurelius


People also ask

Can clang format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

What is clang tidy?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.

How does clang format work?

clang-format is a tool to automatically format C/C++/Objective-C code, so that developers don't need to worry about style issues during code reviews. It is highly recommended to format your changed C++ code before opening pull requests, which will save you and the reviewers' time.

Does Google use clang format?

Clang-Format Style Options are flags that are supported by the ClangFormat tool, which became the de-facto standard to format C++ code. Clang offers the option to use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or to create a custom configuration by using the given flags.


1 Answers

It's not possible.

The only what can be tuned is when a return type is split. By setting penalty PenaltyReturnTypeOnItsOwnLine: 1000, it turns this line

void
foo(float a, float b, ...

into

void foo(
    float a, float b, ...

and by AlwaysBreakAfterReturnType: TopLevelDefinitions format

float A::foo(float a, float b)
{
// ...
}

to

float
A::foo(float a, float b) {
  // ...
}
like image 179
frido Avatar answered Sep 23 '22 20:09

frido