make(1): add test for combining the :@ and :? variable modifiers

This commit is contained in:
rillig 2020-10-31 12:34:03 +00:00
parent 6c314bb2d6
commit 08137c1148
2 changed files with 26 additions and 7 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: varmod-loop.mk,v 1.4 2020/10/18 21:12:13 rillig Exp $
# $NetBSD: varmod-loop.mk,v 1.5 2020/10/31 12:34:03 rillig Exp $
#
# Tests for the :@var@...${var}...@ variable modifier.
@ -13,16 +13,20 @@ all: mod-loop-dollar
# Therefore, in -dL mode, this is forbidden, see lint.mk.
mod-loop-varname:
@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
# ":::" is a very creative variable name, unlikely in practice
# ":::" is a very creative variable name, unlikely in practice.
# The expression ${\:\:\:} would not work since backslashes can only
# be escaped in the modifiers, but not in the variable name.
@echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
# "@@" is another creative variable name.
@echo :${:U1 2 3:@\@\@@x${@@}y@}:
# Even "@" works as a variable name since the variable is installed
# in the "current" scope, which in this case is the one from the
# target.
@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
# In extreme cases, even the backslash can be used as variable name.
# It needs to be doubled though.
@echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
@ -70,7 +74,7 @@ mod-loop-dollar:
# It may happen that there are nested :@ modifiers that use the same name for
# for the loop variable. These modifiers influence each other.
#
# As of 2020-10-18, the :@ modifier is implemented by actually setting an
# As of 2020-10-18, the :@ modifier is implemented by actually setting a
# variable in the context of the expression and deleting it again after the
# loop. This is different from the .for loops, which substitute the variable
# expression with ${:Uvalue}, leading to different unwanted side effects.
@ -81,3 +85,18 @@ mod-loop-dollar:
.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
. error
.endif
# During the loop, the variable is actually defined and nonempty.
# If the loop were implemented in the same way as the .for loop, the variable
# would be neither defined nor nonempty since all expressions of the form
# ${var} would have been replaced with ${:Uword} before evaluating them.
.if defined(var)
. error
.endif
.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
!= "def nonempty"
. error
.endif
.if defined(var)
. error
.endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.617 2020/10/31 12:22:43 rillig Exp $ */
/* $NetBSD: var.c,v 1.618 2020/10/31 12:34:03 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -129,7 +129,7 @@
#include "metachar.h"
/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: var.c,v 1.617 2020/10/31 12:22:43 rillig Exp $");
MAKE_RCSID("$NetBSD: var.c,v 1.618 2020/10/31 12:34:03 rillig Exp $");
#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@ -1465,8 +1465,8 @@ ModifyWord_Loop(const char *word, SepBuf *buf, void *data)
(void)Var_Subst(args->str, args->ctx, args->eflags, &s);
/* TODO: handle errors */
VAR_DEBUG4("ModifyWord_Loop: in \"%s\", replace \"%s\" with \"%s\" "
"to \"%s\"\n",
VAR_DEBUG4("ModifyWord_Loop: "
"in \"%s\", replace \"%s\" with \"%s\" to \"%s\"\n",
word, args->tvar, args->str, s);
if (s[0] == '\n' || Buf_EndsWith(&buf->buf, '\n'))