我使用 clang-format 在 vim 中格式化我的代码。我对我提出的配置非常满意,但是我找不到针对此特定问题的设置。当条件条件具有
[[likely]]
或 [[unlikely]]
属性时,后面不会换行。这反过来让我看起来不愉快,并且还导致 clang-tidy 抱怨 if
与 else
不在同一列。
例如
int main(int argc, char** argv)
{
[[unlikely]]
if (argc == 42)
{
return 1;
}
else
{
return 0;
}
}
将使用我的
.clang-format
配置进行格式化:
int main(int argc, char** argv)
{
[[unlikely]] if (argc == 42)
{
return 1;
}
else
{
return 0;
}
}
我已经查看了文档,并将
BreakAfterAttributes
设置为Always
,但这似乎不适用于此属性。似乎没有其他设置相关。
我用
Ubuntu clang-format version 18.0.0 (++20230727042307+fe9c3c786837-1~exp1~20230727042428.1083)
格式文件是这样的。
---
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
...
尚不支持此功能。您可以在此处检查
BreakAfterAttributes
的差异:https://reviews.llvm.org/D140956
从差异来看:
此补丁仅处理函数声明/定义名称之前的 C++11 属性。它不涵盖变量声明中的属性,将来可能会支持。
不幸的是没有选项
BeforeIf
如BeforeCatch
,BeforeWhile
等