11# SPDX-License-Identifier: Apache-2.0
22#
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
3+ # RyanJson clang-format 基线配置
4+ # 说明:
5+ # - 注释尽量覆盖每个配置项,便于长期维护
6+ # - 以 LLVM 为基线,只覆盖项目明确约束
7+ # - 参考:https://clang.llvm.org/docs/ClangFormatStyleOptions.html
118
129---
13- # 基于 LLVM 的代码风格作为起点,随后覆盖指定字段
10+ # 基础风格模板:以 LLVM 为起点
1411BasedOnStyle : LLVM
1512
16- # 连续宏定义的对齐方式
17- # Enabled: true -> 启用对齐连续宏定义
18- # AcrossComments: true -> 跨注释也会对齐,适合一组宏中间穿插注释的情况
19- AlignConsecutiveMacros :
20- Enabled : true
21- AcrossComments : true
13+ # 单行最大宽度:超过后自动换行
14+ ColumnLimit : 140
2215
23- # 是否允许短代码块(如 { ... })出现在单行
24- AllowShortBlocksOnASingleLine : true
16+ # 基础缩进宽度(单位:列)
17+ IndentWidth : 8
2518
26- # 是否允许短 case 标签单行
27- # true -> 允许 `case X: doSomething();`
28- AllowShortCaseLabelsOnASingleLine : true
19+ # Tab 显示宽度(单位:列)
20+ TabWidth : 8
2921
30- # 是否允许短枚举在一行
31- # true -> 允许短枚举如 `enum { A, B };`
32- AllowShortEnumsOnASingleLine : false
22+ # 续行缩进宽度(函数参数折行、表达式折行等)
23+ ContinuationIndentWidth : 8
3324
34- # 是否允许短函数在单行
35- AllowShortFunctionsOnASingleLine : true
25+ # 构造函数初始化列表缩进宽度(主要影响 C++,保留统一)
26+ ConstructorInitializerIndentWidth : 8
3627
37- AllowShortCaseExpressionOnASingleLine : true
28+ # Tab 使用策略:缩进和续行使用 Tab
29+ UseTab : ForContinuationAndIndentation
3830
39- # 短 if 语句单行显示策略
40- # Always -> 允许并尽可能保留短 if 语句为单行(包括带 else 的情况)
41- # 你希望单行 + 大括号时使用这个选项
42- AllowShortIfStatementsOnASingleLine : true
31+ # 注释重排:false 表示不自动重排注释文本
32+ ReflowComments : false
4333
44- # 是否允许短循环(for/while)单行显示
45- AllowShortLoopsOnASingleLine : true
34+ # 是否允许短代码块单行(如 { return; })
35+ AllowShortBlocksOnASingleLine : Always
4636
47- # 属性宏列表,列出在格式化时应视为属性的宏(影响对齐、换行等)
48- # 如果代码库使用自定义属性宏,把它们列在这里可以提升格式化准确性
49- AttributeMacros :
50- - __aligned
51- - __deprecated
52- - __packed
53- - __printf_like
54- - __syscall
55- - __syscall_always_inline
56- - __subsystem
37+ # 是否允许短函数单行
38+ AllowShortFunctionsOnASingleLine : false
5739
58- # 位字段冒号后的空格:After 表示 `int x : 3;` 中冒号后带一个空格(风格选择)
59- BitFieldColonSpacing : After
40+ # 是否允许短 if 单行
41+ AllowShortIfStatementsOnASingleLine : WithoutElse
42+
43+ # 是否允许短循环单行
44+ AllowShortLoopsOnASingleLine : false
6045
61- # 大括号换行策略:使用 Custom 配合 BraceWrapping 指定细节
62- # 你用了 Custom,这意味着下面的 BraceWrapping 字段决定具体行为
46+ # 是否允许短 case 标签单行
47+ AllowShortCaseLabelsOnASingleLine : true
48+
49+ # 是否允许短 case 表达式单行
50+ AllowShortCaseExpressionOnASingleLine : true
51+
52+ # 是否允许短枚举单行
53+ AllowShortEnumsOnASingleLine : false
54+
55+ # 花括号总策略:自定义
6356BreakBeforeBraces : Custom
57+
58+ # case 标签后的大括号是否换行
6459BraceWrapping :
65- AfterCaseLabel : false # case 标签后不另起行放 {,通常 case: 仍和语句对齐
66- AfterClass : true # class 后大括号另起行
67- AfterControlStatement : Always # 控制语句(if/for/while)后通常将 { 放在新行(可被覆盖)
68- AfterEnum : true # enum 后另起行
60+ AfterCaseLabel : false
61+
62+ # class 后的大括号是否换行
63+ AfterClass : true
64+
65+ # 控制语句(if/for/while)后的大括号换行策略
66+ AfterControlStatement : Always
67+
68+ # enum 后的大括号是否换行
69+ AfterEnum : true
70+
71+ # extern block 后的大括号是否换行
6972 AfterExternBlock : false
70- AfterFunction : true # 函数体大括号另起行
73+
74+ # 函数定义后的大括号是否换行
75+ AfterFunction : true
76+
77+ # namespace 后的大括号是否换行
7178 AfterNamespace : true
79+
80+ # ObjC 声明后的大括号是否换行
7281 AfterObjCDeclaration : true
82+
83+ # struct 后的大括号是否换行
7384 AfterStruct : true
85+
86+ # union 后的大括号是否换行
7487 AfterUnion : false
88+
89+ # catch 前是否换行
7590 BeforeCatch : true
91+
92+ # else 前是否换行
7693 BeforeElse : true
94+
95+ # lambda 体前是否换行
7796 BeforeLambdaBody : false
97+
98+ # do...while 中 while 前是否换行
7899 BeforeWhile : false
79- IndentBraces : false # 不单独缩进大括号行
100+
101+ # 大括号行本身是否额外缩进
102+ IndentBraces : false
103+
104+ # 空函数是否分裂成多行
80105 SplitEmptyFunction : true
106+
107+ # 空记录(如空 struct)是否分裂成多行
81108 SplitEmptyRecord : true
109+
110+ # 空命名空间是否分裂成多行
82111 SplitEmptyNamespace : true
83112
84- # 单行代码的最大列数(换行阈值)
85- ColumnLimit : 140
113+ # switch 内 case 标签是否额外缩进
114+ IndentCaseLabels : false
86115
87- # 构造函数初始化列表的缩进宽度(可针对长列表调整可读性)
88- ConstructorInitializerIndentWidth : 8
116+ # goto 标签是否额外缩进
117+ IndentGotoLabels : false
89118
90- # 折行缩进宽度(续行缩进 )
91- ContinuationIndentWidth : 8
119+ # 是否强制插入花括号(单语句控制流也加花括号 )
120+ InsertBraces : true
92121
93- # ForEach 宏列表:告诉 clang-format 哪些宏应当当作循环处理(便于格式化块体)
94- ForEachMacros :
95- - " ARRAY_FOR_EACH"
96- - " ARRAY_FOR_EACH_PTR"
97- - " FOR_EACH"
122+ # 文件末尾是否补换行
123+ InsertNewlineAtEOF : true
98124
99- # If 宏列表:把 CHECKIF 等宏视为 if 语句(影响括号和后续块处理)
100- IfMacros :
101- - " CHECKIF"
125+ # 位域冒号前后空格策略
126+ BitFieldColonSpacing : After
127+
128+ # 控制语句括号前空格策略(if (x))
129+ SpaceBeforeParens : ControlStatementsExceptControlMacros
130+
131+ # 继承冒号前空格策略(主要影响 C++)
132+ SpaceBeforeInheritanceColon : false
102133
103- # include 文件的分类和排序优先级
104- # Regex: 正则匹配,Priority: 数字越小优先级越高(越先放)
134+ # 连续宏定义对齐策略
135+ AlignConsecutiveMacros :
136+ # 是否启用连续宏对齐
137+ Enabled : true
138+
139+ # 是否跨注释继续对齐
140+ AcrossComments : true
141+
142+ # include 是否自动排序(Never 表示保持人工顺序)
143+ SortIncludes : Never
144+
145+ # include 分类规则(数值越小优先级越高)
105146IncludeCategories :
147+ # 项目内双引号头文件
106148 - Regex : ' ^".*\.h"$'
107149 Priority : 0
150+
151+ # C 标准库头文件
108152 - 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>$'
109153 Priority : 1
110- - Regex : ' ^\<Ryan/.*\.h\>$'
154+
155+ # Ryan 体系头文件
156+ - Regex : ' ^<Ryan/.*\.h>$'
111157 Priority : 2
158+
159+ # 兜底分类
112160 - Regex : " .*"
113161 Priority : 3
114162
115- # case 标签是否缩进(true 会将 case 缩进到 switch 中)
116- # false -> case 与 switch 对齐(你原先设置 false)
117- IndentCaseLabels : false
118-
119- # goto 标签是否缩进(false 表示标签在行首)
120- IndentGotoLabels : false
121-
122- # 缩进宽度(通常与制表符策略配合使用)
123- IndentWidth : 8
124-
125- # 自动插入大括号(即使单语句也插入 { })
126- # 这可以避免单行语句因为后续添加语句而引入 bug
127- InsertBraces : true
128-
129- # 文件末尾自动插入换行
130- InsertNewlineAtEOF : true
131-
132- # 继承冒号前是否加空格(False 表示不加空格:"class A: public B")
133- SpaceBeforeInheritanceColon : False
134-
135- # 控制语句后是否加空格(这个值控制 if/for/while 等的格式)
136- # ControlStatementsExceptControlMacros -> 控制语句(非宏)前加空格:`if (cond)` 而非 `if(cond)`
137- SpaceBeforeParens : ControlStatementsExceptControlMacros
163+ # 视为 foreach 语义的宏列表(用于正确缩进与换行)
164+ ForEachMacros :
165+ - RyanJsonArrayForEach
166+ - RyanJsonObjectForEach
138167
139- # 包含文件是否自动排序(Never 表示不排序)
140- SortIncludes : Never
168+ # 视为 if 语义的宏列表
169+ IfMacros :
170+ - CHECKIF
141171
142- # 缩进与续行使用制表符策略
143- # ForContinuationAndIndentation -> 续行与缩进使用制表符,其他空格仍按规则
144- UseTab : ForContinuationAndIndentation
172+ # 视为属性的宏列表(影响断行与对齐)
173+ AttributeMacros :
174+ - __aligned
175+ - __deprecated
176+ - __packed
177+ - __printf_like
178+ - __syscall
179+ - __syscall_always_inline
180+ - __subsystem
145181
146- # 对空白敏感的宏列表(多用于预处理器宏展开格式保持)
182+ # 空白敏感宏:保持参数空格布局,避免被格式化破坏
147183WhitespaceSensitiveMacros :
148184 - COND_CODE_0
149185 - COND_CODE_1
@@ -153,11 +189,3 @@ WhitespaceSensitiveMacros:
153189 - STRINGIFY
154190 - Z_STRINGIFY
155191 - DT_FOREACH_PROP_ELEM_SEP
156-
157- # --------------------------
158- # 可选:降低 clang-format 拆行惩罚,使其更倾向于保留短 if/else 单行
159- # 下面两个值可以帮助把格式化后的多行 if/else 更可能压缩成单行(仅在 AllowShortIfStatementsOnASingleLine: Always 有效时可用)
160- # PenaltyBreakIfElse: 0
161- # PenaltyBreakStatement: 0
162-
163- # 注:上面的 Penalty 设置是可选的,如果你发现 clang-format 依旧不把某些 if/else 压成单行,可以取消注释并试验效果。
0 commit comments