You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置clang-format让[[likely]]/[[unlikely]]后自动换行?

clang-format对[[likely]]/[[unlikely]]属性不换行的问题

我在Vim中使用clang-format格式化C++代码,大部分配置都符合需求,但碰到一个特定问题:当条件语句带有[[likely]]或[[unlikely]]属性时,clang-format不会在属性后方插入换行。这不仅排版难看,还会触发clang-tidy报错,提示if和else不在同一列。

期望格式

int main(int argc, char** argv)
{
    [[unlikely]] 
    if (argc == 42)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

实际格式化结果

int main(int argc, char** argv)
{
    [[unlikely]] if (argc == 42)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

我已查阅clang-format官方文档,将BreakAfterAttributes设置为Always,但该配置对这类属性无效,也未找到其他相关配置项。

使用版本:Ubuntu clang-format version 18.0.0 (++20230727042307+fe9c3c786837-1exp120230727042428.1083)

我的.clang-format配置

---

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

...

内容的提问来源于stack exchange,提问作者FalcoGer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:40:55