Module Name:    src
Committed By:   rillig
Date:           Sun Mar 14 17:34:50 UTC 2021

Modified Files:
        src/usr.bin/make: var.c

Log Message:
make: merge duplicate code in ApplyModifier_Remember

This way, parsing and evaluating the modifier is only written once in
the code.  The downside is that the variable name is allocated even if
VARE_WANTRES is not set, but since this modifier is so obscure and
seldom used this doesn't matter in practice.


To generate a diff of this commit:
cvs rdiff -u -r1.867 -r1.868 src/usr.bin/make/var.c

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.867 src/usr.bin/make/var.c:1.868
--- src/usr.bin/make/var.c:1.867	Sun Mar 14 17:27:27 2021
+++ src/usr.bin/make/var.c	Sun Mar 14 17:34:50 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.867 2021/03/14 17:27:27 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.868 2021/03/14 17:34:50 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.867 2021/03/14 17:27:27 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.868 2021/03/14 17:34:50 rillig Exp $");
 
 typedef enum VarFlags {
 	VFL_NONE	= 0,
@@ -3418,10 +3418,12 @@ ApplyModifier_Remember(const char **pp, 
 {
 	Expr *expr = st->expr;
 	const char *mod = *pp;
+	FStr name;
 
 	if (!ModMatchEq(mod, "_", st))
 		return AMR_UNKNOWN;
 
+	name = FStr_InitRefer("_");
 	if (mod[1] == '=') {
 		/*
 		 * XXX: This ad-hoc call to strcspn deviates from the usual
@@ -3430,18 +3432,14 @@ ApplyModifier_Remember(const char **pp, 
 		 */
 		size_t n = strcspn(mod + 2, ":)}");
 		*pp = mod + 2 + n;
-
-		if (expr->eflags & VARE_WANTRES) {
-			char *name = bmake_strldup(mod + 2, n);
-			Var_Set(expr->scope, name, expr->value.str);
-			free(name);
-		}
-	} else {
+		name = FStr_InitOwn(bmake_strldup(mod + 2, n));
+	} else
 		*pp = mod + 1;
 
-		if (expr->eflags & VARE_WANTRES)
-			Var_Set(expr->scope, "_", expr->value.str);
-	}
+	if (expr->eflags & VARE_WANTRES)
+		Var_Set(expr->scope, name.str, expr->value.str);
+	FStr_Done(&name);
+
 	return AMR_OK;
 }
 

Reply via email to