xref: /freebsd/contrib/bmake/unit-tests/directive-for.mk (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1# $NetBSD: directive-for.mk,v 1.20 2023/05/10 13:03:06 rillig Exp $
2#
3# Tests for the .for directive.
4#
5# TODO: Describe naming conventions for the loop variables.
6#	.for f in values
7#	.for file in values
8#	.for _FILE_ in values
9#	.for .FILE. in values
10#	.for _f_ in values
11#
12# See also:
13#	varmod-loop.mk		The ':@var@...@' modifier
14
15# expect-all
16
17# A typical use case for a .for loop is to populate a variable with a list of
18# values depending on other variables.  In simple cases, the same effect can
19# be achieved using the ':@var@${var}@' modifier.
20.undef NUMBERS
21.for num in 1 2 3
22NUMBERS+=	${num}
23.endfor
24.if ${NUMBERS} != "1 2 3"
25.  error
26.endif
27
28
29# The .for loop also works for multiple iteration variables.
30# This is something that the modifier :@ cannot do.
31.for name value in VARNAME value NAME2 value2
32${name}=	${value}
33.endfor
34.if ${VARNAME} != "value" || ${NAME2} != "value2"
35.  error
36.endif
37
38
39# The .for loop splits the items at whitespace, taking quotes into account,
40# just like the :M or :S modifiers.
41#
42# Until 2012-06-03, the .for loop had split the items exactly at whitespace,
43# without taking the quotes into account.  This had resulted in 10 words.
44.undef WORDS
45.for var in one t\ w\ o "three three" 'four four' `five six`
46WORDS+=	counted
47.endfor
48.if ${WORDS:[#]} != 6
49.  error
50.endif
51
52
53# In the body of the .for loop, the iteration variables can be accessed
54# like normal variables, even though they are not really variables.
55#
56# Instead, before interpreting the body of the .for loop, the body is
57# generated by replacing each expression ${var} with ${:U1}, ${:U2} and so
58# on.
59#
60# A noticeable effect of this implementation technique is that the .for
61# iteration variables and the normal global variables live in separate
62# namespaces and do not influence each other.  The "scope" of the .for loop
63# variables is restricted to the current makefile, it does not reach over to
64# any included makefiles.
65var=	value before
66var2=	value before
67.for var var2 in 1 2 3 4
68.endfor
69.if ${var} != "value before"
70.  warning After the .for loop, var must still have its original value.
71.endif
72.if ${var2} != "value before"
73.  warning After the .for loop, var2 must still have its original value.
74.endif
75
76# Everything from the paragraph above also applies if the loop body is
77# empty.  In this particular example, the items to be iterated are empty as
78# well.
79var=	value before
80var2=	value before
81.for var var2 in ${:U}
82.endfor
83.if ${var} != "value before"
84.  warning After the .for loop, var must still have its original value.
85.endif
86.if ${var2} != "value before"
87.  warning After the .for loop, var2 must still have its original value.
88.endif
89
90# Until 2008-12-21, the values of the iteration variables were simply
91# inserted as plain text and then parsed as usual, which made it possible
92# to achieve all kinds of strange effects, such as generating '.if'
93# directives or inserting '$' characters in random places, thereby changing
94# how following '$' are interpreted.
95#
96# Before that date, the .for loop below expanded to:
97#	EXPANSION+= value
98# Since that date, the .for loop below expands to:
99#	EXPANSION${:U+}= value
100#
101EXPANSION=		before
102EXPANSION+ =		before
103.for plus in +
104EXPANSION${plus}=	value
105.endfor
106.if ${EXPANSION} != "before"
107.  error This must be a make from before 2009.
108.endif
109.if ${EXPANSION+} != "value"
110.  error This must be a make from before 2009.
111.endif
112
113# When the outer .for loop is expanded, it sees the expression ${i} and
114# expands it.  The inner loop then only sees the expression ${:Uouter} and
115# has nothing more to expand.
116.for i in outer
117.  for i in inner
118# expect+1: outer
119.    info ${i}
120.  endfor
121.endfor
122
123
124# From https://gnats.netbsd.org/29985.
125#
126# Until 2008-12-21, the .for loop was expanded by replacing the variable
127# value literally in the body.  This could lead to situations where the
128# characters from the variable value were interpreted as markup rather than
129# plain text.
130#
131# Until 2012-06-03, the .for loop had split the words at whitespace, without
132# taking quotes into account.  This made it possible to have variable values
133# like "a:\ a:\file.txt" that ended in a single backslash.  Since then, the
134# variable values have been replaced with expressions of the form ${:U...},
135# which are not interpreted as code anymore.
136.for path in a:\ a:\file.txt d:\\ d:\\file.txt
137.  info ${path}
138.endfor
139# expect-2: a:\ a:\file.txt
140# expect-3: d:\\
141# expect-4: d:\\file.txt
142
143
144# Ensure that braces and parentheses are properly escaped by the .for loop.
145# Each line must print the same word 3 times.
146# See ForLoop_SubstBody.
147.for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{
148.  info $v ${v} $(v)
149.endfor
150# expect-02: ( ( (
151# expect-03: [ [ [
152# expect-04: { { {
153# expect-05: ) ) )
154# expect-06: ] ] ]
155# expect-07: } } }
156# expect-08: (()) (()) (())
157# expect-09: [[]] [[]] [[]]
158# expect-10: {{}} {{}} {{}}
159# expect-11: )( )( )(
160# expect-12: ][ ][ ][
161# expect-13: }{ }{ }{
162
163# Before 2023-05-09, the variable names could contain arbitrary characters,
164# except for whitespace, allowing for creative side effects, as usual for
165# arbitrary code injection.
166var=	outer
167# expect+1: invalid character ':' in .for loop variable name
168.for var:Q in value "quoted"
169.  info <${var}> <${var:Q}> <${var:Q:Q}>
170.endfor
171
172# Before 2023-05-09, when variable names could contain '$', the short
173# expression '$$' was preserved, the long expressions were substituted.
174# expect+1: invalid character '$' in .for loop variable name
175.for $ in value
176.  info <$$> <${$}> <$($)>
177.endfor
178
179
180# https://gnats.netbsd.org/53146 mentions the idea of using a dynamic
181# variable name in .for loops, based on some other variable.  The .for loops
182# are already tricky enough to understand in detail, even without this
183# possibility, therefore the variable names are restricted to using harmless
184# characters only.
185INDIRECT=	direct
186# expect+1: invalid character '$' in .for loop variable name
187.for $(INDIRECT) in value
188# If the variable name could be chosen dynamically, the iteration variable
189# might have been 'direct', thereby expanding the expression '${direct}'.
190.  info <$(INDIRECT)> <$(direct)> <$($(INDIRECT))>
191.endfor
192
193
194# XXX: A parse error or evaluation error in the items of the .for loop
195# should skip the whole loop.  As of 2023-05-09, the loop is expanded as
196# usual.
197# expect+1: Unknown modifier "Z"
198.for var in word1 ${:Uword2:Z} word3
199.  info XXX: Not reached ${var}
200.endfor
201# expect-2: XXX: Not reached word1
202# expect-3: XXX: Not reached word3
203
204
205# An empty list of variables to the left of the 'in' is a parse error.
206.for in value			# expect+0: no iteration variables in for
207.  error
208.endfor
209
210# An empty list of iteration values to the right of the 'in' is accepted.
211# Unlike in the shell, it is not a parse error.
212.for var in
213.  error
214.endfor
215
216# If the iteration values become empty after expanding the expressions, the
217# body of the loop is not evaluated.  It is not a parse error.
218.for var in ${:U}
219.  error
220.endfor
221
222
223# The loop body can be empty.
224.for var in 1 2 3
225.endfor
226
227
228# A mismatched .if inside a .for loop is detected each time when the loop body
229# is processed.
230.for var in value
231.  if 0
232.endfor				# expect+0: 1 open conditional
233
234# If there are no iteration values, the loop body is not processed, and the
235# check for mismatched conditionals is not performed.
236.for var in ${:U}
237.  if 0
238.endfor
239
240
241# When a .for without the corresponding .endfor occurs in an inactive branch
242# of an .if, the .for directive is just skipped, it does not even need a
243# corresponding .endfor.  In other words, the behavior of the parser depends
244# on the actual values of the conditions in the .if clauses.
245.if 0
246.  for var in value		# does not need a corresponding .endfor
247.endif
248.endfor				# expect+0: for-less endfor
249.endif				# expect+0: if-less endif
250
251
252# When a .for without the corresponding .endfor occurs in an active branch of
253# an .if, the parser just counts the number of .for and .endfor directives,
254# without looking at any other directives.
255.if 1
256.  for var in value
257.    endif			# expect+0: if-less endif
258.  endfor			# no 'for-less endfor'
259.endif				# no 'if-less endif'
260
261
262# Before for.c 1.172 from 2023-05-08, when make parsed a .for loop, it
263# assumed that there was no line continuation between the '.' and the 'for'
264# or 'endfor', as there is no practical reason to break the line at this
265# point.
266#
267# When make scanned the outer .for loop, it did not recognize the inner .for
268# loop as such and instead treated it as an unknown directive.  The body of
269# the outer .for loop thus ended above the '.endfor'.
270#
271# When make scanned the inner .for loop, it did not recognize the inner
272# .endfor as such, which led to a parse error 'Unexpected end of file in .for
273# loop' from the '.endfor' line, followed by a second parse error 'for-less
274# .endfor' from the '.\\n endfor' line.
275.MAKEFLAGS: -df
276.for outer in o
277.\
278   for inner in i
279.\
280   endfor
281.endfor
282.MAKEFLAGS: -d0
283
284
285# When there is a variable definition 'scope=cmdline' from the command line
286# (which has higher precedence than global variables) and a .for loop iterates
287# over a variable of the same name, the expression '${scope}' expands to the
288# value from the .for loop.  This is because when the body of the .for loop is
289# expanded, the expression '${scope}' is textually replaced with ${:Uloop}',
290# without resolving any other variable names (ForLoop_SubstBody).  Later, when
291# the body of the .for loop is actually interpreted, the body text doesn't
292# contain the word 'scope' anymore.
293.MAKEFLAGS: scope=cmdline
294.for scope in loop
295.  if ${scope} != "loop"
296.    error
297.  endif
298.endfor
299
300
301# Since at least 1993, iteration stops at the first newline.
302# Back then, the .newline variable didn't exist, therefore it was unlikely
303# that a newline ever occurred.
304.for var in a${.newline}b${.newline}c
305.  info newline-item=(${var})
306.endfor
307# expect-2: newline-item=(a)
308