.clang-format 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # SPDX-License-Identifier: Apache-2.0
  2. #
  3. # Note: The list of ForEachMacros can be obtained using:
  4. #
  5. # git grep -h '^#define [^[:space:]]*FOR_EACH[^[:space:]]*(' include/ \
  6. # | sed "s,^#define \([^[:space:]]*FOR_EACH[^[:space:]]*\)(.*$, - '\1'," \
  7. # | sort | uniq
  8. #
  9. # References:
  10. # - https://clang.llvm.org/docs/ClangFormatStyleOptions.html
  11. ---
  12. # 基于 LLVM 的代码风格作为起点,随后覆盖指定字段
  13. BasedOnStyle: LLVM
  14. # 连续宏定义的对齐方式
  15. # Enabled: true -> 启用对齐连续宏定义
  16. # AcrossComments: true -> 跨注释也会对齐,适合一组宏中间穿插注释的情况
  17. AlignConsecutiveMacros:
  18. Enabled: true
  19. AcrossComments: true
  20. # 是否允许短代码块(如 { ... })出现在单行
  21. AllowShortBlocksOnASingleLine: true
  22. # 是否允许短 case 标签单行
  23. # true -> 允许 `case X: doSomething();`
  24. AllowShortCaseLabelsOnASingleLine: true
  25. # 是否允许短枚举在一行
  26. # true -> 允许短枚举如 `enum { A, B };`
  27. AllowShortEnumsOnASingleLine: false
  28. # 是否允许短函数在单行
  29. AllowShortFunctionsOnASingleLine: true
  30. AllowShortCaseExpressionOnASingleLine: true
  31. # 短 if 语句单行显示策略
  32. # Always -> 允许并尽可能保留短 if 语句为单行(包括带 else 的情况)
  33. # 你希望单行 + 大括号时使用这个选项
  34. AllowShortIfStatementsOnASingleLine: true
  35. # 是否允许短循环(for/while)单行显示
  36. AllowShortLoopsOnASingleLine: true
  37. # 属性宏列表,列出在格式化时应视为属性的宏(影响对齐、换行等)
  38. # 如果代码库使用自定义属性宏,把它们列在这里可以提升格式化准确性
  39. AttributeMacros:
  40. - __aligned
  41. - __deprecated
  42. - __packed
  43. - __printf_like
  44. - __syscall
  45. - __syscall_always_inline
  46. - __subsystem
  47. # 位字段冒号后的空格:After 表示 `int x : 3;` 中冒号后带一个空格(风格选择)
  48. BitFieldColonSpacing: After
  49. # 大括号换行策略:使用 Custom 配合 BraceWrapping 指定细节
  50. # 你用了 Custom,这意味着下面的 BraceWrapping 字段决定具体行为
  51. BreakBeforeBraces: Custom
  52. BraceWrapping:
  53. AfterCaseLabel: false # case 标签后不另起行放 {,通常 case: 仍和语句对齐
  54. AfterClass: true # class 后大括号另起行
  55. AfterControlStatement: Always # 控制语句(if/for/while)后通常将 { 放在新行(可被覆盖)
  56. AfterEnum: true # enum 后另起行
  57. AfterExternBlock: false
  58. AfterFunction: true # 函数体大括号另起行
  59. AfterNamespace: true
  60. AfterObjCDeclaration: true
  61. AfterStruct: true
  62. AfterUnion: false
  63. BeforeCatch: true
  64. BeforeElse: true
  65. BeforeLambdaBody: false
  66. BeforeWhile: false
  67. IndentBraces: false # 不单独缩进大括号行
  68. SplitEmptyFunction: true
  69. SplitEmptyRecord: true
  70. SplitEmptyNamespace: true
  71. # 单行代码的最大列数(换行阈值)
  72. ColumnLimit: 140
  73. # 构造函数初始化列表的缩进宽度(可针对长列表调整可读性)
  74. ConstructorInitializerIndentWidth: 8
  75. # 折行缩进宽度(续行缩进)
  76. ContinuationIndentWidth: 8
  77. # ForEach 宏列表:告诉 clang-format 哪些宏应当当作循环处理(便于格式化块体)
  78. ForEachMacros:
  79. - "ARRAY_FOR_EACH"
  80. - "ARRAY_FOR_EACH_PTR"
  81. - "FOR_EACH"
  82. # If 宏列表:把 CHECKIF 等宏视为 if 语句(影响括号和后续块处理)
  83. IfMacros:
  84. - "CHECKIF"
  85. # include 文件的分类和排序优先级
  86. # Regex: 正则匹配,Priority: 数字越小优先级越高(越先放)
  87. IncludeCategories:
  88. - Regex: '^".*\.h"$'
  89. Priority: 0
  90. - 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>$'
  91. Priority: 1
  92. - Regex: '^\<Ryan/.*\.h\>$'
  93. Priority: 2
  94. - Regex: ".*"
  95. Priority: 3
  96. # case 标签是否缩进(true 会将 case 缩进到 switch 中)
  97. # false -> case 与 switch 对齐(你原先设置 false)
  98. IndentCaseLabels: false
  99. # goto 标签是否缩进(false 表示标签在行首)
  100. IndentGotoLabels: false
  101. # 缩进宽度(通常与制表符策略配合使用)
  102. IndentWidth: 8
  103. # 自动插入大括号(即使单语句也插入 { })
  104. # 这可以避免单行语句因为后续添加语句而引入 bug
  105. InsertBraces: true
  106. # 文件末尾自动插入换行
  107. InsertNewlineAtEOF: true
  108. # 继承冒号前是否加空格(False 表示不加空格:"class A: public B")
  109. SpaceBeforeInheritanceColon: False
  110. # 控制语句后是否加空格(这个值控制 if/for/while 等的格式)
  111. # ControlStatementsExceptControlMacros -> 控制语句(非宏)前加空格:`if (cond)` 而非 `if(cond)`
  112. SpaceBeforeParens: ControlStatementsExceptControlMacros
  113. # 包含文件是否自动排序(Never 表示不排序)
  114. SortIncludes: Never
  115. # 缩进与续行使用制表符策略
  116. # ForContinuationAndIndentation -> 续行与缩进使用制表符,其他空格仍按规则
  117. UseTab: ForContinuationAndIndentation
  118. # 对空白敏感的宏列表(多用于预处理器宏展开格式保持)
  119. WhitespaceSensitiveMacros:
  120. - COND_CODE_0
  121. - COND_CODE_1
  122. - IF_DISABLED
  123. - IF_ENABLED
  124. - LISTIFY
  125. - STRINGIFY
  126. - Z_STRINGIFY
  127. - DT_FOREACH_PROP_ELEM_SEP
  128. # --------------------------
  129. # 可选:降低 clang-format 拆行惩罚,使其更倾向于保留短 if/else 单行
  130. # 下面两个值可以帮助把格式化后的多行 if/else 更可能压缩成单行(仅在 AllowShortIfStatementsOnASingleLine: Always 有效时可用)
  131. # PenaltyBreakIfElse: 0
  132. # PenaltyBreakStatement: 0
  133. # 注:上面的 Penalty 设置是可选的,如果你发现 clang-format 依旧不把某些 if/else 压成单行,可以取消注释并试验效果。