I'm using a deprecated attribute in my code and I haven't found a clang-format documented way to break after the C++ attribute. I would like that the definition of the do_not_use() function starts on a next line after the [[deprecated]] attribute.
Is there a way to break after C++ attributes?
Current state:
[[deprecated("Will be removed in version 2.3.0 as it doesn't support "
"multi-threading processing. "
"Use mt_process_info() instead")]] void
do_not_use()
{
std::cout << "This function should not be used" << std::endl;
}
Desired state:
[[deprecated("Will be removed in version 2.3.0 as it doesn't support "
"multi-threading processing. "
"Use mt_process_info() instead")]]
void do_not_use()
{
std::cout << "This function should not be used" << std::endl;
}
.clang-format:
Language: Cpp
Standard: c++20
AccessModifierOffset: 0
AlignEscapedNewlines: Right
AlignOperands: true
AllowShortFunctionsOnASingleLine: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
ColumnLimit: 100
Cpp11BracedListStyle: true
IncludeBlocks: Regroup
IndentAccessModifiers: true
IndentCaseLabels: true
IndentWidth: 4
PointerAlignment: Left
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
UseTab: Never
I've tried fiddling with the AttributeMacros and StatementAttributeLikeMacros clang-format settings but I haven't found a solution for stated problem.
Good news, a very recent clang-format commit (https://github.com/llvm/llvm-project/commit/a28f0747c2f3728bd8a6f64f7c8ba80b4e0cda9f) has added a new format option: BreakAfterAttributes.
Using BreakAfterAttributes: Always gives the output you want:
clang-format --style="{BreakAfterAttributes: Always}"
[[deprecated("Will be removed in version 2.3.0 as it doesn't support "
"multi-threading processing. "
"Use mt_process_info() instead")]]
void do_not_use() {
std::cout << "This function should not be used" << std::endl;
}
Since this option is so new, it isn't yet in any released version, but it is guaranteed to be in version 16, or you may build clang-format from source.
Related
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"
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'm using clang-format from LLVM 7.0.0 with Windows 10 in C++.
I have following class
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) = delete;
};
and after running clang-format it should look like this
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC& operator=(
const FooooooooooooooooooC& ) = delete;
};
But actually it look like this after running clang-format
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC&
operator=( const FooooooooooooooooooC& ) = delete;
};
My clang-fromat settings in .clang-format are
---
AccessModifierOffset: -3
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
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: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
CommentPragmas: '^ IWYU pragma:'
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 3
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 3
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 1000000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
Standard: Cpp11
TabWidth: 3
UseTab: Never
...
Does somebody know how to configure clang-fromat to create a break after
operator=(
Thanks!
In summary, you can't quite do what you want.
Here is how to configure clang-format to not put a break after const FooooooooooooooooooC&.
When deciding how to break up the line, clang-format uses several weighting factors whose names all start with Penalty. In this case, you desire the return type to stay on the same line as the function name, so you want to adjust PenaltyReturnTypeOnItsOwnLine. The value in your .clang-format is 60. Instead, use:
PenaltyReturnTypeOnItsOwnLine: 200
Make it any value 110 or larger to prevent the line from get broken after the const FooooooooooooooooooC& return type. I suggest 200 to match the pre-defined clang-format styles for Chromium, Google, and Mozilla. (Also, I don't know why 110 is the threshold; the penalty values are fairly opaque and I only found that value by experimenting.)
However, what you then end up with is this:
const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) =
delete;
I don't believe there is any way to force the break after operator=(. If your class name was 4 characters longer, then you would get what you're asking for, because the above split before the delete would exceed 80 characters.
The comments above mention ColumnLimit. Even if you were allowed to increase the column limit, it would only allow you to keep the operator= declaration on one line. It wouldn't allow you to force it to split the line after operator=(.
The comments above mention AllowAllParametersOfDeclarationOnNextLine: false. As you discovered, this doesn't solve the problem. When there is more than one parameter, this affects the decision of whether all parameters get put onto separate lines. But you have only one parameter. (See the documentation.)
Finally, a caveat, I am using clang-format 6.0.0, compared to your 7.0.0. But there do not appear to be any differences in clang-format which would make any difference here:
The ReleaseNotes.html doesn't mention anything that would matter
There are several new clang-format style options in 7.0.0 compared to 6.0.0 (which I notice because clang-format 6.0.0 complains about them in your .clang-format file), but none are related to this issue.