Home | History | Annotate | Line # | Download | only in unit-tests
varmod-loop.mk revision 1.8
      1  1.8  rillig # $NetBSD: varmod-loop.mk,v 1.8 2020/11/12 00:40:55 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.2  rillig all: mod-loop-varname
      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.2  rillig mod-loop-varname:
     17  1.2  rillig 	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
     18  1.5  rillig 
     19  1.5  rillig 	# ":::" is a very creative variable name, unlikely in practice.
     20  1.2  rillig 	# The expression ${\:\:\:} would not work since backslashes can only
     21  1.2  rillig 	# be escaped in the modifiers, but not in the variable name.
     22  1.2  rillig 	@echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
     23  1.5  rillig 
     24  1.2  rillig 	# "@@" is another creative variable name.
     25  1.2  rillig 	@echo :${:U1 2 3:@\@\@@x${@@}y@}:
     26  1.5  rillig 
     27  1.2  rillig 	# Even "@" works as a variable name since the variable is installed
     28  1.2  rillig 	# in the "current" scope, which in this case is the one from the
     29  1.2  rillig 	# target.
     30  1.2  rillig 	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
     31  1.5  rillig 
     32  1.2  rillig 	# In extreme cases, even the backslash can be used as variable name.
     33  1.2  rillig 	# It needs to be doubled though.
     34  1.2  rillig 	@echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
     35  1.2  rillig 
     36  1.3  rillig 	# The variable name can technically be empty, and in this situation
     37  1.3  rillig 	# the variable value cannot be accessed since the empty variable is
     38  1.3  rillig 	# protected to always return an empty string.
     39  1.3  rillig 	@echo empty: :${:U1 2 3:@@x${}y@}:
     40  1.3  rillig 
     41  1.2  rillig # The :@ modifier resolves the variables a little more often than expected.
     42  1.2  rillig # In particular, it resolves _all_ variables from the context, and not only
     43  1.2  rillig # the loop variable (in this case v).
     44  1.2  rillig #
     45  1.2  rillig # The d means direct reference, the i means indirect reference.
     46  1.2  rillig RESOLVE=	${RES1} $${RES1}
     47  1.2  rillig RES1=		1d${RES2} 1i$${RES2}
     48  1.2  rillig RES2=		2d${RES3} 2i$${RES3}
     49  1.2  rillig RES3=		3
     50  1.2  rillig 
     51  1.2  rillig mod-loop-resolve:
     52  1.2  rillig 	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
     53  1.2  rillig 
     54  1.2  rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
     55  1.2  rillig # or two dollar signs, which were silently ignored.
     56  1.2  rillig # There's no point in allowing a dollar sign in that position.
     57  1.2  rillig mod-loop-varname-dollar:
     58  1.2  rillig 	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
     59  1.2  rillig 	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
     60  1.2  rillig 	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
     61  1.2  rillig 
     62  1.6  rillig # Demonstrate that it is possible to generate dollar signs using the
     63  1.2  rillig # :@ modifier.
     64  1.2  rillig #
     65  1.2  rillig # These are edge cases that could have resulted in a parse error as well
     66  1.2  rillig # since the $@ at the end could have been interpreted as a variable, which
     67  1.2  rillig # would mean a missing closing @ delimiter.
     68  1.2  rillig mod-loop-dollar:
     69  1.2  rillig 	@echo $@:${:U1:@word@${word}$@:Q}:
     70  1.2  rillig 	@echo $@:${:U2:@word@$${word}$$@:Q}:
     71  1.2  rillig 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
     72  1.2  rillig 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
     73  1.2  rillig 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
     74  1.2  rillig 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
     75  1.4  rillig 
     76  1.4  rillig # It may happen that there are nested :@ modifiers that use the same name for
     77  1.4  rillig # for the loop variable.  These modifiers influence each other.
     78  1.4  rillig #
     79  1.5  rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
     80  1.4  rillig # variable in the context of the expression and deleting it again after the
     81  1.4  rillig # loop.  This is different from the .for loops, which substitute the variable
     82  1.4  rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
     83  1.4  rillig #
     84  1.4  rillig # To make the behavior more predictable, the :@ modifier should restore the
     85  1.4  rillig # loop variable to the value it had before the loop.  This would result in
     86  1.4  rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
     87  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"
     88  1.4  rillig .  error
     89  1.4  rillig .endif
     90  1.5  rillig 
     91  1.5  rillig # During the loop, the variable is actually defined and nonempty.
     92  1.5  rillig # If the loop were implemented in the same way as the .for loop, the variable
     93  1.5  rillig # would be neither defined nor nonempty since all expressions of the form
     94  1.5  rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
     95  1.5  rillig .if defined(var)
     96  1.5  rillig .  error
     97  1.5  rillig .endif
     98  1.5  rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
     99  1.5  rillig     != "def nonempty"
    100  1.5  rillig .  error
    101  1.5  rillig .endif
    102  1.5  rillig .if defined(var)
    103  1.5  rillig .  error
    104  1.5  rillig .endif
    105  1.7  rillig 
    106  1.7  rillig # Assignment using the ':=' operator, combined with the :@var@ modifier
    107  1.7  rillig #
    108  1.7  rillig 8_DOLLARS=	$$$$$$$$
    109  1.7  rillig # This string literal is written with 8 dollars, and this is saved as the
    110  1.7  rillig # variable value.  But as soon as this value is evaluated, it goes through
    111  1.7  rillig # Var_Subst, which replaces each '$$' with a single '$'.  This could be
    112  1.7  rillig # prevented by VARE_KEEP_DOLLAR, but that flag is usually removed before
    113  1.7  rillig # expanding subexpressions.  See ApplyModifier_Loop and ParseModifierPart
    114  1.7  rillig # for examples.
    115  1.7  rillig #
    116  1.7  rillig .MAKEFLAGS: -dcp
    117  1.7  rillig USE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
    118  1.7  rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
    119  1.7  rillig .  error
    120  1.7  rillig .endif
    121  1.7  rillig #
    122  1.7  rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
    123  1.7  rillig # The ':=' assignment operator evaluates the variable value using the flag
    124  1.7  rillig # VARE_KEEP_DOLLAR, which means that some dollar signs are preserved, but not
    125  1.7  rillig # all.  The dollar signs in the top-level expression and in the indirect
    126  1.7  rillig # ${8_DOLLARS} are preserved.
    127  1.7  rillig #
    128  1.7  rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
    129  1.7  rillig # matter in which context it is evaluated.  What happens in detail is:
    130  1.7  rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
    131  1.7  rillig # Next, each word of the value is expanded on its own, and at this moment
    132  1.7  rillig # in ApplyModifier_Loop, the VARE_KEEP_DOLLAR flag is not passed down to
    133  1.7  rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
    134  1.7  rillig #
    135  1.7  rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
    136  1.7  rillig # modifier and are thus expanded with the flag VARE_KEEP_DOLLAR in action.
    137  1.7  rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
    138  1.7  rillig # "$$$$ $$$$$$$$ $$$$$$$$".
    139  1.7  rillig #
    140  1.7  rillig # The variable expression in the condition then expands this raw stored value
    141  1.7  rillig # once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
    142  1.7  rillig # longer take place since they had only been active during the evaluation of
    143  1.7  rillig # the variable assignment.
    144  1.7  rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
    145  1.7  rillig .  error
    146  1.7  rillig .endif
    147  1.7  rillig .MAKEFLAGS: -d0
    148