|
|
@@ -1,77 +1,46 @@
|
|
|
-# SPDX-License-Identifier: Apache-2.0
|
|
|
-#
|
|
|
-# Note: The list of ForEachMacros can be obtained using:
|
|
|
-#
|
|
|
-# git grep -h '^#define [^[:space:]]*FOR_EACH[^[:space:]]*(' include/ \
|
|
|
-# | sed "s,^#define \([^[:space:]]*FOR_EACH[^[:space:]]*\)(.*$, - '\1'," \
|
|
|
-# | sort | uniq
|
|
|
-#
|
|
|
-# References:
|
|
|
-# - https://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
|
|
-
|
|
|
---
|
|
|
-# 基于 LLVM 的代码风格作为起点,随后覆盖指定字段
|
|
|
BasedOnStyle: LLVM
|
|
|
|
|
|
-# 连续宏定义的对齐方式
|
|
|
-# Enabled: true -> 启用对齐连续宏定义
|
|
|
-# AcrossComments: true -> 跨注释也会对齐,适合一组宏中间穿插注释的情况
|
|
|
+# --- 对齐策略 ---
|
|
|
+# 对齐连续宏定义,不跨空行
|
|
|
AlignConsecutiveMacros:
|
|
|
Enabled: true
|
|
|
AcrossComments: true
|
|
|
+ AcrossEmptyLines: false
|
|
|
|
|
|
-# 是否允许短代码块(如 { ... })出现在单行
|
|
|
-# true -> 允许短块单行(例如 `{}` 或 `{ a(); }`)
|
|
|
-# 你的原值 true:保持允许(便于简短动作写在一行)
|
|
|
-AllowShortBlocksOnASingleLine: true
|
|
|
+# 对齐位字段和连续赋值(嵌入式配置常用)
|
|
|
+AlignConsecutiveAssignments:
|
|
|
+ Enabled: true
|
|
|
+ AcrossEmptyLines: false
|
|
|
+AlignConsecutiveBitFields:
|
|
|
+ Enabled: true
|
|
|
|
|
|
-# 是否允许短 case 标签单行
|
|
|
-# true -> 允许 `case X: doSomething();`
|
|
|
+# --- 单行短代码压缩策略 ---
|
|
|
+AllowShortBlocksOnASingleLine: true
|
|
|
AllowShortCaseLabelsOnASingleLine: true
|
|
|
-
|
|
|
-# 是否允许短枚举在一行
|
|
|
-# true -> 允许短枚举如 `enum { A, B };`
|
|
|
AllowShortEnumsOnASingleLine: false
|
|
|
-
|
|
|
-# 是否允许短函数在单行
|
|
|
-# None 表示对所有函数都应用此规则(即不限制单行函数)
|
|
|
-# 通常我们不希望把所有函数压成一行,所以保留 None(更严格)
|
|
|
-AllowShortFunctionsOnASingleLine: true
|
|
|
-
|
|
|
-AllowShortCaseExpressionOnASingleLine: true
|
|
|
-
|
|
|
-# 短 if 语句单行显示策略
|
|
|
-# Always -> 允许并尽可能保留短 if 语句为单行(包括带 else 的情况)
|
|
|
-# 你希望单行 + 大括号时使用这个选项
|
|
|
-AllowShortIfStatementsOnASingleLine: true
|
|
|
-
|
|
|
-# 是否允许短循环(for/while)单行显示
|
|
|
+AllowShortFunctionsOnASingleLine: All # 建议显式设为 All
|
|
|
+AllowShortIfStatementsOnASingleLine: AllIfAndElse # 允许 if/else 都在单行
|
|
|
AllowShortLoopsOnASingleLine: true
|
|
|
|
|
|
-# 属性宏列表,列出在格式化时应视为属性的宏(影响对齐、换行等)
|
|
|
-# 如果代码库使用自定义属性宏,把它们列在这里可以提升格式化准确性
|
|
|
-AttributeMacros:
|
|
|
- - __aligned
|
|
|
- - __deprecated
|
|
|
- - __packed
|
|
|
- - __printf_like
|
|
|
- - __syscall
|
|
|
- - __syscall_always_inline
|
|
|
- - __subsystem
|
|
|
-
|
|
|
-# 位字段冒号后的空格:After 表示 `int x : 3;` 中冒号后带一个空格(风格选择)
|
|
|
-BitFieldColonSpacing: After
|
|
|
+# --- 缩进与制表符 ---
|
|
|
+IndentWidth: 8
|
|
|
+TabWidth: 8 # 必须与 IndentWidth 一致
|
|
|
+UseTab: ForContinuationAndIndentation
|
|
|
+ContinuationIndentWidth: 8
|
|
|
+ConstructorInitializerIndentWidth: 8
|
|
|
|
|
|
-# 大括号换行策略:使用 Custom 配合 BraceWrapping 指定细节
|
|
|
-# 你用了 Custom,这意味着下面的 BraceWrapping 字段决定具体行为
|
|
|
+# --- 大括号换行 (重点修正) ---
|
|
|
BreakBeforeBraces: Custom
|
|
|
BraceWrapping:
|
|
|
- AfterCaseLabel: false # case 标签后不另起行放 {,通常 case: 仍和语句对齐
|
|
|
- AfterClass: true # class 后大括号另起行
|
|
|
- AfterControlStatement: Always # 控制语句(if/for/while)后通常将 { 放在新行(可被覆盖)
|
|
|
- AfterEnum: true # enum 后另起行
|
|
|
+ AfterCaseLabel: false
|
|
|
+ AfterClass: true
|
|
|
+ # 改为 MultiLine: 只有当 if 内容本身跨行时,大括号才换行
|
|
|
+ # 这样可以配合 AllowShortIfStatements 保持真正的单行短语句
|
|
|
+ AfterControlStatement: MultiLine
|
|
|
+ AfterEnum: true
|
|
|
AfterExternBlock: false
|
|
|
- AfterFunction: true # 函数体大括号另起行
|
|
|
+ AfterFunction: true
|
|
|
AfterNamespace: true
|
|
|
AfterObjCDeclaration: true
|
|
|
AfterStruct: true
|
|
|
@@ -80,88 +49,21 @@ BraceWrapping:
|
|
|
BeforeElse: true
|
|
|
BeforeLambdaBody: false
|
|
|
BeforeWhile: false
|
|
|
- IndentBraces: false # 不单独缩进大括号行
|
|
|
+ IndentBraces: false
|
|
|
SplitEmptyFunction: true
|
|
|
SplitEmptyRecord: true
|
|
|
SplitEmptyNamespace: true
|
|
|
|
|
|
-# 单行代码的最大列数(换行阈值)
|
|
|
+# --- 细节修正 ---
|
|
|
ColumnLimit: 140
|
|
|
-
|
|
|
-# 构造函数初始化列表的缩进宽度(可针对长列表调整可读性)
|
|
|
-ConstructorInitializerIndentWidth: 8
|
|
|
-
|
|
|
-# 折行缩进宽度(续行缩进)
|
|
|
-ContinuationIndentWidth: 8
|
|
|
-
|
|
|
-# ForEach 宏列表:告诉 clang-format 哪些宏应当当作循环处理(便于格式化块体)
|
|
|
-ForEachMacros:
|
|
|
- - "ARRAY_FOR_EACH"
|
|
|
- - "ARRAY_FOR_EACH_PTR"
|
|
|
- - "FOR_EACH"
|
|
|
-
|
|
|
-# If 宏列表:把 CHECKIF 等宏视为 if 语句(影响括号和后续块处理)
|
|
|
-IfMacros:
|
|
|
- - "CHECKIF"
|
|
|
-
|
|
|
-# include 文件的分类和排序优先级
|
|
|
-# Regex: 正则匹配,Priority: 数字越小优先级越高(越先放)
|
|
|
-IncludeCategories:
|
|
|
- - Regex: '^".*\.h"$'
|
|
|
- Priority: 0
|
|
|
- - Regex: '^<(assert|complex|ctype|errno|fenv|float|inttypes|limits|locale|math|setjmp|signal|stdarg|stdbool|stddef|stdint|stdio|stdlib|string|tgmath|time|wchar|wctype)\.h>$'
|
|
|
- Priority: 1
|
|
|
- - Regex: '^\<Ryan/.*\.h\>$'
|
|
|
- Priority: 2
|
|
|
- - Regex: ".*"
|
|
|
- Priority: 3
|
|
|
-
|
|
|
-# case 标签是否缩进(true 会将 case 缩进到 switch 中)
|
|
|
-# false -> case 与 switch 对齐(你原先设置 false)
|
|
|
IndentCaseLabels: false
|
|
|
-
|
|
|
-# goto 标签是否缩进(false 表示标签在行首)
|
|
|
IndentGotoLabels: false
|
|
|
-
|
|
|
-# 缩进宽度(通常与制表符策略配合使用)
|
|
|
-IndentWidth: 8
|
|
|
-
|
|
|
-# 自动插入大括号(即使单语句也插入 { })
|
|
|
-# 这可以避免单行语句因为后续添加语句而引入 bug
|
|
|
InsertBraces: true
|
|
|
-
|
|
|
-# 文件末尾自动插入换行
|
|
|
InsertNewlineAtEOF: true
|
|
|
+SortIncludes: Never # 既然你不排序,IncludeCategories 的优先级其实失效了
|
|
|
|
|
|
-# 继承冒号前是否加空格(False 表示不加空格:"class A: public B")
|
|
|
-SpaceBeforeInheritanceColon: False
|
|
|
+# 强制在指针符号左侧对齐 (int* p),如果你喜欢嵌入式风格 (int *p),改为 Right
|
|
|
+PointerAlignment: Right
|
|
|
|
|
|
-# 控制语句后是否加空格(这个值控制 if/for/while 等的格式)
|
|
|
-# ControlStatementsExceptControlMacros -> 控制语句(非宏)前加空格:`if (cond)` 而非 `if(cond)`
|
|
|
+# 控制语句空格
|
|
|
SpaceBeforeParens: ControlStatementsExceptControlMacros
|
|
|
-
|
|
|
-# 包含文件是否自动排序(Never 表示不排序)
|
|
|
-SortIncludes: Never
|
|
|
-
|
|
|
-# 缩进与续行使用制表符策略
|
|
|
-# ForContinuationAndIndentation -> 续行与缩进使用制表符,其他空格仍按规则
|
|
|
-UseTab: ForContinuationAndIndentation
|
|
|
-
|
|
|
-# 对空白敏感的宏列表(多用于预处理器宏展开格式保持)
|
|
|
-WhitespaceSensitiveMacros:
|
|
|
- - COND_CODE_0
|
|
|
- - COND_CODE_1
|
|
|
- - IF_DISABLED
|
|
|
- - IF_ENABLED
|
|
|
- - LISTIFY
|
|
|
- - STRINGIFY
|
|
|
- - Z_STRINGIFY
|
|
|
- - DT_FOREACH_PROP_ELEM_SEP
|
|
|
-
|
|
|
-# --------------------------
|
|
|
-# 可选:降低 clang-format 拆行惩罚,使其更倾向于保留短 if/else 单行
|
|
|
-# 下面两个值可以帮助把格式化后的多行 if/else 更可能压缩成单行(仅在 AllowShortIfStatementsOnASingleLine: Always 有效时可用)
|
|
|
-# PenaltyBreakIfElse: 0
|
|
|
-# PenaltyBreakStatement: 0
|
|
|
-
|
|
|
-# 注:上面的 Penalty 设置是可选的,如果你发现 clang-format 依旧不把某些 if/else 压成单行,可以取消注释并试验效果。
|