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