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