pr_comment.c revision 1.77 1 /* $NetBSD: pr_comment.c,v 1.77 2021/10/13 22:38:02 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.77 2021/10/13 22:38:02 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 <stdio.h>
53 #include <string.h>
54
55 #include "indent.h"
56
57 static void
58 com_add_char(char ch)
59 {
60 if (com.e + 1 >= com.l)
61 buf_expand(&com, 1);
62 *com.e++ = ch;
63 }
64
65 static void
66 com_add_delim(void)
67 {
68 if (!opt.star_comment_cont)
69 return;
70 size_t len = 3;
71 if (com.e + len >= com.l)
72 buf_expand(&com, len);
73 memcpy(com.e, " * ", len);
74 com.e += len;
75 }
76
77 static void
78 com_terminate(void)
79 {
80 if (com.e + 1 >= com.l)
81 buf_expand(&com, 1);
82 *com.e = '\0';
83 }
84
85 static bool
86 fits_in_one_line(int max_line_length)
87 {
88 for (const char *p = inp.s; *p != '\n'; p++) {
89 assert(*p != '\0');
90 assert(inp.e - p >= 2);
91 if (!(p[0] == '*' && p[1] == '/'))
92 continue;
93
94 int len = indentation_after_range(ps.com_ind + 3, inp.s, p);
95 len += is_hspace(p[-1]) ? 2 : 3;
96 if (len <= max_line_length)
97 return true;
98 }
99 return false;
100 }
101
102 /*
103 * Scan, reformat and output a single comment, which is either a block comment
104 * starting with '/' '*' or an end-of-line comment starting with '//'.
105 *
106 * Try to keep comments from going over the maximum line length. If a line is
107 * too long, move everything starting from the last blank to the next comment
108 * line. Blanks and tabs from the beginning of the input line are removed.
109 *
110 * ALGORITHM:
111 * 1) Decide where the comment should be aligned, and if lines should
112 * be broken.
113 * 2) If lines should not be broken and filled, just copy up to end of
114 * comment.
115 * 3) If lines should be filled, then scan through the input buffer,
116 * copying characters to com_buf. Remember where the last blank,
117 * tab, or newline was. When line is filled, print up to last blank
118 * and continue copying.
119 */
120 void
121 process_comment(void)
122 {
123 int adj_max_line_length; /* Adjusted max_line_length for comments that
124 * spill over the right margin */
125 ssize_t last_blank; /* index of the last blank in com.buf */
126 bool break_delim = opt.comment_delimiter_on_blankline;
127 int l_just_saw_decl = ps.just_saw_decl;
128
129 adj_max_line_length = opt.max_line_length;
130 ps.just_saw_decl = 0;
131 last_blank = -1; /* no blanks found so far */
132 bool may_wrap = true;
133 ps.stats.comments++;
134
135 /* Figure where to align and how to treat the comment */
136
137 if (ps.col_1 && !opt.format_col1_comments) {
138 may_wrap = false;
139 break_delim = false;
140 ps.com_ind = 0;
141
142 } else {
143 if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' ||
144 (*inp.s == '\n' && !opt.format_block_comments)) {
145 may_wrap = false;
146 break_delim = false;
147 }
148
149 if (lab.s == lab.e && code.s == code.e) {
150 ps.com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
151 adj_max_line_length = opt.block_comment_max_line_length;
152 if (ps.com_ind <= 0)
153 ps.com_ind = opt.format_col1_comments ? 0 : 1;
154
155 } else {
156 break_delim = false;
157
158 int target_ind;
159 if (code.s != code.e)
160 target_ind = indentation_after(compute_code_indent(), code.s);
161 else if (lab.s != lab.e)
162 target_ind = indentation_after(compute_label_indent(), lab.s);
163 else
164 target_ind = 0;
165
166 ps.com_ind = ps.decl_on_line || ps.ind_level == 0
167 ? opt.decl_comment_column - 1 : opt.comment_column - 1;
168 if (ps.com_ind <= target_ind)
169 ps.com_ind = next_tab(target_ind);
170 /* XXX: the '+ 1' smells like an off-by-one error */
171 if (ps.com_ind + 1 + 24 > adj_max_line_length)
172 adj_max_line_length = ps.com_ind + 1 + 24;
173 }
174 }
175
176 if (!may_wrap) {
177 /*
178 * Find out how much indentation there was originally, because that
179 * much will have to be ignored by dump_line(). This is a box comment,
180 * so nothing changes -- not even indentation.
181 *
182 * The comment we're about to read usually comes from inp.buf,
183 * unless it has been copied into save_com.
184 */
185 const char *start;
186
187 /*
188 * XXX: ordered comparison between pointers from different objects
189 * invokes undefined behavior (C99 6.5.8).
190 */
191 start = inp.s >= save_com && inp.s < save_com + sc_size ?
192 sc_buf : inp.buf;
193 ps.n_comment_delta = -indentation_after_range(0, start, inp.s - 2);
194 } else {
195 ps.n_comment_delta = 0;
196 while (is_hspace(*inp.s))
197 inp.s++;
198 }
199
200 ps.comment_delta = 0;
201 com_add_char('/');
202 com_add_char(token.e[-1]); /* either '*' or '/' */
203 if (*inp.s != ' ' && may_wrap)
204 com_add_char(' ');
205
206 if (break_delim && fits_in_one_line(adj_max_line_length))
207 break_delim = false;
208
209 if (break_delim) {
210 char *t = com.e;
211 com.e = com.s + 2;
212 *com.e = '\0';
213 if (opt.blanklines_before_block_comments && ps.last_token != lbrace)
214 prefix_blankline_requested = true;
215 dump_line();
216 com.e = com.s = t;
217 com_add_delim();
218 }
219
220 /* Start to copy the comment */
221
222 for (;;) { /* this loop will go until the comment is
223 * copied */
224 switch (*inp.s) { /* this checks for various special cases */
225 case '\f':
226 if (may_wrap) { /* in a text comment, break the line here */
227 ps.use_ff = true;
228 dump_line();
229 last_blank = -1;
230 com_add_delim();
231 inp.s++;
232 while (is_hspace(*inp.s))
233 inp.s++;
234 } else {
235 inbuf_skip();
236 com_add_char('\f');
237 }
238 break;
239
240 case '\n':
241 if (token.e[-1] == '/')
242 goto end_of_line_comment;
243
244 if (had_eof) {
245 diag(1, "Unterminated comment");
246 dump_line();
247 return;
248 }
249
250 last_blank = -1;
251 if (!may_wrap || ps.last_nl) { /* if this is a boxed comment,
252 * we handle the newline */
253 if (com.s == com.e)
254 com_add_char(' ');
255 if (may_wrap && com.e - com.s > 3) {
256 dump_line();
257 com_add_delim();
258 }
259 dump_line();
260 if (may_wrap)
261 com_add_delim();
262
263 } else {
264 ps.last_nl = true;
265 if (!is_hspace(com.e[-1]))
266 com_add_char(' ');
267 last_blank = com.e - 1 - com.buf;
268 }
269 ++line_no;
270 if (may_wrap) {
271 bool skip_asterisk = true;
272 do { /* flush any blanks and/or tabs at start of
273 * next line */
274 inbuf_skip();
275 if (*inp.s == '*' && skip_asterisk) {
276 skip_asterisk = false;
277 inbuf_skip();
278 if (*inp.s == '/')
279 goto end_of_comment;
280 }
281 } while (is_hspace(*inp.s));
282 } else
283 inbuf_skip();
284 break; /* end of case for newline */
285
286 case '*':
287 inbuf_skip();
288 if (*inp.s == '/') {
289 end_of_comment:
290 inbuf_skip();
291
292 end_of_line_comment:
293 if (break_delim) {
294 if (com.e > com.s + 3)
295 dump_line();
296 else
297 com.s = com.e; /* XXX: why not e = s? */
298 com_add_char(' ');
299 }
300
301 if (!is_hspace(com.e[-1]) && may_wrap)
302 com_add_char(' ');
303 if (token.e[-1] != '/') {
304 com_add_char('*');
305 com_add_char('/');
306 }
307 com_terminate();
308
309 ps.just_saw_decl = l_just_saw_decl;
310 return;
311
312 } else /* handle isolated '*' */
313 com_add_char('*');
314 break;
315
316 default: /* we have a random char */
317 ;
318 int now_len = indentation_after_range(ps.com_ind, com.s, com.e);
319 for (;;) {
320 char ch = inbuf_next();
321 if (is_hspace(ch))
322 last_blank = com.e - com.buf;
323 com_add_char(ch);
324 now_len++;
325 if (memchr("*\n\r\b\t", *inp.s, 6) != NULL)
326 break;
327 if (now_len >= adj_max_line_length && last_blank != -1)
328 break;
329 }
330
331 ps.last_nl = false;
332
333 /* XXX: signed character comparison '>' does not work for UTF-8 */
334 if (now_len > adj_max_line_length &&
335 may_wrap && com.e[-1] > ' ') {
336
337 /* the comment is too long, it must be broken up */
338 if (last_blank == -1) {
339 dump_line();
340 com_add_delim();
341 break;
342 }
343
344 com_terminate(); /* mark the end of the last word */
345 com.e = com.buf + last_blank;
346 dump_line();
347
348 com_add_delim();
349
350 const char *p = com.buf + last_blank + 1;
351 while (is_hspace(*p))
352 p++;
353 last_blank = -1;
354
355 /*
356 * p still points to the last word from the previous line, in
357 * the same buffer that it is copied to, but to the right of
358 * the writing region [com.s, com.e). Calling dump_line only
359 * moved com.e back to com.s, it did not clear the contents of
360 * the buffer. This ensures that the buffer is already large
361 * enough.
362 */
363 while (*p != '\0') {
364 assert(!is_hspace(*p));
365 *com.e++ = *p++;
366 }
367 }
368 break;
369 }
370 }
371 }
372