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.
This commit is contained in:
rillig 2021-03-14 17:34:50 +00:00
parent 0fc517c113
commit 71128ca3aa
1 changed files with 10 additions and 12 deletions

View File

@ -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, ApplyModifiersState *st)
{
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, ApplyModifiersState *st)
*/
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;
}