1 # $NetBSD: varmod-indirect.mk,v 1.24 2025/03/30 16:43:10 rillig Exp $ 2 # 3 # Tests for indirect variable modifiers, such as in ${VAR:${M_modifiers}}. 4 # These can be used for very basic purposes like converting a string to either 5 # uppercase or lowercase, as well as for fairly advanced modifiers that first 6 # look like line noise and are hard to decipher. 7 # 8 # Initial support for indirect modifiers was added in var.c 1.101 from 9 # 2006-02-18. Since var.c 1.108 from 2006-05-11 it is possible to use 10 # indirect modifiers for all but the very first modifier as well. 11 12 13 # To apply a modifier indirectly via another variable, the whole 14 # modifier must be put into a single expression. 15 # The following expression generates a parse error since its indirect 16 # modifier contains more than a sole expression. 17 # 18 # expect+1: Unknown modifier ":${" 19 .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}" 20 . warning unexpected 21 .endif 22 23 24 # Adding another level of indirection (the 2 nested :U expressions) helps. 25 .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement" 26 . warning unexpected 27 .endif 28 29 30 # Multiple indirect modifiers can be applied one after another as long as 31 # they are separated with colons. 32 .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE" 33 . warning unexpected 34 .endif 35 36 37 # An indirect variable that evaluates to the empty string is allowed. 38 # It is even allowed to write another modifier directly afterwards. 39 # There is no practical use case for this feature though, as demonstrated 40 # in the test case directly below. 41 .if ${value:L:${:Dempty}S,value,replaced,} != "replaced" 42 . warning unexpected 43 .endif 44 45 # If an expression for an indirect modifier evaluates to anything else than an 46 # empty string and is neither followed by a ':' nor '}', this produces a parse 47 # error. Due to this parse error, this construct cannot be used reasonably 48 # in practice. 49 # 50 # expect+2: Unknown modifier ":${" 51 #.MAKEFLAGS: -dvc 52 .if ${value:L:${:UM*}S,value,replaced,} == "anything" 53 . error 54 .else 55 . error 56 .endif 57 #.MAKEFLAGS: -d0 58 59 # An indirect modifier can be followed by other modifiers, no matter if the 60 # indirect modifier evaluates to an empty string or not. 61 # 62 # This makes it possible to define conditional modifiers, like this: 63 # 64 # M.little-endian= S,1234,4321, 65 # M.big-endian= # none 66 .if ${value:L:${:D empty }:S,value,replaced,} != "replaced" 67 . error 68 .endif 69 70 71 # The nested expression expands to "tu", and this is interpreted as 72 # a variable modifier for the value "Upper", resulting in "UPPER". 73 .if ${Upper:L:${:Utu}} != "UPPER" 74 . error 75 .endif 76 77 # The nested expression expands to "tl", and this is interpreted as 78 # a variable modifier for the value "Lower", resulting in "lower". 79 .if ${Lower:L:${:Utl}} != "lower" 80 . error 81 .endif 82 83 84 # The nested expression is ${1 != 1:?Z:tl}, consisting of the 85 # condition "1 != 1", the then-branch "Z" and the else-branch "tl". Since 86 # the condition evaluates to false, the then-branch is ignored (it would 87 # have been an unknown modifier anyway) and the ":tl" modifier is applied. 88 .if ${Mixed:L:${1 != 1:?Z:tl}} != "mixed" 89 . error 90 .endif 91 92 93 # The indirect modifier can also replace an ':L' modifier, which allows for 94 # brain twisters since by reading the expression alone, it is not possible 95 # to say whether the variable name will be evaluated as a variable name or 96 # as the immediate value of the expression. 97 VAR= value 98 M_ExpandVar= # an empty modifier 99 M_VarAsValue= L 100 # 101 .if ${VAR:${M_ExpandVar}} != "value" 102 . error 103 .endif 104 .if ${VAR:${M_VarAsValue}} != "VAR" 105 . error 106 .endif 107 108 # The indirect modifier M_ListToSkip, when applied to a list of patterns, 109 # expands to a sequence of ':N' modifiers, each of which filters one of the 110 # patterns. This list of patterns can then be applied to another variable 111 # to actually filter that variable. 112 # 113 M_ListToSkip= @pat@N$${pat}@:ts: 114 # 115 # The dollar signs need to be doubled in the above modifier expression, 116 # otherwise they would be expanded too early, that is, when parsing the 117 # modifier itself. 118 # 119 # In the following example, M_NoPrimes expands to 'N2:N3:N5:N7:N1[1379]'. 120 # The 'N' comes from the expression 'N${pat}', the separating colons come 121 # from the modifier ':ts:'. 122 # 123 #.MAKEFLAGS: -dcv # Uncomment this line to see the details 124 # 125 PRIMES= 2 3 5 7 1[1379] 126 M_NoPrimes= ${PRIMES:${M_ListToSkip}} 127 .if ${:U:range=20:${M_NoPrimes}} != "1 4 6 8 9 10 12 14 15 16 18 20" 128 . error 129 .endif 130 .MAKEFLAGS: -d0 131 132 133 # In contrast to the .if conditions, the .for loop allows undefined 134 # expressions. These expressions expand to empty strings. 135 136 # An undefined expression without any modifiers expands to an empty string. 137 .for var in before ${UNDEF} after 138 # expect+2: before 139 # expect+1: after 140 . info ${var} 141 .endfor 142 143 # An undefined expression with only modifiers that keep the expression 144 # undefined expands to an empty string. 145 .for var in before ${UNDEF:${:US,a,a,}} after 146 # expect+2: before 147 # expect+1: after 148 . info ${var} 149 .endfor 150 151 # Even in an indirect modifier based on an undefined variable, the value of 152 # the expression in Var_Parse is a simple empty string. 153 .for var in before ${UNDEF:${:U}} after 154 # expect+2: before 155 # expect+1: after 156 . info ${var} 157 .endfor 158 159 # An error in an indirect modifier. 160 # expect+1: Unknown modifier ":Z" 161 .for var in before ${UNDEF:${:UZ}} after 162 . error 163 .endfor 164 165 166 # Another slightly different evaluation context is the right-hand side of 167 # a variable assignment using ':='. 168 .MAKEFLAGS: -dpv 169 170 # The undefined expression is kept as-is. 171 _:= before ${UNDEF} after 172 173 # The undefined expression is kept as-is. 174 _:= before ${UNDEF:${:US,a,a,}} after 175 176 # XXX: The subexpression ${:U} is fully defined, therefore it is expanded. 177 # This results in ${UNDEF:}, which can lead to tricky parse errors later, 178 # when the variable '_' is expanded further. 179 # 180 # XXX: What should be the correct strategy here? One possibility is to 181 # expand the defined subexpression and replace it with ${:U...}, just like 182 # in .for loops. This would preserve the structure of the expression while 183 # at the same time expanding the expression as far as possible. 184 _:= before ${UNDEF:${:U}} after 185 186 # XXX: This expands to ${UNDEF:Z}, which will behave differently if the 187 # variable '_' is used in a context where the expression ${_} is 188 # parsed but not evaluated. 189 # expect+1: Unknown modifier ":Z" 190 _:= before ${UNDEF:${:UZ}} after 191 192 .MAKEFLAGS: -d0 193 .undef _ 194 195 196 # When evaluating indirect modifiers, these modifiers may expand to ':tW', 197 # which modifies the interpretation of the expression value. This modified 198 # interpretation only lasts until the end of the indirect modifier, it does 199 # not influence the outer expression. 200 .if ${1 2 3:L:tW:[#]} != 1 # direct :tW applies to the :[#] 201 . error 202 .endif 203 .if ${1 2 3:L:${:UtW}:[#]} != 3 # indirect :tW does not apply to :[#] 204 . error 205 .endif 206 207 208 # When evaluating indirect modifiers, these modifiers may expand to ':ts*', 209 # which modifies the interpretation of the expression value. This modified 210 # interpretation only lasts until the end of the indirect modifier, it does 211 # not influence the outer expression. 212 # 213 # In this first expression, the direct ':ts*' has no effect since ':U' does not 214 # treat the expression value as a list of words but as a single word. It has 215 # to be ':U', not ':D', since the "expression name" is "1 2 3" and there is no 216 # variable of that name. 217 #.MAKEFLAGS: -dcpv 218 .if ${1 2 3:L:ts*:Ua b c} != "a b c" 219 . error 220 .endif 221 # In this expression, the direct ':ts*' affects the ':M' at the end. 222 .if ${1 2 3:L:ts*:Ua b c:M*} != "a*b*c" 223 . error 224 .endif 225 # In this expression, the ':ts*' is indirect, therefore the changed separator 226 # only applies to the modifiers from the indirect text. It does not affect 227 # the ':M' since that is not part of the text from the indirect modifier. 228 # 229 # Implementation detail: when ApplyModifiersIndirect calls ApplyModifiers 230 # (which creates a new ModChain containing a fresh separator), 231 # the outer separator character is not passed by reference to the inner 232 # evaluation, therefore the scope of the inner separator ends after applying 233 # the modifier ':ts*'. 234 .if ${1 2 3:L:${:Uts*}:Ua b c:M*} != "a b c" 235 . error 236 .endif 237 238 # A direct modifier ':U' turns the expression from undefined to defined. 239 # An indirect modifier ':U' has the same effect, unlike the separator from 240 # ':ts*' or the single-word marker from ':tW'. 241 # 242 # This is because when ApplyModifiersIndirect calls ApplyModifiers, it passes 243 # the definedness of the outer expression by reference. If that weren't the 244 # case, the first condition below would result in a parse error because its 245 # left-hand side would be undefined. 246 .if ${UNDEF:${:UUindirect-fallback}} != "indirect-fallback" 247 . error 248 .endif 249 .if ${UNDEF:${:UUindirect-fallback}:Uouter-fallback} != "outer-fallback" 250 . error 251 .endif 252 253 254 # In parse-only mode, the indirect modifiers must not be evaluated. 255 # 256 # Before var.c 1.1098 from 2024-02-04, the expression for an indirect modifier 257 # was partially evaluated (only the variable value, without applying any 258 # modifiers) and then interpreted as modifiers to the main expression. 259 # 260 # The expression ${:UZ} starts with the value "", and in parse-only mode, the 261 # modifier ':UZ' does not modify the expression value. This results in an 262 # empty string for the indirect modifiers, generating no warning. 263 .if 0 && ${VAR:${:UZ}} 264 .endif 265 # The expression ${M_invalid} starts with the value "Z", which is an unknown 266 # modifier. Trying to apply this unknown modifier generated a warning. 267 M_invalid= Z 268 .if 0 && ${VAR:${M_invalid}} 269 .endif 270 # The ':S' modifier does not change the expression value in parse-only mode, 271 # keeping the "Z", which is then skipped in parse-only mode. 272 .if 0 && ${VAR:${M_invalid:S,^,N*,:ts:}} 273 .endif 274 # The ':@' modifier does not change the expression value in parse-only mode, 275 # keeping the "Z", which is then skipped in parse-only mode. 276 .if 0 && ${VAR:${M_invalid:@m@N*$m@:ts:}} 277 .endif 278