diff options
Diffstat (limited to 'contrib/bmake/unit-tests/varmod-loop.mk')
-rw-r--r-- | contrib/bmake/unit-tests/varmod-loop.mk | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/contrib/bmake/unit-tests/varmod-loop.mk b/contrib/bmake/unit-tests/varmod-loop.mk index 4fdaa3ff4e61..64cc6ca85043 100644 --- a/contrib/bmake/unit-tests/varmod-loop.mk +++ b/contrib/bmake/unit-tests/varmod-loop.mk @@ -1,7 +1,24 @@ -# $NetBSD: varmod-loop.mk,v 1.15 2021/04/11 13:35:56 rillig Exp $ +# $NetBSD: varmod-loop.mk,v 1.24 2023/11/19 21:47:52 rillig Exp $ # -# Tests for the :@var@...${var}...@ variable modifier. +# Tests for the expression modifier ':@var@body@', which replaces each word of +# the expression with the expanded body, which may contain references to the +# variable 'var'. For example, '${1 2 3:L:@word@<${word}>@}' encloses each +# word in angle quotes, resulting in '<1> <2> <3>'. +# +# The variable name can be chosen freely, except that it must not contain a +# '$'. For simplicity and readability, variable names should only use the +# characters 'A-Za-z0-9'. +# +# The body may contain subexpressions in the form '${...}' or '$(...)'. These +# subexpressions differ from everywhere else in makefiles in that the parser +# only scans '${...}' for balanced '{' and '}', likewise for '$(...)'. Any +# other '$' is left as-is during parsing. Later, when the body is expanded +# for each word, each '$$' is interpreted as a single '$', and the remaining +# '$' are interpreted as expressions, like when evaluating a regular variable. +# Force the test results to be independent of the default value of this +# setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake +# distribution and pkgsrc/devel/bmake. .MAKE.SAVE_DOLLARS= yes all: varname-overwriting-target @@ -16,7 +33,6 @@ varname-overwriting-target: @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@: - # Demonstrate that it is possible to generate dollar signs using the # :@ modifier. # @@ -36,7 +52,7 @@ mod-loop-dollar: # # As of 2020-10-18, the :@ modifier is implemented by actually setting a # variable in the scope of the expression and deleting it again after the -# loop. This is different from the .for loops, which substitute the variable +# loop. This is different from the .for loops, which substitute the # expression with ${:Uvalue}, leading to different unwanted side effects. # # To make the behavior more predictable, the :@ modifier should restore the @@ -95,7 +111,7 @@ SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS} # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value # "$$$$ $$$$$$$$ $$$$$$$$". # -# The variable expression in the condition then expands this raw stored value +# The expression in the condition then expands this raw stored value # once, resulting in "$$ $$$$ $$$$". The effects from VARE_KEEP_DOLLAR no # longer take place since they had only been active during the evaluation of # the variable assignment. @@ -184,6 +200,47 @@ CMDLINE= global # needed for deleting the environment .endif -# TODO: Actually trigger the undefined behavior (use after free) that was -# already suspected in Var_Parse, in the comment 'the value of the variable -# must not change'. +# In the loop body text of the ':@' modifier, a literal '$' is written as '$$', +# not '\$'. In the following example, each '$$' turns into a single '$', +# except for '$i', which is replaced with the then-current value '1' of the +# iteration variable. +# +# See parse-var.mk, keyword 'BRACE_GROUP'. +all: varmod-loop-literal-dollar +varmod-loop-literal-dollar: .PHONY + : ${:U1:@i@ t=$$(( $${t:-0} + $i ))@} + + +# When parsing the loop body, each '\$', '\@' and '\\' is unescaped to '$', +# '@' and '\', respectively; all other backslashes are retained. +# +# In practice, the '$' is not escaped as '\$', as there is a second round of +# unescaping '$$' to '$' later when the loop body is expanded after setting the +# iteration variable. +# +# After the iteration variable has been set, the loop body is expanded with +# this unescaping, regardless of whether .MAKE.SAVE_DOLLARS is set or not: +# $$ a literal '$' +# $x, ${var}, $(var) a nested expression +# any other character itself +all: escape-modifier +escape-modifier: .PHONY + # In the first round, '\$ ' is unescaped to '$ ', and since the + # variable named ' ' is not defined, the expression '$ ' expands to an + # empty string. + # expect: : dollar=end + : ${:U1:@i@ dollar=\$ end@} + + # Like in other modifiers, '\ ' is preserved, since ' ' is not one of + # the characters that _must_ be escaped. + # expect: : backslash=\ end + : ${:U1:@i@ backslash=\ end@} + + # expect: : dollar=$ at=@ backslash=\ end + : ${:U1:@i@ dollar=\$\$ at=\@ backslash=\\ end@} + # expect: : dollar=$$ at=@@ backslash=\\ end + : ${:U1:@i@ dollar=\$\$\$\$ at=\@\@ backslash=\\\\ end@} + # expect: : dollar=$$ at=@@ backslash=\\ end + : ${:U1:@i@ dollar=$$$$ at=\@\@ backslash=\\\\ end@} + +all: .PHONY |