Skip to content

Commit

Permalink
[bugfix] Untangle the connection between pr_comment.c and io.c, fixin…
Browse files Browse the repository at this point in the history
…g at least two bugs.

It's pr_comment.c that should decide whether to put a "star comment continuation" or not. This duplicates code a bit, but it simplifies pr_comment() at the same time since pr_comment() no longer has to "signal" whether a star continuation is needed or not.

This change requires indent(1) to not wrap comment lines that lack a blank character, but I think it's for the better if you look at cases when that happens (mostly long URIs and file system paths, which arguably shouldn't be wrapped).

It also fixes two bugs:

1. Cases where asterisk is a part of the comment's content (like in "*we* are the champions") and happens to appear at the beginning of the line, misleading dump_line() into thinking that this is part of the star comment continuation, leading to misalignment.

2. Cases where blank starred lines had three too many characters on the line when wrapped.
  • Loading branch information
pstef committed Jul 30, 2016
1 parent 345663c commit 3b41ee7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 61 deletions.
11 changes: 1 addition & 10 deletions io.c
Expand Up @@ -242,17 +242,8 @@ dump_line(void)
while (e_com > com_st && isspace(e_com[-1]))
e_com--;
cur_col = pad_output(cur_col, target);
if (!ps.box_com) {
if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) {
if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
com_st[1] = '*';
else
fwrite(" * ", isspace((unsigned int) com_st[0]) ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
}
}
fwrite(com_st, e_com - com_st, 1, output);
ps.comment_delta = ps.n_comment_delta;
cur_col = count_spaces(cur_col, com_st);
++ps.com_lines; /* count lines with comments */
}
}
Expand Down Expand Up @@ -282,7 +273,7 @@ dump_line(void)
ps.dumped_decl_indent = 0;
*(e_lab = s_lab) = '\0'; /* reset buffers */
*(e_code = s_code) = '\0';
*(e_com = s_com) = '\0';
*(e_com = s_com = combuf + 1) = '\0';
ps.ind_level = ps.i_l_follow;
ps.paren_level = ps.p_l_follow;
if (ps.paren_level > 0)
Expand Down
87 changes: 36 additions & 51 deletions pr_comment.c
Expand Up @@ -179,11 +179,11 @@ pr_comment(void)
if (blanklines_before_blockcomments)
prefix_blankline_requested = 1;
dump_line();
e_com = t;
s_com[0] = s_com[1] = s_com[2] = ' ';
e_com = s_com = t;
if (!ps.box_com && star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
}

*e_com = '\0';
if (troff) {
adj_max_col = 80;
}
Expand All @@ -200,10 +200,10 @@ pr_comment(void)
/* fix so dump_line uses a form feed */
dump_line();
last_bl = NULL;
*e_com++ = ' ';
*e_com++ = '*';
*e_com++ = ' ';
while (*++buf_ptr == ' ' || *buf_ptr == '\t');
if (!ps.box_com && star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
while (*++buf_ptr == ' ' || *buf_ptr == '\t')
;
}
else {
if (++buf_ptr >= buf_end)
Expand All @@ -215,25 +215,22 @@ pr_comment(void)
case '\n':
if (had_eof) { /* check for unexpected eof */
printf("Unterminated comment\n");
*e_com = '\0';
dump_line();
return;
}
last_bl = NULL;
if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
* we dont ignore the newline */
if (s_com == e_com) {
if (s_com == e_com)
*e_com++ = ' ';
*e_com++ = ' ';
}
*e_com = '\0';
if (!ps.box_com && e_com - s_com > 3) {
dump_line();
CHECK_SIZE_COM;
*e_com++ = ' ';
*e_com++ = ' ';
if (star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
}
dump_line();
if (!ps.box_com && star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
}
else {
ps.last_nl = 1;
Expand Down Expand Up @@ -277,20 +274,19 @@ pr_comment(void)
end_of_comment:
if (++buf_ptr >= buf_end)
fill_buffer();
/* ensure blank before end */
if (e_com[-1] != ' ' && !ps.box_com) {
*e_com++ = ' ';
}
CHECK_SIZE_COM;
if (break_delim) {
if (e_com > s_com + 3) {
*e_com = '\0';
dump_line();
}
else
s_com = e_com;
*e_com++ = ' ';
}
CHECK_SIZE_COM;
*e_com++ = '*';
*e_com++ = '/';
*e_com = '\0';
if (e_com[-1] != ' ' && !ps.box_com) {
*e_com++ = ' '; /* ensure blank before end */
}
*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
ps.just_saw_decl = l_just_saw_decl;
return;
}
Expand All @@ -308,40 +304,29 @@ pr_comment(void)
last_bl = e_com; /* remember we saw a blank */
++e_com;
now_col++;
} while (!memchr("*\n\r\b\t", *buf_ptr, 6) && now_col <= adj_max_col);
} while (!memchr("*\n\r\b\t", *buf_ptr, 6) && (now_col <= adj_max_col || !last_bl));
ps.last_nl = false;
if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
/*
* the comment is too long, it must be broken up
*/
if (last_bl == NULL) { /* we have seen no blanks */
last_bl = e_com; /* fake it */
*e_com++ = ' ';
/* the comment is too long, it must be broken up */
if (last_bl == NULL) {
dump_line();
if (!ps.box_com && star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
break;
}
*e_com = '\0'; /* print what we have */
*last_bl = '\0';
while (last_bl > s_com && last_bl[-1] < 040)
*--last_bl = 0;
*e_com = '\0';
e_com = last_bl;
dump_line();

*e_com++ = ' '; /* add blanks for continuation */
*e_com++ = ' ';
*e_com++ = ' ';

t_ptr = last_bl + 1;
if (!ps.box_com && star_comment_cont)
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t'; t_ptr++)
;
last_bl = NULL;
if (t_ptr >= e_com) {
while (*t_ptr == ' ' || *t_ptr == '\t')
t_ptr++;
while (*t_ptr != '\0') { /* move unprinted part of
* comment down in buffer */
if (*t_ptr == ' ' || *t_ptr == '\t')
last_bl = e_com;
*e_com++ = *t_ptr++;
}
while (*t_ptr != '\0') {
if (*t_ptr == ' ' || *t_ptr == '\t')
last_bl = e_com;
*e_com++ = *t_ptr++;
}
*e_com = '\0';
}
break;
}
Expand Down

0 comments on commit 3b41ee7

Please sign in to comment.