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