Module Name:    src
Committed By:   rillig
Date:           Sun Jun 12 13:37:32 UTC 2022

Modified Files:
        src/usr.bin/make: make.h parse.c var.c

Log Message:
make: reorganize Parse_Error

Determining the location where the error occurred is now done by
ParseVErrorInternal.  This frees the remaining code from keeping the
filename and the line number together.  It also makes Parse_Error short
enough that it might be worth providing a separate function for each of
the 3 log levels.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.302 -r1.303 src/usr.bin/make/make.h
cvs rdiff -u -r1.679 -r1.680 src/usr.bin/make/parse.c
cvs rdiff -u -r1.1021 -r1.1022 src/usr.bin/make/var.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/make/make.h
diff -u src/usr.bin/make/make.h:1.302 src/usr.bin/make/make.h:1.303
--- src/usr.bin/make/make.h:1.302	Sat May  7 17:49:47 2022
+++ src/usr.bin/make/make.h	Sun Jun 12 13:37:32 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.h,v 1.302 2022/05/07 17:49:47 rillig Exp $	*/
+/*	$NetBSD: make.h,v 1.303 2022/06/12 13:37:32 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -852,7 +852,7 @@ bool GetBooleanExpr(const char *, bool);
 void Parse_Init(void);
 void Parse_End(void);
 
-void PrintLocation(FILE *, bool, const char *, unsigned);
+void PrintLocation(FILE *, bool, const GNode *);
 void PrintStackTrace(bool);
 void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
 bool Parse_VarAssign(const char *, bool, GNode *) MAKE_ATTR_USE;

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.679 src/usr.bin/make/parse.c:1.680
--- src/usr.bin/make/parse.c:1.679	Sat Jun 11 17:58:15 2022
+++ src/usr.bin/make/parse.c	Sun Jun 12 13:37:32 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.679 2022/06/11 17:58:15 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.680 2022/06/12 13:37:32 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -105,7 +105,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.679 2022/06/11 17:58:15 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.680 2022/06/12 13:37:32 rillig Exp $");
 
 /*
  * A file being read.
@@ -436,10 +436,22 @@ FindKeyword(const char *str)
 }
 
 void
-PrintLocation(FILE *f, bool useVars, const char *fname, unsigned lineno)
+PrintLocation(FILE *f, bool useVars, const GNode *gn)
 {
 	char dirbuf[MAXPATHLEN + 1];
 	FStr dir, base;
+	const char *fname;
+	unsigned lineno;
+
+	if (gn != NULL) {
+		fname = gn->fname;
+		lineno = gn->lineno;
+	} else if (includes.len > 0) {
+		IncludedFile *curFile = CurFile();
+		fname = curFile->name.str;
+		lineno = curFile->lineno;
+	} else
+		return;
 
 	if (!useVars || fname[0] == '/' || strcmp(fname, "(stdin)") == 0) {
 		(void)fprintf(f, "\"%s\" line %u: ", fname, lineno);
@@ -462,16 +474,15 @@ PrintLocation(FILE *f, bool useVars, con
 	FStr_Done(&dir);
 }
 
-static void MAKE_ATTR_PRINTFLIKE(6, 0)
-ParseVErrorInternal(FILE *f, bool useVars, const char *fname, unsigned lineno,
+static void MAKE_ATTR_PRINTFLIKE(5, 0)
+ParseVErrorInternal(FILE *f, bool useVars, const GNode *gn,
 		    ParseErrorLevel level, const char *fmt, va_list ap)
 {
 	static bool fatal_warning_error_printed = false;
 
 	(void)fprintf(f, "%s: ", progname);
 
-	if (fname != NULL)
-		PrintLocation(f, useVars, fname, lineno);
+	PrintLocation(f, useVars, gn);
 	if (level == PARSE_WARNING)
 		(void)fprintf(f, "warning: ");
 	(void)vfprintf(f, fmt, ap);
@@ -492,20 +503,20 @@ ParseVErrorInternal(FILE *f, bool useVar
 		PrintStackTrace(false);
 }
 
-static void MAKE_ATTR_PRINTFLIKE(4, 5)
-ParseErrorInternal(const char *fname, unsigned lineno,
+static void MAKE_ATTR_PRINTFLIKE(3, 4)
+ParseErrorInternal(const GNode *gn,
 		   ParseErrorLevel level, const char *fmt, ...)
 {
 	va_list ap;
 
 	(void)fflush(stdout);
 	va_start(ap, fmt);
-	ParseVErrorInternal(stderr, false, fname, lineno, level, fmt, ap);
+	ParseVErrorInternal(stderr, false, gn, level, fmt, ap);
 	va_end(ap);
 
 	if (opts.debug_file != stdout && opts.debug_file != stderr) {
 		va_start(ap, fmt);
-		ParseVErrorInternal(opts.debug_file, false, fname, lineno,
+		ParseVErrorInternal(opts.debug_file, false, gn,
 		    level, fmt, ap);
 		va_end(ap);
 	}
@@ -523,26 +534,15 @@ void
 Parse_Error(ParseErrorLevel level, const char *fmt, ...)
 {
 	va_list ap;
-	const char *fname;
-	unsigned lineno;
-
-	if (includes.len == 0) {
-		fname = NULL;
-		lineno = 0;
-	} else {
-		IncludedFile *curFile = CurFile();
-		fname = curFile->name.str;
-		lineno = curFile->lineno;
-	}
 
 	(void)fflush(stdout);
 	va_start(ap, fmt);
-	ParseVErrorInternal(stderr, true, fname, lineno, level, fmt, ap);
+	ParseVErrorInternal(stderr, true, NULL, level, fmt, ap);
 	va_end(ap);
 
 	if (opts.debug_file != stdout && opts.debug_file != stderr) {
 		va_start(ap, fmt);
-		ParseVErrorInternal(opts.debug_file, true, fname, lineno,
+		ParseVErrorInternal(opts.debug_file, true, NULL,
 		    level, fmt, ap);
 		va_end(ap);
 	}
@@ -1921,7 +1921,7 @@ GNode_AddCommand(GNode *gn, char *cmd)
 		Parse_Error(PARSE_WARNING,
 		    "duplicate script for target \"%s\" ignored",
 		    gn->name);
-		ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
+		ParseErrorInternal(gn, PARSE_WARNING,
 		    "using previous script for \"%s\" defined here",
 		    gn->name);
 #endif

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1021 src/usr.bin/make/var.c:1.1022
--- src/usr.bin/make/var.c:1.1021	Sat May 14 12:25:16 2022
+++ src/usr.bin/make/var.c	Sun Jun 12 13:37:32 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1021 2022/05/14 12:25:16 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1022 2022/06/12 13:37:32 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1021 2022/05/14 12:25:16 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1022 2022/06/12 13:37:32 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -4523,8 +4523,7 @@ Var_Parse(const char **pp, GNode *scope,
 	if (v->inUse) {
 		if (scope->fname != NULL) {
 			fprintf(stderr, "In a command near ");
-			PrintLocation(stderr, false,
-			    scope->fname, scope->lineno);
+			PrintLocation(stderr, false, scope);
 		}
 		Fatal("Variable %s is recursive.", v->name.str);
 	}

Reply via email to