varmod-loop-varname.mk revision 1.12 1 # $NetBSD: varmod-loop-varname.mk,v 1.12 2025/01/11 20:54:46 rillig Exp $
2 #
3 # Tests for the first part of the variable modifier ':@var (a] ...@', which
4 # contains the variable name to use during the loop.
5
6 # Force the test results to be independent of the default value of this
7 # setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
8 # distribution and pkgsrc/devel/bmake.
9 .MAKE.SAVE_DOLLARS= yes
10
11
12 # Before 2021-04-04, the name of the loop variable could be generated
13 # dynamically. There was no practical use-case for this.
14 # Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
15 # variable name.
16 # expect+1: In the :@ modifier, the variable name "${:Ubar:S,b,v,}" must not contain a dollar
17 .if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
18 . error
19 .else
20 . error
21 .endif
22
23
24 # ":::" is a very creative variable name, unlikely to occur in practice.
25 # The expression ${\:\:\:} would not work since backslashes can only
26 # be escaped in the modifiers, but not in the variable name, therefore
27 # the extra indirection via the modifier ':U'.
28 .if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
29 . error
30 .endif
31
32
33 # "@@" is another creative variable name.
34 .if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
35 . error
36 .endif
37
38
39 # In extreme cases, even the backslash can be used as variable name.
40 # It needs to be doubled though.
41 .if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
42 . error
43 .endif
44
45
46 # The variable name can technically be empty, and in this situation
47 # the variable value cannot be accessed since the empty "variable"
48 # is protected to always return an empty string.
49 .if ${:U1 2 3:@@x${}y@} != "xy xy xy"
50 . error
51 .endif
52
53
54 # The :@ modifier resolves the variables from the replacement text once more
55 # than expected. In particular, it resolves _all_ variables from the scope,
56 # and not only the loop variable (in this case v).
57 SRCS= source
58 CFLAGS.source= before
59 ALL_CFLAGS:= ${SRCS:@src@${CFLAGS.${src}}@} # note the ':='
60 CFLAGS.source+= after
61 .if ${ALL_CFLAGS} != "before"
62 . error
63 .endif
64
65
66 # In the following example, the modifier ':@' expands the '$$' to '$'. This
67 # means that when the resulting expression is evaluated, these resulting '$'
68 # will be interpreted as starting a subexpression.
69 #
70 # The d means direct reference, the i means indirect reference.
71 RESOLVE= ${RES1} $${RES1}
72 RES1= 1d${RES2} 1i$${RES2}
73 RES2= 2d${RES3} 2i$${RES3}
74 RES3= 3
75
76 .if ${RESOLVE:@v@w${v}w@} != "w1d2d3w w2i3w w1i2d3 2i\${RES3}w w1d2d3 2i\${RES3} 1i\${RES2}w"
77 . error
78 .endif
79
80
81 # Until 2020-07-20, the variable name of the :@ modifier could end with one
82 # or two dollar signs, which were silently ignored.
83 # There's no point in allowing a dollar sign in that position.
84 # Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
85 # variable name.
86 # expect+1: In the :@ modifier, the variable name "v$" must not contain a dollar
87 .if ${1 2 3:L:@v$@($v)@} != "(1) (2) (3)"
88 . error
89 .else
90 . error
91 .endif
92 # expect+1: In the :@ modifier, the variable name "v$$" must not contain a dollar
93 .if ${1 2 3:L:@v$$@($v)@} != "() () ()"
94 . error
95 .else
96 . error
97 .endif
98 # expect+1: In the :@ modifier, the variable name "v$$$" must not contain a dollar
99 .if ${1 2 3:L:@v$$$@($v)@} != "() () ()"
100 . error
101 .else
102 . error
103 .endif
104
105
106 # It may happen that there are nested :@ modifiers that use the same name for
107 # for the loop variable. These modifiers influence each other.
108 #
109 # As of 2020-10-18, the :@ modifier is implemented by actually setting a
110 # variable in the scope of the expression and deleting it again after the
111 # loop. This is different from the .for loops, which substitute the
112 # expression with ${:Uvalue}, leading to different unwanted side effects.
113 #
114 # To make the behavior more predictable, the :@ modifier should restore the
115 # loop variable to the value it had before the loop. This would result in
116 # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
117 .if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
118 . error
119 .endif
120
121 # During the loop, the variable is actually defined and nonempty.
122 # If the loop were implemented in the same way as the .for loop, the variable
123 # would be neither defined nor nonempty since all expressions of the form
124 # ${var} would have been replaced with ${:Uword} before evaluating them.
125 .if defined(var)
126 . error
127 .endif
128 .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
129 != "def nonempty"
130 . error
131 .endif
132 .if defined(var)
133 . error
134 .endif
135
136 all: .PHONY
137