Module Name:    src
Committed By:   rillig
Date:           Mon Jul 27 21:54:25 UTC 2020

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: modmisc.exp modmisc.mk

Log Message:
make(1): replace macros with functions

Having the hidden parameter st->endc in the macro made it unnecessarily
difficult to understand the code.


To generate a diff of this commit:
cvs rdiff -u -r1.339 -r1.340 src/usr.bin/make/var.c
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/make/unit-tests/modmisc.exp
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/make/unit-tests/modmisc.mk

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/var.c
diff -u src/usr.bin/make/var.c:1.339 src/usr.bin/make/var.c:1.340
--- src/usr.bin/make/var.c:1.339	Mon Jul 27 21:08:41 2020
+++ src/usr.bin/make/var.c	Mon Jul 27 21:54:25 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.339 2020/07/27 21:08:41 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.340 2020/07/27 21:54:25 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.339 2020/07/27 21:08:41 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.340 2020/07/27 21:54:25 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.339 2020/07/27 21:08:41 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.340 2020/07/27 21:54:25 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2067,12 +2067,21 @@ typedef struct {
 } ApplyModifiersState;
 
 /* we now have some modifiers with long names */
-#define STRMOD_MATCH(s, want, n) \
-    (strncmp(s, want, n) == 0 && (s[n] == st->endc || s[n] == ':'))
-#define STRMOD_MATCHX(s, want, n) \
-    (strncmp(s, want, n) == 0 && \
-     (s[n] == st->endc || s[n] == ':' || s[n] == '='))
-#define CHARMOD_MATCH(c) (c == st->endc || c == ':')
+static Boolean
+ModMatch(const char *mod, const char *modname, char endc)
+{
+    size_t n = strlen(modname);
+    return strncmp(mod, modname, n) == 0 &&
+	   (mod[n] == endc || mod[n] == ':');
+}
+
+static inline Boolean
+ModMatchEq(const char *mod, const char *modname, char endc)
+{
+    size_t n = strlen(modname);
+    return strncmp(mod, modname, n) == 0 &&
+	   (mod[n] == endc || mod[n] == ':' || mod[n] == '=');
+}
 
 /* :@var@...${var}...@ */
 static Boolean
@@ -2179,13 +2188,14 @@ ApplyModifier_Defined(const char *mod, A
 static Boolean
 ApplyModifier_Gmtime(const char *mod, ApplyModifiersState *st)
 {
-    time_t utc;
-    char *ep;
-
-    st->cp = mod + 1;		/* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "gmtime", 6))
+    if (!ModMatchEq(mod, "gmtime", st->endc)) {
+	st->cp = mod + 1;
 	return FALSE;
+    }
+
+    time_t utc;
     if (mod[6] == '=') {
+	char *ep;
 	utc = strtoul(mod + 7, &ep, 10);
 	st->cp = ep;
     } else {
@@ -2201,14 +2211,14 @@ ApplyModifier_Gmtime(const char *mod, Ap
 static Boolean
 ApplyModifier_Localtime(const char *mod, ApplyModifiersState *st)
 {
-    time_t utc;
-    char *ep;
-
-    st->cp = mod + 1;	/* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "localtime", 9))
+    if (!ModMatchEq(mod, "localtime", st->endc)) {
+	st->cp = mod + 1;
 	return FALSE;
+    }
 
+    time_t utc;
     if (mod[9] == '=') {
+	char *ep;
 	utc = strtoul(mod + 10, &ep, 10);
 	st->cp = ep;
     } else {
@@ -2224,9 +2234,11 @@ ApplyModifier_Localtime(const char *mod,
 static Boolean
 ApplyModifier_Hash(const char *mod, ApplyModifiersState *st)
 {
-    st->cp = mod + 1;	/* make sure it is set */
-    if (!STRMOD_MATCH(mod, "hash", 4))
+    if (!ModMatch(mod, "hash", st->endc)) {
+	st->cp = mod + 1;
 	return FALSE;
+    }
+
     st->newStr = VarHash(st->nstr);
     st->cp = mod + 4;
     st->termc = *st->cp;
@@ -2286,14 +2298,14 @@ ApplyModifier_Exclam(const char *mod, Ap
 static Boolean
 ApplyModifier_Range(const char *mod, ApplyModifiersState *st)
 {
-    int n;
-    char *ep;
-
-    st->cp = mod + 1;	/* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "range", 5))
+    if (!ModMatchEq(mod, "range", st->endc)) {
+	st->cp = mod + 1;
 	return FALSE;
+    }
 
+    int n;
     if (mod[5] == '=') {
+	char *ep;
 	n = strtoul(mod + 6, &ep, 10);
 	st->cp = ep;
     } else {
@@ -2910,23 +2922,20 @@ ApplyModifier_Assign(const char *mod, Ap
 static Boolean
 ApplyModifier_Remember(const char *mod, ApplyModifiersState *st)
 {
-    st->cp = mod + 1;	/* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "_", 1))
+    if (!ModMatchEq(mod, "_", st->endc)) {
+	st->cp = mod + 1;
 	return FALSE;
+    }
 
     if (mod[1] == '=') {
-	char *np;
-	int n;
-
-	st->cp++;
-	n = strcspn(st->cp, ":)}");
-	np = bmake_strndup(st->cp, n + 1);
-	np[n] = '\0';
+	size_t n = strcspn(mod + 2, ":)}");
+	char *name = bmake_strndup(mod + 2, n);
+	Var_Set(name, st->nstr, st->ctxt);
+	free(name);
 	st->cp = mod + 2 + n;
-	Var_Set(np, st->nstr, st->ctxt);
-	free(np);
     } else {
 	Var_Set("_", st->nstr, st->ctxt);
+	st->cp = mod + 1;
     }
     st->newStr = st->nstr;
     st->termc = *st->cp;

Index: src/usr.bin/make/unit-tests/modmisc.exp
diff -u src/usr.bin/make/unit-tests/modmisc.exp:1.29 src/usr.bin/make/unit-tests/modmisc.exp:1.30
--- src/usr.bin/make/unit-tests/modmisc.exp:1.29	Sun Jul 26 11:39:55 2020
+++ src/usr.bin/make/unit-tests/modmisc.exp	Mon Jul 27 21:54:25 2020
@@ -91,4 +91,6 @@ mod-quote: new
 
 line
 mod-break-many-words: 500
+mod-remember: 1 2 3 1 2 3 1 2 3
+mod-remember: 1 2 3, SAVED=3
 exit status 0

Index: src/usr.bin/make/unit-tests/modmisc.mk
diff -u src/usr.bin/make/unit-tests/modmisc.mk:1.26 src/usr.bin/make/unit-tests/modmisc.mk:1.27
--- src/usr.bin/make/unit-tests/modmisc.mk:1.26	Sun Jul 26 13:09:53 2020
+++ src/usr.bin/make/unit-tests/modmisc.mk	Mon Jul 27 21:54:25 2020
@@ -1,4 +1,4 @@
-# $Id: modmisc.mk,v 1.26 2020/07/26 13:09:53 rillig Exp $
+# $Id: modmisc.mk,v 1.27 2020/07/27 21:54:25 rillig Exp $
 #
 # miscellaneous modifier tests
 
@@ -27,6 +27,7 @@ all:	mod-assign-nested
 all:	mod-tu-space
 all:	mod-quote
 all:	mod-break-many-words
+all:	mod-remember
 
 # See also sysv.mk.
 modsysv:
@@ -242,3 +243,11 @@ mod-quote:
 # Cover the bmake_realloc in brk_string.
 mod-break-many-words:
 	@echo $@: ${UNDEF:U:range=500:[#]}
+
+# Demonstrate the :_ modifier.
+# In the parameterized form, having the variable name on the right side
+# of the = assignment operator is confusing. Luckily this modifier is
+# only rarely needed.
+mod-remember:
+	@echo $@: ${1 2 3:L:_:@var@${_}@}
+	@echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}

Reply via email to