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