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