.clang-format

本文深入探讨了C++代码的格式规范,包括基于WebKit风格的详细设置,如缩进、括号放置、注释格式等,旨在提高代码的可读性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---
Language:        Cpp
# BasedOnStyle:  WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:   
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     0
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:   
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeCategories: 
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentWidth:     4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments:  true
SortIncludes:    false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        8
UseTab:          Never
...
### clang-format 文件常见配置项 Clang-format 是一种强大的工具,用于自动格式化 C/C++/Java/Objective-C 等编程语言的源代码。通过 `.clang-format` 文件可以自定义代码风格设置。常见的配置项及其功能描述如下: #### 1. 基本样式参数 - **基于现有风格** - `BasedOnStyle`: 定义基础风格,可选值有 LLVM、Google、Chromium、Mozilla 和 Microsoft。 - 使用命令如 `clang-format -style=llvm -dump-config > .clang-format` 可以创建基于特定风格的基础配置文件[^1]。 #### 2. 缩进控制 - **缩进宽度** - `IndentWidth`: 设置每一级缩进所使用的空格数,默认为 2 或者遵循选定的基本风格设定。 - **连续声明对齐** - `AlignConsecutiveDeclarations`: 控制是否使连续变量声明左对齐。 - **宏定义对齐** - 对于某些版本可能不支持像 `AlignConsecutiveMacros` 的选项,在遇到此类情况时应考虑更新 Clang 版本来获得最新特性支持[^3]。 #### 3. 行宽与换行策略 - **最大行长度** - `ColumnLimit`: 设定每行的最大字符数量限制;当超过此限值时会尝试折行处理。 - **短语拆分方式** - `BinPackParameters`: 影响函数调用中的参数列表如何被包裹成多行。 - 当设为 `true` 时倾向于保持紧凑布局; - 若为 `false` 则更偏好让每个实参独占一行。 #### 4. 大括号放置规则 - **类体大括号位置** - `AccessModifierOffset`: 调整访问修饰符(public, private, protected)相对于类名的偏移量。 - **其他结构的大括号** - `BreakBeforeBraces`: 指明在哪些情况下应该把开括号放在新行上而不是紧跟其前的内容之后。 - 支持多种模式如 Allman、GNU、Stroustrup 等不同编码习惯下的大括号摆放方案。 以下是部分常用配置项的一个简单例子: ```yaml --- Language: Cpp # Based on the Google style guide. BasedOnStyle: Google IndentWidth: 4 ContinuationIndentWidth: 4 UseTab: Never ColumnLimit: 80 AccessModifierOffset: -4 AllowShortIfStatementsOnASingleLine: false AlwaysBreakTemplateDeclarations: true ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值