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