cclang-format

Clang-format inconsistent formatting with struct initialization


Clang-format is behaving inconsistently when aligning a struct initialization. I would like the code to be formated like this:

struct RAMP ramp = {
    ramp_init,
    ramp_compute,
    ramp_update,
    ramp_compute_spin_start,
    ramp_reference,
};

struct PE_BRAKECC BRAKECC = {
    brakeCC_init,
    brakeCC_logic,
    brakeCC_Kill,
    &brakeCC_EV,
};

However, when I run Clang-format (v20.1.0), it formats the file like this:

struct RAMP ramp = {
    ramp_init, ramp_compute, ramp_update, ramp_compute_spin_start, ramp_reference,
};

struct PE_BRAKECC BRAKECC = {
    brakeCC_init,
    brakeCC_logic,
    brakeCC_Kill,
    &brakeCC_EV,
};

This is my clang-format file:

# Generated from CLion C/C++ Code Style settings
BasedOnStyle: GNU
AccessModifierOffset: 0
AlignAfterOpenBracket: Align
#AlignArrayOfStructures: Right 
AlignConsecutiveAssignments: true 
AlignConsecutiveBitFields: true
AlignConsecutiveDeclarations: true
AlignConsecutiveMacros: true
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BreakBeforeBraces: Allman
BraceWrapping:
  AfterCaseLabel: true
  AfterClass: true
  AfterControlStatement: MultiLine
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterUnion: true
  BeforeCatch: true
  BeforeElse: true
  IndentBraces: true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 132
CompactNamespaces: false
ContinuationIndentWidth: 4
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: true
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: true
TabWidth: 4
UseTab: Never

What can I modify in .clang-format to make it behave consistently and as I want?


Solution

  • Try adding this to your clang format config. Good luck!

    Cpp11BracedListStyle: true
    

    See documentation here: https://releases.llvm.org/20.1.0/tools/clang/docs/ClangFormatStyleOptions.html#cpp11bracedliststyle