xref: /freebsd/contrib/bmake/unit-tests/directive-for-escape.mk (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1# $NetBSD: directive-for-escape.mk,v 1.3 2020/12/31 14:26:37 rillig Exp $
2#
3# Test escaping of special characters in the iteration values of a .for loop.
4# These values get expanded later using the :U variable modifier, and this
5# escaping and unescaping must pass all characters and strings effectively
6# unmodified.
7
8.MAKEFLAGS: -df
9
10# Even though the .for loops takes quotes into account when splitting the
11# string into words, the quotes don't need to be balances, as of 2020-12-31.
12# This could be considered a bug.
13ASCII=	!"\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
14
15# XXX: As of 2020-12-31, the '#' is not preserved in the expanded body of
16# the loop since it would not need only the escaping for the :U variable
17# modifier but also the escaping for the line-end comment.
18.for chars in ${ASCII}
19.  info ${chars}
20.endfor
21
22# As of 2020-12-31, using 2 backslashes before be '#' would treat the '#'
23# as comment character.  Using 3 backslashes doesn't help either since
24# then the situation is essentially the same as with 1 backslash.
25# This means that a '#' sign cannot be passed in the value of a .for loop
26# at all.
27ASCII.2020-12-31=	!"\\\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
28.for chars in ${ASCII.2020-12-31}
29.  info ${chars}
30.endfor
31
32# Cover the code in for_var_len.
33#
34# XXX: It is unexpected that the variable V gets expanded in the loop body.
35# The double '$$' should prevent exactly this.  Probably nobody was
36# adventurous enough to use literal dollar signs in the values for a .for
37# loop.
38V=		value
39VALUES=		$$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
40.for i in ${VALUES}
41.  info $i
42.endfor
43
44# Cover the code for nested '{}' in for_var_len.
45#
46# The value of VALUES is not a variable expression.  Instead, it is meant to
47# represent dollar, lbrace, "UNDEF:U", backslash, dollar, backslash, dollar,
48# space, nested braces, space, "end}".
49VALUES=		$${UNDEF:U\$$\$$ {{}} end}
50# XXX: Where does the '\$$\$$' get converted into a single '\$'?
51.for i in ${VALUES}
52.  info $i
53.endfor
54
55# A single trailing dollar doesn't happen in practice.
56# The dollar sign is correctly passed through to the body of the .for loop.
57# There, it is expanded by the .info directive, but even there a trailing
58# dollar sign is kept as-is.
59.for i in ${:U\$}
60.  info ${i}
61.endfor
62
63# As of 2020-12-31, the name of the iteration variable can even contain
64# colons, which then affects variable expressions having this exact modifier.
65# This is clearly an unintended side effect of the implementation.
66NUMBERS=	one two three
67.for NUMBERS:M*e in replaced
68.  info ${NUMBERS} ${NUMBERS:M*e}
69.endfor
70
71# As of 2020-12-31, the name of the iteration variable can contain braces,
72# which gets even more surprising than colons, since it allows to replace
73# sequences of variable expressions.  There is no practical use case for
74# this, though.
75BASENAME=	one
76EXT=		.c
77.for BASENAME}${EXT in replaced
78.  info ${BASENAME}${EXT}
79.endfor
80
81# Demonstrate the various ways to refer to the iteration variable.
82i=		outer
83i2=		two
84i,=		comma
85.for i in inner
86.  info .        $$i: $i
87.  info .      $${i}: ${i}
88.  info .   $${i:M*}: ${i:M*}
89.  info .      $$(i): $(i)
90.  info .   $$(i:M*): $(i:M*)
91.  info . $${i$${:U}}: ${i${:U}}
92.  info .    $${i\}}: ${i\}}	# XXX: unclear why SubstVarLong needs this
93.  info .     $${i2}: ${i2}
94.  info .     $${i,}: ${i,}
95.  info .  adjacent: $i${i}${i:M*}$i
96.endfor
97