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