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: extract common code into Expr_Str



details:   https://anonhg.NetBSD.org/src/rev/794ef26c16b2
branches:  trunk
changeset: 1026773:794ef26c16b2
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Dec 06 21:24:07 2021 +0000

description:
make: extract common code into Expr_Str

This change turns Expr into a more abstract data type.

No functional change.

diffstat:

 usr.bin/make/var.c |  56 +++++++++++++++++++++++++++++------------------------
 1 files changed, 31 insertions(+), 25 deletions(-)

diffs (229 lines):

diff -r d21ebfe080d8 -r 794ef26c16b2 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Mon Dec 06 17:51:59 2021 +0000
+++ b/usr.bin/make/var.c        Mon Dec 06 21:24:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.964 2021/12/05 17:00:02 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.965 2021/12/06 21:24:07 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.964 2021/12/05 17:00:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.965 2021/12/06 21:24:07 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2085,6 +2085,12 @@
                expr->defined = DEF_DEFINED;
 }
 
+static const char *
+Expr_Str(const Expr *expr)
+{
+       return expr->value.str;
+}
+
 static void
 Expr_SetValue(Expr *expr, FStr value)
 {
@@ -2383,7 +2389,7 @@
            bool oneBigWord)
 {
        Expr *expr = ch->expr;
-       const char *val = expr->value.str;
+       const char *val = Expr_Str(expr);
        SepBuf result;
        SubstringWords words;
        size_t i;
@@ -2587,7 +2593,7 @@
        expr = ch->expr;
        if (Expr_ShouldEval(expr))
                Expr_SetValueOwn(expr,
-                   VarStrftime(expr->value.str, true, utc));
+                   VarStrftime(Expr_Str(expr), true, utc));
 
        return AMR_OK;
 }
@@ -2619,7 +2625,7 @@
        expr = ch->expr;
        if (Expr_ShouldEval(expr))
                Expr_SetValueOwn(expr,
-                   VarStrftime(expr->value.str, false, utc));
+                   VarStrftime(Expr_Str(expr), false, utc));
 
        return AMR_OK;
 }
@@ -2633,7 +2639,7 @@
        *pp += 4;
 
        if (ModChain_ShouldEval(ch))
-               Expr_SetValueOwn(ch->expr, VarHash(ch->expr->value.str));
+               Expr_SetValueOwn(ch->expr, VarHash(Expr_Str(ch->expr)));
 
        return AMR_OK;
 }
@@ -2732,7 +2738,7 @@
                return AMR_OK;
 
        if (n == 0) {
-               SubstringWords words = Substring_Words(ch->expr->value.str,
+               SubstringWords words = Substring_Words(Expr_Str(ch->expr),
                    false);
                n = words.len;
                SubstringWords_Free(words);
@@ -3003,7 +3009,7 @@
        if (!ModChain_ShouldEval(ch))
                return AMR_OK;
 
-       VarQuote(ch->expr->value.str, quoteDollar, &buf);
+       VarQuote(Expr_Str(ch->expr), quoteDollar, &buf);
        if (buf.data != NULL)
                Expr_SetValue(ch->expr, LazyBuf_DoneGet(&buf));
        else
@@ -3155,14 +3161,14 @@
        if (mod[1] == 'u') {                            /* :tu */
                *pp = mod + 2;
                if (Expr_ShouldEval(expr))
-                       Expr_SetValueOwn(expr, str_toupper(expr->value.str));
+                       Expr_SetValueOwn(expr, str_toupper(Expr_Str(expr)));
                return AMR_OK;
        }
 
        if (mod[1] == 'l') {                            /* :tl */
                *pp = mod + 2;
                if (Expr_ShouldEval(expr))
-                       Expr_SetValueOwn(expr, str_tolower(expr->value.str));
+                       Expr_SetValueOwn(expr, str_tolower(Expr_Str(expr)));
                return AMR_OK;
        }
 
@@ -3212,7 +3218,7 @@
                        Buffer buf;
 
                        SubstringWords words = Substring_Words(
-                           expr->value.str, false);
+                           Expr_Str(expr), false);
                        size_t ac = words.len;
                        SubstringWords_Free(words);
 
@@ -3268,7 +3274,7 @@
 
        /* Normal case: select the words described by first and last. */
        Expr_SetValueOwn(expr,
-           VarSelectWords(expr->value.str, first, last,
+           VarSelectWords(Expr_Str(expr), first, last,
                ch->sep, ch->oneBigWord));
 
 ok:
@@ -3393,7 +3399,7 @@
        if (!ModChain_ShouldEval(ch))
                return AMR_OK;
 
-       words = Substring_Words(ch->expr->value.str, false);
+       words = Substring_Words(Expr_Str(ch->expr), false);
        if (cmp == NULL)
                ShuffleSubstrings(words.words, words.len);
        else {
@@ -3602,7 +3608,7 @@
                *pp = mod + 1;
 
        if (Expr_ShouldEval(expr))
-               Var_Set(expr->scope, name.str, expr->value.str);
+               Var_Set(expr->scope, name.str, Expr_Str(expr));
        FStr_Done(&name);
 
        return AMR_OK;
@@ -3639,7 +3645,7 @@
        if (!ModChain_ShouldEval(ch))
                return AMR_OK;
 
-       words = Substring_Words(ch->expr->value.str, false);
+       words = Substring_Words(Expr_Str(ch->expr), false);
 
        if (words.len > 1) {
                size_t si, di;
@@ -3711,7 +3717,7 @@
        (*pp)--;                /* Go back to the ch->endc. */
 
        /* Do not turn an empty expression into non-empty. */
-       if (lhsBuf.len == 0 && expr->value.str[0] == '\0')
+       if (lhsBuf.len == 0 && Expr_Str(expr)[0] == '\0')
                goto done;
 
        lhs = LazyBuf_Get(&lhsBuf);
@@ -3745,9 +3751,9 @@
 
        if (Expr_ShouldEval(expr)) {
                const char *errfmt;
-               char *output = Cmd_Exec(expr->value.str, &errfmt);
+               char *output = Cmd_Exec(Expr_Str(expr), &errfmt);
                if (errfmt != NULL)
-                       Error(errfmt, expr->value.str);
+                       Error(errfmt, Expr_Str(expr));
                Expr_SetValueOwn(expr, output);
        }
 
@@ -3777,13 +3783,13 @@
                debug_printf(
                    "Evaluating modifier ${%s:%c%s} on value \"%s\"\n",
                    expr->name, mod[0], is_single_char ? "" : "...",
-                   expr->value.str);
+                   Expr_Str(expr));
                return;
        }
 
        debug_printf(
            "Evaluating modifier ${%s:%c%s} on value \"%s\" (%s, %s)\n",
-           expr->name, mod[0], is_single_char ? "" : "...", expr->value.str,
+           expr->name, mod[0], is_single_char ? "" : "...", Expr_Str(expr),
            VarEvalMode_Name[expr->emode], ExprDefined_Name[expr->defined]);
 }
 
@@ -3791,7 +3797,7 @@
 LogAfterApply(const ModChain *ch, const char *p, const char *mod)
 {
        const Expr *expr = ch->expr;
-       const char *value = expr->value.str;
+       const char *value = Expr_Str(expr);
        const char *quot = value == var_Error ? "" : "\"";
 
        if ((expr->emode == VARE_WANTRES || expr->emode == VARE_UNDEFERR) &&
@@ -3922,7 +3928,7 @@
        if (mods.str[0] != '\0') {
                const char *modsp = mods.str;
                ApplyModifiers(expr, &modsp, '\0', '\0');
-               if (expr->value.str == var_Error || *modsp != '\0') {
+               if (Expr_Str(expr) == var_Error || *modsp != '\0') {
                        FStr_Done(&mods);
                        *pp = p;
                        return AMIR_OUT;        /* error already reported */
@@ -3990,7 +3996,7 @@
                    "modifier \"%.*s\" of variable \"%s\" with value \"%s\"",
                    ch->endc,
                    (int)(p - mod), mod,
-                   ch->expr->name, ch->expr->value.str);
+                   ch->expr->name, Expr_Str(ch->expr));
        } else if (*p == ':') {
                p++;
        } else if (opts.strict && *p != '\0' && *p != ch->endc) {
@@ -4038,7 +4044,7 @@
 
        assert(startc == '(' || startc == '{' || startc == '\0');
        assert(endc == ')' || endc == '}' || endc == '\0');
-       assert(expr->value.str != NULL);
+       assert(Expr_Str(expr) != NULL);
 
        p = *pp;
 
@@ -4076,7 +4082,7 @@
        }
 
        *pp = p;
-       assert(expr->value.str != NULL); /* Use var_Error or varUndefined. */
+       assert(Expr_Str(expr) != NULL); /* Use var_Error or varUndefined. */
        return;
 
 bad_modifier:



Home | Main Index | Thread Index | Old Index