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