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