-
Notifications
You must be signed in to change notification settings - Fork 0
/
.clang-format
275 lines (200 loc) · 11.1 KB
/
.clang-format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Version: 1
# Minimum clang-format version is 18.
# My personal opinion on what a good-ish style for C is, based on at least some amount of reason (sometimes) ;)
# This forgoes a lot of the C++/ObjC-specific stuff, simply because I practically don't ever write C++/ObjC.
BasedOnStyle: WebKit
Language: Cpp # This is also for C.
# Makes numbers look better, especially if there's a unary operand in front.
AlignArrayOfStructures: Right
# For alignment, there's a bit of a tradeoff between readability and diff size.
# For all assignments, I think this would be obnoxious, which is why they're disabled.
# But for switch cases and bitfields, which are rarer and (at least for bitfields) are less likely to provide valuable information in e.g. a blame, I think readability trumps.
# TODO Same argument for enums, but unfortunately, clang-format lumps those in with assignments at the moment, so we have to disable that for now: https://github.com/llvm/llvm-project/issues/52983
# TODO As for alignment after open brackets, this seems to be a bit of a mess: https://github.com/llvm/llvm-project/issues/80049
AlignAfterOpenBracket: BlockIndent
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: Left
AlignConsecutiveShortCaseStatements: # clang-format 17
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCaseColons: false
SpaceBeforeCaseColon: false
# This is prettier because all terms of a binary expression are aligned this way, including the first one:
# int a = b +
# c;
# vs:
# int a = b
# + c;
AlignOperands: DontAlign
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
AlignTrailingComments: Always # clang-format 17
# Better than having them on the same line, because for big argument/parameter lists, it's easier to see what's what, and each argument can be changed individually in the diff.
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
# clang-format 19: BreakFunctionDefinitionParameters: false
# Never allow this because a block could modify control flow, which is harder to spot when bughunting if it's not on its own line.
# I do allow this for case labels though, because they tend to be shorter than if/while/for which can be arbitrarily long, and because we anyway align them (which makes control-flow modifying operations easier to spot).
# Anyways, you can usually assume there's control-flow modification with a case label if it's on one line, because otherwise it would be on multiple lines with a break at the end.
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
# There's really no need for this, single-line functions are uncommon enough.
# Disallow empty ones too because almost always when a function is empty it's because content is intended later.
AllowShortFunctionsOnASingleLine: None
# I want each enum value to appear clearly in the diff.
AllowShortEnumsOnASingleLine: false
# More readable for long variable declarations, and has the added benefit of making diffs more localized.
AlwaysBreakBeforeMultilineStrings: true
# Allow groupings of arguments when split over multiple lines, e.g.:
# resize(
# ...
# width, height,
# );
# TODO Not sure this does what I think it does.
# BinPackArguments: true
# BinPackParameters: true
BinPackArguments: false
BinPackParameters: false
# Might as well have more spacing, fields aren't generally too long so we can afford it.
BitFieldColonSpacing: Both
# I really don't see the point of brace wrapping (sorry Eric).
# For the Before*'s, I might change my mind on this because technically they don't cause any diff-related problems, but I can't get myself to like no-wrapping aesthetically.
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeWhile: false
IndentBraces: false # GNU is crazy for this
SplitEmptyFunction: false # redundant with AllowShortFunctionsOnASingleLine I think
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakAdjacentStringLiterals: false # clang-format 18
# The user probably had a reason for why they wrote it the way they did (e.g. I wouldn't be putting a __printf__ on the same line as the function declaration but I would with __unused__ on a variable declaration).
# There are arguments both ways.
BreakAfterAttributes: Leave # clang-format 16
# Honestly there's a good argument to be made for this.
# In FreeBSD's source, it was really nice to be able to search for function definitions with the ^ character in regex.
# This is the reason for setting IndentWrappedFunctionNames to false.
# But really, I should be depending on the LSP for this rather, and it takes up an extra line for "nothing".
PenaltyReturnTypeOnItsOwnLine: 100
AlwaysBreakAfterDefinitionReturnType: None
# clang-format 19: BreakAfterReturnType: None
IndentWrappedFunctionNames: false
# Usually this is more readable, as this stuff can get very complex.
# Inline ASM is never very extensively used, so I think the complexity is worth the extra breathing room.
BreakBeforeInlineASMColon: Always # clang-format 16
# The user knows this best.
BreakStringLiterals: true
# The user knows this best.
# For comments, I think wrapping is a fine solution.
ColumnLimit: 0
ReflowComments: false
# A bit of a touchy subject here and I get the idea behind right-hand pointers/references, but nowadays this is so much associated to the type that it really makes more sense to me to have it on the left.
DerivePointerAlignment: false
PointerAlignment: Left
ReferenceAlignment: Left
SpaceAroundPointerQualifiers: Default
# TODO Which ones are used in Linux/FreeBSD?
ForEachMacros: ['FOREACH']
IfMacros: []
MacroBlockBegin: ''
MacroBlockEnd: ''
Macros: [] # clang-format 17
SkipMacroDefinitionBody: false # clang-format 18
StatementAttributeLikeMacros: []
StatementMacros: []
TypeNames: [] # clang-format 17
TypenameMacros: []
WhitespaceSensitiveMacros: ["STRINGIZE"]
# I think it's best if different kinds of includes are segregated; it makes them easier to find (though I think I need a bit more configuration for IncludeBlocks: Regroup).
# Sorting them just normalizes things.
# TODO IncludeBlocks: Regroup
IncludeBlocks: Preserve
# TODO IncludeCategories
SortIncludes: true
# In generally I find it best to reduce indentation, and this is especially the case for cases which would otherwise start off with a level of indentation due to the switch statement.
# It also follows the most common convention for labels.
# For labels, I don't have a strong opinion one way or another, so might as well mimic the switch cases.
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: false
IndentGotoLabels: false
IndentPPDirectives: AfterHash
PPIndentWidth: 1
# Also a bit of a touchy subject, but one I feel strongly about.
# There is literally no reason I can conceive to use spaces as opposed to tabs.
# And though I've searched far and wide for a good reason to, most people base themselves off of moronic logic or emotional arguments.
# The biggest argument for using tabs for me though is accessibility; someone with visual impairments, who usually code with a larger font size, like to reduce their indentation size sometimes down to 1 or 2 spaces in order to use horizontal space as efficiently as possible.
# If you use spaces, you're forcing them to scroll horizontally or to have a lot of text wrapping, which is uncool.
# As for the indentation width, that's purely a matter of taste with tabs.
# I think 3 is a good compromise between readability and horizontal space usage (I was inspired by Valgrind's source).
UseTab: AlignWithSpaces # I really don't know what the difference between this and ForContinuationAndIndentation is - the documentation is unclear.
IndentWidth: 3
TabWidth: 3
ContinuationIndentWidth: 3
BracedInitializerIndentWidth: 3
ObjCBlockIndentWidth: 3 # Are also sometimes used as an extension in regular C (-fblocks).
# I go back and forth on this one, but a lot of languages anyway force this, it reduces ambiguity, and can prevent the famous Apple goto fail bug (though realistically your compiler should anyway warn you about code like that).
InsertBraces: true
# Anyone who doesn't do this is a monster.
# I have no clue whatsoever why this isn't the default for VSCode and IntelliJ.
# Well, I guess that makes sense for JetBrains, they like doing stuff stupidly.
# Without this, everytime you add a line to the end of a file, you're changing two lines in the diff instead of just one.
# Same argument goes for trailing commas (though the option doesn't do anything for C I believe).
InsertNewlineAtEOF: true # clang-format 16
KeepEmptyLinesAtEOF: false # clang-format 17
InsertTrailingCommas: Wrapped
# In general, I think avoiding empty lines where unnecessary is best, because vertical space is expensive.
# But definition blocks are more readable with spacing between them and are uncommon enough that it's not a big deal.
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
SeparateDefinitionBlocks: Always
# Windows is insane.
LineEnding: LF # clang-format 16
# Is much more consistent, because you anyway have to put the qualifier to the right side of the star if you wanna make a pointer itself const.
# So instead of writing const int* const, you just write int const* const.
# Also it removes that ambiguity for newbies about what the const really applies to.
QualifierAlignment: Right
# The user knows this best, and apparently this is a risky option to set.
RemoveParentheses: Leave # clang-format 17
# There's no point to keeping this.
RemoveSemicolon: true # clang-format 16
# Most places in this style have a space after a closing parenthesis, but I don't otherwise feel strongly about this.
SpaceAfterCStyleCast: true
# Unary operators rarely have a space after them, so I don't see why nots should.
SpaceAfterLogicalNot: false
# Personal preference, but I think it shows stronger separation between the LHS and the expression (especially since that LHS has spaces in it if it's a declaration).
SpaceBeforeAssignmentOperators: true
# This is really personal preference, I have no good argument for this.
SpaceBeforeParens: ControlStatementsExceptControlMacros
# Since this is strongly associated to the identifier as it has a high precedence, I think having a space makes it harder to read.
SpaceBeforeSquareBrackets: false
# Don't see the point.
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesInParens: Never # clang-format 17
SpacesInSquareBrackets: false
# I go back and forth on this one, but not having spaces makes it consistent with argument lists.
# Apparently, this is what's preferred in C++11.
SpacesInContainerLiterals: false
Cpp11BracedListStyle: true
# Comments are in a sense the lowest precedence things, so I think having spacing around them is best.
SpacesBeforeTrailingComments: 1
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1