xref: /freebsd/contrib/bmake/unit-tests/varmod-loop-varname.mk (revision 226192822cddc30cacecd55bccb48f39c653058c)
1*22619282SSimon J. Gerraty# $NetBSD: varmod-loop-varname.mk,v 1.9 2024/07/05 19:47:22 rillig Exp $
2b0c40a00SSimon J. Gerraty#
3b0c40a00SSimon J. Gerraty# Tests for the first part of the variable modifier ':@var@...@', which
4b0c40a00SSimon J. Gerraty# contains the variable name to use during the loop.
5b0c40a00SSimon J. Gerraty
612904384SSimon J. Gerraty# Force the test results to be independent of the default value of this
712904384SSimon J. Gerraty# setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
812904384SSimon J. Gerraty# distribution and pkgsrc/devel/bmake.
9b0c40a00SSimon J. Gerraty.MAKE.SAVE_DOLLARS=	yes
10b0c40a00SSimon J. Gerraty
11b0c40a00SSimon J. Gerraty
12b0c40a00SSimon J. Gerraty# Before 2021-04-04, the name of the loop variable could be generated
13b0c40a00SSimon J. Gerraty# dynamically.  There was no practical use-case for this.
14b0c40a00SSimon J. Gerraty# Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
15b0c40a00SSimon J. Gerraty# variable name.
16*22619282SSimon J. Gerraty# expect+2: while evaluating "${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"" with value "one two three": In the :@ modifier, the variable name "${:Ubar:S,b,v,}" must not contain a dollar
17148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+")
18b0c40a00SSimon J. Gerraty.if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
19b0c40a00SSimon J. Gerraty.  error
2012904384SSimon J. Gerraty.else
2112904384SSimon J. Gerraty.  error
22b0c40a00SSimon J. Gerraty.endif
23b0c40a00SSimon J. Gerraty
24b0c40a00SSimon J. Gerraty
25b0c40a00SSimon J. Gerraty# ":::" is a very creative variable name, unlikely to occur in practice.
26b0c40a00SSimon J. Gerraty# The expression ${\:\:\:} would not work since backslashes can only
27b0c40a00SSimon J. Gerraty# be escaped in the modifiers, but not in the variable name, therefore
28b0c40a00SSimon J. Gerraty# the extra indirection via the modifier ':U'.
29b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
30b0c40a00SSimon J. Gerraty.  error
31b0c40a00SSimon J. Gerraty.endif
32b0c40a00SSimon J. Gerraty
33b0c40a00SSimon J. Gerraty
34b0c40a00SSimon J. Gerraty# "@@" is another creative variable name.
35b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
36b0c40a00SSimon J. Gerraty.  error
37b0c40a00SSimon J. Gerraty.endif
38b0c40a00SSimon J. Gerraty
39b0c40a00SSimon J. Gerraty
40b0c40a00SSimon J. Gerraty# In extreme cases, even the backslash can be used as variable name.
41b0c40a00SSimon J. Gerraty# It needs to be doubled though.
42b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
43b0c40a00SSimon J. Gerraty.  error
44b0c40a00SSimon J. Gerraty.endif
45b0c40a00SSimon J. Gerraty
46b0c40a00SSimon J. Gerraty
47b0c40a00SSimon J. Gerraty# The variable name can technically be empty, and in this situation
48b0c40a00SSimon J. Gerraty# the variable value cannot be accessed since the empty "variable"
49b0c40a00SSimon J. Gerraty# is protected to always return an empty string.
50b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@@x${}y@} != "xy xy xy"
51b0c40a00SSimon J. Gerraty.  error
52b0c40a00SSimon J. Gerraty.endif
53b0c40a00SSimon J. Gerraty
54b0c40a00SSimon J. Gerraty
55b0c40a00SSimon J. Gerraty# The :@ modifier resolves the variables from the replacement text once more
56b0c40a00SSimon J. Gerraty# than expected.  In particular, it resolves _all_ variables from the scope,
57b0c40a00SSimon J. Gerraty# and not only the loop variable (in this case v).
58b0c40a00SSimon J. GerratySRCS=		source
59b0c40a00SSimon J. GerratyCFLAGS.source=	before
60b0c40a00SSimon J. GerratyALL_CFLAGS:=	${SRCS:@src@${CFLAGS.${src}}@}	# note the ':='
61b0c40a00SSimon J. GerratyCFLAGS.source+=	after
62b0c40a00SSimon J. Gerraty.if ${ALL_CFLAGS} != "before"
63b0c40a00SSimon J. Gerraty.  error
64b0c40a00SSimon J. Gerraty.endif
65b0c40a00SSimon J. Gerraty
66b0c40a00SSimon J. Gerraty
67b0c40a00SSimon J. Gerraty# In the following example, the modifier ':@' expands the '$$' to '$'.  This
68b0c40a00SSimon J. Gerraty# means that when the resulting expression is evaluated, these resulting '$'
69b0c40a00SSimon J. Gerraty# will be interpreted as starting a subexpression.
70b0c40a00SSimon J. Gerraty#
71b0c40a00SSimon J. Gerraty# The d means direct reference, the i means indirect reference.
72b0c40a00SSimon J. GerratyRESOLVE=	${RES1} $${RES1}
73b0c40a00SSimon J. GerratyRES1=		1d${RES2} 1i$${RES2}
74b0c40a00SSimon J. GerratyRES2=		2d${RES3} 2i$${RES3}
75b0c40a00SSimon J. GerratyRES3=		3
76b0c40a00SSimon J. Gerraty
77b0c40a00SSimon J. Gerraty.if ${RESOLVE:@v@w${v}w@} != "w1d2d3w w2i3w w1i2d3 2i\${RES3}w w1d2d3 2i\${RES3} 1i\${RES2}w"
78b0c40a00SSimon J. Gerraty.  error
79b0c40a00SSimon J. Gerraty.endif
80b0c40a00SSimon J. Gerraty
81b0c40a00SSimon J. Gerraty
82b0c40a00SSimon J. Gerraty# Until 2020-07-20, the variable name of the :@ modifier could end with one
83b0c40a00SSimon J. Gerraty# or two dollar signs, which were silently ignored.
84b0c40a00SSimon J. Gerraty# There's no point in allowing a dollar sign in that position.
85b0c40a00SSimon J. Gerraty# Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
86b0c40a00SSimon J. Gerraty# variable name.
87*22619282SSimon J. Gerraty# expect+2: while evaluating variable "1 2 3" with value "1 2 3": In the :@ modifier, the variable name "v$" must not contain a dollar
88148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${1 2 3:L:@v$@($v)@} != "(1) (2) (3)")
89b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$@($v)@} != "(1) (2) (3)"
90b0c40a00SSimon J. Gerraty.  error
91b0c40a00SSimon J. Gerraty.else
92b0c40a00SSimon J. Gerraty.  error
93b0c40a00SSimon J. Gerraty.endif
94*22619282SSimon J. Gerraty# expect+2: while evaluating variable "1 2 3" with value "1 2 3": In the :@ modifier, the variable name "v$$" must not contain a dollar
95148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${1 2 3:L:@v$$@($v)@} != "() () ()")
96b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$$@($v)@} != "() () ()"
97b0c40a00SSimon J. Gerraty.  error
98b0c40a00SSimon J. Gerraty.else
99b0c40a00SSimon J. Gerraty.  error
100b0c40a00SSimon J. Gerraty.endif
101*22619282SSimon J. Gerraty# expect+2: while evaluating variable "1 2 3" with value "1 2 3": In the :@ modifier, the variable name "v$$$" must not contain a dollar
102148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${1 2 3:L:@v$$$@($v)@} != "() () ()")
103b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$$$@($v)@} != "() () ()"
104b0c40a00SSimon J. Gerraty.  error
105b0c40a00SSimon J. Gerraty.else
106b0c40a00SSimon J. Gerraty.  error
107b0c40a00SSimon J. Gerraty.endif
108b0c40a00SSimon J. Gerraty
109b0c40a00SSimon J. Gerraty
110b0c40a00SSimon J. Gerraty# It may happen that there are nested :@ modifiers that use the same name for
111b0c40a00SSimon J. Gerraty# for the loop variable.  These modifiers influence each other.
112b0c40a00SSimon J. Gerraty#
113b0c40a00SSimon J. Gerraty# As of 2020-10-18, the :@ modifier is implemented by actually setting a
114b0c40a00SSimon J. Gerraty# variable in the scope of the expression and deleting it again after the
115d5e0a182SSimon J. Gerraty# loop.  This is different from the .for loops, which substitute the
116b0c40a00SSimon J. Gerraty# expression with ${:Uvalue}, leading to different unwanted side effects.
117b0c40a00SSimon J. Gerraty#
118b0c40a00SSimon J. Gerraty# To make the behavior more predictable, the :@ modifier should restore the
119b0c40a00SSimon J. Gerraty# loop variable to the value it had before the loop.  This would result in
120b0c40a00SSimon J. Gerraty# the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
121b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
122b0c40a00SSimon J. Gerraty.  error
123b0c40a00SSimon J. Gerraty.endif
124b0c40a00SSimon J. Gerraty
125b0c40a00SSimon J. Gerraty# During the loop, the variable is actually defined and nonempty.
126b0c40a00SSimon J. Gerraty# If the loop were implemented in the same way as the .for loop, the variable
127b0c40a00SSimon J. Gerraty# would be neither defined nor nonempty since all expressions of the form
128b0c40a00SSimon J. Gerraty# ${var} would have been replaced with ${:Uword} before evaluating them.
129b0c40a00SSimon J. Gerraty.if defined(var)
130b0c40a00SSimon J. Gerraty.  error
131b0c40a00SSimon J. Gerraty.endif
132b0c40a00SSimon J. Gerraty.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
133b0c40a00SSimon J. Gerraty    != "def nonempty"
134b0c40a00SSimon J. Gerraty.  error
135b0c40a00SSimon J. Gerraty.endif
136b0c40a00SSimon J. Gerraty.if defined(var)
137b0c40a00SSimon J. Gerraty.  error
138b0c40a00SSimon J. Gerraty.endif
139b0c40a00SSimon J. Gerraty
140b0c40a00SSimon J. Gerratyall: .PHONY
141