I'm trying to use clang-format (in VS code) to format my C++ files and configure it to my preferred style. For an array of structs (for getopts) it is adding a load of extra spaces and messing up the brace wrapping:
I'll append my .clang-format at the end of this query
Here's the way I want my array to appear:
int main()
{
const struct option longopts[]=
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}
};
}
Here's how it actually appears:
int main()
{
const struct option longopts[] =
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}};
}
My .clang-format file contains:
BasedOnStyle: LLVM
IndentWidth: 2
AlignAfterOpenBracket: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BinPackParameters: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IndentCaseLabels: true
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: false
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never
Any solutions welcome!
I'm not sure that exactly what you want could be achived, but sometimes easiest way is to slighly modify the source file to help-up the clang-format utility. At first you need to add an ContinuationIndentWidth: 2 option to your format file. Then add a comma after the last item in the array:
{nullptr, 0, nullptr, 0}, // <---
And finnaly move the first curly brace on the same line as array name. The resulting file will be like this:
int main()
{
const struct option longopts[] = {
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0},
};
}
Running the clang-format will leave it as it is. Tested on clang-format from the LLVM snapshot build LLVM-9.0.0-r351376-win64.exe.
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;
}
In clang-format file code is appared in the same verticle position as initial parameters. I'd like to have the initial parameters have no space but rest of the code should have space of 4 spaces(single tab size). How do I achieve the desired format?
I have this:
int main() {
int i;
int j; //parameters initialized
if() {
// stuff //rest of the code
}
}
I want this:
int main() {
int i;
int j; //parameters initialized
if() {
// stuff //rest of the code
}
}
My clang-format file looks like this:
# Abgeänderte .clang-format-Datei
# Anhaltspunkt ist https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# sowie: https://clangformat.com
#for visual studio 2017 you have to comment this one thing:
#BreakInheritanceList: AfterColon
#version for visual studio 2017:
#Language: Cpp
#BasedOnStyle: LLVM
AccessModifierOffset: 0
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
#BreakInheritanceList: AfterColon
ColumnLimit: 300
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
SortIncludes: false
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
#PenaltyBreakBeforeFirstCallParameter: 19
#PenaltyBreakComment: 300
#PenaltyBreakFirstLessLess: 120
#PenaltyBreakString: 1000
#PenaltyExcessCharacter: 1000000
#PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 4
SpacesInAngles: true
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: true
SpacesInSquareBrackets: true
TabWidth: 4
UseTab: Never
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);