Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): replace macros with functions



details:   https://anonhg.NetBSD.org/src/rev/de8d42745086
branches:  trunk
changeset: 936491:de8d42745086
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Jul 27 21:54:25 2020 +0000

description:
make(1): replace macros with functions

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

diffstat:

 usr.bin/make/unit-tests/modmisc.exp |   2 +
 usr.bin/make/unit-tests/modmisc.mk  |  11 ++++-
 usr.bin/make/var.c                  |  87 ++++++++++++++++++++----------------
 3 files changed, 60 insertions(+), 40 deletions(-)

diffs (201 lines):

diff -r 85bac34dd0c8 -r de8d42745086 usr.bin/make/unit-tests/modmisc.exp
--- a/usr.bin/make/unit-tests/modmisc.exp       Mon Jul 27 21:08:41 2020 +0000
+++ b/usr.bin/make/unit-tests/modmisc.exp       Mon Jul 27 21:54:25 2020 +0000
@@ -91,4 +91,6 @@
 
 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
diff -r 85bac34dd0c8 -r de8d42745086 usr.bin/make/unit-tests/modmisc.mk
--- a/usr.bin/make/unit-tests/modmisc.mk        Mon Jul 27 21:08:41 2020 +0000
+++ b/usr.bin/make/unit-tests/modmisc.mk        Mon Jul 27 21:54:25 2020 +0000
@@ -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-tu-space
 all:   mod-quote
 all:   mod-break-many-words
+all:   mod-remember
 
 # See also sysv.mk.
 modsysv:
@@ -242,3 +243,11 @@
 # 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}
diff -r 85bac34dd0c8 -r de8d42745086 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Mon Jul 27 21:08:41 2020 +0000
+++ b/usr.bin/make/var.c        Mon Jul 27 21:54:25 2020 +0000
@@ -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 @@
 } 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 @@
 static Boolean
 ApplyModifier_Gmtime(const char *mod, ApplyModifiersState *st)
 {
+    if (!ModMatchEq(mod, "gmtime", st->endc)) {
+       st->cp = mod + 1;
+       return FALSE;
+    }
+
     time_t utc;
-    char *ep;
-
-    st->cp = mod + 1;          /* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "gmtime", 6))
-       return FALSE;
     if (mod[6] == '=') {
+       char *ep;
        utc = strtoul(mod + 7, &ep, 10);
        st->cp = ep;
     } else {
@@ -2201,14 +2211,14 @@
 static Boolean
 ApplyModifier_Localtime(const char *mod, ApplyModifiersState *st)
 {
+    if (!ModMatchEq(mod, "localtime", st->endc)) {
+       st->cp = mod + 1;
+       return FALSE;
+    }
+
     time_t utc;
-    char *ep;
-
-    st->cp = mod + 1;  /* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "localtime", 9))
-       return FALSE;
-
     if (mod[9] == '=') {
+       char *ep;
        utc = strtoul(mod + 10, &ep, 10);
        st->cp = ep;
     } else {
@@ -2224,9 +2234,11 @@
 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 @@
 static Boolean
 ApplyModifier_Range(const char *mod, ApplyModifiersState *st)
 {
+    if (!ModMatchEq(mod, "range", st->endc)) {
+       st->cp = mod + 1;
+       return FALSE;
+    }
+
     int n;
-    char *ep;
-
-    st->cp = mod + 1;  /* make sure it is set */
-    if (!STRMOD_MATCHX(mod, "range", 5))
-       return FALSE;
-
     if (mod[5] == '=') {
+       char *ep;
        n = strtoul(mod + 6, &ep, 10);
        st->cp = ep;
     } else {
@@ -2910,23 +2922,20 @@
 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;



Home | Main Index | Thread Index | Old Index