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: when parsing ':D' or ':U', only copy text...



details:   https://anonhg.NetBSD.org/src/rev/55ebe3c5a8af
branches:  trunk
changeset: 368794:55ebe3c5a8af
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Aug 05 20:59:54 2022 +0000

description:
make: when parsing ':D' or ':U', only copy text if necessary

When parsing the expression ${:D any ${uninterpreted} text}, parsing of
the ':D' modifier only needs to skip to the next delimiter, there is no
need to keep record of the text that has been skipped.  In this
particular example, there had been an unnecessary memory allocation
since the nested expression was not copied to the result buffer.  Due to
that, the resulting buffer contained " any  text", which was not a
substring of the text passed to the parser, thereby forcing the memory
allocation.

No functional change.

diffstat:

 usr.bin/make/var.c |  43 +++++++++++++++++++++----------------------
 1 files changed, 21 insertions(+), 22 deletions(-)

diffs (93 lines):

diff -r 230b0371582e -r 55ebe3c5a8af usr.bin/make/var.c
--- a/usr.bin/make/var.c        Fri Aug 05 20:35:55 2022 +0000
+++ b/usr.bin/make/var.c        Fri Aug 05 20:59:54 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.1026 2022/08/05 20:35:55 rillig Exp $        */
+/*     $NetBSD: var.c,v 1.1027 2022/08/05 20:59:54 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1026 2022/08/05 20:35:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1027 2022/08/05 20:59:54 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2440,7 +2440,7 @@
 }
 
 static void
-ParseModifier_Defined(const char **pp, ModChain *ch, VarEvalMode emode,
+ParseModifier_Defined(const char **pp, ModChain *ch, bool shouldEval,
                      LazyBuf *buf)
 {
        const char *p;
@@ -2460,7 +2460,8 @@
                if (*p == '\\') {
                        char c = p[1];
                        if (IsDelimiter(c, ch) || c == '$' || c == '\\') {
-                               LazyBuf_Add(buf, c);
+                               if (shouldEval)
+                                       LazyBuf_Add(buf, c);
                                p += 2;
                                continue;
                        }
@@ -2468,18 +2469,21 @@
 
                /* Nested variable expression */
                if (*p == '$') {
-                       FStr nested_val;
-
-                       (void)Var_Parse(&p, ch->expr->scope, emode, &nested_val);
+                       FStr val;
+
+                       (void)Var_Parse(&p, ch->expr->scope,
+                           shouldEval ? ch->expr->emode : VARE_PARSE_ONLY,
+                           &val);
                        /* TODO: handle errors */
-                       if (ModChain_ShouldEval(ch))
-                               LazyBuf_AddStr(buf, nested_val.str);
-                       FStr_Done(&nested_val);
+                       if (shouldEval)
+                               LazyBuf_AddStr(buf, val.str);
+                       FStr_Done(&val);
                        continue;
                }
 
                /* Ordinary text */
-               LazyBuf_Add(buf, *p);
+               if (shouldEval)
+                       LazyBuf_Add(buf, *p);
                p++;
        }
        *pp = p;
@@ -2491,20 +2495,15 @@
 {
        Expr *expr = ch->expr;
        LazyBuf buf;
-
-       VarEvalMode emode = VARE_PARSE_ONLY;
-       if (Expr_ShouldEval(expr))
-               if ((**pp == 'D') == (expr->defined == DEF_REGULAR))
-                       emode = expr->emode;
-
-       ParseModifier_Defined(pp, ch, emode, &buf);
+       bool shouldEval =
+           Expr_ShouldEval(expr) &&
+           (**pp == 'D') == (expr->defined == DEF_REGULAR);
+
+       ParseModifier_Defined(pp, ch, shouldEval, &buf);
 
        Expr_Define(expr);
-
-       if (VarEvalMode_ShouldEval(emode))
+       if (shouldEval)
                Expr_SetValue(expr, Substring_Str(LazyBuf_Get(&buf)));
-       else
-               LazyBuf_Done(&buf);
 
        return AMR_OK;
 }



Home | Main Index | Thread Index | Old Index