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
|
/* $NetBSD: lsym_binary_op.c,v 1.12 2023/06/14 08:25:15 rillig Exp $ */
/*
* Tests for the token lsym_binary_op, which represents a binary operator in
* an expression. Examples for binary operators are '>>', '=', '+', '&&'.
*
* Binary operators are surrounded by blanks.
*
* Some tokens like '+', '*' or '&' can be either binary or unary operators,
* with an entirely different meaning.
*
* The token '*' is not only a binary or a unary operator, it is used in types
* as well, to derive a pointer type.
*
* See also:
* lsym_postfix_op.c for postfix unary operators
* lsym_unary_op.c for prefix unary operators
* lsym_colon.c for ':'
* lsym_question.c for '?'
* lsym_comma.c for ','
* C99 6.4.6 "Punctuators"
*/
//indent input
void
binary_operators(void)
{
/* In the order of appearance in C11 6.5. */
a = a * a;
a = a / a;
a = a % a;
a = a + a;
a = a - a;
a = a << a;
a = a >> a;
a = a < a;
a = a > a;
a = a <= a;
a = a >= a;
a = a == a;
a = a != a;
a = a & a;
a = a ^ a;
a = a | a;
a = a && a;
a = a || a;
a = a ? a : a;
a = a;
a *= a;
a /= a;
a %= a;
a += a;
a -= a;
a <<= a;
a >>= a;
a &= a;
a ^= a;
a |= a;
a = a, a;
}
//indent end
//indent run-equals-input
/*
* If a '*' is immediately followed by another '*', they still form separate
* operators. The first is a binary operator, the second is unary.
*/
//indent input
int var = expr**ptr;
//indent end
//indent run -di0
int var = expr * *ptr;
//indent end
/*
* Before 2023-06-04, indent allowed for arbitrary repetitions of some operator
* characters, followed by an arbitrary amount of '='. This could be used for
* operators like '&&' or '|||==='.
*
* Before 2021-03-07 22:11:01, the comment '//' was treated as a binary
* operator as well, and so was the comment '/////', leading to unexpected
* spacing.
*
* See lexi.c, lexi, "default:".
*/
//indent input
void
long_run_of_operators(void)
{
if (a &&&&&&& b)
return;
if (a |||=== b)
return;
}
//indent end
//indent run
void
long_run_of_operators(void)
{
if (a && && && &b)
return;
if (a || |= == b)
return;
}
//indent end
/*
* Long chains of '+' and '-' must be split into several operators as the
* lexer has to distinguish between '++' and '+' early. The following
* sequence is thus tokenized as:
*
* word "a"
* postfix_op "++"
* binary_op "++"
* unary_op "++"
* unary_op "+"
* word "b"
*
* See lexi.c, lexi, "case '+':".
*/
//indent input
void
joined_unary_and_binary_operators(void)
{
if (a +++++++ b)
return;
}
//indent end
//indent run
void
joined_unary_and_binary_operators(void)
{
if (a++ ++ ++ +b)
return;
}
//indent end
/*
* Ensure that the result of the indentation does not depend on whether a
* token from the input starts in column 1 or 9.
*
* See process_binary_op.
*/
//indent input
int col_1 //
= //
1;
int col_9 //
= //
9;
//indent end
//indent run
int col_1 //
= //
1;
int col_9 //
= //
9;
//indent end
/*
* The ternary conditional operator is not a binary operator, but both its
* components '?' and ':' follow the same spacing rules.
*/
//indent input
int conditional = condition ? number : number;
//indent end
//indent run-equals-input -di0
// After a ']', a '*' is a binary operator.
//indent input
int x = arr[3]*y;
//indent end
//indent run -di0
int x = arr[3] * y;
//indent end
/*
* Ensure that after an assignment, a '*=' operator is properly spaced, like
* any other binary operator.
*/
//indent input
{
a = a;
a *= b *= c;
}
//indent end
//indent run-equals-input -di0
|