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