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