Is there a way to get clang-format to produce the following formatting:
void foo()
{
if(cond)
{ return; }
}
void f(int x)
{ return 2*x; }
I.e. I want always to see break before {, so I have
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
however, I want short statements to be on a single line, i.e.
{ return; }
// vs
{
return;
}
So far, I am noticing that even with
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
those blocks are expanded into 3 lines.
If I set
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
I still get the same thing
I would like a configuration something like "AllowShortBlocksWhateverTheyAre"
Related
I've managed to achieve following output after using clang-format:
void doSomethingBlaBlaBla(
very::looooooooooooong::Type veryLongParamNameThatExceedsColumnLimitIfPutAbove
) override;
Meaning that if such line:
void doSomethingBlaBlaBla(very::looooooooooooong::Type veryLongParamNameThatExceedsColumnLimitIfPutAbove) override;
exceeds ColumnLimit, it's being broken like you see above. But I can see that in corner cases clang would handle it differently - where corner case is that this exceeds ColumnLimit:
void doSomethingBlaBlaBla(very::looong::Type veryLongParamNameThatExceedsColumnLimitIfPutAbove) override;
but this alone (note lack of ) override;) doesn't:
void doSomethingBlaBlaBla(very::looong::Type veryLongParamNameThatExceedsColumnLimitIfPutAbove
In such case, clang formats it in a following manner:
void doSomethingBlaBlaBla(very::looong::Type veryLongParamNameThatExceedsColumnLimitIfPutAbove
) override;
It doesn't break line after opening parenthesis, but breaks it before closing one. Is there a config option that would break line after opening parenthesis in this situation?
This is my clang-format so far:
BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignAfterOpenBracket: BlockIndent
AllowShortLambdasOnASingleLine: None
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterNamespace: false
AfterControlStatement: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: AfterColon
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DeriveLineEnding: false
DerivePointerAlignment: true
ExperimentalAutoDetectBinPacking: true
MaxEmptyLinesToKeep: 2
SortIncludes: false
SpaceAfterTemplateKeyword: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeRangeBasedForLoopColon: false
UseCRLF: true
ReflowComments: false
AlwaysBreakTemplateDeclarations: Yes
DeriveLineEnding: true
AllowAllArgumentsOnNextLine: false
...
I'm looking for a configuration options(s) for .clang-format that will make clang-format keep a MACRO on the same line as an if statement.
Current:
What I want:
Here is my current .clang-format: https://pastebin.com/GYH79k7u
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Cpp11BracedListStyle: false
...
Thank you!
Clang-format 12 adds AttributeMacros, as in
AttributeMacros: ['_LIKELY', '_UNLIKELY']
The "clang-format 12.0.1" installed by Homebrew doesn't yet support this option, but clang-format 14.0.6 (the current Homebrew release) does. The result with your .clang-format file plus the above line seems to give exactly what you wanted:
if( GWorldServer->SendAsync( Buffer ) != RSuccess ) _UNLIKELY
{
return RFail;
}
I have this option in settings:
Clang_format_fallback Style : Visual studio
Clang_format_style : file
But when I write
int main(){
}
braces are on the same line, but I want to put them on the new line like this:
int main()
{
}
How to achieve that?
edit: settings like
"javascript.format.placeOpenBraceOnNewLineForFunctions": true,
do not help for c++ (only for js)
EDIT2: .clang-format:
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
#AfterExternBlock: false # Unknown to clang-format-5.0
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBraces: Custom
Cpp11BracedListStyle: false
which of these settings are responsible for my issue?
I can't find a configuration for the linebreak behaviour after access modifiers.
(But without setting MaxEmptyLinesToKeep to 0)
(To remove the linebreak after an access modifier change)
I have my current clang configuration like this:
Language: Cpp
CompactNamespaces: false
FixNamespaceComments: false
Cpp11BracedListStyle: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpacesInParentheses: false
SpaceInEmptyParentheses: false
SpaceAfterTemplateKeyword: false
SortUsingDeclarations: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
AccessModifierOffset: -1
UseTab: true
MaxEmptyLinesToKeep: 2
BreakBeforeBraces: Allman
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
KeepEmptyLinesAtTheStartOfBlocks: false
AllowShortFunctionsOnASingleLine: None
BraceWrapping:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: true
AfterEnum: true
AfterUnion: true
AfterExternBlock: true
AfterNamespace: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: true
SplitEmptyRecord: true
IndentBraces: false
BreakBeforeBraces: Custom
Example:
At the moment clang produces:
class Test
{
public:
int test();
private:
int m_test;
}
But i want it to remove the linebreak so that it becomes:
class Test
{
public:
int test();
private:
int m_test;
}
I'm trying to use a .clang-format file with Visual Studio 2017 but it doesn't always seem to respect the BinPackParameters argument which I set to false.
With the following parameters set:
AlignAfterOpenBracket: AlwaysBreak
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 110
I would expect my method calls that were too long (i.e. greater than the ColumnWidth parameter) to always line break every parameter to a method so that they are each on different lines like so:
LongMethodNameCall(
someLongParameter1,
someLongParameter2,
someLongParameter3,
someLongParameter4 );
This is the case sometimes, but other times I get:
LongMethodNameCall(
someLongParameter1, someLongParameter2, someLongParameter3, someLongParameter4 );
To be clear I want it to always line-break after the opening bracket and put each parameter onto one line if the whole statement exceeds the ColumnWidth parameter.
Here is my .clang-format file:
Language: Cpp
#DisableFormat: true
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
BreakStringLiterals: true
ColumnLimit: 110
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
IncludeCategories:
- Regex: '\/stdafx.h'
Priority: -1
IndentCaseLabels: true
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 100
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 0
PenaltyBreakFirstLessLess: 100
PenaltyBreakString: 50
PenaltyExcessCharacter: 20
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInAngles: true
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
TabWidth: 4
UseTab: Never
The only time BinPackParameters: false is not respected is when AllowAllParametersOfDeclarationOnNextLine is set to true, which is not in your case.
I tried with the provided config and could not reproduce the issue.
The tool has probably been modified in the meanwhile.
To clarify, it is crucial to set PenaltyReturnTypeOnItsOwnLine: 1000, which you are already doing. It prevents having the return code on a single line if it enables fitting the rest on a single line:
std::vector<std::string>
test(std::string const & var1, std::string const & var2);
becomes
std::vector<std::string> test(std::string const & var1,
std::string const & var2);