xref: /netbsd-src/tests/usr.bin/indent/indent_off_on.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /* $NetBSD: indent_off_on.c,v 1.8 2022/04/24 09:04:12 rillig Exp $ */
2 
3 /*
4  * Tests for the comments 'INDENT OFF' and 'INDENT ON', which temporarily
5  * disable formatting, copying the input directly to the output.  Internally,
6  * indent still keeps track of the number of braces and other indentation.
7  */
8 
9 //indent input
10 {}
11 
12 /*INDENT OFF*/
13 /*INDENT ON*/
14 
15 {}
16 //indent end
17 
18 /*
19  * XXX: It is asymmetric that 'INDENT OFF' is kept as is, while 'INDENT ON'
20  * gets enclosed with spaces.
21  */
22 //indent run
23 {
24 }
25 /* $ FIXME: This empty line must stay. */
26 /*INDENT OFF*/
27 /* INDENT ON */
28 
29 {
30 }
31 //indent end
32 
33 
34 //indent input
35 {}
36 
37 
38 /*INDENT OFF*/
39 /*INDENT ON*/
40 //indent end
41 
42 //indent run
43 {
44 }
45 /* $ FIXME: This empty line must stay. */
46 /* $ FIXME: This empty line must stay. */
47 /*INDENT OFF*/
48 /* INDENT ON */
49 //indent end
50 
51 
52 //indent input
53 {}
54  /* INDENT OFF */
55  /* INDENT ON */
56 {}
57 //indent end
58 
59 /*
60  * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
61  * is aligned.
62  */
63 //indent run
64 {
65 }
66  /* INDENT OFF */
67 /* INDENT ON */
68 {
69 }
70 //indent end
71 
72 
73 //indent input
74 {}
75 	/* INDENT OFF */
76 	/* INDENT ON */
77 {}
78 //indent end
79 
80 /*
81  * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
82  * is aligned.
83  */
84 //indent run
85 {
86 }
87 	/* INDENT OFF */
88 /* INDENT ON */
89 {
90 }
91 //indent end
92 
93 
94 /*
95  * The INDENT comments can be written without space between the words, but
96  * nobody does this.
97  */
98 //indent input
99 int   decl   ;
100 /*INDENTOFF*/
101 int   decl   ;
102 /*INDENTON*/
103 int   decl   ;
104 //indent end
105 
106 //indent run -di0
107 int decl;
108 /*INDENTOFF*/
109 int   decl   ;
110 /* INDENTON */
111 int decl;
112 //indent end
113 
114 
115 /*
116  * Any whitespace around the 'INDENT ON/OFF' is ignored, as is any whitespace
117  * between the two words.
118  */
119 //indent input
120 int   decl   ;
121 /*		INDENT		OFF		*/
122 int   decl   ;
123 /*		INDENT		ON		*/
124 int   decl   ;
125 //indent end
126 
127 /*
128  * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
129  * is pushed to the start of the line.
130  */
131 //indent run -di0
132 int decl;
133 /*		INDENT		OFF		*/
134 int   decl   ;
135 /* INDENT		ON		*/
136 int decl;
137 //indent end
138 
139 
140 //indent input
141 /*INDENT OFF*/
142 /* No formatting takes place here. */
143 int format( void ) {{{
144 /*INDENT ON*/
145 }}}
146 //indent end
147 
148 //indent run
149 /*INDENT OFF*/
150 /* No formatting takes place here. */
151 int format( void ) {{{
152 /* $ XXX: Why is the INDENT ON comment indented? */
153 /* $ XXX: Why does the INDENT ON comment get spaces, but not the OFF comment? */
154 			/* INDENT ON */
155 }
156 }
157 }
158 //indent end
159 
160 
161 //indent input
162 /* INDENT OFF */
163 void indent_off ( void ) ;
164 /*  INDENT */
165 void indent_on ( void ) ;
166 /* INDENT OFF */
167 void indent_off ( void ) ;
168 	/* INDENT ON */
169 void indent_on ( void ) ;	/* the comment may be indented */
170 /* INDENT		OFF					*/
171 void indent_off ( void ) ;
172 /* INDENTATION ON */
173 void indent_still_off ( void ) ;	/* due to the word 'INDENTATION' */
174 /* INDENT ON * */
175 void indent_still_off ( void ) ;	/* due to the extra '*' at the end */
176 /* INDENT ON */
177 void indent_on ( void ) ;
178 /* INDENT: OFF */
179 void indent_still_on ( void ) ;	/* due to the colon in the middle */
180 /* INDENT OFF */		/* extra comment */
181 void indent_still_on ( void ) ;	/* due to the extra comment to the right */
182 //indent end
183 
184 //indent run
185 /* INDENT OFF */
186 void indent_off ( void ) ;
187 /* $ XXX: The double space from the below comment got merged to a single */
188 /* $ XXX: space even though the comment might be regarded to be still in */
189 /* $ XXX: the OFF section. */
190 /* INDENT */
191 void		indent_on(void);
192 /* INDENT OFF */
193 void indent_off ( void ) ;
194 /* $ XXX: The below comment got moved from column 9 to column 1. */
195 /* INDENT ON */
196 void		indent_on(void);	/* the comment may be indented */
197 /* INDENT		OFF					*/
198 void indent_off ( void ) ;
199 /* INDENTATION ON */
200 void indent_still_off ( void ) ;	/* due to the word 'INDENTATION' */
201 /* INDENT ON * */
202 void indent_still_off ( void ) ;	/* due to the extra '*' at the end */
203 /* INDENT ON */
204 void		indent_on(void);
205 /* INDENT: OFF */
206 void		indent_still_on(void);	/* due to the colon in the middle */
207 /* $ The extra comment got moved to the left since there is no code in */
208 /* $ that line. */
209 /* INDENT OFF *//* extra comment */
210 void		indent_still_on(void);	/* due to the extra comment to the
211 					 * right */
212 //indent end
213 
214 
215 /*
216  * Try to confuse indent by having a string literal that has an embedded
217  * INDENT comment.  Indent doesn't get confused though because it requires the
218  * INDENT comment to go from the very beginning of the line to the very end of
219  * the line.
220  */
221 //indent input
222 const char *str = "\
223 /* INDENT OFF */\
224 "   ,   ch;
225 //indent end
226 
227 //indent run
228 const char     *str = "\
229 /* INDENT OFF */\
230 ", ch;
231 //indent end
232 
233 
234 /*
235  * The keywords in the INDENT comments must all be uppercase.
236  */
237 //indent input
238 int   on   ;
239 /* indent off */
240 int   still_on   ;
241 /* INDENT off */
242 int   still_on   ;
243 /* indent OFF */
244 int   still_on   ;
245 /* INDENT OFF */
246 int   finally_off   ;
247 //indent end
248 
249 //indent run -di0
250 int on;
251 /* indent off */
252 int still_on;
253 /* INDENT off */
254 int still_on;
255 /* indent OFF */
256 int still_on;
257 /* INDENT OFF */
258 int   finally_off   ;
259 //indent end
260