indent: fix handling of '//' end-of-line comments

This commit is contained in:
rillig 2021-03-07 22:11:01 +00:00
parent b7a595bc61
commit 73071b8322
5 changed files with 46 additions and 35 deletions

View File

@ -1,11 +1,12 @@
/* $NetBSD: comment-line-end.0,v 1.3 2021/03/07 08:57:38 rillig Exp $ */
/* $NetBSD: comment-line-end.0,v 1.4 2021/03/07 22:11:01 rillig Exp $ */
/* $FreeBSD$ */
/*
* Demonstrates handling of line-end comments.
* Demonstrates handling of line-end '//' comments.
*
* Even though this type of comments was added in C99, indent doesn't support
* them, as of 2021, and instead messes up the code in unpredictable ways.
* Even though this type of comments had been added in C99, indent didn't
* support these comments until 2021 and instead messed up the code in
* unpredictable ways.
*/
int dummy // comment
@ -18,8 +19,8 @@ int dummy // comment
void function(void){}
// Note: removing one of these line-end comments affects the formatting
// of the main function below.
// Note: removing one of these line-end comments affected the formatting
// of the main function below, before indent supported '//' comments.
int
main(void)

View File

@ -1,33 +1,31 @@
/* $NetBSD: comment-line-end.0.stdout,v 1.3 2021/03/07 08:57:38 rillig Exp $ */
/* $NetBSD: comment-line-end.0.stdout,v 1.4 2021/03/07 22:11:01 rillig Exp $ */
/* $FreeBSD$ */
/*
* Demonstrates handling of line-end comments.
* Demonstrates handling of line-end '//' comments.
*
* Even though this type of comments was added in C99, indent doesn't support
* them, as of 2021, and instead messes up the code in unpredictable ways.
* Even though this type of comments had been added in C99, indent didn't
* support these comments until 2021 and instead messed up the code in
* unpredictable ways.
*/
int dummy //comment
= //eq
1 // one
+ //plus
2;
//two
int dummy // comment
= // eq
1 // one
+ // plus
2; // two
///// separator /////
/////separator/////
void
function(void)
{
}
/* $ FIXME: The space between 'Note: removing' must be preserved. */
/* $ FIXME: The spacing around the '-' in 'line-end' must be preserved. */
//Note:removing one of these line - end comments affects the formatting
// of the main function below.
// Note: removing one of these line-end comments affected the formatting
// of the main function below, before indent supported '//' comments.
int
/* $ FIXME: The '{' must be in column 1, not directly after the ')'. */
main(void){
main(void)
{
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: io.c,v 1.23 2021/03/07 20:47:13 rillig Exp $ */
/* $NetBSD: io.c,v 1.24 2021/03/07 22:11:01 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -46,7 +46,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
__RCSID("$NetBSD: io.c,v 1.23 2021/03/07 20:47:13 rillig Exp $");
__RCSID("$NetBSD: io.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@ -208,6 +208,11 @@ dump_line(void)
prefix_blankline_requested = postfix_blankline_requested;
postfix_blankline_requested = 0;
}
/* keep blank lines after '//' comments */
if (e_com - s_com > 1 && s_com[1] == '/')
fprintf(output, "%.*s", (int)(e_token - s_token), s_token);
ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
* declaration, remember that fact for
* proper comment indentation */

View File

@ -1,4 +1,4 @@
/* $NetBSD: lexi.c,v 1.23 2021/03/07 20:47:13 rillig Exp $ */
/* $NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
__RCSID("$NetBSD: lexi.c,v 1.23 2021/03/07 20:47:13 rillig Exp $");
__RCSID("$NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@ -619,9 +619,9 @@ stop_lit:
break;
default:
if (token[0] == '/' && *buf_ptr == '*') {
if (token[0] == '/' && (*buf_ptr == '*' || *buf_ptr == '/')) {
/* it is start of comment */
*e_token++ = '*';
*e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();

View File

@ -1,4 +1,4 @@
/* $NetBSD: pr_comment.c,v 1.12 2021/03/07 10:42:48 rillig Exp $ */
/* $NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -46,7 +46,7 @@ static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
__RCSID("$NetBSD: pr_comment.c,v 1.12 2021/03/07 10:42:48 rillig Exp $");
__RCSID("$NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@ -119,7 +119,7 @@ pr_comment(void)
ps.com_col = 1;
}
else {
if (*buf_ptr == '-' || *buf_ptr == '*' ||
if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' ||
(*buf_ptr == '\n' && !opt.format_block_comments)) {
ps.box_com = true; /* A comment with a '-' or '*' immediately
* after the /+* is assumed to be a boxed
@ -179,8 +179,8 @@ pr_comment(void)
buf_ptr++;
}
ps.comment_delta = 0;
*e_com++ = '/'; /* put '/' followed by '*' into buffer */
*e_com++ = '*';
*e_com++ = '/';
*e_com++ = e_token[-1];
if (*buf_ptr != ' ' && !ps.box_com)
*e_com++ = ' ';
@ -235,6 +235,10 @@ pr_comment(void)
break;
case '\n':
if (e_token[-1] == '/') {
++line_no;
goto end_of_comment;
}
if (had_eof) { /* check for unexpected eof */
printf("Unterminated comment\n");
dump_line();
@ -306,7 +310,10 @@ pr_comment(void)
}
if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
*e_com++ = ' '; /* ensure blank before end */
*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
if (e_token[-1] == '/')
*e_com++ = '\n', *e_com = '\0';
else
*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
ps.just_saw_decl = l_just_saw_decl;
return;
}