xref: /freebsd/contrib/bmake/unit-tests/varmod-loop.mk (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1# $NetBSD: varmod-loop.mk,v 1.9 2021/02/04 21:42:47 rillig Exp $
2#
3# Tests for the :@var@...${var}...@ variable modifier.
4
5.MAKE.SAVE_DOLLARS=	yes
6
7all: mod-loop-varname
8all: mod-loop-resolve
9all: mod-loop-varname-dollar
10all: mod-loop-dollar
11
12# In the :@ modifier, the name of the loop variable can even be generated
13# dynamically.  There's no practical use-case for this, and hopefully nobody
14# will ever depend on this, but technically it's possible.
15# Therefore, in -dL mode, this is forbidden, see lint.mk.
16mod-loop-varname:
17	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
18
19	# ":::" is a very creative variable name, unlikely in practice.
20	# The expression ${\:\:\:} would not work since backslashes can only
21	# be escaped in the modifiers, but not in the variable name.
22	@echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
23
24	# "@@" is another creative variable name.
25	@echo :${:U1 2 3:@\@\@@x${@@}y@}:
26
27	# Even "@" works as a variable name since the variable is installed
28	# in the "current" scope, which in this case is the one from the
29	# target.
30	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
31
32	# In extreme cases, even the backslash can be used as variable name.
33	# It needs to be doubled though.
34	@echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
35
36	# The variable name can technically be empty, and in this situation
37	# the variable value cannot be accessed since the empty variable is
38	# protected to always return an empty string.
39	@echo empty: :${:U1 2 3:@@x${}y@}:
40
41# The :@ modifier resolves the variables a little more often than expected.
42# In particular, it resolves _all_ variables from the scope, and not only
43# the loop variable (in this case v).
44#
45# The d means direct reference, the i means indirect reference.
46RESOLVE=	${RES1} $${RES1}
47RES1=		1d${RES2} 1i$${RES2}
48RES2=		2d${RES3} 2i$${RES3}
49RES3=		3
50
51mod-loop-resolve:
52	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
53
54# Until 2020-07-20, the variable name of the :@ modifier could end with one
55# or two dollar signs, which were silently ignored.
56# There's no point in allowing a dollar sign in that position.
57mod-loop-varname-dollar:
58	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
59	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
60	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
61
62# Demonstrate that it is possible to generate dollar signs using the
63# :@ modifier.
64#
65# These are edge cases that could have resulted in a parse error as well
66# since the $@ at the end could have been interpreted as a variable, which
67# would mean a missing closing @ delimiter.
68mod-loop-dollar:
69	@echo $@:${:U1:@word@${word}$@:Q}:
70	@echo $@:${:U2:@word@$${word}$$@:Q}:
71	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
72	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
73	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
74	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
75
76# It may happen that there are nested :@ modifiers that use the same name for
77# for the loop variable.  These modifiers influence each other.
78#
79# As of 2020-10-18, the :@ modifier is implemented by actually setting a
80# variable in the scope of the expression and deleting it again after the
81# loop.  This is different from the .for loops, which substitute the variable
82# expression with ${:Uvalue}, leading to different unwanted side effects.
83#
84# To make the behavior more predictable, the :@ modifier should restore the
85# loop variable to the value it had before the loop.  This would result in
86# the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
87.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
88.  error
89.endif
90
91# During the loop, the variable is actually defined and nonempty.
92# If the loop were implemented in the same way as the .for loop, the variable
93# would be neither defined nor nonempty since all expressions of the form
94# ${var} would have been replaced with ${:Uword} before evaluating them.
95.if defined(var)
96.  error
97.endif
98.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
99    != "def nonempty"
100.  error
101.endif
102.if defined(var)
103.  error
104.endif
105
106# Assignment using the ':=' operator, combined with the :@var@ modifier
107#
1088_DOLLARS=	$$$$$$$$
109# This string literal is written with 8 dollars, and this is saved as the
110# variable value.  But as soon as this value is evaluated, it goes through
111# Var_Subst, which replaces each '$$' with a single '$'.  This could be
112# prevented by VARE_KEEP_DOLLAR, but that flag is usually removed before
113# expanding subexpressions.  See ApplyModifier_Loop and ParseModifierPart
114# for examples.
115#
116.MAKEFLAGS: -dcp
117USE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
118.if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
119.  error
120.endif
121#
122SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
123# The ':=' assignment operator evaluates the variable value using the flag
124# VARE_KEEP_DOLLAR, which means that some dollar signs are preserved, but not
125# all.  The dollar signs in the top-level expression and in the indirect
126# ${8_DOLLARS} are preserved.
127#
128# The variable modifier :@var@ does not preserve the dollar signs though, no
129# matter in which context it is evaluated.  What happens in detail is:
130# First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
131# Next, each word of the value is expanded on its own, and at this moment
132# in ApplyModifier_Loop, the VARE_KEEP_DOLLAR flag is not passed down to
133# ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
134#
135# The remaining words of USE_8_DOLLARS are not affected by any variable
136# modifier and are thus expanded with the flag VARE_KEEP_DOLLAR in action.
137# The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
138# "$$$$ $$$$$$$$ $$$$$$$$".
139#
140# The variable expression in the condition then expands this raw stored value
141# once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
142# longer take place since they had only been active during the evaluation of
143# the variable assignment.
144.if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
145.  error
146.endif
147.MAKEFLAGS: -d0
148