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