Home | History | Annotate | Line # | Download | only in indent
pr_comment.c revision 1.152
      1 /*	$NetBSD: pr_comment.c,v 1.152 2023/06/06 06:51:44 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.152 2023/06/06 06:51:44 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 		return;
     58 	buf_add_chars(&com, " * ", 3);
     59 }
     60 
     61 static bool
     62 fits_in_one_line(int com_ind, int max_line_length)
     63 {
     64 	for (const char *start = inp_p, *p = start; *p != '\n'; p++) {
     65 		if (p[0] == '*' && p[1] == '/') {
     66 			int len = ind_add(com_ind + 3,
     67 			    start, (size_t)(p - start));
     68 			len += p == start || ch_isblank(p[-1]) ? 2 : 3;
     69 			return len <= max_line_length;
     70 		}
     71 	}
     72 	return false;
     73 }
     74 
     75 static void
     76 analyze_comment(bool *p_may_wrap, bool *p_delim,
     77     int *p_ind, int *p_line_length)
     78 {
     79 	bool may_wrap = true;
     80 	bool delim = opt.comment_delimiter_on_blankline;
     81 	int ind;
     82 	int line_length = opt.max_line_length;
     83 
     84 	if (ps.curr_col_1 && !opt.format_col1_comments) {
     85 		may_wrap = false;
     86 		delim = false;
     87 		ind = 0;
     88 
     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 			delim = false;
     95 		}
     96 		if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
     97 			out.line_kind = lk_block_comment;
     98 
     99 		if (com.len > 0)
    100 			output_line();
    101 		if (lab.len == 0 && code.len == 0) {
    102 			ind = (ps.ind_level - opt.unindent_displace)
    103 			    * opt.indent_size;
    104 			if (ind <= 0)
    105 				ind = opt.format_col1_comments ? 0 : 1;
    106 			line_length = opt.block_comment_max_line_length;
    107 		} else {
    108 			delim = false;
    109 
    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.decl_on_line || 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 	ps.com_ind = ind;
    125 
    126 	if (!may_wrap) {
    127 		/* Find out how much indentation there was originally, because
    128 		 * that much will have to be ignored by output_line. */
    129 		size_t len = (size_t)(inp_p - 2 - inp.s);
    130 		ps.n_comment_delta = -ind_add(0, inp.s, len);
    131 	} else {
    132 		ps.n_comment_delta = 0;
    133 		if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1])))
    134 			while (ch_isblank(inp_p[0]))
    135 				inp_p++;
    136 	}
    137 
    138 	*p_may_wrap = may_wrap;
    139 	*p_delim = delim;
    140 	*p_ind = ind;
    141 	*p_line_length = line_length;
    142 }
    143 
    144 static void
    145 copy_comment_start(bool may_wrap, bool *delim, int ind, int line_length)
    146 {
    147 	ps.comment_delta = 0;
    148 	com_add_char('/');
    149 	com_add_char(token.s[token.len - 1]);	/* either '*' or '/' */
    150 
    151 	if (may_wrap && !ch_isblank(inp_p[0]))
    152 		com_add_char(' ');
    153 
    154 	if (*delim && fits_in_one_line(ind, line_length))
    155 		*delim = false;
    156 
    157 	if (*delim) {
    158 		output_line();
    159 		com_add_delim();
    160 	}
    161 }
    162 
    163 static void
    164 copy_comment_wrap_text(int line_length, ssize_t *last_blank)
    165 {
    166 	int now_len = ind_add(ps.com_ind, com.s, com.len);
    167 	for (;;) {
    168 		char ch = inp_next();
    169 		if (ch_isblank(ch))
    170 			*last_blank = (ssize_t)com.len;
    171 		com_add_char(ch);
    172 		now_len++;
    173 		if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL)
    174 			break;
    175 		if (now_len >= line_length && *last_blank != -1)
    176 			break;
    177 	}
    178 
    179 	ps.next_col_1 = false;
    180 
    181 	if (now_len <= line_length)
    182 		return;
    183 	if (ch_isspace(com.s[com.len - 1]))
    184 		return;
    185 
    186 	if (*last_blank == -1) {
    187 		/* only a single word in this line */
    188 		output_line();
    189 		com_add_delim();
    190 		return;
    191 	}
    192 
    193 	const char *last_word_s = com.s + *last_blank + 1;
    194 	size_t last_word_len = com.len - (size_t)(*last_blank + 1);
    195 	com.len = (size_t)*last_blank;
    196 	output_line();
    197 	com_add_delim();
    198 
    199 	/* Assume that output_line and com_add_delim don't
    200 	 * invalidate the "unused" part of the buffer beyond
    201 	 * com.s + com.len. */
    202 	memmove(com.s + com.len, last_word_s, last_word_len);
    203 	com.len += last_word_len;
    204 	*last_blank = -1;
    205 }
    206 
    207 static bool
    208 copy_comment_wrap_newline(ssize_t *last_blank)
    209 {
    210 	*last_blank = -1;
    211 	if (ps.next_col_1) {
    212 		if (com.len == 0)
    213 			com_add_char(' ');	/* force empty output line */
    214 		if (com.len > 3) {
    215 			output_line();
    216 			com_add_delim();
    217 		}
    218 		output_line();
    219 		com_add_delim();
    220 	} else {
    221 		ps.next_col_1 = true;
    222 		if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
    223 			com_add_char(' ');
    224 		*last_blank = (int)com.len - 1;
    225 	}
    226 	++line_no;
    227 
    228 	/* flush any blanks and/or tabs at start of next line */
    229 	bool skip_asterisk = true;
    230 	do {
    231 		inp_skip();
    232 		if (inp_p[0] == '*' && skip_asterisk) {
    233 			skip_asterisk = false;
    234 			inp_p++;
    235 			if (inp_p[0] == '/')
    236 				return false;
    237 		}
    238 	} while (ch_isblank(inp_p[0]));
    239 
    240 	return true;
    241 }
    242 
    243 static void
    244 copy_comment_wrap_finish(int line_length, bool delim)
    245 {
    246 	inp_p++;
    247 
    248 	if (delim) {
    249 		if (com.len > 3)
    250 			output_line();
    251 		else
    252 			com.len = 0;
    253 		com_add_char(' ');
    254 	} else {
    255 		size_t len = com.len;
    256 		while (ch_isblank(com.s[len - 1]))
    257 			len--;
    258 		int now_len = ind_add(ps.com_ind, com.s, len);
    259 		if (now_len + 3 > line_length)
    260 			output_line();
    261 	}
    262 
    263 	if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
    264 		com_add_char(' ');
    265 	com_add_char('*');
    266 	com_add_char('/');
    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 
    280 	for (;;) {
    281 		if (inp_p[0] == '\n') {
    282 			if (had_eof)
    283 				goto unterminated_comment;
    284 			if (!copy_comment_wrap_newline(&last_blank))
    285 				goto end_of_comment;
    286 		} else if (inp_p[0] == '*' && inp_p[1] == '/') {
    287 			inp_p++;
    288 			goto end_of_comment;
    289 		} else
    290 			copy_comment_wrap_text(line_length, &last_blank);
    291 	}
    292 
    293 end_of_comment:
    294 	copy_comment_wrap_finish(line_length, delim);
    295 	return;
    296 
    297 unterminated_comment:
    298 	diag(1, "Unterminated comment");
    299 	output_line();
    300 }
    301 
    302 static void
    303 copy_comment_nowrap(void)
    304 {
    305 	char kind = token.s[token.len - 1];
    306 
    307 	for (;;) {
    308 		if (inp_p[0] == '\n') {
    309 			if (kind == '/')
    310 				return;
    311 
    312 			if (had_eof) {
    313 				diag(1, "Unterminated comment");
    314 				output_line();
    315 				return;
    316 			}
    317 
    318 			if (com.len == 0)
    319 				com_add_char(' ');	/* force output of an
    320 							 * empty line */
    321 			output_line();
    322 			++line_no;
    323 			inp_skip();
    324 			continue;
    325 		}
    326 
    327 		com_add_char(*inp_p++);
    328 		if (com.len >= 2
    329 		    && com.s[com.len - 2] == '*'
    330 		    && com.s[com.len - 1] == '/'
    331 		    && kind == '*')
    332 			return;
    333 	}
    334 }
    335 
    336 /*
    337  * Scan, reformat and output a single comment, which is either a block comment
    338  * starting with '/' '*' or an end-of-line comment starting with '//'.
    339  */
    340 void
    341 process_comment(void)
    342 {
    343 	bool may_wrap, delim;
    344 	int ind, line_length;
    345 
    346 	analyze_comment(&may_wrap, &delim, &ind, &line_length);
    347 	copy_comment_start(may_wrap, &delim, ind, line_length);
    348 	if (may_wrap)
    349 		copy_comment_wrap(line_length, delim);
    350 	else
    351 		copy_comment_nowrap();
    352 }
    353