2023-06-18 10:32:33 +03:00
|
|
|
/* $NetBSD: pr_comment.c,v 1.170 2023/06/18 07:32:33 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-18 10:32:33 +03:00
|
|
|
__RCSID("$NetBSD: pr_comment.c,v 1.170 2023/06/18 07:32:33 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
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star(void)
|
2021-10-12 22:56:07 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-06-18 02:03:20 +03:00
|
|
|
static bool
|
|
|
|
is_block_comment(void)
|
|
|
|
{
|
|
|
|
const char *p = inp_p;
|
|
|
|
while (*p == '*')
|
|
|
|
p++;
|
|
|
|
return *p == '\n';
|
|
|
|
}
|
|
|
|
|
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-18 01:28:49 +03:00
|
|
|
bool delim = false; // only relevant if may_wrap
|
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;
|
|
|
|
|
|
|
|
if (com.len > 0)
|
|
|
|
output_line();
|
|
|
|
if (lab.len == 0 && code.len == 0) {
|
2023-06-18 10:32:33 +03:00
|
|
|
if (is_block_comment())
|
|
|
|
out.line_kind = lk_block_comment;
|
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 {
|
2023-06-18 01:28:49 +03:00
|
|
|
int min_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-06-18 01:28:49 +03:00
|
|
|
if (ind <= min_ind)
|
|
|
|
ind = next_tab(min_ind);
|
2023-05-18 07:23:03 +03:00
|
|
|
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-16 14:27:49 +03:00
|
|
|
ps.comment_cont = false;
|
2023-06-18 02:03:20 +03:00
|
|
|
buf_add_chars(&com, token.s, token.len); // "/*" 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();
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star();
|
2023-06-09 10:04:51 +03:00
|
|
|
}
|
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-18 01:28:49 +03:00
|
|
|
int ind = 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);
|
2023-06-18 01:28:49 +03:00
|
|
|
ind++;
|
2023-06-18 10:10:24 +03:00
|
|
|
if (memchr("*\n\r\t", inp_p[0], 5) != NULL)
|
2023-06-06 09:51:44 +03:00
|
|
|
break;
|
2023-06-18 01:28:49 +03:00
|
|
|
if (ind >= line_length && *last_blank != -1)
|
2023-06-06 09:51:44 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-06-18 01:28:49 +03:00
|
|
|
if (ind <= line_length)
|
2023-06-06 09:51:44 +03:00
|
|
|
return;
|
|
|
|
if (ch_isspace(com.s[com.len - 1]))
|
|
|
|
return;
|
|
|
|
|
2023-06-18 02:03:20 +03:00
|
|
|
if (*last_blank == -1) { /* only a single word in this line */
|
2023-06-06 09:51:44 +03:00
|
|
|
output_line();
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star();
|
2023-06-06 09:51:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-18 01:28:49 +03:00
|
|
|
// Move the overlong word to the next line.
|
|
|
|
const char *last_word = com.s + *last_blank + 1;
|
2023-06-06 09:51:44 +03:00
|
|
|
size_t last_word_len = com.len - (size_t)(*last_blank + 1);
|
|
|
|
com.len = (size_t)*last_blank;
|
2023-06-18 01:28:49 +03:00
|
|
|
buf_terminate(&com);
|
2023-06-06 09:51:44 +03:00
|
|
|
output_line();
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star();
|
2023-06-06 09:51:44 +03:00
|
|
|
|
2023-06-18 01:28:49 +03:00
|
|
|
/* Assume that output_line and com_add_delim left the "unused" part of
|
|
|
|
* the now truncated buffer beyond com.s + com.len as-is. */
|
|
|
|
memmove(com.s + com.len, last_word, last_word_len);
|
2023-06-06 09:51:44 +03:00
|
|
|
com.len += last_word_len;
|
2023-06-18 01:28:49 +03:00
|
|
|
buf_terminate(&com);
|
2023-06-06 09:51:44 +03:00
|
|
|
*last_blank = -1;
|
|
|
|
}
|
|
|
|
|
2023-06-18 01:28:49 +03:00
|
|
|
/* In a comment that is re-wrapped, handle a single newline character. */
|
2023-06-06 09:51:44 +03:00
|
|
|
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 > 3) {
|
|
|
|
output_line();
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star();
|
2023-06-06 09:51:44 +03:00
|
|
|
}
|
|
|
|
output_line();
|
2023-06-18 01:28:49 +03:00
|
|
|
com_add_star();
|
2023-06-06 09:51:44 +03:00
|
|
|
} else {
|
|
|
|
if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
|
|
|
|
com_add_char(' ');
|
|
|
|
*last_blank = (int)com.len - 1;
|
|
|
|
}
|
2023-06-18 01:28:49 +03:00
|
|
|
line_no++;
|
2023-06-06 09:51:44 +03:00
|
|
|
|
|
|
|
/* 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();
|
2023-06-18 02:03:20 +03:00
|
|
|
buf_clear(&com);
|
2023-06-06 09:51:44 +03:00
|
|
|
} else {
|
|
|
|
size_t len = com.len;
|
2023-06-18 01:28:49 +03:00
|
|
|
// XXX: This loop differs from the one below.
|
2023-06-06 09:51:44 +03:00
|
|
|
while (ch_isblank(com.s[len - 1]))
|
|
|
|
len--;
|
2023-06-18 01:28:49 +03:00
|
|
|
if (ind_add(ps.comment_ind, com.s, len) + 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
|
|
|
}
|
|
|
|
|
|
|
|
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 17:11:28 +03:00
|
|
|
seen_newline))
|
2023-06-14 12:31:05 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
output_line();
|
2023-06-18 01:28:49 +03:00
|
|
|
line_no++;
|
2023-05-18 07:23:03 +03:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|