1 year ago

#346916

test-img

Karlovsky120

Clang format array initializer braces not aligning

I have this .clang-format file here:

TabWidth: 4
IndentWidth: 4
UseTab: Never
ColumnLimit: 100

Language: Cpp

DisableFormat: false
Standard: Latest

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false

AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes

BinPackArguments: false
BinPackParameters: false

BitFieldColonSpacing: Both

BreakBeforeBraces: Custom
BraceWrapping: {
  AfterCaseLabel: false,
  AfterClass: true,
  AfterControlStatement: Always,
  AfterEnum: true,
  AfterFunction: true,
  AfterNamespace: false,
  AfterStruct: true,
  AfterUnion: true,
  AfterExternBlock: true,
  BeforeCatch: true,
  BeforeElse: true,
  BeforeLambdaBody: false,
  IndentBraces: false,
  SplitEmptyFunction: false,
  SplitEmptyRecord: false,
  SplitEmptyNamespace: true,
}

BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakInheritanceList: AfterColon
BreakStringLiterals: true

CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true

DerivePointerAlignment: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always

FixNamespaceComments: false
IncludeBlocks: Preserve

IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: Indent
IndentPPDirectives: AfterHash
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature

MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1

NamespaceIndentation: None
PPIndentWidth: 1
PackConstructorInitializers: Never

PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Left
ReferenceAlignment: Left

ReflowComments: true
SortIncludes: CaseInsensitive

SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false

SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInLineCommentPrefix: {Minimum: 0, Maximum: 1}
SpacesInParentheses: false
SpacesInSquareBrackets: false

This is how it formats the following code:

#include <vector>

class B
{
public:
    int32_t a = 0;
    int32_t b = 0;
};

class A
{
public:
    void func(std::vector<B> b, int32_t param) {}
};

int main()
{
    A a;
    a.func(
        {
            {.a = 1, .b = 2}
    },
        3);

    int* ptr;
    return 0;
}

I've been fiddling with the .clang-format for a while, and there are a few issues I can't figure out. Firstly, if I change PointerAlignment from Left to Right, the line int* ptr; will change to int *ptr:, so we can both agree that the file I showed is indeed applied to the code.

However, if I change line BreakBeforeBraces: Custom to BreakBeforeBraces: Attach, nothing changes in the file. In fact, whatever choice I put there, nothing changes. First I thought that the whole BraceWrapping block was throwing it off, so I deleted it, but it still refused to work. Note that if BreakBeforeBraces is set to Custom, changing the various values in the BraceWrapping block changes the formatting of the code.

The reason I was doing that is as research for my final question. I'm sure you noticed how ugly the call to func looks. The main issue here is that the opening brace for the vector is not aligned with its closing brace. The opening brace is indented due to ContinuationIndentWidth: 4, so the opening brace looks correct, but the closing brace is not indented at all.

This is how I'd like it to look:

a.func(
    {
        {.a = 1, .b = 2}
    },
    3);

or, even better:

a.func(
    {{.a = 1, .b = 2}},
    3);

How do I configure my .clang-format file to achieve this? Is there an answer that does involve turning the formatting of for that chunk of code?

P.S. I'm doing all this in CLion, but clang format should override anything it controls. I've used MSYS2 and downloaded clang package pacman -S mingw-w64-x86_64-clang and I've manually ran clang-format.exe on the same file. Other than complaining about not understanding PackConstructorInitializers, it formatted the file the same way as CLion did (after I removed the line that was bothering it), so I doubt CLion is to blame here. The MSYS clang-format version is 13.0.1, I couldn't figure out the version of the CLion one, but given that it does handle PackConstructorInitializers, it has to be at least version 14.

c++

clang-format

0 Answers

Your Answer

Accepted video resources