跳转到内容
新建笔记

clang-format注释

终端窗口
---
# 基于 Google 风格的代码格式化配置,并进行了一些自定义调整
Language: Cpp # 指定语言为 C++
BasedOnStyle: Google # 基于 Google 风格
# 缩进和对齐相关
AccessModifierOffset: -4 # 访问修饰符(如 public, private)的缩进偏移量
AlignAfterOpenBracket: Align # 在左括号后对齐内容
AlignArrayOfStructures: None # 不对数组的结构体进行特殊对齐
AlignConsecutiveAssignments: # 连续赋值语句对齐
Enabled: true # 启用对齐
AcrossEmptyLines: false # 不跨空行对齐
AcrossComments: false # 不跨注释对齐
AlignCompound: false # 不对复合赋值操作符对齐
AlignFunctionPointers: false # 不对函数指针对齐
PadOperators: true # 在操作符周围添加空格
AlignConsecutiveBitFields: # 连续位字段对齐,未启用
Enabled: false
AlignConsecutiveDeclarations: # 连续声明对齐
Enabled: true # 启用对齐
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveMacros: # 宏对齐
Enabled: true # 启用对齐
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveShortCaseStatements: # 短的 case 语句对齐,未启用
Enabled: false
AlignEscapedNewlines: Left # 转义换行符的对齐方式为左对齐
AlignOperands: Align # 操作数对齐
AlignTrailingComments: # 尾部注释对齐
Kind: Always # 总是对齐
OverEmptyLines: 0 # 不跨空行对齐
# 简短代码块和函数的格式化
AllowAllArgumentsOnNextLine: true # 允许所有参数换行
AllowAllParametersOfDeclarationOnNextLine: true # 允许声明的所有参数换行
AllowBreakBeforeNoexceptSpecifier: Never # 不在 noexcept 前换行
AllowShortBlocksOnASingleLine: Never # 不允许简短的代码块写在一行
AllowShortCaseLabelsOnASingleLine: false # 不允许简短的 case 标签写在一行
AllowShortCompoundRequirementOnASingleLine: true # 允许简短的复合要求写在一行
AllowShortEnumsOnASingleLine: true # 允许简短的枚举写在一行
AllowShortFunctionsOnASingleLine: None # 不允许简短的函数写在一行
AllowShortIfStatementsOnASingleLine: Never # 不允许简短的 if 语句写在一行
AllowShortLambdasOnASingleLine: All # 允许简短的 lambda 表达式写在一行
AllowShortLoopsOnASingleLine: true # 允许简短的循环写在一行
# 大括号和代码块的格式化
AlwaysBreakAfterDefinitionReturnType: None # 不在定义的返回类型后强制换行
AlwaysBreakAfterReturnType: None # 不在返回类型后强制换行
AlwaysBreakBeforeMultilineStrings: true # 在多行字符串前强制换行
AlwaysBreakTemplateDeclarations: Yes # 在模板声明后强制换行
AttributeMacros: # 属性宏
- __capability
BinPackArguments: true # 将参数打包到一行
BinPackParameters: true # 将参数打包到一行
BitFieldColonSpacing: Both # 位字段冒号两侧都添加空格
BraceWrapping: # 大括号换行规则
AfterCaseLabel: false # case 标签后不换行
AfterClass: true # 类定义后换行
AfterControlStatement: Always # 控制语句后总是换行
AfterEnum: false # 枚举定义后不换行
AfterExternBlock: false # extern 块后不换行
AfterFunction: true # 函数定义后换行
AfterNamespace: false # 命名空间定义后不换行
AfterObjCDeclaration: false # Objective-C 声明后不换行
AfterStruct: false # 结构体定义后不换行
AfterUnion: false # 联合体定义后不换行
BeforeCatch: false # catch 前不换行
BeforeElse: false # else 前不换行
BeforeLambdaBody: false # lambda 体前不换行
BeforeWhile: false # while 前不换行
IndentBraces: false # 不缩进大括号本身
SplitEmptyFunction: true # 空函数体分两行
SplitEmptyRecord: true # 空记录分两行
SplitEmptyNamespace: true # 空命名空间分两行
BreakAdjacentStringLiterals: true # 分割相邻的字符串字面量
BreakAfterAttributes: Leave # 属性后换行规则:保留原样
BreakAfterJavaFieldAnnotations: false # Java 字段注解后不换行
BreakArrays: true # 数组声明换行
BreakBeforeBinaryOperators: None # 二元运算符前不换行
BreakBeforeConceptDeclarations: Always # 概念声明前换行
BreakBeforeBraces: Custom # 自定义大括号换行规则
BreakBeforeInlineASMColon: OnlyMultiline # 内联汇编冒号前换行规则:仅在多行时
BreakBeforeTernaryOperators: true # 三元运算符前换行
BreakConstructorInitializers: BeforeColon # 构造函数初始化列表前换行
BreakInheritanceList: BeforeColon # 继承列表前换行
BreakStringLiterals: true # 字符串字面量换行
# 代码宽度和换行
ColumnLimit: 200 # 每行最大字符数
ConstructorInitializerIndentWidth: 4 # 构造函数初始化列表的缩进宽度
ContinuationIndentWidth: 4 # 续行缩进宽度
Cpp11BracedListStyle: true # C++11 初始化列表风格
DerivePointerAlignment: true # 自动推导指针对齐方式
DisableFormat: false # 不禁用格式化
EmptyLineAfterAccessModifier: Never # 不在访问修饰符后添加空行
EmptyLineBeforeAccessModifier: LogicalBlock # 在逻辑块前添加空行
ExperimentalAutoDetectBinPacking: false # 不自动检测二进制打包
FixNamespaceComments: true # 修复命名空间注释
ForEachMacros: # foreach 宏
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros: # if 宏
- KJ_IF_MAYBE
IncludeBlocks: Regroup # 重新分组 include 块
IncludeCategories: # include 分类规则
- Regex: '^<ext/.*\.h>' # 匹配 <ext/*.h>
Priority: 2 # 优先级 2
SortPriority: 0 # 排序优先级 0
CaseSensitive: false # 不区分大小写
- Regex: '^<.*\.h>' # 匹配 <*.h>
Priority: 1 # 优先级 1
SortPriority: 0 # 排序优先级 0
CaseSensitive: false # 不区分大小写
- Regex: '^<.*' # 匹配 <*
Priority: 2 # 优先级 2
SortPriority: 0 # 排序优先级 0
CaseSensitive: false # 不区分大小写
- Regex: '.*' # 匹配所有
Priority: 3 # 优先级 3
SortPriority: 0 # 排序优先级 0
CaseSensitive: false # 不区分大小写
IncludeIsMainRegex: '([-_](test|unittest))?$' # 匹配主文件的正则表达式
IncludeIsMainSourceRegex: '' # 匹配主源文件的正则表达式为空
IndentAccessModifiers: false # 不缩进访问修饰符
IndentCaseBlocks: false # 不缩进 case 块
IndentCaseLabels: true # 缩进 case 标签
IndentExternBlock: AfterExternBlock # extern 块缩进规则
IndentGotoLabels: true # 缩进 goto 标签
IndentPPDirectives: BeforeHash # 预处理指令缩进规则:在井号前缩进
IndentRequiresClause: true # 缩进 requires 子句
IndentWidth: 4 # 缩进宽度
IndentWrappedFunctionNames: false # 不缩进包装的函数名
InsertBraces: false # 不插入大括号
InsertNewlineAtEOF: false # 不在文件末尾插入换行符
InsertTrailingCommas: None # 不插入尾随逗号
IntegerLiteralSeparator: # 整数字面量分隔符
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave # 保留 JavaScript 引号
JavaScriptWrapImports: true # 包装 JavaScript 导入
KeepEmptyLinesAtTheStartOfBlocks: false # 不保留块开头的空行
KeepEmptyLinesAtEOF: false # 不保留文件末尾的空行
LambdaBodyIndentation: Signature # lambda 体缩进规则
LineEnding: DeriveLF # 自动推导换行符为 LF
MacroBlockBegin: '' # 宏块开始为空
MacroBlockEnd: '' # 宏块结束为空
MaxEmptyLinesToKeep: 1 # 保留的最大空行数
NamespaceIndentation: None # 命名空间不缩进
ObjCBinPackProtocolList: Never # Objective-C 协议列表不打包
ObjCBlockIndentWidth: 4 # Objective-C 块缩进宽度
ObjCBreakBeforeNestedBlockParam: true # Objective-C 嵌套块参数前换行
ObjCSpaceAfterProperty: false # Objective-C 属性后不加空格
ObjCSpaceBeforeProtocolList: true # Objective-C 协议列表前加空格
PackConstructorInitializers: NextLine # 构造函数初始化列表换行
PenaltyBreakAssignment: 2 # 分割赋值语句的惩罚值
PenaltyBreakBeforeFirstCallParameter: 1 # 分割第一个调用参数的惩罚值
PenaltyBreakComment: 300 # 分割注释的惩罚值
PenaltyBreakFirstLessLess: 120 # 分割第一个 << 的惩罚值
PenaltyBreakOpenParenthesis: 0 # 分割左括号的惩罚值
PenaltyBreakScopeResolution: 500 # 分割作用域解析的惩罚值
PenaltyBreakString: 1000 # 分割字符串的惩罚值
PenaltyBreakTemplateDeclaration: 10 # 分割模板声明的惩罚值
PenaltyExcessCharacter: 1000000 # 超出字符的惩罚值
PenaltyIndentedWhitespace: 0 # 缩进空格的惩罚值
PenaltyReturnTypeOnItsOwnLine: 200 # 返回类型单独一行的惩罚值
PointerAlignment: Left # 指针对齐方式为左对齐
PPIndentWidth: -1 # 预处理指令缩进宽度
QualifierAlignment: Leave # 限定符对齐规则:保留原样
RawStringFormats: # 原始字符串格式
- Language: Cpp # 语言为 C++
Delimiters: # 分隔符
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: '' # 标准分隔符为空
BasedOnStyle: google # 基于 Google 风格
- Language: TextProto # 语言为 TextProto
Delimiters: # 分隔符
- pb
- PB
- proto
- PROTO
EnclosingFunctions: # 包裹函数
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb # 标准分隔符为 pb
BasedOnStyle: google # 基于 Google 风格
ReferenceAlignment: Pointer # 引用对齐方式与指针相同
ReflowComments: true # 重新排版注释
RemoveBracesLLVM: false # 不移除 LLVM 风格的大括号
RemoveParentheses: Leave # 不移除括号
RemoveSemicolon: false # 不移除分号
RequiresClausePosition: OwnLine # requires 子句位置:单独一行
RequiresExpressionIndentation: OuterScope # requires 表达式缩进规则
SeparateDefinitionBlocks: Leave # 分离定义块规则:保留原样
ShortNamespaceLines: 1 # 简短命名空间的行数
SkipMacroDefinitionBody: false # 不跳过宏定义体
SortIncludes: CaseSensitive # 区分大小写排序 include
SortJavaStaticImport: Before # Java 静态导入排序规则
SortUsingDeclarations: LexicographicNumeric # using 声明排序规则
SpaceAfterCStyleCast: false # C 风格强制转换后不加空格
SpaceAfterLogicalNot: false # 逻辑非操作符后不加空格
SpaceAfterTemplateKeyword: true # 模板关键字后加空格
SpaceAroundPointerQualifiers: Default # 指针限定符周围空格规则:默认
SpaceBeforeAssignmentOperators: true # 赋值操作符前加空格
SpaceBeforeCaseColon: false # case 冒号前不加空格
SpaceBeforeCpp11BracedList: false # C++11 初始化列表前不加空格
SpaceBeforeCtorInitializerColon: true # 构造函数初始化列表前加空格
SpaceBeforeInheritanceColon: true # 继承冒号前加空格
SpaceBeforeJsonColon: false # JSON 冒号前不加空格
SpaceBeforeParens: Never # 左括号前不加空格
SpaceBeforeParensOptions: # 左括号前空格选项
AfterControlStatements: true # 控制语句后加空格
AfterForeachMacros: true # foreach 宏后加空格
AfterFunctionDefinitionName: false # 函数定义名后不加空格
AfterFunctionDeclarationName: false # 函数声明名后不加空格
AfterIfMacros: true # if 宏后加空格
AfterOverloadedOperator: false # 重载操作符后不加空格
AfterPlacementOperator: true # 放置操作符后加空格
AfterRequiresInClause: false # requires 子句后不加空格
AfterRequiresInExpression: false # requires 表达式后不加空格
BeforeNonEmptyParentheses: false # 非空括号前不加空格
SpaceBeforeRangeBasedForLoopColon: true # 基于范围的 for 循环冒号前加空格
SpaceBeforeSquareBrackets: false # 方括号前不加空格
SpaceInEmptyBlock: false # 空代码块内不加空格
SpacesBeforeTrailingComments: 2 # 尾部注释前的空格数
SpacesInAngles: Never # 尖括号内不加空格
SpacesInContainerLiterals: true # 容器字面量内加空格
SpacesInLineCommentPrefix: # 行注释前缀空格
Minimum: 1 # 最小空格数
Maximum: -1 # 最大空格数(无限制)
SpacesInParens: Never # 括号内不加空格
SpacesInParensOptions: # 括号内空格选项
InCStyleCasts: false # C 风格强制转换内不加空格
InConditionalStatements: false # 条件语句内不加空格
InEmptyParentheses: false # 空括号内不加空格
Other: false # 其他情况不加空格
SpacesInSquareBrackets: false # 方括号内不加空格
Standard: Auto # 自动检测 C++ 标准
StatementAttributeLikeMacros: # 类似属性的宏
- Q_EMIT
StatementMacros: # 语句宏
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8 # 制表符宽度
UseTab: Never # 不使用制表符
VerilogBreakBetweenInstancePorts: true # Verilog 实例端口之间换行
WhitespaceSensitiveMacros: # 对空格敏感的宏
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
...

来源:CSDN 原文,首次发布于 2025-04-16。