pr_comment.c revision 1.100 1 /* $NetBSD: pr_comment.c,v 1.100 2021/11/07 08:31:46 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: pr_comment.c,v 1.100 2021/11/07 08:31:46 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
49 #endif
50
51 #include <assert.h>
52 #include <ctype.h>
53 #include <stdio.h>
54 #include <string.h>
55
56 #include "indent.h"
57
58 static void
59 com_add_char(char ch)
60 {
61 if (1 >= com.l - com.e)
62 buf_expand(&com, 1);
63 *com.e++ = ch;
64 }
65
66 static void
67 com_add_delim(void)
68 {
69 if (!opt.star_comment_cont)
70 return;
71 size_t len = 3;
72 if (len >= (size_t)(com.l - com.e))
73 buf_expand(&com, len);
74 memcpy(com.e, " * ", len);
75 com.e += len;
76 }
77
78 static void
79 com_terminate(void)
80 {
81 if (1 >= com.l - com.e)
82 buf_expand(&com, 1);
83 *com.e = '\0';
84 }
85
86 static bool
87 fits_in_one_line(int max_line_length)
88 {
89 for (const char *p = inp.s; *p != '\n'; p++) {
90 assert(*p != '\0');
91 assert(inp.e - p >= 2);
92 if (!(p[0] == '*' && p[1] == '/'))
93 continue;
94
95 int len = ind_add(ps.com_ind + 3, inp.s, p);
96 len += ch_isblank(p[-1]) ? 2 : 3;
97 return len <= max_line_length;
98 }
99 return false;
100 }
101
102 static void
103 analyze_comment(int *p_adj_max_line_length, bool *p_break_delim,
104 bool *p_may_wrap)
105 {
106 int adj_max_line_length; /* Adjusted max_line_length for comments that
107 * spill over the right margin */
108 bool break_delim = opt.comment_delimiter_on_blankline;
109 int com_ind;
110
111 adj_max_line_length = opt.max_line_length;
112 bool may_wrap = true;
113
114 if (ps.curr_col_1 && !opt.format_col1_comments) {
115 may_wrap = false;
116 break_delim = false;
117 com_ind = 0;
118
119 } else {
120 if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' ||
121 (*inp.s == '\n' && !opt.format_block_comments)) {
122 may_wrap = false;
123 break_delim = false;
124 }
125
126 if (lab.s == lab.e && code.s == code.e) {
127 com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
128 adj_max_line_length = opt.block_comment_max_line_length;
129 if (com_ind <= 0)
130 com_ind = opt.format_col1_comments ? 0 : 1;
131
132 } else {
133 break_delim = false;
134
135 int target_ind;
136 if (code.s != code.e)
137 target_ind = ind_add(compute_code_indent(), code.s, code.e);
138 else if (lab.s != lab.e)
139 target_ind = ind_add(compute_label_indent(), lab.s, lab.e);
140 else
141 target_ind = 0;
142
143 com_ind = ps.decl_on_line || ps.ind_level == 0
144 ? opt.decl_comment_column - 1 : opt.comment_column - 1;
145 if (com_ind <= target_ind)
146 com_ind = next_tab(target_ind);
147 if (com_ind + 25 > adj_max_line_length)
148 adj_max_line_length = com_ind + 25;
149 }
150 }
151
152 ps.com_ind = com_ind;
153
154 if (!may_wrap) {
155 /*
156 * Find out how much indentation there was originally, because that
157 * much will have to be ignored by dump_line(). This is a box comment,
158 * so nothing changes -- not even indentation.
159 *
160 * The comment we're about to read usually comes from inp.buf, unless
161 * it has been copied into save_com.
162 */
163 const char *start;
164
165 /*
166 * XXX: ordered comparison between pointers from different objects
167 * invokes undefined behavior (C99 6.5.8).
168 */
169 start = inp.s >= sc_buf && inp.s < sc_buf + sc_size ?
170 sc_buf : inp.buf;
171 ps.n_comment_delta = -ind_add(0, start, inp.s - 2);
172 } else {
173 ps.n_comment_delta = 0;
174 while (ch_isblank(*inp.s))
175 inp.s++;
176 }
177
178 ps.comment_delta = 0;
179 com_add_char('/');
180 com_add_char(token.e[-1]); /* either '*' or '/' */
181 if (*inp.s != ' ' && may_wrap)
182 com_add_char(' ');
183
184 if (break_delim && fits_in_one_line(adj_max_line_length))
185 break_delim = false;
186
187 if (break_delim) {
188 char *t = com.e;
189 com.e = com.s + 2;
190 *com.e = '\0';
191 if (opt.blanklines_before_block_comments &&
192 ps.prev_token != lsym_lbrace)
193 blank_line_before = true;
194 dump_line();
195 com.e = com.s = t;
196 com_add_delim();
197 }
198
199 *p_adj_max_line_length = adj_max_line_length;
200 *p_break_delim = break_delim;
201 *p_may_wrap = may_wrap;
202 }
203
204 static void
205 copy_comment_wrap(int adj_max_line_length, bool break_delim)
206 {
207 bool may_wrap = true;
208 ssize_t last_blank = -1; /* index of the last blank in com.buf */
209
210 for (;;) {
211 switch (*inp.s) {
212 case '\f':
213 if (may_wrap) { /* in a text comment, break the line here */
214 dump_line_ff();
215 last_blank = -1;
216 com_add_delim();
217 inp.s++;
218 while (ch_isblank(*inp.s))
219 inp.s++;
220 } else {
221 inp_skip();
222 com_add_char('\f');
223 }
224 break;
225
226 case '\n':
227 if (token.e[-1] == '/')
228 goto end_of_line_comment;
229
230 if (had_eof) {
231 diag(1, "Unterminated comment");
232 dump_line();
233 return;
234 }
235
236 last_blank = -1;
237 if (!may_wrap || ps.next_col_1) { /* if this is a boxed comment,
238 * we handle the newline */
239 if (com.s == com.e)
240 com_add_char(' ');
241 if (may_wrap && com.e - com.s > 3) {
242 dump_line();
243 com_add_delim();
244 }
245 dump_line();
246 if (may_wrap)
247 com_add_delim();
248
249 } else {
250 ps.next_col_1 = true;
251 if (!ch_isblank(com.e[-1]))
252 com_add_char(' ');
253 last_blank = com.e - 1 - com.buf;
254 }
255 ++line_no;
256 if (may_wrap) {
257 bool skip_asterisk = true;
258 do { /* flush any blanks and/or tabs at start of
259 * next line */
260 inp_skip();
261 if (*inp.s == '*' && skip_asterisk) {
262 skip_asterisk = false;
263 inp_skip();
264 if (*inp.s == '/')
265 goto end_of_comment;
266 }
267 } while (ch_isblank(*inp.s));
268 } else
269 inp_skip();
270 break; /* end of case for newline */
271
272 case '*':
273 inp_skip();
274 if (*inp.s == '/' && token.e[-1] == '*') {
275 end_of_comment:
276 inp_skip();
277
278 end_of_line_comment:
279 if (break_delim) {
280 if (com.e > com.s + 3)
281 dump_line();
282 else
283 com.s = com.e; /* XXX: why not e = s? */
284 com_add_char(' ');
285 }
286
287 if (!ch_isblank(com.e[-1]) && may_wrap)
288 com_add_char(' ');
289 if (token.e[-1] == '*') {
290 com_add_char('*');
291 com_add_char('/');
292 }
293 com_terminate();
294 return;
295
296 } else /* handle isolated '*' */
297 com_add_char('*');
298 break;
299
300 default: /* we have a random char */
301 ;
302 int now_len = ind_add(ps.com_ind, com.s, com.e);
303 for (;;) {
304 char ch = inp_next();
305 if (ch_isblank(ch))
306 last_blank = com.e - com.buf;
307 com_add_char(ch);
308 now_len++;
309 if (memchr("*\n\r\b\t", *inp.s, 6) != NULL)
310 break;
311 if (now_len >= adj_max_line_length && last_blank != -1)
312 break;
313 }
314
315 ps.next_col_1 = false;
316
317 if (now_len <= adj_max_line_length || !may_wrap)
318 break;
319 if (isspace((unsigned char)com.e[-1]))
320 break;
321
322 if (last_blank == -1) { /* only a single word in this line */
323 dump_line();
324 com_add_delim();
325 break;
326 }
327
328 const char *last_word_s = com.buf + last_blank + 1;
329 size_t last_word_len = (size_t)(com.e - last_word_s);
330 com.e = com.buf + last_blank;
331 dump_line();
332 com_add_delim();
333
334 memcpy(com.e, last_word_s, last_word_len);
335 com.e += last_word_len;
336 last_blank = -1;
337 }
338 }
339 }
340
341 static void
342 copy_comment_nowrap(int adj_max_line_length, bool break_delim)
343 {
344 ssize_t last_blank = -1; /* index of the last blank in com.buf */
345
346 for (;;) {
347 switch (*inp.s) {
348 case '\f':
349 inp_skip();
350 com_add_char('\f');
351 break;
352
353 case '\n':
354 if (token.e[-1] == '/')
355 goto end_of_line_comment;
356
357 if (had_eof) {
358 diag(1, "Unterminated comment");
359 dump_line();
360 return;
361 }
362
363 last_blank = -1;
364 if (com.s == com.e)
365 com_add_char(' '); /* force output of an empty line */
366 dump_line();
367 ++line_no;
368 inp_skip();
369 break; /* end of case for newline */
370
371 case '*':
372 inp_skip();
373 if (*inp.s == '/' && token.e[-1] == '*') {
374 inp_skip();
375
376 end_of_line_comment:
377 if (break_delim) {
378 if (com.e > com.s + 3)
379 dump_line();
380 else
381 com.s = com.e; /* XXX: why not e = s? */
382 com_add_char(' ');
383 }
384
385 if (token.e[-1] == '*') {
386 com_add_char('*');
387 com_add_char('/');
388 }
389 com_terminate();
390 return;
391
392 } else /* handle isolated '*' */
393 com_add_char('*');
394 break;
395
396 default: /* we have a random char */
397 ;
398 int now_len = ind_add(ps.com_ind, com.s, com.e);
399 for (;;) {
400 char ch = inp_next();
401 if (ch_isblank(ch))
402 last_blank = com.e - com.buf;
403 com_add_char(ch);
404 now_len++;
405 if (memchr("*\n\r\b\t", *inp.s, 6) != NULL)
406 break;
407 if (now_len >= adj_max_line_length && last_blank != -1)
408 break;
409 }
410
411 ps.next_col_1 = false;
412 break;
413 }
414 }
415 }
416
417 /*
418 * Scan, reformat and output a single comment, which is either a block comment
419 * starting with '/' '*' or an end-of-line comment starting with '//'.
420 *
421 * Try to keep comments from going over the maximum line length. If a line is
422 * too long, move everything starting from the last blank to the next comment
423 * line. Blanks and tabs from the beginning of the input line are removed.
424 *
425 * ALGORITHM:
426 * 1) Decide where the comment should be aligned, and if lines should
427 * be broken.
428 * 2) If lines should not be broken and filled, just copy up to end of
429 * comment.
430 * 3) If lines should be filled, then scan through the input buffer,
431 * copying characters to com_buf. Remember where the last blank,
432 * tab, or newline was. When line is filled, print up to last blank
433 * and continue copying.
434 */
435 void
436 process_comment(void)
437 {
438 int adj_max_line_length; /* Adjusted max_line_length for comments that
439 * spill over the right margin */
440 bool break_delim = opt.comment_delimiter_on_blankline;
441
442 adj_max_line_length = opt.max_line_length;
443 ps.just_saw_decl = 0;
444 bool may_wrap = true;
445 ps.stats.comments++;
446
447 int l_just_saw_decl = ps.just_saw_decl;
448 analyze_comment(&adj_max_line_length, &break_delim, &may_wrap);
449 if (may_wrap)
450 copy_comment_wrap(adj_max_line_length, break_delim);
451 else
452 copy_comment_nowrap(adj_max_line_length, break_delim);
453 ps.just_saw_decl = l_just_saw_decl;
454 }
455