indent(1): improve handling of boxed comments indentation

The trick is to copy everything from the start of the line into the buffer
that stores newlines and comments until indent finds a brace or an else.
pr_comment() will use that information to calculate the original indentation
of the boxed comment.

This requires storing two pieces of information: the real start of the
buffer (sc_buf) and the start of the comment (save_com).
This commit is contained in:
Piotr Pawel Stefaniak 2018-06-03 15:28:55 +00:00
parent 63c3f22696
commit 3c51c3cf5f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=334563
5 changed files with 67 additions and 8 deletions

View File

@ -341,6 +341,7 @@ main(int argc, char **argv)
switch (type_code) {
case newline:
if (sc_end == NULL) {
save_com = sc_buf;
save_com[0] = save_com[1] = ' ';
sc_end = &save_com[2];
}
@ -359,6 +360,13 @@ main(int argc, char **argv)
break;
case comment:
if (sc_end == NULL) {
/*
* Copy everything from the start of the line, because
* pr_comment() will use that to calculate original
* indentation of a boxed comment.
*/
memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
save_com = sc_buf + (buf_ptr - in_buffer - 4);
save_com[0] = save_com[1] = ' ';
sc_end = &save_com[2];
}
@ -1172,9 +1180,11 @@ main(int argc, char **argv)
e_lab--;
if (e_lab - s_lab == com_end && bp_save == NULL) {
/* comment on preprocessor line */
if (sc_end == NULL) /* if this is the first comment, we
* must set up the buffer */
sc_end = &(save_com[0]);
if (sc_end == NULL) { /* if this is the first comment,
* we must set up the buffer */
save_com = sc_buf;
sc_end = &save_com[0];
}
else {
*sc_end++ = '\n'; /* add newline between
* comments */

View File

@ -126,8 +126,9 @@ char *buf_ptr; /* ptr to next character to be taken from
* in_buffer */
char *buf_end; /* ptr to first after last char in in_buffer */
char save_com[sc_size]; /* input text is saved here when looking for
char sc_buf[sc_size]; /* input text is saved here when looking for
* the brace after an if, while, etc */
char *save_com; /* start of the comment stored in sc_buf */
char *sc_end; /* pointer into save_com buffer */
char *bp_save; /* saved value of buf_ptr when taking input
@ -241,8 +242,12 @@ struct parser_state {
int box_com; /* set to true when we are in a "boxed"
* comment. In that case, the first non-blank
* char should be lined up with the / in / followed by * */
int comment_delta,
n_comment_delta;
int comment_delta; /* used to set up indentation for all lines
* of a boxed comment after the first one */
int n_comment_delta;/* remembers how many columns there were
* before the start of a box comment so that
* forthcoming lines of the comment are
* indented properly */
int cast_mask; /* indicates which close parens potentially
* close off casts */
int not_cast_mask; /* indicates which close parens definitely

View File

@ -158,8 +158,11 @@ pr_comment(void)
* The comment we're about to read usually comes from in_buffer,
* unless it has been copied into save_com.
*/
char *start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? bp_save : buf_ptr;
ps.n_comment_delta = 1 - count_spaces_until(1, in_buffer, start - 2);
char *start;
start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
sc_buf : in_buffer;
ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
}
else {
ps.n_comment_delta = 0;

View File

@ -30,3 +30,23 @@ void t(void) {
/* r309343 */
}
int c(void)
{
if (1) { /*- a christmas tree *
***
***** */
/*- another one *
***
***** */
7;
}
if (1) /*- a christmas tree *
***
***** */
/*- another one *
***
***** */
1;
}

View File

@ -37,3 +37,24 @@ t(void)
/* r309343 */
}
int
c(void)
{
if (1) { /*- a christmas tree *
***
***** */
/*- another one *
***
***** */
7;
}
if (1) /*- a christmas tree *
***
***** */
/*- another one *
***
***** */
1;
}