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