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
|
/* $NetBSD: token_preprocessing.c,v 1.2 2021/11/26 14:27:19 rillig Exp $ */
/* $FreeBSD$ */
/*-
* Tests for indenting preprocessing directives:
*
* #define
* #ifdef
* #pragma
* #line
*/
#indent input
#include <system-header.h>
#include "local-header.h"
#indent end
#indent run-equals-input
/*
* Nested conditional compilation.
*/
#indent input
#if 0
#else
#endif
#if 0 /* if comment */
#else /* else comment */
#endif /* endif comment */
#if 0 /* outer if comment */
# if nested /* inner if comment */
# else /* inner else comment */
# endif /* inner endif comment */
#endif /* outer endif comment */
#indent end
#indent run
#if 0
#else
#endif
#if 0 /* if comment */
#else /* else comment */
#endif /* endif comment */
#if 0 /* outer if comment */
/* $ XXX: The indentation is removed, which can get confusing */
#if nested /* inner if comment */
#else /* inner else comment */
#endif /* inner endif comment */
#endif /* outer endif comment */
#indent end
#indent input
#define multi_line_definition /* first line
* middle
* final line
*/ actual_value
#indent end
#indent run-equals-input
/*
* Before indent.c 1.129 from 2021-10-08, indent mistakenly interpreted quotes
* in comments as starting a string literal. The '"' in the comment started a
* string, the next '"' finished the string, and the following '/' '*' was
* interpreted as the beginning of a comment. This comment lasted until the
* next '*' '/', which in this test is another preprocessor directive, solely
* for symmetry.
*
* The effect was that the extra space after d2 was not formatted, as that
* line was considered part of the comment.
*/
#indent input
#define comment_in_string_literal "/* no comment "
int this_is_an_ordinary_line_again;
int d1 ;
#define confuse_d /*"*/ "/*"
int d2 ;
#define resolve_d "*/"
int d3 ;
int s1 ;
#define confuse_s /*'*/ '/*'
int s2 ;
#define resolve_s '*/'
int s3 ;
#indent end
#indent run
#define comment_in_string_literal "/* no comment "
int this_is_an_ordinary_line_again;
int d1;
#define confuse_d /*"*/ "/*"
int d2;
#define resolve_d "*/"
int d3;
int s1;
#define confuse_s /*'*/ '/*'
int s2;
#define resolve_s '*/'
int s3;
#indent end
/*
* A preprocessing directive inside an expression keeps the state about
* whether the next operator is unary or binary.
*/
#indent input
int binary_plus = 3
#define intermediate 1
+4;
int unary_plus =
#define intermediate 1
+ 4;
#indent end
#indent run
int binary_plus = 3
#define intermediate 1
+ 4;
int unary_plus =
#define intermediate 1
+4;
#indent end
/*
* Before io.c 1.135 from 2021-11-26, indent fixed malformed preprocessing
* lines that had arguments even though they shouldn't. It is not the task of
* an indenter to fix code, that's what a linter is for.
*/
#indent input
#if 0
#elif 1
#else if 3
#endif 0
#indent end
#indent run-equals-input
/*
* Existing comments are indented just like code comments.
*
* This means that the above wrong preprocessing lines (#else with argument)
* need to be fed through indent twice until they become stable. Since
* compilers issue warnings about these invalid lines, not much code still has
* these, making this automatic fix an edge case.
*/
#indent input
#if 0 /* comment */
#else /* comment */
#endif /* comment */
#if 0/* comment */
#else/* comment */
#endif/* comment */
#indent end
#indent run
#if 0 /* comment */
#else /* comment */
#endif /* comment */
#if 0 /* comment */
#else /* comment */
#endif /* comment */
#indent end
|