Module Name:    src
Committed By:   rillig
Date:           Sun Apr 18 08:52:04 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: err.c externs1.h lint1.h

Log Message:
lint: add error_at, warning_at, message_at

Right now there are several places in the code that use the global
variable curr_pos for passing a parameter to the diagnostic functions.
That's not what global variables are for.

Make it easy for the code to migrate to the parameter-passing style.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.106 -r1.107 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.96 -r1.97 src/usr.bin/xlint/lint1/lint1.h

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/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.112 src/usr.bin/xlint/lint1/err.c:1.113
--- src/usr.bin/xlint/lint1/err.c:1.112	Sun Apr 18 08:07:04 2021
+++ src/usr.bin/xlint/lint1/err.c	Sun Apr 18 08:52:04 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.112 2021/04/18 08:07:04 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.113 2021/04/18 08:52:04 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: err.c,v 1.112 2021/04/18 08:07:04 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.113 2021/04/18 08:52:04 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -484,15 +484,15 @@ lbasename(const char *path)
 }
 
 static void
-verror(int n, va_list ap)
+verror_at(pos_t pos, int n, va_list ap)
 {
 	const	char *fn;
 
 	if (ERR_ISSET(n, &msgset))
 		return;
 
-	fn = lbasename(curr_pos.p_file);
-	(void)printf("%s(%d): error: ", fn, curr_pos.p_line);
+	fn = lbasename(pos.p_file);
+	(void)printf("%s(%d): error: ", fn, pos.p_line);
 	(void)vprintf(msgs[n], ap);
 	(void)printf(" [%d]\n", n);
 	nerr++;
@@ -500,7 +500,7 @@ verror(int n, va_list ap)
 }
 
 static void
-vwarning(int n, va_list ap)
+vwarning_at(pos_t pos, int n, va_list ap)
 {
 	const	char *fn;
 
@@ -514,8 +514,8 @@ vwarning(int n, va_list ap)
 		/* this warning is suppressed by a LINTED comment */
 		return;
 
-	fn = lbasename(curr_pos.p_file);
-	(void)printf("%s(%d): warning: ", fn, curr_pos.p_line);
+	fn = lbasename(pos.p_file);
+	(void)printf("%s(%d): warning: ", fn, pos.p_line);
 	(void)vprintf(msgs[n], ap);
 	(void)printf(" [%d]\n", n);
 	if (wflag)
@@ -523,13 +523,38 @@ vwarning(int n, va_list ap)
 	print_stack_trace();
 }
 
+static void
+vmessage_at(int n, pos_t pos, va_list ap)
+{
+	const char *fn;
+
+	if (ERR_ISSET(n, &msgset))
+		return;
+
+	fn = lbasename(pos.p_file);
+	(void)printf("%s(%d): ", fn, pos.p_line);
+	(void)vprintf(msgs[n], ap);
+	(void)printf(" [%d]\n", n);
+	print_stack_trace();
+}
+
+void
+(error_at)(int n, pos_t pos, ...)
+{
+	va_list	ap;
+
+	va_start(ap, pos);
+	verror_at(pos, n, ap);
+	va_end(ap);
+}
+
 void
 (error)(int n, ...)
 {
 	va_list	ap;
 
 	va_start(ap, n);
-	verror(n, ap);
+	verror_at(curr_pos, n, ap);
 	va_end(ap);
 }
 
@@ -564,12 +589,32 @@ assert_failed(const char *file, int line
 }
 
 void
+(warning_at)(int n, pos_t pos, ...)
+{
+	va_list	ap;
+
+	va_start(ap, pos);
+	vwarning_at(pos, n, ap);
+	va_end(ap);
+}
+
+void
 (warning)(int n, ...)
 {
 	va_list	ap;
 
 	va_start(ap, n);
-	vwarning(n, ap);
+	vwarning_at(curr_pos, n, ap);
+	va_end(ap);
+}
+
+void
+(message_at)(int n, pos_t pos, ...)
+{
+	va_list ap;
+
+	va_start(ap, pos);
+	vmessage_at(n, pos, ap);
 	va_end(ap);
 }
 
@@ -577,18 +622,10 @@ void
 (message)(int n, ...)
 {
 	va_list	ap;
-	const	char *fn;
-
-	if (ERR_ISSET(n, &msgset))
-		return;
 
 	va_start(ap, n);
-	fn = lbasename(curr_pos.p_file);
-	(void)printf("%s(%d): ", fn, curr_pos.p_line);
-	(void)vprintf(msgs[n], ap);
-	(void)printf(" [%d]\n", n);
+	vmessage_at(n, curr_pos, ap);
 	va_end(ap);
-	print_stack_trace();
 }
 
 /*
@@ -605,9 +642,9 @@ void
 
 	va_start(ap, n);
 	if (sflag && !extensions_ok) {
-		verror(n, ap);
+		verror_at(curr_pos, n, ap);
 	} else if (sflag || !extensions_ok) {
-		vwarning(n, ap);
+		vwarning_at(curr_pos, n, ap);
 	}
 	va_end(ap);
 }
@@ -620,7 +657,7 @@ void
 	if (c11flag || gflag)
 		return;
 	va_start(ap, n);
-	verror(n, ap);
+	verror_at(curr_pos, n, ap);
 	va_end(ap);
 }
 
@@ -631,9 +668,9 @@ void
 
 	va_start(ap, n);
 	if (sflag && !gflag) {
-		verror(n, ap);
+		verror_at(curr_pos, n, ap);
 	} else if (sflag || !gflag) {
-		vwarning(n, ap);
+		vwarning_at(curr_pos, n, ap);
 	}
 	va_end(ap);
 }

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.106 src/usr.bin/xlint/lint1/externs1.h:1.107
--- src/usr.bin/xlint/lint1/externs1.h:1.106	Sun Apr 18 08:07:04 2021
+++ src/usr.bin/xlint/lint1/externs1.h	Sun Apr 18 08:52:04 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.106 2021/04/18 08:07:04 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.107 2021/04/18 08:52:04 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -120,6 +120,9 @@ extern	int	sytxerr;
 extern	const char *const msgs[];
 
 extern	void	msglist(void);
+extern	void	error_at(int, pos_t, ...);
+extern	void	warning_at(int, pos_t, ...);
+extern	void	message_at(int, pos_t, ...);
 extern	void	error(int, ...);
 extern	void	warning(int, ...);
 extern	void	message(int, ...);

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.96 src/usr.bin/xlint/lint1/lint1.h:1.97
--- src/usr.bin/xlint/lint1/lint1.h:1.96	Sun Apr 18 07:31:47 2021
+++ src/usr.bin/xlint/lint1/lint1.h	Sun Apr 18 08:52:04 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.96 2021/04/18 07:31:47 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.97 2021/04/18 08:52:04 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -476,6 +476,19 @@ check_printf(const char *fmt, ...)
 {
 }
 
+#  define wrap_check_printf_at(func, id, pos, args...)			\
+	do {								\
+		check_printf(__CONCAT(MSG_, id), ##args);		\
+		(func)(id, pos, ##args);				\
+	} while (/*CONSTCOND*/false)
+
+#  define error_at(id, pos, args...) \
+	wrap_check_printf_at(error_at, id, pos, ##args)
+#  define warning_at(id, pos, args...) \
+	wrap_check_printf_at(warning_at, id, pos, ##args)
+#  define message_at(id, pos, args...) \
+	wrap_check_printf_at(message_at, id, pos, ##args)
+
 #  define wrap_check_printf(func, id, args...)				\
 	do {								\
 		check_printf(__CONCAT(MSG_, id), ##args);		\

Reply via email to