Module Name:    src
Committed By:   rillig
Date:           Sun Nov  7 18:26:17 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c indent.h lexi.c parse.c pr_comment.c

Log Message:
indent: various cleanups

Make several comments more precise.

Rename process_end_of_file to process_eof to match the token name.

Change the order of assignments in analyze_comment to keep the com_ind
computations closer together.

In copy_comment_wrap, use pointer difference instead of pointer addition
to stay away from undefined behavior.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.215 -r1.216 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.85 -r1.86 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.137 -r1.138 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.115 -r1.116 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.215 src/usr.bin/indent/indent.c:1.216
--- src/usr.bin/indent/indent.c:1.215	Sun Nov  7 15:18:25 2021
+++ src/usr.bin/indent/indent.c	Sun Nov  7 18:26:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.215 2021/11/07 15:18:25 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.216 2021/11/07 18:26:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c	5.1
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.215 2021/11/07 15:18:25 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.216 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -294,8 +294,8 @@ search_stmt_comment(void)
     if (sc_end == NULL) {
 	/*
 	 * Copy everything from the start of the line, because
-	 * process_comment() will use that to calculate original indentation
-	 * of a boxed comment.
+	 * process_comment() will use that to calculate the original
+	 * indentation of a boxed comment.
 	 */
 	/*
 	 * FIXME: This '4' needs an explanation. For example, in the snippet
@@ -665,7 +665,7 @@ code_add_decl_indent(int decl_ind, bool 
 }
 
 static void __attribute__((__noreturn__))
-process_end_of_file(void)
+process_eof(void)
 {
     if (lab.s != lab.e || code.s != code.e || com.s != com.e)
 	dump_line();
@@ -940,8 +940,8 @@ process_semicolon(bool *seen_case, int *
     }
 
     ps.in_decl = ps.decl_level > 0;	/* if we were in a first level
-					 * structure declaration, we aren't
-					 * anymore */
+					 * structure declaration before, we
+					 * aren't anymore */
 
     if ((!*spaced_expr || hd != hd_for) && ps.p_l_follow > 0) {
 
@@ -1395,7 +1395,7 @@ main_loop(void)
 	search_stmt(&lsym, &force_nl, &last_else);
 
 	if (lsym == lsym_eof) {
-	    process_end_of_file();
+	    process_eof();
 	    /* NOTREACHED */
 	}
 

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.85 src/usr.bin/indent/indent.h:1.86
--- src/usr.bin/indent/indent.h:1.85	Sun Nov  7 15:18:25 2021
+++ src/usr.bin/indent/indent.h	Sun Nov  7 18:26:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.85 2021/11/07 15:18:25 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.86 2021/11/07 18:26:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -150,7 +150,8 @@ extern FILE *input;
 extern FILE *output;
 
 extern struct buffer inp;	/* one line of input, ready to be split into
-				 * tokens */
+				 * tokens; occasionally this buffer switches
+				 * to sc_buf */
 
 extern struct buffer token;	/* the current token to be processed, is
 				 * typically copied to the buffer 'code',
@@ -271,7 +272,9 @@ extern struct parser_state {
     bool next_unary;		/* whether the following operator should be
 				 * unary */
 
-    char procname[100];		/* The name of the current procedure */
+    char procname[100];		/* The name of the current procedure; TODO:
+				 * document the difference between procname[0]
+				 * being '\0', ' ' and a real character */
 
 
     bool want_blank;		/* whether the following token should be
@@ -279,10 +282,11 @@ extern struct parser_state {
 				 * ignored in some cases.) */
 
     int paren_level;		/* parenthesization level. used to indent
-				 * within statements */
+				 * within statements, initializers and
+				 * declarations */
     /* TODO: rename to next_line_paren_level */
     int p_l_follow;		/* how to indent the remaining lines of the
-				 * statement */
+				 * statement or initializer or declaration */
     short paren_indents[20];	/* indentation of the operand/argument of each
 				 * level of parentheses or brackets, relative
 				 * to the enclosing statement; if negative,
@@ -327,7 +331,9 @@ extern struct parser_state {
     bool decl_indent_done;	/* whether the indentation for a declaration
 				 * has been added to the code buffer. */
 
-    bool in_stmt;
+    bool in_stmt;		/* TODO: rename to something appropriate; this
+				 * is set to true in struct declarations as
+				 * well, so 'stmt' isn't accurate */
     bool ind_stmt;		/* whether the next line should have an extra
 				 * indentation level because we are in the
 				 * middle of a statement */
@@ -336,7 +342,9 @@ extern struct parser_state {
 
     bool search_stmt;		/* whether it is necessary to buffer up all
 				 * text up to the start of a statement after
-				 * an 'if', 'while', etc. */
+				 * an 'if (expr)', 'while (expr)', etc., to
+				 * move the comments after the opening brace
+				 * of the following statement */
 
     int tos;			/* pointer to top of stack */
     parser_symbol s_sym[STACKSIZE];

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.137 src/usr.bin/indent/lexi.c:1.138
--- src/usr.bin/indent/lexi.c:1.137	Sun Nov  7 15:18:25 2021
+++ src/usr.bin/indent/lexi.c	Sun Nov  7 18:26:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.137 2021/11/07 15:18:25 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.138 2021/11/07 18:26:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.137 2021/11/07 15:18:25 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.138 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -362,7 +362,7 @@ lex_number(void)
 	     * lex_number_state[0][s - 'A'] now indicates the type:
 	     * f = floating, i = integer, u = unknown
 	     */
-	    break;
+	    return;
 	}
 
 	s = lex_number_state[row][s - 'A'];
@@ -464,7 +464,7 @@ cmp_keyword_by_name(const void *key, con
     return strcmp(key, ((const struct keyword *)elem)->name);
 }
 
-/* Read an alphanumeric token into 'token', or return end_of_file. */
+/* Read an alphanumeric token into 'token', or return lsym_eof. */
 static lexer_symbol
 lexi_alnum(void)
 {

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.47 src/usr.bin/indent/parse.c:1.48
--- src/usr.bin/indent/parse.c:1.47	Fri Oct 29 23:48:50 2021
+++ src/usr.bin/indent/parse.c	Sun Nov  7 18:26:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.47 2021/10/29 23:48:50 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.48 2021/11/07 18:26:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -160,7 +160,7 @@ parse(parser_symbol psym)
 		 * for a switch, brace should be two levels out from the code
 		 */
 		if (ps.s_sym[ps.tos] == psym_switch_expr &&
-			opt.case_indent >= 1)
+			opt.case_indent >= 1.0F)
 		    --ps.ind_level;
 	    }
 	}
@@ -191,11 +191,10 @@ parse(parser_symbol psym)
 	if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
 	    diag(1, "Unmatched 'else'");
 	else {
-	    /* The indentation for 'else' should be the same as for 'if'. */
 	    ps.ind_level = ps.s_ind_level[ps.tos];
 	    ps.ind_level_follow = ps.ind_level + 1;
 	    ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
-	    /* remember if with else */
+
 	    ps.search_stmt = opt.brace_same_line || opt.else_if;
 	}
 	break;

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.115 src/usr.bin/indent/pr_comment.c:1.116
--- src/usr.bin/indent/pr_comment.c:1.115	Sun Nov  7 13:38:32 2021
+++ src/usr.bin/indent/pr_comment.c	Sun Nov  7 18:26:17 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.115 2021/11/07 13:38:32 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.115 2021/11/07 13:38:32 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -124,8 +124,8 @@ analyze_comment(bool *p_may_wrap, bool *
 	}
 
 	if (lab.s == lab.e && code.s == code.e) {
-	    com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
 	    adj_max_line_length = opt.block_comment_max_line_length;
+	    com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
 	    if (com_ind <= 0)
 		com_ind = opt.format_col1_comments ? 0 : 1;
 
@@ -150,20 +150,16 @@ analyze_comment(bool *p_may_wrap, bool *
     if (!may_wrap) {
 	/*
 	 * Find out how much indentation there was originally, because that
-	 * much will have to be ignored by dump_line(). This is a box comment,
-	 * so nothing changes -- not even indentation.
+	 * much will have to be ignored by dump_line().
 	 *
 	 * The comment we're about to read usually comes from inp.buf, unless
 	 * it has been copied into save_com.
-	 */
-	const char *start;
-
-	/*
+	 *
 	 * XXX: ordered comparison between pointers from different objects
 	 * invokes undefined behavior (C99 6.5.8).
 	 */
-	start = inp.s >= sc_buf && inp.s < sc_buf + sc_size ?
-	    sc_buf : inp.buf;
+	const char *start = inp.s >= sc_buf && inp.s < sc_buf + sc_size
+	    ? sc_buf : inp.buf;
 	ps.n_comment_delta = -ind_add(0, start, inp.s - 2);
     } else {
 	ps.n_comment_delta = 0;
@@ -174,6 +170,8 @@ analyze_comment(bool *p_may_wrap, bool *
     ps.comment_delta = 0;
     com_add_char('/');
     com_add_char(token.e[-1]);	/* either '*' or '/' */
+
+    /* TODO: Maybe preserve a single '\t' as well. */
     if (*inp.s != ' ' && may_wrap)
 	com_add_char(' ');
 
@@ -227,7 +225,7 @@ copy_comment_wrap(int adj_max_line_lengt
 	    last_blank = -1;
 	    if (ps.next_col_1) {
 		if (com.s == com.e)
-		    com_add_char(' ');
+		    com_add_char(' ');	/* force empty line of output */
 		if (com.e - com.s > 3) {
 		    dump_line();
 		    com_add_delim();
@@ -264,7 +262,7 @@ copy_comment_wrap(int adj_max_line_lengt
 		inp_skip();
 
 		if (break_delim) {
-		    if (com.e > com.s + 3)
+		    if (com.e - com.s > 3)
 			dump_line();
 		    else
 			com.e = com.s;

Reply via email to