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