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): use specialized return type for ApplyM...



details:   https://anonhg.NetBSD.org/src/rev/32201806b45c
branches:  trunk
changeset: 936550:32201806b45c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jul 29 20:57:31 2020 +0000

description:
make(1): use specialized return type for ApplyModifier functions

This makes it immediately obvious what happens after a modifier has been
applied, instead of having to translate single-character mnemonics or
booleans to their actual intention.

This also reduces the size of the binary since there are fewer jumps.

diffstat:

 usr.bin/make/Makefile |    9 +-
 usr.bin/make/var.c    |  313 ++++++++++++++++++++++++-------------------------
 2 files changed, 161 insertions(+), 161 deletions(-)

diffs (truncated from 821 to 300 lines):

diff -r 7f77f3b3ba33 -r 32201806b45c usr.bin/make/Makefile
--- a/usr.bin/make/Makefile     Wed Jul 29 20:33:38 2020 +0000
+++ b/usr.bin/make/Makefile     Wed Jul 29 20:57:31 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.75 2020/07/26 20:04:57 rillig Exp $
+#      $NetBSD: Makefile,v 1.76 2020/07/29 20:57:31 rillig Exp $
 #      @(#)Makefile    5.2 (Berkeley) 12/28/90
 
 PROG=  make
@@ -77,3 +77,10 @@
 
 accept: .MAKE
        cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
+
+retest:
+       ${.MAKE} -C ${.CURDIR}/unit-tests cleandir
+.if ${USE_COVERAGE} == yes
+       rm -f *.gcov *.gcda
+.endif
+       ${.MAKE} test
diff -r 7f77f3b3ba33 -r 32201806b45c usr.bin/make/var.c
--- a/usr.bin/make/var.c        Wed Jul 29 20:33:38 2020 +0000
+++ b/usr.bin/make/var.c        Wed Jul 29 20:57:31 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.355 2020/07/29 20:33:38 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.356 2020/07/29 20:57:31 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.355 2020/07/29 20:33:38 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.356 2020/07/29 20:57:31 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.355 2020/07/29 20:33:38 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.356 2020/07/29 20:57:31 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2066,6 +2066,14 @@
 
 } ApplyModifiersState;
 
+typedef enum {
+    AMR_OK,                    /* Continue parsing */
+    AMR_UNKNOWN,               /* Not a match, try others as well */
+    AMR_BAD,                   /* Error out with message */
+    AMR_CLEANUP                        /* Error out with "missing delimiter",
+                                * if st->missing_delim is set. */
+} ApplyModifierResult;
+
 /* we now have some modifiers with long names */
 static Boolean
 ModMatch(const char *mod, const char *modname, char endc)
@@ -2084,7 +2092,7 @@
 }
 
 /* :@var@...${var}...@ */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Loop(const char *mod, ApplyModifiersState *st) {
     ModifyWord_LoopArgs args;
 
@@ -2095,14 +2103,14 @@
                                  st->ctxt, NULL, NULL, NULL);
     if (args.tvar == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
 
     args.str = ParseModifierPart(&st->next, delim, st->eflags & ~VARE_WANTRES,
                                 st->ctxt, NULL, NULL, NULL);
     if (args.str == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
 
     args.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES);
@@ -2114,11 +2122,11 @@
     Var_Delete(args.tvar, st->ctxt);
     free(args.tvar);
     free(args.str);
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :Ddefined or :Uundefined */
-static void
+static ApplyModifierResult
 ApplyModifier_Defined(const char *mod, ApplyModifiersState *st)
 {
     Buffer buf;                        /* Buffer for patterns */
@@ -2179,15 +2187,16 @@
        st->newVal = st->val;
        Buf_Destroy(&buf, TRUE);
     }
+    return AMR_OK;
 }
 
 /* :gmtime */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Gmtime(const char *mod, ApplyModifiersState *st)
 {
     if (!ModMatchEq(mod, "gmtime", st->endc)) {
        st->next = mod + 1;
-       return FALSE;
+       return AMR_UNKNOWN;
     }
 
     time_t utc;
@@ -2200,7 +2209,7 @@
        st->next = mod + 6;
     }
     st->newVal = VarStrftime(st->val, 1, utc);
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :localtime */
@@ -2209,7 +2218,7 @@
 {
     if (!ModMatchEq(mod, "localtime", st->endc)) {
        st->next = mod + 1;
-       return FALSE;
+       return AMR_UNKNOWN;
     }
 
     time_t utc;
@@ -2222,25 +2231,25 @@
        st->next = mod + 9;
     }
     st->newVal = VarStrftime(st->val, 0, utc);
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :hash */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Hash(const char *mod, ApplyModifiersState *st)
 {
     if (!ModMatch(mod, "hash", st->endc)) {
        st->next = mod + 1;
-       return FALSE;
+       return AMR_UNKNOWN;
     }
 
     st->newVal = VarHash(st->val);
     st->next = mod + 4;
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :P */
-static void
+static ApplyModifierResult
 ApplyModifier_Path(const char *mod, ApplyModifiersState *st)
 {
     if (st->v->flags & VAR_JUNK)
@@ -2256,10 +2265,11 @@
     if (!st->newVal)
        st->newVal = bmake_strdup(st->v->name);
     st->next = mod + 1;
+    return AMR_OK;
 }
 
 /* :!cmd! */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Exclam(const char *mod, ApplyModifiersState *st)
 {
     st->next = mod + 1;
@@ -2268,7 +2278,7 @@
                                  NULL, NULL, NULL);
     if (cmd == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
 
     const char *emsg = NULL;
@@ -2279,20 +2289,20 @@
     free(cmd);
 
     if (emsg)
-       Error(emsg, st->val);
+       Error(emsg, st->val);   /* XXX: why still return AMR_OK? */
 
     if (st->v->flags & VAR_JUNK)
        st->v->flags |= VAR_KEEP;
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :range */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Range(const char *mod, ApplyModifiersState *st)
 {
     if (!ModMatchEq(mod, "range", st->endc)) {
        st->next = mod + 1;
-       return FALSE;
+       return AMR_UNKNOWN;
     }
 
     int n;
@@ -2305,11 +2315,11 @@
        st->next = mod + 5;
     }
     st->newVal = VarRange(st->val, n);
-    return TRUE;
+    return AMR_OK;
 }
 
 /* :Mpattern or :Npattern */
-static void
+static ApplyModifierResult
 ApplyModifier_Match(const char *mod, ApplyModifiersState *st)
 {
     Boolean copy = FALSE;      /* pattern should be, or has been, copied */
@@ -2381,10 +2391,11 @@
     st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
                             callback, pattern);
     free(pattern);
+    return AMR_OK;
 }
 
 /* :S,from,to, */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Subst(const char * const mod, ApplyModifiersState *st)
 {
     ModifyWord_SubstArgs args;
@@ -2407,7 +2418,7 @@
                                  &args.lhsLen, &args.pflags, NULL);
     if (lhs == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
     args.lhs = lhs;
 
@@ -2415,7 +2426,7 @@
                                  &args.rhsLen, NULL, &args);
     if (rhs == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
     args.rhs = rhs;
 
@@ -2444,13 +2455,13 @@
 
     free(lhs);
     free(rhs);
-    return TRUE;
+    return AMR_OK;
 }
 
 #ifndef NO_REGEX
 
 /* :C,from,to, */
-static Boolean
+static ApplyModifierResult
 ApplyModifier_Regex(const char *mod, ApplyModifiersState *st)
 {
     ModifyWord_SubstRegexArgs args;
@@ -2465,7 +2476,7 @@
                                 NULL, NULL, NULL);
     if (re == NULL) {
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
 
     args.replace = ParseModifierPart(&st->next, delim, st->eflags, st->ctxt,
@@ -2473,7 +2484,7 @@
     if (args.replace == NULL) {
        free(re);
        st->missing_delim = delim;
-       return FALSE;
+       return AMR_CLEANUP;
     }
 
     for (;; st->next++) {
@@ -2496,7 +2507,7 @@
     if (error) {
        VarREError(error, &args.re, "RE substitution error");
        free(args.replace);
-       return FALSE;



Home | Main Index | Thread Index | Old Index