varmod-loop.mk revision 1.13 1 1.13 rillig # $NetBSD: varmod-loop.mk,v 1.13 2021/03/15 17:54:49 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :@var (a] ...${var}...@ variable modifier.
4 1.1 rillig
5 1.8 rillig .MAKE.SAVE_DOLLARS= yes
6 1.8 rillig
7 1.13 rillig all: varname-overwriting-target
8 1.2 rillig all: mod-loop-resolve
9 1.2 rillig all: mod-loop-varname-dollar
10 1.2 rillig all: mod-loop-dollar
11 1.1 rillig
12 1.2 rillig # In the :@ modifier, the name of the loop variable can even be generated
13 1.2 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
14 1.2 rillig # will ever depend on this, but technically it's possible.
15 1.2 rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
16 1.13 rillig .if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
17 1.13 rillig . error
18 1.13 rillig .endif
19 1.13 rillig
20 1.13 rillig # ":::" is a very creative variable name, unlikely in practice.
21 1.13 rillig # The expression ${\:\:\:} would not work since backslashes can only
22 1.13 rillig # be escaped in the modifiers, but not in the variable name.
23 1.13 rillig .if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
24 1.13 rillig . error
25 1.13 rillig .endif
26 1.5 rillig
27 1.13 rillig # "@@" is another creative variable name.
28 1.13 rillig .if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
29 1.13 rillig . error
30 1.13 rillig .endif
31 1.5 rillig
32 1.13 rillig varname-overwriting-target:
33 1.2 rillig # Even "@" works as a variable name since the variable is installed
34 1.2 rillig # in the "current" scope, which in this case is the one from the
35 1.13 rillig # target. Because of this, after the loop has finished, '$@' is
36 1.13 rillig # undefined. This is something that make doesn't expect, this may
37 1.13 rillig # even trigger an assertion failure somewhere.
38 1.2 rillig @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
39 1.5 rillig
40 1.13 rillig # In extreme cases, even the backslash can be used as variable name.
41 1.13 rillig # It needs to be doubled though.
42 1.13 rillig .if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
43 1.13 rillig . error
44 1.13 rillig .endif
45 1.13 rillig
46 1.13 rillig # The variable name can technically be empty, and in this situation
47 1.13 rillig # the variable value cannot be accessed since the empty "variable"
48 1.13 rillig # is protected to always return an empty string.
49 1.13 rillig .if ${:U1 2 3:@@x${}y@} != "xy xy xy"
50 1.13 rillig . error
51 1.13 rillig .endif
52 1.3 rillig
53 1.10 rillig
54 1.10 rillig # The :@ modifier resolves the variables from the replacement text once more
55 1.10 rillig # than expected. In particular, it resolves _all_ variables from the scope,
56 1.10 rillig # and not only the loop variable (in this case v).
57 1.10 rillig SRCS= source
58 1.10 rillig CFLAGS.source= before
59 1.10 rillig ALL_CFLAGS:= ${SRCS:@src@${CFLAGS.${src}}@} # note the ':='
60 1.10 rillig CFLAGS.source+= after
61 1.10 rillig .if ${ALL_CFLAGS} != "before"
62 1.10 rillig . error
63 1.10 rillig .endif
64 1.10 rillig
65 1.10 rillig
66 1.10 rillig # In the following example, the modifier ':@' expands the '$$' to '$'. This
67 1.10 rillig # means that when the resulting expression is evaluated, these resulting '$'
68 1.10 rillig # will be interpreted as starting a subexpression.
69 1.2 rillig #
70 1.2 rillig # The d means direct reference, the i means indirect reference.
71 1.2 rillig RESOLVE= ${RES1} $${RES1}
72 1.2 rillig RES1= 1d${RES2} 1i$${RES2}
73 1.2 rillig RES2= 2d${RES3} 2i$${RES3}
74 1.2 rillig RES3= 3
75 1.2 rillig
76 1.10 rillig # TODO: convert to '.if'.
77 1.2 rillig mod-loop-resolve:
78 1.2 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
79 1.2 rillig
80 1.10 rillig
81 1.2 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
82 1.2 rillig # or two dollar signs, which were silently ignored.
83 1.2 rillig # There's no point in allowing a dollar sign in that position.
84 1.2 rillig mod-loop-varname-dollar:
85 1.2 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
86 1.2 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
87 1.2 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
88 1.2 rillig
89 1.6 rillig # Demonstrate that it is possible to generate dollar signs using the
90 1.2 rillig # :@ modifier.
91 1.2 rillig #
92 1.2 rillig # These are edge cases that could have resulted in a parse error as well
93 1.2 rillig # since the $@ at the end could have been interpreted as a variable, which
94 1.2 rillig # would mean a missing closing @ delimiter.
95 1.2 rillig mod-loop-dollar:
96 1.2 rillig @echo $@:${:U1:@word@${word}$@:Q}:
97 1.2 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
98 1.2 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
99 1.2 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
100 1.2 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
101 1.2 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
102 1.4 rillig
103 1.4 rillig # It may happen that there are nested :@ modifiers that use the same name for
104 1.4 rillig # for the loop variable. These modifiers influence each other.
105 1.4 rillig #
106 1.5 rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
107 1.9 rillig # variable in the scope of the expression and deleting it again after the
108 1.4 rillig # loop. This is different from the .for loops, which substitute the variable
109 1.4 rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
110 1.4 rillig #
111 1.4 rillig # To make the behavior more predictable, the :@ modifier should restore the
112 1.4 rillig # loop variable to the value it had before the loop. This would result in
113 1.4 rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
114 1.4 rillig .if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
115 1.4 rillig . error
116 1.4 rillig .endif
117 1.5 rillig
118 1.5 rillig # During the loop, the variable is actually defined and nonempty.
119 1.5 rillig # If the loop were implemented in the same way as the .for loop, the variable
120 1.5 rillig # would be neither defined nor nonempty since all expressions of the form
121 1.5 rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
122 1.5 rillig .if defined(var)
123 1.5 rillig . error
124 1.5 rillig .endif
125 1.5 rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
126 1.5 rillig != "def nonempty"
127 1.5 rillig . error
128 1.5 rillig .endif
129 1.5 rillig .if defined(var)
130 1.5 rillig . error
131 1.5 rillig .endif
132 1.7 rillig
133 1.7 rillig # Assignment using the ':=' operator, combined with the :@var@ modifier
134 1.7 rillig #
135 1.7 rillig 8_DOLLARS= $$$$$$$$
136 1.7 rillig # This string literal is written with 8 dollars, and this is saved as the
137 1.7 rillig # variable value. But as soon as this value is evaluated, it goes through
138 1.7 rillig # Var_Subst, which replaces each '$$' with a single '$'. This could be
139 1.11 rillig # prevented by VarEvalFlags.keepDollar, but that flag is usually removed
140 1.11 rillig # before expanding subexpressions. See ApplyModifier_Loop and
141 1.11 rillig # ParseModifierPart for examples.
142 1.7 rillig #
143 1.7 rillig .MAKEFLAGS: -dcp
144 1.7 rillig USE_8_DOLLARS= ${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
145 1.7 rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
146 1.7 rillig . error
147 1.7 rillig .endif
148 1.7 rillig #
149 1.7 rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
150 1.11 rillig # The ':=' assignment operator evaluates the variable value using the mode
151 1.11 rillig # VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
152 1.11 rillig # but not all. The dollar signs in the top-level expression and in the
153 1.11 rillig # indirect ${8_DOLLARS} are preserved.
154 1.7 rillig #
155 1.7 rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
156 1.7 rillig # matter in which context it is evaluated. What happens in detail is:
157 1.7 rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
158 1.7 rillig # Next, each word of the value is expanded on its own, and at this moment
159 1.11 rillig # in ApplyModifier_Loop, the flag keepDollar is not passed down to
160 1.7 rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
161 1.7 rillig #
162 1.7 rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
163 1.11 rillig # modifier and are thus expanded with the flag keepDollar in action.
164 1.7 rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
165 1.7 rillig # "$$$$ $$$$$$$$ $$$$$$$$".
166 1.7 rillig #
167 1.7 rillig # The variable expression in the condition then expands this raw stored value
168 1.7 rillig # once, resulting in "$$ $$$$ $$$$". The effects from VARE_KEEP_DOLLAR no
169 1.7 rillig # longer take place since they had only been active during the evaluation of
170 1.7 rillig # the variable assignment.
171 1.7 rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
172 1.7 rillig . error
173 1.7 rillig .endif
174 1.7 rillig .MAKEFLAGS: -d0
175 1.10 rillig
176 1.10 rillig # After looping over the words of the expression, the loop variable gets
177 1.10 rillig # undefined. The modifier ':@' uses an ordinary global variable for this,
178 1.10 rillig # which is different from the '.for' loop, which replaces ${var} with
179 1.10 rillig # ${:Uvalue} in the body of the loop. This choice of implementation detail
180 1.10 rillig # can be used for a nasty side effect. The expression ${:U:@VAR@@} evaluates
181 1.10 rillig # to an empty string, plus it undefines the variable 'VAR'. This is the only
182 1.10 rillig # possibility to undefine a global variable during evaluation.
183 1.10 rillig GLOBAL= before-global
184 1.10 rillig RESULT:= ${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
185 1.10 rillig .if ${RESULT} != "before-global undefined"
186 1.10 rillig . error
187 1.10 rillig .endif
188 1.10 rillig
189 1.10 rillig # The above side effect of undefining a variable from a certain scope can be
190 1.10 rillig # further combined with the otherwise undocumented implementation detail that
191 1.10 rillig # the argument of an '.if' directive is evaluated in cmdline scope. Putting
192 1.10 rillig # these together makes it possible to undefine variables from the cmdline
193 1.10 rillig # scope, something that is not possible in a straight-forward way.
194 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline
195 1.10 rillig .if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
196 1.10 rillig . error
197 1.10 rillig .endif
198 1.10 rillig # Now the cmdline variable got undefined.
199 1.10 rillig .if ${CMDLINE} != "cmdline"
200 1.10 rillig . error
201 1.10 rillig .endif
202 1.10 rillig # At this point, it still looks as if the cmdline variable were defined,
203 1.10 rillig # since the value of CMDLINE is still "cmdline". That impression is only
204 1.10 rillig # superficial though, the cmdline variable is actually deleted. To
205 1.10 rillig # demonstrate this, it is now possible to override its value using a global
206 1.10 rillig # variable, something that was not possible before:
207 1.10 rillig CMDLINE= global
208 1.10 rillig .if ${CMDLINE} != "global"
209 1.10 rillig . error
210 1.10 rillig .endif
211 1.10 rillig # Now undefine that global variable again, to get back to the original value.
212 1.10 rillig .undef CMDLINE
213 1.10 rillig .if ${CMDLINE} != "cmdline"
214 1.10 rillig . error
215 1.10 rillig .endif
216 1.10 rillig # What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
217 1.10 rillig # target in the cmdline scope, that same variable was exported to the
218 1.10 rillig # environment, see Var_SetWithFlags.
219 1.10 rillig .unexport CMDLINE
220 1.10 rillig .if ${CMDLINE} != "cmdline"
221 1.10 rillig . error
222 1.10 rillig .endif
223 1.10 rillig # The above '.unexport' has no effect since UnexportVar requires a global
224 1.10 rillig # variable of the same name to be defined, otherwise nothing is unexported.
225 1.10 rillig CMDLINE= global
226 1.10 rillig .unexport CMDLINE
227 1.10 rillig .undef CMDLINE
228 1.10 rillig .if ${CMDLINE} != "cmdline"
229 1.10 rillig . error
230 1.10 rillig .endif
231 1.10 rillig # This still didn't work since there must not only be a global variable, the
232 1.10 rillig # variable must be marked as exported as well, which it wasn't before.
233 1.10 rillig CMDLINE= global
234 1.10 rillig .export CMDLINE
235 1.10 rillig .unexport CMDLINE
236 1.10 rillig .undef CMDLINE
237 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
238 1.10 rillig . error
239 1.10 rillig .endif
240 1.10 rillig # Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
241 1.10 rillig # traces from the environment are gone as well. To do that, a global variable
242 1.10 rillig # had to be defined and exported, something that is far from obvious. To
243 1.10 rillig # recap, here is the essence of the above story:
244 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline # have a cmdline + environment variable
245 1.10 rillig .if ${:U:@CMDLINE@@}} # undefine cmdline, keep environment
246 1.10 rillig .endif
247 1.10 rillig CMDLINE= global # needed for deleting the environment
248 1.10 rillig .export CMDLINE # needed for deleting the environment
249 1.10 rillig .unexport CMDLINE # delete the environment
250 1.10 rillig .undef CMDLINE # delete the global helper variable
251 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
252 1.10 rillig . error # 'CMDLINE' is gone now from all scopes
253 1.10 rillig .endif
254 1.10 rillig
255 1.10 rillig
256 1.10 rillig # TODO: Actually trigger the undefined behavior (use after free) that was
257 1.10 rillig # already suspected in Var_Parse, in the comment 'the value of the variable
258 1.10 rillig # must not change'.
259