I use clang-format to format my code in vim. I'm mostly happy with the configuration I came up with, however I can't find a setting for this particular issue. When having a [[likely]]
or [[unlikely]]
attribute for conditionals, it doesn't put a line break afterwards. That in turn looks unpleasant to me and also causes clang-tidy to complain that the if
isn't at the same column as the else
.
For example
int main(int argc, char** argv)
{
[[unlikely]]
if (argc == 42)
{
return 1;
}
else
{
return 0;
}
}
would be formatted like so with my .clang-format
configuration:
int main(int argc, char** argv)
{
[[unlikely]] if (argc == 42)
{
return 1;
}
else
{
return 0;
}
}
I have already looked at the documentation, and have set BreakAfterAttributes
to Always
, but that doesn't seem to apply to this attribute. No other setting seems related.
I use Ubuntu clang-format version 18.0.0 (++20230727042307+fe9c3c786837-1~exp1~20230727042428.1083)
The format file is this.
---
DisableFormat: false
BasedOnStyle: LLVM
ColumnLimit: 120
UseTab: Never
TabWidth: 4
Language: Cpp
Standard: Auto
LineEnding: LF
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignConsecutiveBitFields: AcrossEmptyLinesAndComments
AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments
AlignAfterOpenBracket: BlockIndent
AlignEscapedNewlines: Left
AlignArrayOfStructures: Left
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 1
PointerAlignment: Left
ReferenceAlignment: Pointer
QualifierAlignment: Custom
QualifierOrder: ['static', 'friend', 'inline', 'constexpr', 'const', 'type', 'volatile', 'restrict']
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: 'Yes'
BreakAfterAttributes: Always
BreakAfterJavaFieldAnnotations: true
BreakArrays: false
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: AfterComma
BreakStringLiterals: true
CompactNamespaces: false
PackConstructorInitializers: NextLine
ReflowComments: true
BreakBeforeBraces: Custom
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: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BitFieldColonSpacing: Both
Cpp11BracedListStyle: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: false
AfterFunctionDeclarationName: false
AfterFunctionDefinitionName: false
AfterIfMacros: false
AfterOverloadedOperator: true
AfterRequiresInClause: true
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 4
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
InsertNewlineAtEOF: true
KeepEmptyLinesAtEOF: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
IndentWidth: 4
ContinuationIndentWidth: 2
PPIndentWidth: 2
ConstructorInitializerIndentWidth: 8
AccessModifierOffset: -2
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: Indent
IndentGotoLabels: false
IndentPPDirectives: AfterHash
IndentRequiresClause: true
IndentWrappedFunctionNames: true
LambdaBodyIndentation: Signature
NamespaceIndentation: Inner
BinPackArguments: false
BinPackParameters: false
CommentPragmas: 'NOLINT|(@|\\)(attention|author|brief|bug|cond|date|deprecated|else(if|cond)?|end(cond|if)|exception|if(not)?|invariant|note|par|t?param|post|pre|return|retval|sa|see|since|test|throw|todo|version|warning|xrefitem)|^!'
FixNamespaceComments: true
ShortNamespaceLines: 5
SortIncludes: CaseInsensitive
InsertBraces: true
InsertTrailingCommas: Wrapped
IntegerLiteralSeparator:
Binary: 4
Decimal: 3
DecimalMinDigits: 5
Hex: 2
HexMinDigits: 5
RemoveSemicolon: false
SortUsingDeclarations: Lexicographic
...
This is not supported yet. You can check the diff for BreakAfterAttributes
here: https://reviews.llvm.org/D140956
From the diff:
This patch only handles C++11 attributes before a function declaration/definition name. It doesn't cover attributes in variable declarations, which may be supported in the future.
Unfortunately there is no option BeforeIf
like BeforeCatch
, BeforeWhile
, etc.