Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format BinPackArguments not working as expected

clang-format has 2 options called BinPackParameters and BinPackArguments. They seem to control how function declarations and function calls are indented.

BinPackParameters seems to provide the expected result for a function declaration but BinPackArguments does not seem to work as one would expect for a function call.

Here is a simple test file:

#include <stdbool.h>

void function_with_a_huge_name_that_should_just_not_be(unsigned int a, char *b, unsigned int c, unsigned int d, unsigned int e)
{
    return;
}

int main()
{
    function_with_a_huge_name_that_should_just_not_be(13, "bb", 1234234, 4324324, 2355345);
}

And this is how it is formatted:

#include <stdbool.h>

void function_with_a_huge_name_that_should_just_not_be(unsigned int a,
    char *b,
    unsigned int c,
    unsigned int d,
    unsigned int e)
{
    return;
}

int main()
{
    function_with_a_huge_name_that_should_just_not_be(
        13, "bb", 1234234, 4324324, 2355345);
}

My .clang-format file is as follows:

---
AccessModifierOffset: -2
AlignAfterOpenBracket: false
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BinPackArguments: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit:     80
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
IndentCaseLabels: false
IndentWidth:     4
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
KeepEmptyLinesAtTheStartOfBlocks: false
Language:        Cpp
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Auto
TabWidth:        4
UseTab:          Never

My clang-format version is: 3.6.0 (tags/RELEASE_360/final)

With both BinPackParameters and BinPackArguments being false I would have expected to get the same indentation for the function call as I am getting for the function declaration.

Any idea what I am doing wrong?

like image 451
Lefteris Avatar asked May 05 '15 15:05

Lefteris


2 Answers

I don't think you are doing anything wrong. What happens is that clang-format realizes that the line where you are calling the function is longer than your column limit (80 chars in your settings). Your AlignAfterOpenBracket is set to false, so clang-format places the arguments on a new line (Note that AlignAfterOpenBracket has gained additional possibilities in later versions of clang-format).

You have set both BinPack... settings to false, however there is an additional setting that controls the function declaration vs the function call, AllowAllParametersOfDeclarationOnNextLine (set to false in your example). For the function declaration, this will cause all parameters to be on separate lines if they do not fit on the same line as the function name. For the function call there is no corresponding setting.

In your case, the arguments that you give to the function are placed on the next line after the function name. The length of the second line is < 80, so clang-format does not do anything more with it. If the arguments line would have been longer than your column limit, clang-format would have placed them on separate lines.

So the answer is that as of version 3.9, there is no way to configure clang-format to place each argument on a separate line if they fit on one line.

like image 149
villintehaspam Avatar answered Oct 19 '22 12:10

villintehaspam


Try to set ColumnLimit to 0. It looks like this option "overrides" or has a higher priority over BinPackParameters and BinPackArguments options.

like image 33
Konstantin Avatar answered Oct 19 '22 11:10

Konstantin