varmod-loop.mk revision 1.4 1 1.4 rillig # $NetBSD: varmod-loop.mk,v 1.4 2020/10/18 21:12:13 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :@var (a] ...${var}...@ variable modifier.
4 1.1 rillig
5 1.2 rillig all: mod-loop-varname
6 1.2 rillig all: mod-loop-resolve
7 1.2 rillig all: mod-loop-varname-dollar
8 1.2 rillig all: mod-loop-dollar
9 1.1 rillig
10 1.2 rillig # In the :@ modifier, the name of the loop variable can even be generated
11 1.2 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
12 1.2 rillig # will ever depend on this, but technically it's possible.
13 1.2 rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
14 1.2 rillig mod-loop-varname:
15 1.2 rillig @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
16 1.2 rillig # ":::" is a very creative variable name, unlikely in practice
17 1.2 rillig # The expression ${\:\:\:} would not work since backslashes can only
18 1.2 rillig # be escaped in the modifiers, but not in the variable name.
19 1.2 rillig @echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
20 1.2 rillig # "@@" is another creative variable name.
21 1.2 rillig @echo :${:U1 2 3:@\@\@@x${@@}y@}:
22 1.2 rillig # Even "@" works as a variable name since the variable is installed
23 1.2 rillig # in the "current" scope, which in this case is the one from the
24 1.2 rillig # target.
25 1.2 rillig @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
26 1.2 rillig # In extreme cases, even the backslash can be used as variable name.
27 1.2 rillig # It needs to be doubled though.
28 1.2 rillig @echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
29 1.2 rillig
30 1.3 rillig # The variable name can technically be empty, and in this situation
31 1.3 rillig # the variable value cannot be accessed since the empty variable is
32 1.3 rillig # protected to always return an empty string.
33 1.3 rillig @echo empty: :${:U1 2 3:@@x${}y@}:
34 1.3 rillig
35 1.2 rillig # The :@ modifier resolves the variables a little more often than expected.
36 1.2 rillig # In particular, it resolves _all_ variables from the context, and not only
37 1.2 rillig # the loop variable (in this case v).
38 1.2 rillig #
39 1.2 rillig # The d means direct reference, the i means indirect reference.
40 1.2 rillig RESOLVE= ${RES1} $${RES1}
41 1.2 rillig RES1= 1d${RES2} 1i$${RES2}
42 1.2 rillig RES2= 2d${RES3} 2i$${RES3}
43 1.2 rillig RES3= 3
44 1.2 rillig
45 1.2 rillig mod-loop-resolve:
46 1.2 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
47 1.2 rillig
48 1.2 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
49 1.2 rillig # or two dollar signs, which were silently ignored.
50 1.2 rillig # There's no point in allowing a dollar sign in that position.
51 1.2 rillig mod-loop-varname-dollar:
52 1.2 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
53 1.2 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
54 1.2 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
55 1.2 rillig
56 1.2 rillig # Demonstrate that it is possible to generate dollar characters using the
57 1.2 rillig # :@ modifier.
58 1.2 rillig #
59 1.2 rillig # These are edge cases that could have resulted in a parse error as well
60 1.2 rillig # since the $@ at the end could have been interpreted as a variable, which
61 1.2 rillig # would mean a missing closing @ delimiter.
62 1.2 rillig mod-loop-dollar:
63 1.2 rillig @echo $@:${:U1:@word@${word}$@:Q}:
64 1.2 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
65 1.2 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
66 1.2 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
67 1.2 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
68 1.2 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
69 1.4 rillig
70 1.4 rillig # It may happen that there are nested :@ modifiers that use the same name for
71 1.4 rillig # for the loop variable. These modifiers influence each other.
72 1.4 rillig #
73 1.4 rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting an
74 1.4 rillig # variable in the context of the expression and deleting it again after the
75 1.4 rillig # loop. This is different from the .for loops, which substitute the variable
76 1.4 rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
77 1.4 rillig #
78 1.4 rillig # To make the behavior more predictable, the :@ modifier should restore the
79 1.4 rillig # loop variable to the value it had before the loop. This would result in
80 1.4 rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
81 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"
82 1.4 rillig . error
83 1.4 rillig .endif
84