1# $NetBSD: varmod-loop.mk,v 1.21 2022/08/23 21:13:46 rillig Exp $
2#
3# Tests for the :@var@...${var}...@ variable modifier.
4
5# Force the test results to be independent of the default value of this
6# setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
7# distribution and pkgsrc/devel/bmake.
8.MAKE.SAVE_DOLLARS=	yes
9
10all: varname-overwriting-target
11all: mod-loop-dollar
12
13varname-overwriting-target:
14	# Even "@" works as a variable name since the variable is installed
15	# in the "current" scope, which in this case is the one from the
16	# target.  Because of this, after the loop has finished, '$@' is
17	# undefined.  This is something that make doesn't expect, this may
18	# even trigger an assertion failure somewhere.
19	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
20
21
22
23# Demonstrate that it is possible to generate dollar signs using the
24# :@ modifier.
25#
26# These are edge cases that could have resulted in a parse error as well
27# since the $@ at the end could have been interpreted as a variable, which
28# would mean a missing closing @ delimiter.
29mod-loop-dollar:
30	@echo $@:${:U1:@word@${word}$@:Q}:
31	@echo $@:${:U2:@word@$${word}$$@:Q}:
32	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
33	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
34	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
35	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
36
37# It may happen that there are nested :@ modifiers that use the same name for
38# for the loop variable.  These modifiers influence each other.
39#
40# As of 2020-10-18, the :@ modifier is implemented by actually setting a
41# variable in the scope of the expression and deleting it again after the
42# loop.  This is different from the .for loops, which substitute the variable
43# expression with ${:Uvalue}, leading to different unwanted side effects.
44#
45# To make the behavior more predictable, the :@ modifier should restore the
46# loop variable to the value it had before the loop.  This would result in
47# the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
48.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
49.  error
50.endif
51
52# During the loop, the variable is actually defined and nonempty.
53# If the loop were implemented in the same way as the .for loop, the variable
54# would be neither defined nor nonempty since all expressions of the form
55# ${var} would have been replaced with ${:Uword} before evaluating them.
56.if defined(var)
57.  error
58.endif
59.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
60    != "def nonempty"
61.  error
62.endif
63.if defined(var)
64.  error
65.endif
66
67# Assignment using the ':=' operator, combined with the :@var@ modifier
68#
698_DOLLARS=	$$$$$$$$
70# This string literal is written with 8 dollars, and this is saved as the
71# variable value.  But as soon as this value is evaluated, it goes through
72# Var_Subst, which replaces each '$$' with a single '$'.  This could be
73# prevented by VARE_EVAL_KEEP_DOLLAR, but that flag is usually removed
74# before expanding subexpressions.  See ApplyModifier_Loop and
75# ParseModifierPart for examples.
76#
77.MAKEFLAGS: -dcp
78USE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
79.if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
80.  error
81.endif
82#
83SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
84# The ':=' assignment operator evaluates the variable value using the mode
85# VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
86# but not all.  The dollar signs in the top-level expression and in the
87# indirect ${8_DOLLARS} are preserved.
88#
89# The variable modifier :@var@ does not preserve the dollar signs though, no
90# matter in which context it is evaluated.  What happens in detail is:
91# First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
92# Next, each word of the value is expanded on its own, and at this moment
93# in ApplyModifier_Loop, the flag keepDollar is not passed down to
94# ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
95#
96# The remaining words of USE_8_DOLLARS are not affected by any variable
97# modifier and are thus expanded with the flag keepDollar in action.
98# The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
99# "$$$$ $$$$$$$$ $$$$$$$$".
100#
101# The variable expression in the condition then expands this raw stored value
102# once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
103# longer take place since they had only been active during the evaluation of
104# the variable assignment.
105.if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
106.  error
107.endif
108.MAKEFLAGS: -d0
109
110# After looping over the words of the expression, the loop variable gets
111# undefined.  The modifier ':@' uses an ordinary global variable for this,
112# which is different from the '.for' loop, which replaces ${var} with
113# ${:Uvalue} in the body of the loop.  This choice of implementation detail
114# can be used for a nasty side effect.  The expression ${:U:@VAR@@} evaluates
115# to an empty string, plus it undefines the variable 'VAR'.  This is the only
116# possibility to undefine a global variable during evaluation.
117GLOBAL=		before-global
118RESULT:=	${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
119.if ${RESULT} != "before-global  undefined"
120.  error
121.endif
122
123# The above side effect of undefining a variable from a certain scope can be
124# further combined with the otherwise undocumented implementation detail that
125# the argument of an '.if' directive is evaluated in cmdline scope.  Putting
126# these together makes it possible to undefine variables from the cmdline
127# scope, something that is not possible in a straight-forward way.
128.MAKEFLAGS: CMDLINE=cmdline
129.if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
130.  error
131.endif
132# Now the cmdline variable got undefined.
133.if ${CMDLINE} != "cmdline"
134.  error
135.endif
136# At this point, it still looks as if the cmdline variable were defined,
137# since the value of CMDLINE is still "cmdline".  That impression is only
138# superficial though, the cmdline variable is actually deleted.  To
139# demonstrate this, it is now possible to override its value using a global
140# variable, something that was not possible before:
141CMDLINE=	global
142.if ${CMDLINE} != "global"
143.  error
144.endif
145# Now undefine that global variable again, to get back to the original value.
146.undef CMDLINE
147.if ${CMDLINE} != "cmdline"
148.  error
149.endif
150# What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
151# target in the cmdline scope, that same variable was exported to the
152# environment, see Var_SetWithFlags.
153.unexport CMDLINE
154.if ${CMDLINE} != "cmdline"
155.  error
156.endif
157# The above '.unexport' has no effect since UnexportVar requires a global
158# variable of the same name to be defined, otherwise nothing is unexported.
159CMDLINE=	global
160.unexport CMDLINE
161.undef CMDLINE
162.if ${CMDLINE} != "cmdline"
163.  error
164.endif
165# This still didn't work since there must not only be a global variable, the
166# variable must be marked as exported as well, which it wasn't before.
167CMDLINE=	global
168.export CMDLINE
169.unexport CMDLINE
170.undef CMDLINE
171.if ${CMDLINE:Uundefined} != "undefined"
172.  error
173.endif
174# Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
175# traces from the environment are gone as well.  To do that, a global variable
176# had to be defined and exported, something that is far from obvious.  To
177# recap, here is the essence of the above story:
178.MAKEFLAGS: CMDLINE=cmdline	# have a cmdline + environment variable
179.if ${:U:@CMDLINE@@}}		# undefine cmdline, keep environment
180.endif
181CMDLINE=	global		# needed for deleting the environment
182.export CMDLINE			# needed for deleting the environment
183.unexport CMDLINE		# delete the environment
184.undef CMDLINE			# delete the global helper variable
185.if ${CMDLINE:Uundefined} != "undefined"
186.  error			# 'CMDLINE' is gone now from all scopes
187.endif
188
189
190# In the loop body text of the ':@' modifier, a literal '$' is written as '$$',
191# not '\$'.  In the following example, each '$$' turns into a single '$',
192# except for '$i', which is replaced with the then-current value '1' of the
193# iteration variable.
194#
195# XXX: was broken in var.c 1.1028 from 2022-08-08, reverted in var.c 1.1029
196# from 2022-08-23; see parse-var.mk, keyword 'BRACE_GROUP'.
197all: varmod-loop-literal-dollar
198varmod-loop-literal-dollar: .PHONY
199	: ${:U1:@i@ t=$$(( $${t:-0} + $i ))@}
200
201
202# When parsing the loop body, each '\$', '\@' and '\\' is unescaped to '$',
203# '@' and '\'; all other backslashes are retained.
204#
205# In practice, the '$' is not escaped as '\$', as there is a second round of
206# unescaping '$$' to '$' later when the loop body is expanded after setting the
207# iteration variable.
208#
209# After the iteration variable has been set, the loop body is expanded with
210# this unescaping, regardless of whether .MAKE.SAVE_DOLLARS is set or not:
211#	$$			a literal '$'
212#	$x, ${var}, $(var)	a nested expression
213#	any other character	itself
214all: escape-modifier
215escape-modifier: .PHONY
216	# In the first round, '\$ ' is unescaped to '$ ', and since the
217	# variable named ' ' is not defined, the expression '$ ' expands to an
218	# empty string.
219	# expect: :  dollar=end
220	: ${:U1:@i@ dollar=\$ end@}
221
222	# Like in other modifiers, '\ ' is preserved, since ' ' is not one of
223	# the characters that _must_ be escaped.
224	# expect: :  backslash=\ end
225	: ${:U1:@i@ backslash=\ end@}
226
227	# expect: :  dollar=$ at=@ backslash=\ end
228	: ${:U1:@i@ dollar=\$\$ at=\@ backslash=\\ end@}
229	# expect: :  dollar=$$ at=@@ backslash=\\ end
230	: ${:U1:@i@ dollar=\$\$\$\$ at=\@\@ backslash=\\\\ end@}
231	# expect: :  dollar=$$ at=@@ backslash=\\ end
232	: ${:U1:@i@ dollar=$$$$ at=\@\@ backslash=\\\\ end@}
233
234all: .PHONY
235