Module Name:    src
Committed By:   rillig
Date:           Sun Mar 14 16:43:31 UTC 2021

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: var-eval-short.exp

Log Message:
make: only evaluate the ':_' modifier if the expression is needed

See var-eval-short.mk:46 for the test demonstrating this change.
Previously, the expression ${:Uword:_=VAR} was evaluated including all
its side effects even though it was in an irrelevant branch of the
condition.


To generate a diff of this commit:
cvs rdiff -u -r1.865 -r1.866 src/usr.bin/make/var.c
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/make/unit-tests/var-eval-short.exp

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.865 src/usr.bin/make/var.c:1.866
--- src/usr.bin/make/var.c:1.865	Sun Mar 14 16:03:04 2021
+++ src/usr.bin/make/var.c	Sun Mar 14 16:43:30 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.865 2021/03/14 16:03:04 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.866 2021/03/14 16:43:30 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.865 2021/03/14 16:03:04 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.866 2021/03/14 16:43:30 rillig Exp $");
 
 typedef enum VarFlags {
 	VFL_NONE	= 0,
@@ -3418,6 +3418,7 @@ ApplyModifier_Remember(const char **pp, 
 {
 	Expr *expr = st->expr;
 	const char *mod = *pp;
+
 	if (!ModMatchEq(mod, "_", st))
 		return AMR_UNKNOWN;
 
@@ -3428,19 +3429,24 @@ ApplyModifier_Remember(const char **pp, 
 		 * unnecessary, undocumented inconsistency in make.
 		 */
 		size_t n = strcspn(mod + 2, ":)}");
-		char *name = bmake_strldup(mod + 2, n);
 		*pp = mod + 2 + n;
 
-		/*
-		 * FIXME: do not expand the variable name here; it would only
-		 *  work for single-character variable names anyway.
-		 */
-		Var_SetExpand(expr->scope, name, expr->value.str);
-		free(name);
+		if (expr->eflags & VARE_WANTRES) {
+			char *name = bmake_strldup(mod + 2, n);
+
+			/*
+			 * FIXME: do not expand the variable name here; it
+			 * would only work for single-character variable names
+			 * anyway.
+			 */
+			Var_SetExpand(expr->scope, name, expr->value.str);
+			free(name);
+		}
 	} else {
 		*pp = mod + 1;
 
-		Var_Set(expr->scope, "_", expr->value.str);
+		if (expr->eflags & VARE_WANTRES)
+			Var_Set(expr->scope, "_", expr->value.str);
 	}
 	return AMR_OK;
 }

Index: src/usr.bin/make/unit-tests/var-eval-short.exp
diff -u src/usr.bin/make/unit-tests/var-eval-short.exp:1.1 src/usr.bin/make/unit-tests/var-eval-short.exp:1.2
--- src/usr.bin/make/unit-tests/var-eval-short.exp:1.1	Sun Mar 14 11:49:37 2021
+++ src/usr.bin/make/unit-tests/var-eval-short.exp	Sun Mar 14 16:43:30 2021
@@ -1,7 +1,6 @@
 unexpected
 make: Bad modifier ":[${FAIL" for variable ""
 make: "var-eval-short.mk" line 43: Malformed conditional (0 && ${:Uword:[${FAIL}]})
-make: "var-eval-short.mk" line 48: Missing argument for ".error"
 make: "var-eval-short.mk" line 63: Invalid time value: ${FAIL}}
 make: "var-eval-short.mk" line 63: Malformed conditional (0 && ${:Uword:gmtime=${FAIL}})
 make: "var-eval-short.mk" line 77: Invalid time value: ${FAIL}}

Reply via email to