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