directive-for-escape.mk revision 1.24 1 # $NetBSD: directive-for-escape.mk,v 1.24 2024/07/05 17:41:50 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: make: Unclosed expression, expecting '}' for modifier "U!"" of variable "" with value "!""
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: make: Unclosed expression, expecting '}' for modifier "U!"\\\\" of variable "" with value "!"\\"
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 . info $i
48 .endfor
49 # expect: . info ${:U\$}
50 # expect-3: $
51 # expect: . info ${:U${V}}
52 # expect-5: value
53 # expect: . info ${:U${V:=-with-modifier}}
54 # expect-7: value-with-modifier
55 # expect: . info ${:U$(V)}
56 # expect-9: value
57 # expect: . info ${:U$(V:=-with-modifier)}
58 # expect-11: value-with-modifier
59 #
60 # Providing the loop items directly has the same effect.
61 .for i in $$ $${V} $${V:=-with-modifier} $$(V) $$(V:=-with-modifier)
62 . info $i
63 .endfor
64 # expect: . info ${:U\$}
65 # expect-3: $
66 # expect: . info ${:U${V}}
67 # expect-5: value
68 # expect-6: value-with-modifier
69 # expect-7: value
70 # expect-8: value-with-modifier
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 . info $i
118 .endfor
119 #
120 # expect-3: ${UNDEF:U\backslash$
121 # expect-4: {{}}
122 # expect-5: end}
123 #
124 # FIXME: There was no expression '$\' in the original text of the variable
125 # 'VALUES', that's a surprise in the parser.
126
127
128 # Second try to cover the code for nested '{}' in ExprLen.
129 #
130 # XXX: It is not the job of ExprLen to parse an expression, it is naive to
131 # expect ExprLen to get all the details right in just a few lines of code.
132 # Each variable modifier has its own inconsistent way of parsing nested
133 # expressions, braces and parentheses. (Compare ':M', ':S', and
134 # ':D' for details.) The only sensible thing to do is therefore to let
135 # Var_Parse do all the parsing work.
136 VALUES= begin<$${UNDEF:Ufallback:N{{{}}}}>end
137 .for i in ${VALUES}
138 . info $i
139 .endfor
140 # expect-2: begin<fallback>end
141
142 # A single trailing dollar doesn't happen in practice.
143 # The dollar sign is correctly passed through to the body of the .for loop.
144 # There, it is expanded by the .info directive, but even there a trailing
145 # dollar sign is kept as-is.
146 .for i in ${:U\$}
147 . info ${i}
148 .endfor
149 # expect-2: $
150
151 # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
152 # could contain colons, which affected expressions having this exact
153 # modifier. This possibility was neither intended nor documented.
154 NUMBERS= one two three
155 # expect+1: invalid character ':' in .for loop variable name
156 .for NUMBERS:M*e in replaced
157 . info ${NUMBERS} ${NUMBERS:M*e}
158 .endfor
159
160 # Before for.c 1.173 from 2023-05-08, the name of the iteration variable
161 # could contain braces, which allowed to replace sequences of
162 # expressions. This possibility was neither intended nor documented.
163 BASENAME= one
164 EXT= .c
165 # expect+1: invalid character '}' in .for loop variable name
166 .for BASENAME}${EXT in replaced
167 . info ${BASENAME}${EXT}
168 .endfor
169
170 # Demonstrate the various ways to refer to the iteration variable.
171 i= outer
172 i2= two
173 i,= comma
174 .for i in inner
175 . info . $$i: $i
176 . info . $${i}: ${i}
177 . info . $${i:M*}: ${i:M*}
178 . info . $$(i): $(i)
179 . info . $$(i:M*): $(i:M*)
180 . info . $${i$${:U}}: ${i${:U}}
181 . info . $${i\}}: ${i\}} # XXX: unclear why ForLoop_SubstVarLong needs this
182 . info . $${i2}: ${i2}
183 . info . $${i,}: ${i,}
184 . info . adjacent: $i${i}${i:M*}$i
185 .endfor
186 # expect-11: . $i: inner
187 # expect-11: . ${i}: inner
188 # expect-11: . ${i:M*}: inner
189 # expect-11: . $(i): inner
190 # expect-11: . $(i:M*): inner
191 # expect-11: . ${i${:U}}: outer
192 # expect-11: . ${i\}}: inner}
193 # expect-11: . ${i2}: two
194 # expect-11: . ${i,}: comma
195 # expect-11: . adjacent: innerinnerinnerinner
196
197 # Before for.c 1.173 from 2023-05-08, the variable name could be a single '$'
198 # since there was no check on valid variable names. ForLoop_SubstVarShort
199 # skipped "stupid" variable names though, but ForLoop_SubstVarLong naively
200 # parsed the body of the loop, substituting each '${$}' with an actual
201 # '${:Udollar}'.
202 # expect+1: invalid character '$' in .for loop variable name
203 .for $ in dollar
204 . info eight $$$$$$$$ and no cents.
205 . info eight ${$}${$}${$}${$} and no cents.
206 .endfor
207 # Outside a .for loop, '${$}' is interpreted differently. The outer '$' starts
208 # an expression. The inner '$' is followed by a '}' and is thus a
209 # silent syntax error, the '$' is skipped. The variable name is thus '', and
210 # since since there is never a variable named '', the whole expression '${$}'
211 # evaluates to an empty string.
212 closing-brace= } # guard against an
213 ${closing-brace}= <closing-brace> # alternative interpretation
214 # expect+1: eight and no cents.
215 .info eight ${$}${$}${$}${$} and no cents.
216
217 # What happens if the values from the .for loop contain a literal newline?
218 # Before for.c 1.144 from 2021-06-25, the newline was passed verbatim to the
219 # body of the .for loop, where it was then interpreted as a literal newline,
220 # leading to syntax errors such as "Unclosed variable expression" in the upper
221 # line and "Invalid line type" in the lower line.
222 #
223 # The error message occurs in the line of the .for loop since that's the place
224 # where the body of the .for loop is constructed, and at this point the
225 # newline character gets replaced with a plain space.
226 # expect+2: newline in .for value
227 # expect+1: newline in .for value
228 .for i in "${.newline}"
229 . info short: $i
230 . info long: ${i}
231 .endfor
232 # expect-3: short: " "
233 # expect-3: long: " "
234
235 # No error since the newline character is not actually used.
236 .for i in "${.newline}"
237 .endfor
238
239 # Between for.c 1.161 from 2022-01-08 and before for.c 1.163 from 2022-01-09,
240 # a newline character in a .for loop led to a crash since at the point where
241 # the error message including the stack trace is printed, the body of the .for
242 # loop is assembled, and at that point, ForLoop.nextItem had already been
243 # advanced.
244 .MAKEFLAGS: -dp
245 # expect+1: newline in .for value
246 .for i in "${.newline}"
247 : $i
248 .endfor
249 .MAKEFLAGS: -d0
250
251 .MAKEFLAGS: -df
252 .for i in \# \\\#
253 # $i
254 .endfor
255
256 .for i in $$ $$i $$(i) $${i} $$$$ $$$$$$$$ $${:U\$$\$$}
257 # $i
258 .endfor
259
260 # The expression '${.TARGET}' must be preserved as it is one of the 7 built-in
261 # target-local variables. See for.c 1.45 from 2009-01-14.
262 .for i in ${.TARGET} $${.TARGET} $$${.TARGET} $$$${.TARGET}
263 # $i
264 .endfor
265 # expect: # ${:U${.TARGET}}
266 # XXX: Why does '$' result in the same text as '$$'?
267 # expect: # ${:U${.TARGET}}
268 # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
269 # expect: # ${:U$${.TARGET\}}
270 # XXX: Why does '$' result in the same text as '$$'?
271 # XXX: Why does the '$$' before the '${.TARGET}' lead to an escaped '}'?
272 # expect: # ${:U$${.TARGET\}}
273
274 .for i in ((( {{{ ))) }}}
275 # $i
276 .endfor
277
278
279 # When generating the body of a .for loop, recognizing the expressions is done
280 # using simple heuristics. These can go wrong in ambiguous cases like this.
281 # The variable name ',' is unusual as it is not a pronounceable name, but the
282 # same principle applies for other names as well. In this case, the text '$,'
283 # is replaced with the expression '${:U1}', even though the text does not
284 # represent an expression.
285 .for , in 1
286 # $$i $i
287 # VAR= $$i $i ${a:S,from$,to,}
288 VAR= $$i $i ${a:S,from$,to,}
289 .endfor
290 # expect: # $$i $i
291 # expect: # VAR= $$i $i ${a:S,from${:U1}to,}
292 # expect: VAR= $$i $i ${a:S,from${:U1}to,}
293 #
294 # When the above variable is evaluated, make will complain about the
295 # unfinished modifier ':S', as it is missing a comma.
296