| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- # 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
- # 是否允许短代码块(如 { ... })出现在单行
- AllowShortBlocksOnASingleLine: true
- # 是否允许短 case 标签单行
- # true -> 允许 `case X: doSomething();`
- AllowShortCaseLabelsOnASingleLine: true
- # 是否允许短枚举在一行
- # true -> 允许短枚举如 `enum { A, B };`
- AllowShortEnumsOnASingleLine: false
- # 是否允许短函数在单行
- AllowShortFunctionsOnASingleLine: true
- AllowShortCaseExpressionOnASingleLine: true
- # 短 if 语句单行显示策略
- # Always -> 允许并尽可能保留短 if 语句为单行(包括带 else 的情况)
- # 你希望单行 + 大括号时使用这个选项
- AllowShortIfStatementsOnASingleLine: true
- # 是否允许短循环(for/while)单行显示
- AllowShortLoopsOnASingleLine: true
- # 属性宏列表,列出在格式化时应视为属性的宏(影响对齐、换行等)
- # 如果代码库使用自定义属性宏,把它们列在这里可以提升格式化准确性
- AttributeMacros:
- - __aligned
- - __deprecated
- - __packed
- - __printf_like
- - __syscall
- - __syscall_always_inline
- - __subsystem
- # 位字段冒号后的空格:After 表示 `int x : 3;` 中冒号后带一个空格(风格选择)
- BitFieldColonSpacing: After
- # 大括号换行策略:使用 Custom 配合 BraceWrapping 指定细节
- # 你用了 Custom,这意味着下面的 BraceWrapping 字段决定具体行为
- BreakBeforeBraces: Custom
- BraceWrapping:
- AfterCaseLabel: false # case 标签后不另起行放 {,通常 case: 仍和语句对齐
- AfterClass: true # class 后大括号另起行
- AfterControlStatement: Always # 控制语句(if/for/while)后通常将 { 放在新行(可被覆盖)
- AfterEnum: true # enum 后另起行
- AfterExternBlock: false
- AfterFunction: true # 函数体大括号另起行
- AfterNamespace: true
- AfterObjCDeclaration: true
- AfterStruct: true
- AfterUnion: false
- BeforeCatch: true
- BeforeElse: true
- BeforeLambdaBody: false
- BeforeWhile: 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
- # 继承冒号前是否加空格(False 表示不加空格:"class A: public B")
- SpaceBeforeInheritanceColon: False
- # 控制语句后是否加空格(这个值控制 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 压成单行,可以取消注释并试验效果。
|