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