directive-for-escape.mk revision 1.27 1 # $NetBSD: directive-for-escape.mk,v 1.27 2024/07/06 10:14:35 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 unmodified.
6
7 .MAKEFLAGS: -df
8
9 # Even though the .for loops take quotes into account when splitting the
10 # string into words, the quotes don't need to be balanced, as of 2020-12-31.
11 # This could be considered a bug.
12 ASCII= !"\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
13
14
15 # XXX: As of 2020-12-31, the '#' is not preserved in the expanded body of
16 # the loop. Not only would it need the escaping for the variable modifier
17 # ':U' but also the escaping for the line-end comment.
18 .for chars in ${ASCII}
19 # expect+2: while evaluating "${:U!"" with value "!"": Unclosed expression, expecting '}' for modifier "U!""
20 # expect+1: !"
21 . info ${chars}
22 .endfor
23
24 # As of 2020-12-31, using 2 backslashes before be '#' would treat the '#'
25 # as comment character. Using 3 backslashes doesn't help either since
26 # then the situation is essentially the same as with 1 backslash.
27 # This means that a '#' sign cannot be passed in the value of a .for loop
28 # at all.
29 ASCII.2020-12-31= !"\\\#$$%&'()*+,-./0-9:;<=>?@A-Z[\]_^a-z{|}~
30 .for chars in ${ASCII.2020-12-31}
31 # expect+2: while evaluating "${:U!"\\\\" with value "!"\\": Unclosed expression, expecting '}' for modifier "U!"\\\\"
32 # expect+1: !"\\
33 . info ${chars}
34 .endfor
35
36 # Cover the code in ExprLen.
37 #
38 # XXX: It is unexpected that the variable V gets expanded in the loop body.
39 # The double '$$' should intuitively prevent exactly this. Probably nobody
40 # was adventurous enough to use literal dollar signs in the values of a .for
41 # loop, allowing this edge case to go unnoticed for years.
42 #
43 # See for.c, function ExprLen.
44 V= value
45 VALUES= $$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
46 .for i in ${VALUES}
47 # expect: . info ${:U\$}
48 # expect+9: $
49 # expect: . info ${:U${V}}
50 # expect+7: value
51 # expect: . info ${:U${V:=-with-modifier}}
52 # expect+5: value-with-modifier
53 # expect: . info ${:U$(V)}
54 # expect+3: value
55 # expect: . info ${:U$(V:=-with-modifier)}
56 # expect+1: value-with-modifier
57 . info $i
58 .endfor
59 #
60 # Providing the loop items directly has the same effect.
61 .for i in $$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
62 # expect: . info ${:U\$}
63 # expect+6: $
64 # expect: . info ${:U${V}}
65 # expect+4: value
66 # expect+3: value-with-modifier
67 # expect+2: value
68 # expect+1: value-with-modifier
69 . info $i
70 .endfor
71
72 # Try to cover the code for nested '{}' in ExprLen, without success.
73 #
74 # The value of the variable VALUES is not meant to be an expression.
75 # Instead, it is meant to represent literal text, the only escaping mechanism
76 # being that each '$' is written as '$$'.
77 VALUES= $${UNDEF:U\$$\$$ {{}} end}
78 #
79 # The .for loop splits ${VALUES} into 3 words, at the space characters, since
80 # the '$$' is an ordinary character and the spaces are not escaped.
81 # Word 1 is '${UNDEF:U\$\$'
82 # Word 2 is '{{}}'
83 # Word 3 is 'end}'
84 #
85 # Each of these words is now inserted in the body of the .for loop.
86 .for i in ${VALUES}
87 # $i
88 .endfor
89 #
90 # When these words are injected into the body of the .for loop, each inside a
91 # '${:U...}' expression, the result is:
92 #
93 # expect: For: loop body with i = ${UNDEF:U\$\$:
94 # expect: # ${:U\${UNDEF\:U\\$\\$}
95 # expect: For: loop body with i = {{}}:
96 # expect: # ${:U{{\}\}}
97 # expect: For: loop body with i = end}:
98 # expect: # ${:Uend\}}
99 # expect: For: end for 1
100 #
101 # The first of these expressions is the most interesting one, due to its many
102 # special characters. This expression is properly balanced:
103 #
104 # Text Meaning Explanation
105 # \$ $ escaped
106 # { { ordinary text
107 # UNDEF UNDEF ordinary text
108 # \: : escaped
109 # U U ordinary text
110 # \\ \ escaped
111 # $\ (expr) an expression, the variable name is '\'
112 # \$ $ escaped
113 #
114 # To make the expression '$\' visible, define it to an actual word:
115 ${:U\\}= backslash
116 .for i in ${VALUES}
117 # expect+3: ${UNDEF:U\backslash$
118 # expect+2: {{}}
119 # expect+1: end}
120 . info $i
121 .endfor
122 #
123 # FIXME: There was no expression '$\' in the original text of the variable
124 # 'VALUES', that's a surprise in the parser.
125
126
127 # Second try to cover the code for nested '{}' in ExprLen.
128 #
129 # XXX: It is not the job of ExprLen to parse an expression, it is naive to
130 # expect ExprLen to get all the details right in just a few lines of code.
131 # Each variable modifier has its own inconsistent way of parsing nested
132 # expressions, braces and parentheses. (Compare ':M', ':S', and
133 # ':D' for details.) The only sensible thing to do is therefore to let
134 # Var_Parse do all the parsing work.
135 VALUES= begin<$${UNDEF:Ufallback:N{{{}}}}>end
136 .for i in ${VALUES}
137 # expect+1: begin<fallback>end
138 . info $i
139 .endfor
140
141 # A single trailing dollar doesn't happen in practice.
142 # The dollar sign is correctly passed through to the body of the .for loop.
143 # There, it is expanded by the .info directive, but even there a trailing
144 # dollar sign is kept as-is.
145 .for i in ${:U\$}
146 # expect+1: $
147 . info ${i}
148 .endfor
149
150 # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
151 # could contain colons, which affected expressions having this exact
152 # modifier. This possibility was neither intended nor documented.
153 NUMBERS= one two three
154 # expect+1: invalid character ':' in .for loop variable name
155 .for NUMBERS:M*e in replaced
156 . info ${NUMBERS} ${NUMBERS:M*e}
157 .endfor
158
159 # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
160 # could contain braces, which allowed to replace sequences of
161 # expressions. This possibility was neither intended nor documented.
162 BASENAME= one
163 EXT= .c
164 # expect+1: invalid character '}' in .for loop variable name
165 .for BASENAME}${EXT in replaced
166 . info ${BASENAME}${EXT}
167 .endfor
168
169 # Demonstrate the various ways to refer to the iteration variable.
170 i= outer
171 i2= two
172 i,= comma
173 .for i in inner
174 # expect+1: . $i: inner
175 . info . $$i: $i
176 # expect+1: . ${i}: inner
177 . info . $${i}: ${i}
178 # expect+1: . ${i:M*}: inner
179 . info . $${i:M*}: ${i:M*}
180 # expect+1: . $(i): inner
181 . info . $$(i): $(i)
182 # expect+1: . $(i:M*): inner
183 . info . $$(i:M*): $(i:M*)
184 # expect+1: . ${i${:U}}: outer
185 . info . $${i$${:U}}: ${i${:U}}
186 # expect+1: . ${i\}}: inner}
187 . info . $${i\}}: ${i\}} # XXX: unclear why ForLoop_SubstVarLong needs this
188 # expect+1: . ${i2}: two
189 . info . $${i2}: ${i2}
190 # expect+1: . ${i,}: comma
191 . info . $${i,}: ${i,}
192 # expect+1: . adjacent: innerinnerinnerinner
193 . info . adjacent: $i${i}${i:M*}$i
194 .endfor
195
196 # Before for.c 1.173 from 2023-05-08, the variable name could be a single '$'
197 # since there was no check on valid variable names. ForLoop_SubstVarShort
198 # skipped "stupid" variable names though, but ForLoop_SubstVarLong naively
199 # parsed the body of the loop, substituting each '${$}' with an actual
200 # '${:Udollar}'.
201 # expect+1: invalid character '$' in .for loop variable name
202 .for $ in dollar
203 . info eight $$$$$$$$ and no cents.
204 . info eight ${$}${$}${$}${$} and no cents.
205 .endfor
206 # Outside a .for loop, '${$}' is interpreted differently. The outer '$' starts
207 # an expression. The inner '$' is followed by a '}' and is thus a
208 # silent syntax error, the '$' is skipped. The variable name is thus '', and
209 # since since there is never a variable named '', the whole expression '${$}'
210 # evaluates to an empty string.
211 closing-brace= } # guard against an
212 ${closing-brace}= <closing-brace> # alternative interpretation
213 # expect+1: eight and no cents.
214 .info eight ${$}${$}${$}${$} and no cents.
215
216 # What happens if the values from the .for loop contain a literal newline?
217 # Before for.c 1.144 from 2021-06-25, the newline was passed verbatim to the
218 # body of the .for loop, where it was then interpreted as a literal newline,
219 # leading to syntax errors such as "Unclosed variable expression" in the upper
220 # line and "Invalid line type" in the lower line.
221 #
222 # The error message occurs in the line of the .for loop since that's the place
223 # where the body of the .for loop is constructed, and at this point the
224 # newline character gets replaced with a plain space.
225 # expect+2: newline in .for value
226 # expect+1: newline in .for value
227 .for i in "${.newline}"
228 # expect+1: short: " "
229 . info short: $i
230 # expect+1: long: " "
231 . info long: ${i}
232 .endfor
233 # No error since the newline character is not actually used in the body.
234 .for i in "${.newline}"
235 .endfor
236
237 # Between for.c 1.161 from 2022-01-08 and before for.c 1.163 from 2022-01-09,
238 # a newline character in a .for loop led to a crash since at the point where
239 # the error message including the stack trace is printed, the body of the .for
240 # loop is assembled, and at that point, ForLoop.nextItem had already been
241 # advanced.
242 .MAKEFLAGS: -dp
243 # expect+1: newline in .for value
244 .for i in "${.newline}"
245 : $i
246 .endfor
247 .MAKEFLAGS: -d0
248
249 .MAKEFLAGS: -df
250 .for i in \# \\\#
251 # $i
252 .endfor
253
254 .for i in $$ $$i $$(i) $${i} $$$$ $$$$$$$$ $${:U\$$\$$}
255 # $i
256 .endfor
257
258 # The expression '${.TARGET}' must be preserved as it is one of the 7 built-in
259 # target-local variables. See for.c 1.45 from 2009-01-14.
260 .for i in ${.TARGET} $${.TARGET} $$${.TARGET} $$$${.TARGET}
261 # $i
262 .endfor
263 # expect: # ${:U${.TARGET}}
264 # XXX: Why does '$' result in the same text as '$$'?
265 # expect: # ${:U${.TARGET}}
266 # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
267 # expect: # ${:U$${.TARGET\}}
268 # XXX: Why does '$' result in the same text as '$$'?
269 # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
270 # expect: # ${:U$${.TARGET\}}
271
272 .for i in ((( {{{ ))) }}}
273 # $i
274 .endfor
275
276
277 # When generating the body of a .for loop, recognizing the expressions is done
278 # using simple heuristics. These can go wrong in ambiguous cases like this.
279 # The variable name ',' is unusual as it is not a pronounceable name, but the
280 # same principle applies for other names as well. In this case, the text '$,'
281 # is replaced with the expression '${:U1}', even though the text does not
282 # represent an expression.
283 .for , in 1
284 # $$i $i
285 # VAR= $$i $i ${a:S,from$,to,}
286 VAR= $$i $i ${a:S,from$,to,}
287 .endfor
288 # expect: # $$i $i
289 # expect: # VAR= $$i $i ${a:S,from${:U1}to,}
290 # expect: VAR= $$i $i ${a:S,from${:U1}to,}
291 #
292 # When the above variable is evaluated, make will complain about the
293 # unfinished modifier ':S', as it is missing a comma.
294