Home | History | Annotate | Line # | Download | only in indent
pr_comment.c revision 1.62
      1  1.62  rillig /*	$NetBSD: pr_comment.c,v 1.62 2021/10/08 18:29:36 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.62  rillig __RCSID("$NetBSD: pr_comment.c,v 1.62 2021/10/08 18:29:36 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.1     cgd #include <stdio.h>
     52  1.11   kamil #include <string.h>
     53  1.12  rillig 
     54  1.11   kamil #include "indent.h"
     55  1.12  rillig 
     56  1.14  rillig static void
     57  1.35  rillig check_size_comment(size_t desired_size)
     58  1.14  rillig {
     59  1.46  rillig     if (com.e + desired_size >= com.l)
     60  1.46  rillig 	buf_expand(&com, desired_size);
     61  1.14  rillig }
     62  1.14  rillig 
     63   1.1     cgd /*
     64  1.28  rillig  * Scan, reformat and output a single comment, which is either a block comment
     65  1.28  rillig  * starting with '/' '*' or an end-of-line comment starting with '//'.
     66   1.1     cgd  *
     67  1.28  rillig  * Try to keep comments from going over the maximum line length.  If a line is
     68  1.28  rillig  * too long, move everything starting from the last blank to the next comment
     69  1.28  rillig  * line.  Blanks and tabs from the beginning of the input line are removed.
     70   1.1     cgd  *
     71   1.1     cgd  * ALGORITHM:
     72   1.1     cgd  *	1) Decide where the comment should be aligned, and if lines should
     73   1.1     cgd  *	   be broken.
     74   1.1     cgd  *	2) If lines should not be broken and filled, just copy up to end of
     75   1.1     cgd  *	   comment.
     76  1.28  rillig  *	3) If lines should be filled, then scan through the input buffer,
     77  1.28  rillig  *	   copying characters to com_buf.  Remember where the last blank,
     78  1.28  rillig  *	   tab, or newline was.  When line is filled, print up to last blank
     79  1.28  rillig  *	   and continue copying.
     80   1.1     cgd  */
     81   1.6   lukem void
     82  1.28  rillig process_comment(void)
     83   1.1     cgd {
     84  1.47  rillig     int adj_max_line_length;	/* Adjusted max_line_length for comments that
     85  1.47  rillig 				 * spill over the right margin */
     86  1.36  rillig     ssize_t last_blank;		/* index of the last blank in com.buf */
     87  1.44  rillig     bool break_delim = opt.comment_delimiter_on_blankline;
     88  1.47  rillig     int l_just_saw_decl = ps.just_saw_decl;
     89  1.11   kamil 
     90  1.26  rillig     adj_max_line_length = opt.max_line_length;
     91  1.11   kamil     ps.just_saw_decl = 0;
     92  1.35  rillig     last_blank = -1;		/* no blanks found so far */
     93  1.47  rillig     ps.box_com = false;		/* at first, assume that we are not in a boxed
     94  1.47  rillig 				 * comment or some other comment that should
     95  1.47  rillig 				 * not be touched */
     96  1.41  rillig     ps.stats.comments++;
     97  1.11   kamil 
     98  1.11   kamil     /* Figure where to align and how to treat the comment */
     99  1.11   kamil 
    100  1.28  rillig     if (ps.col_1 && !opt.format_col1_comments) { /* if the comment starts in
    101  1.28  rillig 				 * column 1, it should not be touched */
    102  1.11   kamil 	ps.box_com = true;
    103  1.11   kamil 	break_delim = false;
    104  1.60  rillig 	ps.com_ind = 0;
    105  1.56  rillig 
    106  1.19  rillig     } else {
    107  1.58  rillig 	if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' ||
    108  1.58  rillig 	    (*inp.s == '\n' && !opt.format_block_comments)) {
    109  1.11   kamil 	    ps.box_com = true;	/* A comment with a '-' or '*' immediately
    110  1.11   kamil 				 * after the /+* is assumed to be a boxed
    111  1.11   kamil 				 * comment. A comment with a newline
    112  1.47  rillig 				 * immediately after the /+* is assumed to be
    113  1.47  rillig 				 * a block comment and is treated as a box
    114  1.47  rillig 				 * comment unless format_block_comments is
    115  1.47  rillig 				 * nonzero (the default). */
    116  1.11   kamil 	    break_delim = false;
    117  1.11   kamil 	}
    118  1.56  rillig 
    119  1.45  rillig 	if (lab.s == lab.e && code.s == code.e) {
    120  1.60  rillig 	    ps.com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
    121  1.26  rillig 	    adj_max_line_length = opt.block_comment_max_line_length;
    122  1.60  rillig 	    if (ps.com_ind <= 0)
    123  1.60  rillig 		ps.com_ind = opt.format_col1_comments ? 0 : 1;
    124  1.56  rillig 
    125  1.19  rillig 	} else {
    126  1.33  rillig 	    break_delim = false;
    127  1.34  rillig 
    128  1.59  rillig 	    int target_ind;
    129  1.38  rillig 	    if (code.s != code.e)
    130  1.59  rillig 		target_ind = indentation_after(compute_code_indent(), code.s);
    131  1.37  rillig 	    else if (lab.s != lab.e)
    132  1.59  rillig 		target_ind = indentation_after(compute_label_indent(), lab.s);
    133  1.34  rillig 	    else
    134  1.59  rillig 		target_ind = 0;
    135  1.34  rillig 
    136  1.60  rillig 	    ps.com_ind = ps.decl_on_line || ps.ind_level == 0
    137  1.60  rillig 		? opt.decl_comment_column - 1 : opt.comment_column - 1;
    138  1.60  rillig 	    if (ps.com_ind <= target_ind)
    139  1.60  rillig 		ps.com_ind = opt.tabsize * (1 + target_ind / opt.tabsize);
    140  1.60  rillig 	    /* XXX: the '+ 1' smells like an off-by-one error */
    141  1.60  rillig 	    if (ps.com_ind + 1 + 24 > adj_max_line_length)
    142  1.60  rillig 		adj_max_line_length = ps.com_ind + 1 + 24;
    143  1.11   kamil 	}
    144  1.11   kamil     }
    145  1.56  rillig 
    146  1.11   kamil     if (ps.box_com) {
    147   1.6   lukem 	/*
    148  1.11   kamil 	 * Find out how much indentation there was originally, because that
    149  1.53  rillig 	 * much will have to be ignored by dump_line(). This is a box comment,
    150  1.53  rillig 	 * so nothing changes -- not even indentation.
    151  1.11   kamil 	 *
    152  1.58  rillig 	 * The comment we're about to read usually comes from inp.buf,
    153  1.11   kamil 	 * unless it has been copied into save_com.
    154  1.11   kamil 	 */
    155  1.53  rillig 	const char *start;
    156  1.11   kamil 
    157  1.25  rillig 	/*
    158  1.25  rillig 	 * XXX: ordered comparison between pointers from different objects
    159  1.25  rillig 	 * invokes undefined behavior (C99 6.5.8).
    160  1.25  rillig 	 */
    161  1.58  rillig 	start = inp.s >= save_com && inp.s < save_com + sc_size ?
    162  1.58  rillig 	    sc_buf : inp.buf;
    163  1.58  rillig 	ps.n_comment_delta = -indentation_after_range(0, start, inp.s - 2);
    164  1.19  rillig     } else {
    165  1.11   kamil 	ps.n_comment_delta = 0;
    166  1.58  rillig 	while (is_hspace(*inp.s))
    167  1.58  rillig 	    inp.s++;
    168  1.11   kamil     }
    169  1.56  rillig 
    170  1.11   kamil     ps.comment_delta = 0;
    171  1.36  rillig     *com.e++ = '/';
    172  1.39  rillig     *com.e++ = token.e[-1];
    173  1.58  rillig     if (*inp.s != ' ' && !ps.box_com)
    174  1.36  rillig 	*com.e++ = ' ';
    175  1.11   kamil 
    176  1.55  rillig     /* Don't put a break delimiter if this is a one-liner that won't wrap. */
    177  1.57  rillig     if (break_delim) {
    178  1.61  rillig 	for (const char *p = inp.s; *p != '\0' && *p != '\n'; p++) {
    179  1.61  rillig 	    if (p >= inp.e)
    180  1.11   kamil 		fill_buffer();
    181  1.61  rillig 	    if (p[0] == '*' && p[1] == '/') {
    182  1.47  rillig 		/*
    183  1.47  rillig 		 * XXX: This computation ignores the leading " * ", as well as
    184  1.47  rillig 		 * the trailing ' ' '*' '/'.  In simple cases, these cancel
    185  1.47  rillig 		 * out since they are equally long.
    186  1.47  rillig 		 */
    187  1.60  rillig 		int right_margin = indentation_after_range(ps.com_ind,
    188  1.61  rillig 		    inp.s, p + 2);
    189  1.30  rillig 		if (right_margin < adj_max_line_length)
    190  1.11   kamil 		    break_delim = false;
    191  1.11   kamil 		break;
    192  1.11   kamil 	    }
    193   1.6   lukem 	}
    194  1.57  rillig     }
    195   1.1     cgd 
    196  1.11   kamil     if (break_delim) {
    197  1.47  rillig 	char *t = com.e;
    198  1.36  rillig 	com.e = com.s + 2;
    199  1.49  rillig 	*com.e = '\0';
    200  1.11   kamil 	if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
    201  1.43  rillig 	    prefix_blankline_requested = true;
    202  1.11   kamil 	dump_line();
    203  1.36  rillig 	com.e = com.s = t;
    204  1.11   kamil 	if (!ps.box_com && opt.star_comment_cont)
    205  1.36  rillig 	    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    206  1.11   kamil     }
    207  1.11   kamil 
    208  1.11   kamil     /* Start to copy the comment */
    209   1.1     cgd 
    210  1.31  rillig     for (;;) {			/* this loop will go until the comment is
    211   1.6   lukem 				 * copied */
    212  1.58  rillig 	switch (*inp.s) {	/* this checks for various special cases */
    213  1.51  rillig 	case '\f':
    214  1.35  rillig 	    check_size_comment(3);
    215  1.11   kamil 	    if (!ps.box_com) {	/* in a text comment, break the line here */
    216  1.11   kamil 		ps.use_ff = true;
    217  1.11   kamil 		dump_line();
    218  1.35  rillig 		last_blank = -1;
    219  1.11   kamil 		if (!ps.box_com && opt.star_comment_cont)
    220  1.36  rillig 		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    221  1.58  rillig 		inp.s++;
    222  1.58  rillig 		while (is_hspace(*inp.s))
    223  1.58  rillig 		    inp.s++;
    224  1.19  rillig 	    } else {
    225  1.48  rillig 		inbuf_skip();
    226  1.51  rillig 		*com.e++ = '\f';
    227  1.11   kamil 	    }
    228  1.11   kamil 	    break;
    229  1.11   kamil 
    230  1.11   kamil 	case '\n':
    231  1.62  rillig 	    if (token.e[-1] == '/')
    232  1.62  rillig 		goto end_of_line_comment;
    233  1.56  rillig 
    234  1.54  rillig 	    if (had_eof) {
    235  1.11   kamil 		printf("Unterminated comment\n");
    236  1.11   kamil 		dump_line();
    237  1.11   kamil 		return;
    238  1.11   kamil 	    }
    239  1.56  rillig 
    240  1.35  rillig 	    last_blank = -1;
    241  1.35  rillig 	    check_size_comment(4);
    242  1.11   kamil 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
    243  1.52  rillig 						 * we handle the newline */
    244  1.36  rillig 		if (com.s == com.e)
    245  1.36  rillig 		    *com.e++ = ' ';
    246  1.36  rillig 		if (!ps.box_com && com.e - com.s > 3) {
    247  1.11   kamil 		    dump_line();
    248  1.11   kamil 		    if (opt.star_comment_cont)
    249  1.36  rillig 			*com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    250  1.11   kamil 		}
    251  1.11   kamil 		dump_line();
    252  1.11   kamil 		if (!ps.box_com && opt.star_comment_cont)
    253  1.36  rillig 		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    254  1.56  rillig 
    255  1.19  rillig 	    } else {
    256  1.43  rillig 		ps.last_nl = true;
    257  1.50  rillig 		if (!is_hspace(com.e[-1]))
    258  1.36  rillig 		    *com.e++ = ' ';
    259  1.36  rillig 		last_blank = com.e - 1 - com.buf;
    260  1.11   kamil 	    }
    261  1.54  rillig 	    ++line_no;
    262  1.11   kamil 	    if (!ps.box_com) {
    263  1.52  rillig 		int asterisks_to_skip = 1;
    264  1.11   kamil 		do {		/* flush any blanks and/or tabs at start of
    265  1.11   kamil 				 * next line */
    266  1.48  rillig 		    inbuf_skip();
    267  1.58  rillig 		    if (*inp.s == '*' && --asterisks_to_skip >= 0) {
    268  1.48  rillig 			inbuf_skip();
    269  1.58  rillig 			if (*inp.s == '/')
    270  1.11   kamil 			    goto end_of_comment;
    271  1.11   kamil 		    }
    272  1.58  rillig 		} while (is_hspace(*inp.s));
    273  1.48  rillig 	    } else
    274  1.48  rillig 		inbuf_skip();
    275  1.11   kamil 	    break;		/* end of case for newline */
    276   1.1     cgd 
    277  1.52  rillig 	case '*':
    278  1.48  rillig 	    inbuf_skip();
    279  1.35  rillig 	    check_size_comment(4);
    280  1.58  rillig 	    if (*inp.s == '/') {
    281  1.11   kamil 	end_of_comment:
    282  1.48  rillig 		inbuf_skip();
    283  1.56  rillig 
    284  1.62  rillig 	end_of_line_comment:
    285  1.11   kamil 		if (break_delim) {
    286  1.36  rillig 		    if (com.e > com.s + 3)
    287  1.11   kamil 			dump_line();
    288  1.11   kamil 		    else
    289  1.36  rillig 			com.s = com.e;
    290  1.36  rillig 		    *com.e++ = ' ';
    291  1.11   kamil 		}
    292  1.56  rillig 
    293  1.50  rillig 		if (!is_hspace(com.e[-1]) && !ps.box_com)
    294  1.36  rillig 		    *com.e++ = ' ';	/* ensure blank before end */
    295  1.39  rillig 		if (token.e[-1] == '/')
    296  1.62  rillig 		    *com.e = '\0';
    297  1.13  rillig 		else
    298  1.36  rillig 		    *com.e++ = '*', *com.e++ = '/', *com.e = '\0';
    299  1.56  rillig 
    300  1.11   kamil 		ps.just_saw_decl = l_just_saw_decl;
    301  1.11   kamil 		return;
    302  1.56  rillig 
    303  1.19  rillig 	    } else		/* handle isolated '*' */
    304  1.36  rillig 		*com.e++ = '*';
    305  1.11   kamil 	    break;
    306  1.56  rillig 
    307  1.11   kamil 	default:		/* we have a random char */
    308  1.26  rillig 	    ;
    309  1.60  rillig 	    int now_len = indentation_after_range(ps.com_ind, com.s, com.e);
    310  1.11   kamil 	    do {
    311  1.35  rillig 		check_size_comment(1);
    312  1.49  rillig 		char ch = inbuf_next();
    313  1.50  rillig 		if (is_hspace(ch))
    314  1.49  rillig 		    last_blank = com.e - com.buf;
    315  1.49  rillig 		*com.e++ = ch;
    316  1.26  rillig 		now_len++;
    317  1.58  rillig 	    } while (memchr("*\n\r\b\t", *inp.s, 6) == NULL &&
    318  1.35  rillig 		(now_len < adj_max_line_length || last_blank == -1));
    319  1.56  rillig 
    320  1.11   kamil 	    ps.last_nl = false;
    321  1.56  rillig 
    322  1.26  rillig 	    /* XXX: signed character comparison '>' does not work for UTF-8 */
    323  1.32  rillig 	    if (now_len > adj_max_line_length &&
    324  1.36  rillig 		    !ps.box_com && com.e[-1] > ' ') {
    325  1.56  rillig 
    326  1.55  rillig 		/* the comment is too long, it must be broken up */
    327  1.35  rillig 		if (last_blank == -1) {
    328  1.11   kamil 		    dump_line();
    329  1.11   kamil 		    if (!ps.box_com && opt.star_comment_cont)
    330  1.36  rillig 			*com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    331  1.11   kamil 		    break;
    332  1.11   kamil 		}
    333  1.56  rillig 
    334  1.36  rillig 		*com.e = '\0';
    335  1.36  rillig 		com.e = com.buf + last_blank;
    336  1.11   kamil 		dump_line();
    337  1.56  rillig 
    338  1.11   kamil 		if (!ps.box_com && opt.star_comment_cont)
    339  1.36  rillig 		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
    340  1.56  rillig 
    341  1.61  rillig 		const char *p = com.buf + last_blank + 1;
    342  1.61  rillig 		while (is_hspace(*p))
    343  1.61  rillig 		    p++;
    344  1.35  rillig 		last_blank = -1;
    345  1.56  rillig 
    346  1.11   kamil 		/*
    347  1.61  rillig 		 * p will be somewhere between com.e (dump_line() reset)
    348  1.61  rillig 		 * and com.l. So it's safe to copy byte by byte from p to
    349  1.47  rillig 		 * com.e without any check_size_comment().
    350  1.11   kamil 		 */
    351  1.61  rillig 		while (*p != '\0') {
    352  1.61  rillig 		    if (is_hspace(*p))
    353  1.36  rillig 			last_blank = com.e - com.buf;
    354  1.61  rillig 		    *com.e++ = *p++;
    355   1.1     cgd 		}
    356  1.11   kamil 	    }
    357  1.11   kamil 	    break;
    358   1.1     cgd 	}
    359  1.11   kamil     }
    360   1.1     cgd }
    361