Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: break on function arguments instead of function qualifiers (noexcept)

I'm looking for a way to format the C++ code below with clang-format (version 9.0.0) such that function definitions that exceed the 80 column limit are broken after argument declarations instead of C++ function qualifiers like noexcept:

void scheduler::stop_mark(service &current, service const &stopped) const
    noexcept {
  // ...
}

The snippet above shows my code formatted with the LLVM default style and the code below is the one I want to have after proper formatting:

void scheduler::stop_mark(service& current,
                          service const& stopped) const noexcept {
  // ...
}

The difference between both snippets is that the line is broken after service& current, instead of noexcept.

This behaviour is reproducible when using the LLVM default style but I'm using the options below for reference:

---
BasedOnStyle: LLVM

AlignAfterOpenBracket: Align
AllowAllArgumentsOnNextLine: 'true'
AllowAllConstructorInitializersOnNextLine: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializersBeforeComma: 'true'
ConstructorInitializerIndentWidth: 2
FixNamespaceComments: 'true'
IndentCaseLabels: 'true'
IndentPPDirectives: AfterHash
PenaltyBreakAssignment: 1000
PenaltyBreakBeforeFirstCallParameter: 50
PointerAlignment: Left
...

Is it possible to get such a formatting through clang-format?

I inspected all possible options on https://zed0.co.uk/clang-format-configurator/ already and could not find the matching clang-format option.

like image 355
Denis Blank Avatar asked Mar 14 '20 03:03

Denis Blank


1 Answers

I am in agreement that no combination of rules will get the desired output, but there is a way to force it when you spot stuff like this.

Add a line comment (can be empty) after your first parameter. clang-format will then align your parameters for you.

void scheduler::stop_mark(service& current, //
                          service const& stopped) const noexcept {
  // ...
}
like image 150
sweenish Avatar answered Sep 21 '22 07:09

sweenish