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