pr_comment.c revision 1.138 1 /* $NetBSD: pr_comment.c,v 1.138 2023/05/14 18:05:52 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 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: pr_comment.c,v 1.138 2023/05/14 18:05:52 rillig Exp $");
42
43 #include <string.h>
44
45 #include "indent.h"
46
47 static void
48 com_add_char(char ch)
49 {
50 if (1 >= com.limit - com.e)
51 buf_expand(&com, 1);
52 *com.e++ = ch;
53 }
54
55 static void
56 com_add_delim(void)
57 {
58 if (!opt.star_comment_cont)
59 return;
60 const char *delim = " * ";
61 buf_add_range(&com, delim, delim + 3);
62 }
63
64 static void
65 com_terminate(void)
66 {
67 if (1 >= com.limit - com.e)
68 buf_expand(&com, 1);
69 *com.e = '\0';
70 }
71
72 static bool
73 fits_in_one_line(int com_ind, int max_line_length)
74 {
75 for (const char *start = inp_p(), *p = start; *p != '\n'; p++) {
76 if (p[0] == '*' && p[1] == '/') {
77 int len = ind_add(com_ind + 3, start, p);
78 len += ch_isblank(p[-1]) ? 2 : 3;
79 return len <= max_line_length;
80 }
81 }
82 return false;
83 }
84
85 static void
86 analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
87 {
88 bool may_wrap = true;
89 bool delim = opt.comment_delimiter_on_blankline;
90 int ind;
91 int line_length = opt.max_line_length;
92
93 if (ps.curr_col_1 && !opt.format_col1_comments) {
94 may_wrap = false;
95 delim = false;
96 ind = 0;
97
98 } else {
99 if (inp_peek() == '-' || inp_peek() == '*' ||
100 token.e[-1] == '/' ||
101 (inp_peek() == '\n' && !opt.format_block_comments)) {
102 may_wrap = false;
103 delim = false;
104 }
105
106 if (com.e != com.s)
107 output_line();
108 if (lab.s == lab.e && code.s == code.e) {
109 ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
110 if (ind <= 0)
111 ind = opt.format_col1_comments ? 0 : 1;
112 line_length = opt.block_comment_max_line_length;
113 } else {
114 delim = false;
115
116 int target_ind = code.s != code.e
117 ? ind_add(compute_code_indent(), code.s, code.e)
118 : ind_add(compute_label_indent(), lab.s, lab.e);
119
120 ind = ps.decl_on_line || ps.ind_level == 0
121 ? opt.decl_comment_column - 1 : opt.comment_column - 1;
122 if (ind <= target_ind)
123 ind = next_tab(target_ind);
124 if (ind + 25 > line_length)
125 line_length = ind + 25;
126 }
127 }
128
129 ps.com_ind = ind;
130
131 if (!may_wrap) {
132 /*
133 * Find out how much indentation there was originally, because that
134 * much will have to be ignored by output_complete_line.
135 */
136 ps.n_comment_delta = -ind_add(0, inp_line_start(), inp_p() - 2);
137 } else {
138 ps.n_comment_delta = 0;
139 if (!(inp_peek() == '\t' && !ch_isblank(inp_lookahead(1))))
140 while (ch_isblank(inp_peek()))
141 inp_skip();
142 }
143
144 ps.comment_delta = 0;
145 com_add_char('/');
146 com_add_char(token.e[-1]); /* either '*' or '/' */
147
148 if (may_wrap && !ch_isblank(inp_peek()))
149 com_add_char(' ');
150
151 if (delim && fits_in_one_line(ind, line_length))
152 delim = false;
153
154 if (delim) {
155 output_line();
156 com_add_delim();
157 }
158
159 *p_line_length = line_length;
160 *p_delim = delim;
161 *p_may_wrap = may_wrap;
162 }
163
164 /*
165 * Copy characters from 'inp' to 'com'. Try to keep comments from going over
166 * the maximum line length. To do that, remember where the last blank, tab, or
167 * newline was. When a line is filled, print up to the last blank and continue
168 * copying.
169 */
170 static void
171 copy_comment_wrap(int line_length, bool delim)
172 {
173 ssize_t last_blank = -1; /* index of the last blank in com.mem */
174
175 for (;;) {
176 switch (inp_peek()) {
177 case '\f':
178 output_line_ff();
179 last_blank = -1;
180 com_add_delim();
181 inp_skip();
182 while (ch_isblank(inp_peek()))
183 inp_skip();
184 break;
185
186 case '\n':
187 if (had_eof) {
188 diag(1, "Unterminated comment");
189 output_line();
190 return;
191 }
192
193 last_blank = -1;
194 if (ps.next_col_1) {
195 if (com.s == com.e)
196 com_add_char(' '); /* force empty line of output */
197 if (com.e - com.s > 3) {
198 output_line();
199 com_add_delim();
200 }
201 output_line();
202 com_add_delim();
203
204 } else {
205 ps.next_col_1 = true;
206 if (!(com.e > com.s && ch_isblank(com.e[-1])))
207 com_add_char(' ');
208 last_blank = com.e - 1 - com.mem;
209 }
210 ++line_no;
211
212 bool skip_asterisk = true;
213 do { /* flush any blanks and/or tabs at start of
214 * next line */
215 inp_skip();
216 if (inp_peek() == '*' && skip_asterisk) {
217 skip_asterisk = false;
218 inp_skip();
219 if (inp_peek() == '/')
220 goto end_of_comment;
221 }
222 } while (ch_isblank(inp_peek()));
223
224 break; /* end of case for newline */
225
226 case '*':
227 inp_skip();
228 if (inp_peek() == '/') {
229 end_of_comment:
230 inp_skip();
231
232 if (delim) {
233 if (com.e - com.s > 3)
234 output_line();
235 else
236 com.e = com.s;
237 com_add_char(' ');
238 }
239
240 if (!(com.e > com.s && ch_isblank(com.e[-1])))
241 com_add_char(' ');
242 com_add_char('*');
243 com_add_char('/');
244 com_terminate();
245 return;
246
247 } else /* handle isolated '*' */
248 com_add_char('*');
249 break;
250
251 default: /* we have a random char */
252 ;
253 int now_len = ind_add(ps.com_ind, com.s, com.e);
254 for (;;) {
255 char ch = inp_next();
256 if (ch_isblank(ch))
257 last_blank = com.e - com.mem;
258 com_add_char(ch);
259 now_len++;
260 if (memchr("*\n\r\b\t", inp_peek(), 6) != NULL)
261 break;
262 if (now_len >= line_length && last_blank != -1)
263 break;
264 }
265
266 ps.next_col_1 = false;
267
268 if (now_len <= line_length)
269 break;
270 if (ch_isspace(com.e[-1]))
271 break;
272
273 if (last_blank == -1) { /* only a single word in this line */
274 output_line();
275 com_add_delim();
276 break;
277 }
278
279 const char *last_word_s = com.mem + last_blank + 1;
280 size_t last_word_len = (size_t)(com.e - last_word_s);
281 com.e = com.mem + last_blank;
282 output_line();
283 com_add_delim();
284
285 memcpy(com.e, last_word_s, last_word_len);
286 com.e += last_word_len;
287 last_blank = -1;
288 }
289 }
290 }
291
292 static void
293 copy_comment_nowrap(void)
294 {
295 for (;;) {
296 if (inp_peek() == '\n') {
297 if (token.e[-1] == '/') {
298 com_terminate();
299 return;
300 }
301
302 if (had_eof) {
303 diag(1, "Unterminated comment");
304 output_line();
305 return;
306 }
307
308 if (com.s == com.e)
309 com_add_char(' '); /* force output of an empty line */
310 output_line();
311 ++line_no;
312 inp_skip();
313 continue;
314 }
315
316 com_add_char(inp_next());
317 if (com.e[-2] == '*' && com.e[-1] == '/' && token.e[-1] == '*') {
318 com_terminate();
319 return;
320 }
321 }
322 }
323
324 /*
325 * Scan, reformat and output a single comment, which is either a block comment
326 * starting with '/' '*' or an end-of-line comment starting with '//'.
327 */
328 void
329 process_comment(void)
330 {
331 int line_length;
332 bool may_wrap, delim;
333
334 analyze_comment(&may_wrap, &delim, &line_length);
335 if (may_wrap)
336 copy_comment_wrap(line_length, delim);
337 else
338 copy_comment_nowrap();
339 }
340