9

I can't find anything in the documentation, even BreakBeforeBraces: Allman formats one-line functions that i already split up to

void foo() { bar(); }

I want something similar to

void foo()
{
    bar();
}

I want this for organization of code and uniformity, because this is how every multiline function looks like.

Can you please help me?

notADev
  • 127
  • 3
  • 12
  • Update: If you set ```ColumnLimit: '0'``` it will let your already formatted functions alone and will format them correctly if you set a line break anywhere in this function. This is a huge step, but i would like to have it 100% automatically though... – notADev Dec 12 '19 at 08:14

3 Answers3

13

To have a short function body on a separate line add this to the .clang-format file:

AllowShortFunctionsOnASingleLine: Empty
Dmitry
  • 1,427
  • 15
  • 16
4
  • Yes you can do this. Easiest way to do this is setting
BreakBeforeBraces: Stroustrup
  • Secondary you can do this by setting true in SplitEmptyFunction. for an example
"BraceWrapping":
        "AfterClass":             false
        "AfterControlStatement":  false
        "AfterEnum":              false
        "AfterFunction":          false
        "AfterNamespace":         false
        "AfterObjCDeclaration":   false
        "AfterStruct":            false
        "AfterUnion":             false  
        "BeforeCatch":            false
        "BeforeElse":             false
        "IndentBraces":           false
        "SplitEmptyFunction":     true <-- set this as true
        "SplitEmptyRecord":       true
        "SplitEmptyNamespace":    true

If it is true output will be

void foo()
{
    bar();
}

but if it is false output will be

void foo(){
    bar();
}

Source

Edit -: edit your clang file to look like this

BasedOnStyle:  WebKit
TabWidth:        4
IndentWidth:     4
UseTab:          Always
ColumnLimit: 100

DisableFormat:   false
Standard: Cpp11

AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false

BraceWrapping: {
    AfterClass: 'true'
    AfterControlStatement: 'true'
    AfterEnum : 'true'
    AfterFunction : 'true'
    AfterNamespace : 'true'
    AfterStruct : 'true'
    AfterUnion : 'true'
    BeforeCatch : 'true'
    BeforeElse : 'true'
    IndentBraces : 'false'
    AfterExternBlock : 'true'
    SplitEmptyFunction : 'false'
    SplitEmptyRecord : 'false'
    SplitEmptyNamespace : 'true'
}

BreakAfterJavaFieldAnnotations: true
BreakBeforeInheritanceComma: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakStringLiterals: true

CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
SpaceBeforeCpp11BracedList: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: false
FixNamespaceComments: true
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
JavaScriptQuotes: Double
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000

PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceAfterTemplateKeyword: true
SpaceBeforeInheritanceColon: true

SortUsingDeclarations: true
SortIncludes: true

ReflowComments: false

IncludeBlocks: Preserve
IndentPPDirectives: AfterHash

Source

Kalana
  • 5,631
  • 7
  • 30
  • 51
  • I'm getting an error copying your code: ```Formatting failed: /fake_path/.vscode-server/extensions/ms-vscode.cpptools-0.26.2/bin/../LLVM/bin/clang-format -style=file -fallback-style=LLVM -assume-filename=/fake_path/test.cpp YAML:4:16: error: Unrecognized character while tokenizing. "BraceWrapping":{ ^ Error reading /fake_path/.clang-format: Invalid argument``` Edit: Fixed it (in your post) – notADev Dec 13 '19 at 08:10
  • Thank you, but it still doesn't do what i want, it leaves the one line function on one line, exaclty like my first solution. When i insert a line break it does it correctly but on the same line leaves it as it is – notADev Dec 13 '19 at 08:16
  • do only first step that I have told `BreakBeforeBraces: Stroustrup` – Kalana Dec 13 '19 at 08:44
  • Nope, the documentation says in the final formatting it will still be on one line – notADev Dec 13 '19 at 09:45
  • Does this answer to your question https://stackoverflow.com/questions/29477654/how-to-make-clang-format-add-new-line-before-opening-brace-of-a-function – Kalana Dec 13 '19 at 09:48
  • Thanks, I've already found this one but it didn't provide sufficient anser for me. Thank you for your effort. At one point I got to accept there simply is no way... – notADev Dec 13 '19 at 10:13
  • This answer doesn't really mention it, but the important thing is the style option `AllowShortFunctionsOnASingleLine`, not `BreakBeforeBraces`. But the answer's example `.clang-format` file does set it correctly. – Eric Backus Sep 04 '20 at 04:51
-1

To answer the Question:

No

Using a .clang-format file this specific behavior isn't achievable. Sorry to all the ones hoping to find a way to do here, I hope I save you some time at least.

Closest:

BreakBeforeBraces: Allman
ColumnLimit: '0'

This will keep your formatted functions correct and format them correctly if they are streched on at least 2 lines.

notADev
  • 127
  • 3
  • 12
  • 5
    This is not correct. The style option which will let you achieve the desired formatting is `AllowShortFunctionsOnASingleLine`. Set it to anything other than `All`. See http://clang.llvm.org/docs/ClangFormatStyleOptions.html. – Eric Backus Sep 04 '20 at 04:49