标签搜索

目 录CONTENT

文章目录

clang-format配置说明

沙漠渔
2022-04-01 17:18:09 / 0 评论 / 0 点赞 / 577 阅读 / 5,872 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

使用clang-format进行代码格式化时,可以使用预定义的样式之一(LLVM、Google、Chromium、Mozilla、WebKit、Microsoft ),也可以通过配置特定的样式选项来创建自定义样式,支持两种方式来提供自定义样式选项:在-style=命令行选项中直接指定样式配置,或者使用-style=file并将样式配置放在。项目目录中的clang-format或_clang-format文件。.clang-format文件遵循YAML格式配置,例如:

key1: value1
key2: value2
# A comment.
...

clang-format格式化文件时,它会使用文件名自动检测语言。当格式化标准输入或没有与其语言对应的扩展名的文件时,可以使用-assume-filename=选项来覆盖clang-format用来检测语言的文件名。

针对不同语言,可以在同一个clang-format文件中进行分别处理,如下:

---
# 默认遵循LLVM规则,但是缩进4列
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# 强制C++的指针格式
DerivePointerAlignment: false
PointerAlignment: Left
---
Language: JavaScript
# Use 100 columns for JS.
ColumnLimit: 100
---
Language: Proto
# Don't format .proto files.
DisableFormat: true
---
Language: CSharp
# Use 100 columns for C#.
ColumnLimit: 100
...

可以通过以下方式,获取.clang-format的预定义样式,然后再根据实际要求进行相应的修改:

clang-format -style=llvm -dump-config > .clang-format

在当前文件生成.clang-format,内容如下:

---
Language:        Cpp
# BasedOnStyle:  Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:   
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     80
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:   
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks:   Preserve
IncludeCategories: 
  - Regex:           '^<ext/.*\.h>'
    Priority:        2
  - Regex:           '^<.*\.h>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth:     2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats: 
  - Delimiter:       pb
    Language:        TextProto
    BasedOnStyle:    google
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Auto
TabWidth:        8
UseTab:          Never
...

下面针对每一条内容进行说明。

BasedOnStyle : 用于配置中未专门设置的所有选项的样式。可选项包括:

AccessModifierOffset :访问修饰符的额外缩进或突出 例如:public、private等

AlignAfterOpenBracket :左括号后水平对齐参数。可能值包括:

  • BAS _ Align(配置值:Align)在左括号上对齐参数(AlignAfterOpenBracket: Align)
  • BAS_DontAlign (配置值: DontAlign)不对齐参数,采用ContinuationIndentWidth的值进行缩进
  • BAS_AlwaysBreak(配置值:AlwaysBreak) 如果参数不在一行中,总是在左括号后断开

AlignArrayOfStructures :如果不是None,则对结构数组使用初始化时,将字段与列对齐。可能取值:

  • AIAS_Left (配置值: Left) 左对齐列
  • AIAS_Right(配置值: Right) 右对齐列

AlignConsecutiveAssignments :对齐连续分配的样式

.....

0
广告 广告

评论区