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

Viewed 948

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.

3 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 {
  // ...
}

For what it's worth, it seems like clang-format 10.0.1 does what you want:

$> clang-format --version
clang-format version 10.0.1 (Fedora 10.0.1-3.fc32)
$> echo " void scheduler::stop_mark(service &current, service const &stopped) const noexcept { /* ... */ }" | clang-format --style=LLVM
void scheduler::stop_mark(service &current,
                          service const &stopped) const noexcept { /* ... */
}


To my knowledge, you cannot do this.

My initial thought was to set BinPackParameters to false, but it seems that clang-format has a preference to break on function qualifiers even in this case.

Related