Module Name:    src
Committed By:   rillig
Date:           Sun Dec  3 21:03:58 UTC 2023

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

Log Message:
indent: use line number of the token start in diagnostics

Previously, the line number of the end of the token was used, which was
confusing in debug mode.


To generate a diff of this commit:
cvs rdiff -u -r1.387 -r1.388 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.232 -r1.233 src/usr.bin/indent/io.c
cvs rdiff -u -r1.240 -r1.241 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.171 -r1.172 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.387 src/usr.bin/indent/indent.c:1.388
--- src/usr.bin/indent/indent.c:1.387	Tue Jun 27 04:41:23 2023
+++ src/usr.bin/indent/indent.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.387 2023/06/27 04:41:23 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.387 2023/06/27 04:41:23 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -80,7 +80,6 @@ struct buffer com;
 
 bool found_err;
 bool had_eof;
-int line_no = 1;
 
 static struct {
 	struct parser_state *item;
@@ -158,7 +157,7 @@ diag(int level, const char *msg, ...)
 
 	va_start(ap, msg);
 	fprintf(stderr, "%s: %s:%d: ",
-	    level == 0 ? "warning" : "error", in_name, line_no);
+	    level == 0 ? "warning" : "error", in_name, token_start_line_no);
 	vfprintf(stderr, msg, ap);
 	fprintf(stderr, "\n");
 	va_end(ap);
@@ -580,7 +579,7 @@ process_newline(void)
 	output_line();
 
 stay_in_line:
-	line_no++;
+	token_end_line_no++;
 }
 
 static bool
@@ -1109,7 +1108,8 @@ indent(void)
 		lexer_symbol lsym = lexi();
 
 		debug_blank_line();
-		debug_printf("line %d: %s", line_no, lsym_name[lsym]);
+		debug_printf("line %d: %s",
+		    token_start_line_no, lsym_name[lsym]);
 		debug_print_buf("token", &token);
 		debug_buffers();
 		debug_blank_line();

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.204 src/usr.bin/indent/indent.h:1.205
--- src/usr.bin/indent/indent.h:1.204	Mon Jun 26 20:03:09 2023
+++ src/usr.bin/indent/indent.h	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.205 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -249,7 +249,8 @@ extern struct options {
 
 extern bool found_err;
 extern bool had_eof;		/* whether input is exhausted */
-extern int line_no;		/* the current input line number */
+extern int token_start_line_no;
+extern int token_end_line_no;
 extern enum indent_enabled {
 	indent_on,
 	indent_off,

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.232 src/usr.bin/indent/io.c:1.233
--- src/usr.bin/indent/io.c:1.232	Tue Jun 27 04:41:23 2023
+++ src/usr.bin/indent/io.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.232 2023/06/27 04:41:23 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.232 2023/06/27 04:41:23 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $");
 
 #include <stdio.h>
 
@@ -46,6 +46,8 @@ __RCSID("$NetBSD: io.c,v 1.232 2023/06/2
 
 struct buffer inp;
 const char *inp_p;
+int token_start_line_no;
+int token_end_line_no = 1;
 
 struct output_state out;
 enum indent_enabled indent_enabled;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.240 src/usr.bin/indent/lexi.c:1.241
--- src/usr.bin/indent/lexi.c:1.240	Sun Dec  3 20:42:31 2023
+++ src/usr.bin/indent/lexi.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.241 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.241 2023/12/03 21:03:58 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -188,7 +188,7 @@ skip_line_continuation(void)
 	if (inp_p[0] == '\\' && inp_p[1] == '\n') {
 		inp_p++;
 		inp_skip();
-		line_no++;
+		token_end_line_no++;
 		return true;
 	}
 	return false;
@@ -245,7 +245,7 @@ lex_char_or_string(void)
 
 		if (token.s[token.len - 1] == '\\') {
 			if (*inp_p == '\n')
-				line_no++;
+				token_end_line_no++;
 			token_add_char(inp_next());
 		}
 	}
@@ -524,7 +524,7 @@ lex_asterisk_unary(void)
 		if (*inp_p == '*')
 			token_add_char('*');
 		if (*inp_p == '\n')
-			line_no++;
+			token_end_line_no++;
 		inp_skip();
 	}
 
@@ -579,6 +579,7 @@ lexi(void)
 		else
 			break;
 	}
+	token_start_line_no = token_end_line_no;
 
 	lexer_symbol alnum_lsym = lexi_alnum();
 	if (alnum_lsym != lsym_eof)

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.171 src/usr.bin/indent/pr_comment.c:1.172
--- src/usr.bin/indent/pr_comment.c:1.171	Fri Jun 23 20:59:04 2023
+++ src/usr.bin/indent/pr_comment.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.171 2023/06/23 20:59:04 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.172 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.171 2023/06/23 20:59:04 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.172 2023/12/03 21:03:58 rillig Exp $");
 
 #include <string.h>
 
@@ -227,7 +227,7 @@ copy_comment_wrap_newline(ssize_t *last_
 			com_add_char(' ');
 		*last_blank = (int)com.len - 1;
 	}
-	line_no++;
+	token_end_line_no++;
 
 	/* flush any blanks and/or tabs at start of next line */
 	inp_skip();		/* '\n' */
@@ -299,6 +299,7 @@ copy_comment_wrap(int line_length, bool 
 	return;
 
 unterminated_comment:
+	token_start_line_no = token_end_line_no;
 	diag(1, "Unterminated comment");
 	output_line();
 }
@@ -314,13 +315,14 @@ copy_comment_nowrap(void)
 				return;
 
 			if (had_eof) {
+				token_start_line_no = token_end_line_no;
 				diag(1, "Unterminated comment");
 				output_line();
 				return;
 			}
 
 			output_line();
-			line_no++;
+			token_end_line_no++;
 			inp_skip();
 			continue;
 		}

Reply via email to