2023-06-14 12:31:05 +03:00
|
|
|
/* $NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
2003-08-07 15:13:06 +04:00
|
|
|
*
|
1997-10-18 20:04:21 +04:00
|
|
|
* Copyright (c) 1985 Sun Microsystems, Inc.
|
2019-04-04 18:22:13 +03:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:58:42 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
#include <sys/cdefs.h>
|
2023-06-14 12:31:05 +03:00
|
|
|
__RCSID("$NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $");
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
#include <string.h>
|
2021-03-07 13:42:48 +03:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
#include "indent.h"
|
2021-03-07 13:42:48 +03:00
|
|
|
|
2021-03-08 23:15:42 +03:00
|
|
|
static void
|
2021-10-12 22:56:07 +03:00
|
|
|
com_add_char(char ch)
|
2021-03-08 23:15:42 +03:00
|
|
|
{
|
2023-05-18 07:23:03 +03:00
|
|
|
buf_add_char(&com, ch);
|
2021-10-12 22:56:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_add_delim(void)
|
|
|
|
{
|
2023-06-06 10:14:20 +03:00
|
|
|
if (opt.star_comment_cont)
|
|
|
|
buf_add_chars(&com, " * ", 3);
|
2021-10-12 22:56:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 01:38:02 +03:00
|
|
|
static bool
|
2023-06-14 12:31:05 +03:00
|
|
|
fits_in_one_line(int max_line_length)
|
2021-10-14 01:38:02 +03:00
|
|
|
{
|
2023-06-04 23:51:19 +03:00
|
|
|
for (const char *start = inp_p, *p = start; *p != '\n'; p++) {
|
2023-05-18 07:23:03 +03:00
|
|
|
if (p[0] == '*' && p[1] == '/') {
|
2023-06-06 10:51:35 +03:00
|
|
|
while (p - inp_p >= 2
|
|
|
|
&& ch_isblank(p[-1])
|
|
|
|
&& ch_isblank(p[-2]))
|
|
|
|
p--;
|
2023-06-14 12:31:05 +03:00
|
|
|
int ind = ind_add(ps.comment_ind + 3,
|
2023-05-18 08:33:27 +03:00
|
|
|
start, (size_t)(p - start));
|
2023-06-14 12:31:05 +03:00
|
|
|
ind += p == start || ch_isblank(p[-1]) ? 2 : 3;
|
|
|
|
return ind <= max_line_length;
|
2023-05-18 07:23:03 +03:00
|
|
|
}
|
2023-05-14 17:14:07 +03:00
|
|
|
}
|
2023-05-18 07:23:03 +03:00
|
|
|
return false;
|
2021-10-14 01:38:02 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 20:37:03 +03:00
|
|
|
static void
|
2023-06-14 12:31:05 +03:00
|
|
|
analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
2023-05-18 07:23:03 +03:00
|
|
|
bool may_wrap = true;
|
2023-06-09 10:04:51 +03:00
|
|
|
bool delim = false;
|
2023-05-18 07:23:03 +03:00
|
|
|
int ind;
|
|
|
|
int line_length = opt.max_line_length;
|
|
|
|
|
2023-06-14 11:25:15 +03:00
|
|
|
if (inp_p - inp.s == 2 && !opt.format_col1_comments) {
|
2023-05-18 07:23:03 +03:00
|
|
|
may_wrap = false;
|
|
|
|
ind = 0;
|
|
|
|
} else {
|
2023-06-04 23:51:19 +03:00
|
|
|
if (inp_p[0] == '-' || inp_p[0] == '*' ||
|
|
|
|
token.s[token.len - 1] == '/' ||
|
2023-06-09 10:04:51 +03:00
|
|
|
(inp_p[0] == '\n' && !opt.format_block_comments))
|
2023-05-18 07:23:03 +03:00
|
|
|
may_wrap = false;
|
2023-06-04 23:51:19 +03:00
|
|
|
if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
|
2023-05-20 14:53:53 +03:00
|
|
|
out.line_kind = lk_block_comment;
|
2023-05-18 07:23:03 +03:00
|
|
|
|
|
|
|
if (com.len > 0)
|
|
|
|
output_line();
|
|
|
|
if (lab.len == 0 && code.len == 0) {
|
2023-05-18 08:33:27 +03:00
|
|
|
ind = (ps.ind_level - opt.unindent_displace)
|
|
|
|
* opt.indent_size;
|
2023-05-18 07:23:03 +03:00
|
|
|
if (ind <= 0)
|
|
|
|
ind = opt.format_col1_comments ? 0 : 1;
|
|
|
|
line_length = opt.block_comment_max_line_length;
|
2023-06-09 10:18:52 +03:00
|
|
|
if (may_wrap && inp_p[0] == '\n')
|
|
|
|
delim = true;
|
2023-06-10 19:43:55 +03:00
|
|
|
if (may_wrap && opt.comment_delimiter_on_blank_line)
|
2023-06-09 10:18:52 +03:00
|
|
|
delim = true;
|
2023-05-18 07:23:03 +03:00
|
|
|
} else {
|
|
|
|
int target_ind = code.len > 0
|
2023-06-04 23:51:19 +03:00
|
|
|
? ind_add(compute_code_indent(), code.s, code.len)
|
|
|
|
: ind_add(compute_label_indent(), lab.s, lab.len);
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-10 09:38:20 +03:00
|
|
|
ind = ps.line_has_decl || ps.ind_level == 0
|
2023-05-18 08:33:27 +03:00
|
|
|
? opt.decl_comment_column - 1
|
|
|
|
: opt.comment_column - 1;
|
2023-05-18 07:23:03 +03:00
|
|
|
if (ind <= target_ind)
|
|
|
|
ind = next_tab(target_ind);
|
|
|
|
if (ind + 25 > line_length)
|
|
|
|
line_length = ind + 25;
|
|
|
|
}
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2021-10-08 00:52:54 +03:00
|
|
|
|
2023-05-18 07:23:03 +03:00
|
|
|
if (!may_wrap) {
|
|
|
|
/* Find out how much indentation there was originally, because
|
2023-05-21 13:18:44 +03:00
|
|
|
* that much will have to be ignored by output_line. */
|
2023-06-04 23:51:19 +03:00
|
|
|
size_t len = (size_t)(inp_p - 2 - inp.s);
|
2023-06-14 12:31:05 +03:00
|
|
|
ps.comment_shift = -ind_add(0, inp.s, len);
|
2021-03-13 02:10:18 +03:00
|
|
|
} else {
|
2023-06-14 12:31:05 +03:00
|
|
|
ps.comment_shift = 0;
|
2023-06-04 23:51:19 +03:00
|
|
|
if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1])))
|
|
|
|
while (ch_isblank(inp_p[0]))
|
|
|
|
inp_p++;
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-14 12:31:05 +03:00
|
|
|
ps.comment_ind = ind;
|
2023-06-06 09:51:44 +03:00
|
|
|
*p_may_wrap = may_wrap;
|
|
|
|
*p_delim = delim;
|
|
|
|
*p_line_length = line_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-06-14 12:31:05 +03:00
|
|
|
copy_comment_start(bool may_wrap, bool *delim, int line_length)
|
2023-06-06 09:51:44 +03:00
|
|
|
{
|
2023-06-14 12:31:05 +03:00
|
|
|
ps.comment_in_first_line = true;
|
2023-05-18 07:23:03 +03:00
|
|
|
com_add_char('/');
|
2023-06-04 23:51:19 +03:00
|
|
|
com_add_char(token.s[token.len - 1]); /* either '*' or '/' */
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-09 10:04:51 +03:00
|
|
|
if (may_wrap) {
|
|
|
|
if (!ch_isblank(inp_p[0]))
|
|
|
|
com_add_char(' ');
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-14 12:31:05 +03:00
|
|
|
if (*delim && fits_in_one_line(line_length))
|
2023-06-09 10:04:51 +03:00
|
|
|
*delim = false;
|
|
|
|
if (*delim) {
|
|
|
|
output_line();
|
|
|
|
com_add_delim();
|
|
|
|
}
|
2023-05-18 07:23:03 +03:00
|
|
|
}
|
2023-06-06 09:51:44 +03:00
|
|
|
}
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-06 09:51:44 +03:00
|
|
|
static void
|
|
|
|
copy_comment_wrap_text(int line_length, ssize_t *last_blank)
|
|
|
|
{
|
2023-06-14 12:31:05 +03:00
|
|
|
int now_len = ind_add(ps.comment_ind, com.s, com.len);
|
2023-06-06 09:51:44 +03:00
|
|
|
for (;;) {
|
|
|
|
char ch = inp_next();
|
|
|
|
if (ch_isblank(ch))
|
|
|
|
*last_blank = (ssize_t)com.len;
|
|
|
|
com_add_char(ch);
|
|
|
|
now_len++;
|
|
|
|
if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL)
|
|
|
|
break;
|
|
|
|
if (now_len >= line_length && *last_blank != -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now_len <= line_length)
|
|
|
|
return;
|
|
|
|
if (ch_isspace(com.s[com.len - 1]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*last_blank == -1) {
|
|
|
|
/* only a single word in this line */
|
|
|
|
output_line();
|
|
|
|
com_add_delim();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *last_word_s = com.s + *last_blank + 1;
|
|
|
|
size_t last_word_len = com.len - (size_t)(*last_blank + 1);
|
|
|
|
com.len = (size_t)*last_blank;
|
|
|
|
output_line();
|
|
|
|
com_add_delim();
|
|
|
|
|
2023-06-09 10:20:30 +03:00
|
|
|
/* Assume that output_line and com_add_delim don't invalidate the
|
|
|
|
* "unused" part of the buffer beyond com.s + com.len. */
|
2023-06-06 09:51:44 +03:00
|
|
|
memmove(com.s + com.len, last_word_s, last_word_len);
|
|
|
|
com.len += last_word_len;
|
|
|
|
*last_blank = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2023-06-14 12:31:05 +03:00
|
|
|
copy_comment_wrap_newline(ssize_t *last_blank, bool seen_newline)
|
2023-06-06 09:51:44 +03:00
|
|
|
{
|
|
|
|
*last_blank = -1;
|
2023-06-14 12:31:05 +03:00
|
|
|
if (seen_newline) {
|
2023-06-06 09:51:44 +03:00
|
|
|
if (com.len == 0)
|
|
|
|
com_add_char(' '); /* force empty output line */
|
|
|
|
if (com.len > 3) {
|
|
|
|
output_line();
|
|
|
|
com_add_delim();
|
|
|
|
}
|
|
|
|
output_line();
|
|
|
|
com_add_delim();
|
|
|
|
} else {
|
|
|
|
if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
|
|
|
|
com_add_char(' ');
|
|
|
|
*last_blank = (int)com.len - 1;
|
|
|
|
}
|
|
|
|
++line_no;
|
|
|
|
|
|
|
|
/* flush any blanks and/or tabs at start of next line */
|
2023-06-06 09:59:39 +03:00
|
|
|
inp_skip(); /* '\n' */
|
|
|
|
while (ch_isblank(inp_p[0]))
|
|
|
|
inp_p++;
|
|
|
|
if (inp_p[0] == '*' && inp_p[1] == '/')
|
|
|
|
return false;
|
|
|
|
if (inp_p[0] == '*') {
|
|
|
|
inp_p++;
|
|
|
|
while (ch_isblank(inp_p[0]))
|
2023-06-06 09:51:44 +03:00
|
|
|
inp_p++;
|
2023-06-06 09:59:39 +03:00
|
|
|
}
|
2023-06-06 09:51:44 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_comment_wrap_finish(int line_length, bool delim)
|
|
|
|
{
|
|
|
|
if (delim) {
|
|
|
|
if (com.len > 3)
|
|
|
|
output_line();
|
|
|
|
else
|
2023-06-10 15:59:31 +03:00
|
|
|
buf_clear(&com);
|
2023-06-06 09:51:44 +03:00
|
|
|
com_add_char(' ');
|
|
|
|
} else {
|
|
|
|
size_t len = com.len;
|
|
|
|
while (ch_isblank(com.s[len - 1]))
|
|
|
|
len--;
|
2023-06-14 12:31:05 +03:00
|
|
|
int end_ind = ind_add(ps.comment_ind, com.s, len);
|
2023-06-06 10:14:20 +03:00
|
|
|
if (end_ind + 3 > line_length)
|
2023-06-06 09:51:44 +03:00
|
|
|
output_line();
|
|
|
|
}
|
|
|
|
|
2023-06-06 10:51:35 +03:00
|
|
|
while (com.len >= 2
|
|
|
|
&& ch_isblank(com.s[com.len - 1])
|
|
|
|
&& ch_isblank(com.s[com.len - 2]))
|
|
|
|
com.len--;
|
2023-06-10 15:59:31 +03:00
|
|
|
buf_terminate(&com);
|
2023-06-06 10:51:35 +03:00
|
|
|
|
2023-06-06 09:59:39 +03:00
|
|
|
inp_p += 2;
|
2023-06-06 10:14:20 +03:00
|
|
|
if (com.len > 0 && ch_isblank(com.s[com.len - 1]))
|
|
|
|
buf_add_chars(&com, "*/", 2);
|
|
|
|
else
|
|
|
|
buf_add_chars(&com, " */", 3);
|
2021-11-04 20:37:03 +03:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:56:06 +03:00
|
|
|
/*
|
|
|
|
* Copy characters from 'inp' to 'com'. Try to keep comments from going over
|
|
|
|
* the maximum line length. To do that, remember where the last blank, tab, or
|
|
|
|
* newline was. When a line is filled, print up to the last blank and continue
|
|
|
|
* copying.
|
|
|
|
*/
|
2021-11-04 20:37:03 +03:00
|
|
|
static void
|
2023-05-14 21:05:52 +03:00
|
|
|
copy_comment_wrap(int line_length, bool delim)
|
2021-11-04 20:37:03 +03:00
|
|
|
{
|
2023-06-04 23:51:19 +03:00
|
|
|
ssize_t last_blank = -1; /* index of the last blank in 'com' */
|
2023-06-14 11:36:51 +03:00
|
|
|
bool seen_newline = false;
|
2023-05-18 07:23:03 +03:00
|
|
|
|
|
|
|
for (;;) {
|
2023-06-06 09:51:44 +03:00
|
|
|
if (inp_p[0] == '\n') {
|
|
|
|
if (had_eof)
|
|
|
|
goto unterminated_comment;
|
2023-06-14 11:36:51 +03:00
|
|
|
if (!copy_comment_wrap_newline(&last_blank,
|
2023-06-14 12:31:05 +03:00
|
|
|
seen_newline))
|
|
|
|
break;
|
|
|
|
seen_newline = true;
|
2023-06-06 09:59:39 +03:00
|
|
|
} else if (inp_p[0] == '*' && inp_p[1] == '/')
|
2023-06-14 12:31:05 +03:00
|
|
|
break;
|
2023-06-14 11:36:51 +03:00
|
|
|
else {
|
2023-06-06 09:51:44 +03:00
|
|
|
copy_comment_wrap_text(line_length, &last_blank);
|
2023-06-14 11:36:51 +03:00
|
|
|
seen_newline = false;
|
|
|
|
}
|
2023-06-06 09:51:44 +03:00
|
|
|
}
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-06 09:51:44 +03:00
|
|
|
copy_comment_wrap_finish(line_length, delim);
|
|
|
|
return;
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-06 09:51:44 +03:00
|
|
|
unterminated_comment:
|
|
|
|
diag(1, "Unterminated comment");
|
|
|
|
output_line();
|
2021-11-07 11:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-07 13:13:26 +03:00
|
|
|
copy_comment_nowrap(void)
|
2021-11-07 11:24:50 +03:00
|
|
|
{
|
2023-06-04 23:51:19 +03:00
|
|
|
char kind = token.s[token.len - 1];
|
2023-06-04 23:23:12 +03:00
|
|
|
|
2023-05-18 07:23:03 +03:00
|
|
|
for (;;) {
|
2023-06-04 23:51:19 +03:00
|
|
|
if (inp_p[0] == '\n') {
|
2023-06-04 23:23:12 +03:00
|
|
|
if (kind == '/')
|
2023-05-18 07:23:03 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (had_eof) {
|
|
|
|
diag(1, "Unterminated comment");
|
|
|
|
output_line();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (com.len == 0)
|
|
|
|
com_add_char(' '); /* force output of an
|
|
|
|
* empty line */
|
|
|
|
output_line();
|
|
|
|
++line_no;
|
|
|
|
inp_skip();
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-08 00:52:54 +03:00
|
|
|
|
2023-06-04 23:51:19 +03:00
|
|
|
com_add_char(*inp_p++);
|
2023-06-04 23:23:12 +03:00
|
|
|
if (com.len >= 2
|
2023-06-04 23:51:19 +03:00
|
|
|
&& com.s[com.len - 2] == '*'
|
|
|
|
&& com.s[com.len - 1] == '/'
|
2023-06-04 23:23:12 +03:00
|
|
|
&& kind == '*')
|
2023-05-18 07:23:03 +03:00
|
|
|
return;
|
2021-11-07 13:17:39 +03:00
|
|
|
}
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2021-11-04 20:37:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan, reformat and output a single comment, which is either a block comment
|
|
|
|
* starting with '/' '*' or an end-of-line comment starting with '//'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
process_comment(void)
|
|
|
|
{
|
2023-05-18 07:23:03 +03:00
|
|
|
bool may_wrap, delim;
|
2023-06-14 12:31:05 +03:00
|
|
|
int line_length;
|
2023-05-18 07:23:03 +03:00
|
|
|
|
2023-06-14 12:31:05 +03:00
|
|
|
analyze_comment(&may_wrap, &delim, &line_length);
|
|
|
|
copy_comment_start(may_wrap, &delim, line_length);
|
2023-05-18 07:23:03 +03:00
|
|
|
if (may_wrap)
|
|
|
|
copy_comment_wrap(line_length, delim);
|
|
|
|
else
|
|
|
|
copy_comment_nowrap();
|
2021-11-04 20:37:03 +03:00
|
|
|
}
|