Module Name:    src
Committed By:   rillig
Date:           Sun Sep 13 13:17:44 UTC 2020

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

Log Message:
make(1): reduce complexity of PrintLocation

Analyzing a printf statement that is split into several small pieces is
harder than necessary in this case.  Joing the printf statements into
simple strings.  While here, remove the needless casts to int and add a
few empty lines for visual guidance.


To generate a diff of this commit:
cvs rdiff -u -r1.298 -r1.299 src/usr.bin/make/parse.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/parse.c
diff -u src/usr.bin/make/parse.c:1.298 src/usr.bin/make/parse.c:1.299
--- src/usr.bin/make/parse.c:1.298	Sun Sep 13 09:43:01 2020
+++ src/usr.bin/make/parse.c	Sun Sep 13 13:17:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.298 2020/09/13 09:43:01 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.299 2020/09/13 13:17:44 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: parse.c,v 1.298 2020/09/13 09:43:01 rillig Exp $";
+static char rcsid[] = "$NetBSD: parse.c,v 1.299 2020/09/13 13:17:44 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)parse.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: parse.c,v 1.298 2020/09/13 09:43:01 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.299 2020/09/13 13:17:44 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -614,37 +614,32 @@ static void
 PrintLocation(FILE *f, const char *cfname, size_t clineno)
 {
 	char dirbuf[MAXPATHLEN+1];
+	const char *dir, *fname;
+	char *dir_freeIt, *fname_freeIt;
 
-	(void)fprintf(f, "\"");
-	if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
-		char *cp, *cp2;
-		const char *dir, *fname;
+	if (*cfname == '/' || strcmp(cfname, "(stdin)") == 0) {
+		(void)fprintf(f, "\"%s\" line %zu: ", cfname, clineno);
+		return;
+	}
 
-		/*
-		 * Nothing is more annoying than not knowing
-		 * which Makefile is the culprit; we try ${.PARSEDIR}
-		 * and apply realpath(3) if not absolute.
-		 */
-		dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
-		if (dir == NULL)
-			dir = ".";
-		if (*dir != '/') {
-			dir = realpath(dir, dirbuf);
-		}
-		fname = Var_Value(".PARSEFILE", VAR_GLOBAL, &cp2);
-		if (fname == NULL) {
-			if ((fname = strrchr(cfname, '/')))
-				fname++;
-			else
-				fname = cfname;
-		}
-		(void)fprintf(f, "%s/%s", dir, fname);
-		bmake_free(cp2);
-		bmake_free(cp);
-	} else
-		(void)fprintf(f, "%s", cfname);
+	/* Find out which makefile is the culprit.
+	 * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
 
-	(void)fprintf(f, "\" line %d: ", (int)clineno);
+	dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &dir_freeIt);
+	if (dir == NULL)
+		dir = ".";
+	if (*dir != '/')
+		dir = realpath(dir, dirbuf);
+
+	fname = Var_Value(".PARSEFILE", VAR_GLOBAL, &fname_freeIt);
+	if (fname == NULL) {
+		const char *slash = strrchr(cfname, '/');
+		fname = slash != NULL ? slash + 1 : cfname;
+	}
+
+	(void)fprintf(f, "\"%s/%s\" line %zu: ", dir, fname, clineno);
+	bmake_free(fname_freeIt);
+	bmake_free(dir_freeIt);
 }
 
 /*-

Reply via email to