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): turn ApplyModifiersState.val into a lo...



details:   https://anonhg.NetBSD.org/src/rev/4ccafe4b33c1
branches:  trunk
changeset: 979252:4ccafe4b33c1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Dec 20 17:22:10 2020 +0000

description:
make(1): turn ApplyModifiersState.val into a local variable

This reduces the scope and makes it more obvious at which places this
variable can be changed and how the memory management is done.

diffstat:

 usr.bin/make/var.c |  203 ++++++++++++++++++++++++++--------------------------
 1 files changed, 100 insertions(+), 103 deletions(-)

diffs (truncated from 654 to 300 lines):

diff -r 23bfe7857857 -r 4ccafe4b33c1 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Dec 20 16:38:25 2020 +0000
+++ b/usr.bin/make/var.c        Sun Dec 20 17:22:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.748 2020/12/20 15:31:29 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.749 2020/12/20 17:22:10 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.748 2020/12/20 15:31:29 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.749 2020/12/20 17:22:10 rillig Exp $");
 
 typedef enum VarFlags {
        VAR_NONE        = 0,
@@ -1927,11 +1927,6 @@
        GNode *const ctxt;
        const VarEvalFlags eflags;
        /*
-        * The old value of the expression, before applying the modifier,
-        * never NULL.
-        */
-       char *val;
-       /*
         * The new value of the expression, after applying the modifier,
         * never NULL.
         */
@@ -2201,7 +2196,7 @@
 
 /* :@var@...${var}...@ */
 static ApplyModifierResult
-ApplyModifier_Loop(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Loop(const char **pp, const char *val, ApplyModifiersState *st)
 {
        struct ModifyWord_LoopArgs args;
        char prev_sep;
@@ -2230,7 +2225,7 @@
        args.eflags = st->eflags & ~(unsigned)VARE_KEEP_DOLLAR;
        prev_sep = st->sep;
        st->sep = ' ';          /* XXX: should be st->sep for consistency */
-       st->newVal = ModifyWords(st->val, ModifyWord_Loop, &args,
+       st->newVal = ModifyWords(val, ModifyWord_Loop, &args,
            st->oneBigWord, st->sep);
        st->sep = prev_sep;
        /* XXX: Consider restoring the previous variable instead of deleting. */
@@ -2242,7 +2237,7 @@
 
 /* :Ddefined or :Uundefined */
 static ApplyModifierResult
-ApplyModifier_Defined(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Defined(const char **pp, char *val, ApplyModifiersState *st)
 {
        Buffer buf;
        const char *p;
@@ -2293,7 +2288,7 @@
        if (eflags & VARE_WANTRES) {
                st->newVal = Buf_Destroy(&buf, FALSE);
        } else {
-               st->newVal = st->val;
+               st->newVal = val;
                Buf_Destroy(&buf, TRUE);
        }
        return AMR_OK;
@@ -2330,7 +2325,7 @@
 
 /* :gmtime */
 static ApplyModifierResult
-ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Gmtime(const char **pp, const char *val, ApplyModifiersState *st)
 {
        time_t utc;
 
@@ -2350,13 +2345,14 @@
                utc = 0;
                *pp = mod + 6;
        }
-       st->newVal = VarStrftime(st->val, TRUE, utc);
+       st->newVal = VarStrftime(val, TRUE, utc);
        return AMR_OK;
 }
 
 /* :localtime */
 static ApplyModifierResult
-ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Localtime(const char **pp, const char *val,
+                       ApplyModifiersState *st)
 {
        time_t utc;
 
@@ -2376,18 +2372,18 @@
                utc = 0;
                *pp = mod + 9;
        }
-       st->newVal = VarStrftime(st->val, FALSE, utc);
+       st->newVal = VarStrftime(val, FALSE, utc);
        return AMR_OK;
 }
 
 /* :hash */
 static ApplyModifierResult
-ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Hash(const char **pp, const char *val, ApplyModifiersState *st)
 {
        if (!ModMatch(*pp, "hash", st->endc))
                return AMR_UNKNOWN;
 
-       st->newVal = VarHash(st->val);
+       st->newVal = VarHash(val);
        *pp += 4;
        return AMR_OK;
 }
@@ -2448,7 +2444,7 @@
 /* The :range modifier generates an integer sequence as long as the words.
  * The :range=7 modifier generates an integer sequence from 1 to 7. */
 static ApplyModifierResult
-ApplyModifier_Range(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Range(const char **pp, const char *val, ApplyModifiersState *st)
 {
        size_t n;
        Buffer buf;
@@ -2472,7 +2468,7 @@
        }
 
        if (n == 0) {
-               Words words = Str_Words(st->val, FALSE);
+               Words words = Str_Words(val, FALSE);
                n = words.len;
                Words_Free(words);
        }
@@ -2493,7 +2489,7 @@
 
 /* :Mpattern or :Npattern */
 static ApplyModifierResult
-ApplyModifier_Match(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Match(const char **pp, const char *val, ApplyModifiersState *st)
 {
        const char *mod = *pp;
        Boolean copy = FALSE;   /* pattern should be, or has been, copied */
@@ -2561,10 +2557,10 @@
        }
 
        DEBUG3(VAR, "Pattern[%s] for [%s] is [%s]\n",
-           st->var->name.str, st->val, pattern);
+           st->var->name.str, val, pattern);
 
        callback = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch;
-       st->newVal = ModifyWords(st->val, callback, pattern,
+       st->newVal = ModifyWords(val, callback, pattern,
            st->oneBigWord, st->sep);
        free(pattern);
        return AMR_OK;
@@ -2572,7 +2568,7 @@
 
 /* :S,from,to, */
 static ApplyModifierResult
-ApplyModifier_Subst(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Subst(const char **pp, const char *val, ApplyModifiersState *st)
 {
        struct ModifyWord_SubstArgs args;
        char *lhs, *rhs;
@@ -2628,7 +2624,7 @@
                break;
        }
 
-       st->newVal = ModifyWords(st->val, ModifyWord_Subst, &args,
+       st->newVal = ModifyWords(val, ModifyWord_Subst, &args,
            oneBigWord, st->sep);
 
        free(lhs);
@@ -2640,7 +2636,7 @@
 
 /* :C,from,to, */
 static ApplyModifierResult
-ApplyModifier_Regex(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Regex(const char **pp, const char *val, ApplyModifiersState *st)
 {
        char *re;
        struct ModifyWord_SubstRegexArgs args;
@@ -2698,7 +2694,7 @@
        args.nsub = args.re.re_nsub + 1;
        if (args.nsub > 10)
                args.nsub = 10;
-       st->newVal = ModifyWords(st->val, ModifyWord_SubstRegex, &args,
+       st->newVal = ModifyWords(val, ModifyWord_SubstRegex, &args,
            oneBigWord, st->sep);
        regfree(&args.re);
        free(args.replace);
@@ -2709,10 +2705,10 @@
 
 /* :Q, :q */
 static ApplyModifierResult
-ApplyModifier_Quote(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Quote(const char **pp, const char *val, ApplyModifiersState *st)
 {
        if ((*pp)[1] == st->endc || (*pp)[1] == ':') {
-               st->newVal = VarQuote(st->val, **pp == 'q');
+               st->newVal = VarQuote(val, **pp == 'q');
                (*pp)++;
                return AMR_OK;
        } else
@@ -2727,7 +2723,7 @@
 
 /* :ts<separator> */
 static ApplyModifierResult
-ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st)
+ApplyModifier_ToSep(const char **pp, const char *val, ApplyModifiersState *st)
 {
        const char *sep = *pp + 2;
 
@@ -2792,7 +2788,7 @@
        }
 
 ok:
-       st->newVal = ModifyWords(st->val, ModifyWord_Copy, NULL,
+       st->newVal = ModifyWords(val, ModifyWord_Copy, NULL,
            st->oneBigWord, st->sep);
        return AMR_OK;
 }
@@ -2827,7 +2823,7 @@
 
 /* :tA, :tu, :tl, :ts<separator>, etc. */
 static ApplyModifierResult
-ApplyModifier_To(const char **pp, ApplyModifiersState *st)
+ApplyModifier_To(const char **pp, char *val, ApplyModifiersState *st)
 {
        const char *mod = *pp;
        assert(mod[0] == 't');
@@ -2838,7 +2834,7 @@
        }
 
        if (mod[1] == 's')
-               return ApplyModifier_ToSep(pp, st);
+               return ApplyModifier_ToSep(pp, val, st);
 
        if (mod[2] != st->endc && mod[2] != ':') {
                *pp = mod + 1;
@@ -2847,27 +2843,27 @@
 
        /* Check for two-character options: ":tu", ":tl" */
        if (mod[1] == 'A') {    /* absolute path */
-               st->newVal = ModifyWords(st->val, ModifyWord_Realpath, NULL,
+               st->newVal = ModifyWords(val, ModifyWord_Realpath, NULL,
                    st->oneBigWord, st->sep);
                *pp = mod + 2;
                return AMR_OK;
        }
 
        if (mod[1] == 'u') {    /* :tu */
-               st->newVal = str_toupper(st->val);
+               st->newVal = str_toupper(val);
                *pp = mod + 2;
                return AMR_OK;
        }
 
        if (mod[1] == 'l') {    /* :tl */
-               st->newVal = str_tolower(st->val);
+               st->newVal = str_tolower(val);
                *pp = mod + 2;
                return AMR_OK;
        }
 
        if (mod[1] == 'W' || mod[1] == 'w') { /* :tW, :tw */
                st->oneBigWord = mod[1] == 'W';
-               st->newVal = st->val;
+               st->newVal = val;
                *pp = mod + 2;
                return AMR_OK;
        }
@@ -2879,7 +2875,7 @@
 
 /* :[#], :[1], :[-1..1], etc. */
 static ApplyModifierResult
-ApplyModifier_Words(const char **pp, ApplyModifiersState *st)
+ApplyModifier_Words(const char **pp, char *val, ApplyModifiersState *st)
 {
        char *estr;
        int first, last;
@@ -2905,7 +2901,7 @@
                } else {
                        Buffer buf;
 
-                       Words words = Str_Words(st->val, FALSE);
+                       Words words = Str_Words(val, FALSE);
                        size_t ac = words.len;
                        Words_Free(words);
 
@@ -2920,14 +2916,14 @@
        if (estr[0] == '*' && estr[1] == '\0') {
                /* Found ":[*]" */
                st->oneBigWord = TRUE;
-               st->newVal = st->val;
+               st->newVal = val;
                goto ok;
        }
 
        if (estr[0] == '@' && estr[1] == '\0') {
                /* Found ":[@]" */



Home | Main Index | Thread Index | Old Index