15

I'm trying to create a constructor initializer format like:

Constructor()
  : initializer1(),
    initializer2()

According the documentation, I tried to set BreakConstructorInitializers to BeforeColon (the description says: "Break constructor initializers before the colon and after the commas.", and this is exactly what I want), but it is putting the break before the comma, which is not what I want. Am I doing something wrong or is this a bug in clang-format? I tried both clang-format-5.0 and clang-format-6.0, and they behaved the same way.

Here is my full .clang-format file:

---
Language:        Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: TopLevel
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:   
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: GNU
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     140
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:   
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeCategories: 
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth:     2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments:  true
SortIncludes:    false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  true
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        8
UseTab:          Never
...

I tried every possible combination of the BreakConstructorInitializersBeforeComma and the BreakConstructorInitializers keys; none of them seems what I want.

Here are the results:

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
Constructor() : a(1), b(1)
{
}

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
Constructor()
  : a(1)
  , b(1)
{
}

BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
Constructor() : a(1), b(1)
{
}

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
Constructor()
  : a(1)
  , b(1)
{
}

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeComma
Constructor()
  : a(1)
  , b(1)
{
}   

BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: AfterColon
Constructor() : a(1), b(1)
{
}

7
  • "... it is putting the break before the comma", well, you have BreakConstructorInitializersBeforeComma: true - have you tried changing that?
    – Useless
    Dec 19, 2017 at 10:12
  • Setting it to false removes the breaks before the comma, but there are still no break after the comma. Dec 19, 2017 at 10:27
  • But the break before the comma is what you actually asked about. Perhaps you could describe in the question what you already tried and what you're having trouble with?
    – Useless
    Dec 19, 2017 at 10:34
  • You are right, I updated the question. Dec 19, 2017 at 11:00
  • Can you please clarify your clang version... clang 6 doesn't mention any BreakConstructorInitializersBeforeComma option and some older versions don't mention BreakConstructorInitializers, so having them combined looks like a recipe for strange results.
    – grek40
    Dec 19, 2017 at 14:00

3 Answers 3

6

I have the same problem as you and investigated a bit.

So first, BreakConstructorInitializers was changed to be an enum and BreakConstructorInitializersBeforeComma was deprecated. (https://reviews.llvm.org/D32479)

When I look at what you actually wanted to achieve, you wanted BreakConstructorInitializersAFTERComma instead, which is not a thing. You can achieve what you want via ColumnLimits only. Sadly.

1

this option should set

PackConstructorInitializers: Never 
0

These ones will work.

BreakConstructorInitializers: AfterColon ConstructorInitializerAllOnOneLineOrOnePerLine: true

1
  • 1
    It wont work when the initializers fit a single line, unfortunately.
    – aeliton
    Nov 30, 2018 at 6:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.