Home | History | Annotate | Line # | Download | only in unit-tests
varmod-loop.mk revision 1.15
      1  1.15  rillig # $NetBSD: varmod-loop.mk,v 1.15 2021/04/11 13:35:56 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.13  rillig all: varname-overwriting-target
      8   1.2  rillig all: mod-loop-dollar
      9   1.1  rillig 
     10  1.13  rillig varname-overwriting-target:
     11   1.2  rillig 	# Even "@" works as a variable name since the variable is installed
     12   1.2  rillig 	# in the "current" scope, which in this case is the one from the
     13  1.13  rillig 	# target.  Because of this, after the loop has finished, '$@' is
     14  1.13  rillig 	# undefined.  This is something that make doesn't expect, this may
     15  1.13  rillig 	# even trigger an assertion failure somewhere.
     16   1.2  rillig 	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
     17   1.5  rillig 
     18  1.10  rillig 
     19   1.2  rillig 
     20   1.6  rillig # Demonstrate that it is possible to generate dollar signs using the
     21   1.2  rillig # :@ modifier.
     22   1.2  rillig #
     23   1.2  rillig # These are edge cases that could have resulted in a parse error as well
     24   1.2  rillig # since the $@ at the end could have been interpreted as a variable, which
     25   1.2  rillig # would mean a missing closing @ delimiter.
     26   1.2  rillig mod-loop-dollar:
     27   1.2  rillig 	@echo $@:${:U1:@word@${word}$@:Q}:
     28   1.2  rillig 	@echo $@:${:U2:@word@$${word}$$@:Q}:
     29   1.2  rillig 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
     30   1.2  rillig 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
     31   1.2  rillig 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
     32   1.2  rillig 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
     33   1.4  rillig 
     34   1.4  rillig # It may happen that there are nested :@ modifiers that use the same name for
     35   1.4  rillig # for the loop variable.  These modifiers influence each other.
     36   1.4  rillig #
     37   1.5  rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
     38   1.9  rillig # variable in the scope of the expression and deleting it again after the
     39   1.4  rillig # loop.  This is different from the .for loops, which substitute the variable
     40   1.4  rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
     41   1.4  rillig #
     42   1.4  rillig # To make the behavior more predictable, the :@ modifier should restore the
     43   1.4  rillig # loop variable to the value it had before the loop.  This would result in
     44   1.4  rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
     45   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"
     46   1.4  rillig .  error
     47   1.4  rillig .endif
     48   1.5  rillig 
     49   1.5  rillig # During the loop, the variable is actually defined and nonempty.
     50   1.5  rillig # If the loop were implemented in the same way as the .for loop, the variable
     51   1.5  rillig # would be neither defined nor nonempty since all expressions of the form
     52   1.5  rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
     53   1.5  rillig .if defined(var)
     54   1.5  rillig .  error
     55   1.5  rillig .endif
     56   1.5  rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
     57   1.5  rillig     != "def nonempty"
     58   1.5  rillig .  error
     59   1.5  rillig .endif
     60   1.5  rillig .if defined(var)
     61   1.5  rillig .  error
     62   1.5  rillig .endif
     63   1.7  rillig 
     64   1.7  rillig # Assignment using the ':=' operator, combined with the :@var@ modifier
     65   1.7  rillig #
     66   1.7  rillig 8_DOLLARS=	$$$$$$$$
     67   1.7  rillig # This string literal is written with 8 dollars, and this is saved as the
     68   1.7  rillig # variable value.  But as soon as this value is evaluated, it goes through
     69   1.7  rillig # Var_Subst, which replaces each '$$' with a single '$'.  This could be
     70  1.15  rillig # prevented by VARE_EVAL_KEEP_DOLLAR, but that flag is usually removed
     71  1.11  rillig # before expanding subexpressions.  See ApplyModifier_Loop and
     72  1.11  rillig # ParseModifierPart for examples.
     73   1.7  rillig #
     74   1.7  rillig .MAKEFLAGS: -dcp
     75   1.7  rillig USE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
     76   1.7  rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
     77   1.7  rillig .  error
     78   1.7  rillig .endif
     79   1.7  rillig #
     80   1.7  rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
     81  1.11  rillig # The ':=' assignment operator evaluates the variable value using the mode
     82  1.11  rillig # VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
     83  1.11  rillig # but not all.  The dollar signs in the top-level expression and in the
     84  1.11  rillig # indirect ${8_DOLLARS} are preserved.
     85   1.7  rillig #
     86   1.7  rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
     87   1.7  rillig # matter in which context it is evaluated.  What happens in detail is:
     88   1.7  rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
     89   1.7  rillig # Next, each word of the value is expanded on its own, and at this moment
     90  1.11  rillig # in ApplyModifier_Loop, the flag keepDollar is not passed down to
     91   1.7  rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
     92   1.7  rillig #
     93   1.7  rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
     94  1.11  rillig # modifier and are thus expanded with the flag keepDollar in action.
     95   1.7  rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
     96   1.7  rillig # "$$$$ $$$$$$$$ $$$$$$$$".
     97   1.7  rillig #
     98   1.7  rillig # The variable expression in the condition then expands this raw stored value
     99   1.7  rillig # once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
    100   1.7  rillig # longer take place since they had only been active during the evaluation of
    101   1.7  rillig # the variable assignment.
    102   1.7  rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
    103   1.7  rillig .  error
    104   1.7  rillig .endif
    105   1.7  rillig .MAKEFLAGS: -d0
    106  1.10  rillig 
    107  1.10  rillig # After looping over the words of the expression, the loop variable gets
    108  1.10  rillig # undefined.  The modifier ':@' uses an ordinary global variable for this,
    109  1.10  rillig # which is different from the '.for' loop, which replaces ${var} with
    110  1.10  rillig # ${:Uvalue} in the body of the loop.  This choice of implementation detail
    111  1.10  rillig # can be used for a nasty side effect.  The expression ${:U:@VAR@@} evaluates
    112  1.10  rillig # to an empty string, plus it undefines the variable 'VAR'.  This is the only
    113  1.10  rillig # possibility to undefine a global variable during evaluation.
    114  1.10  rillig GLOBAL=		before-global
    115  1.10  rillig RESULT:=	${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
    116  1.10  rillig .if ${RESULT} != "before-global  undefined"
    117  1.10  rillig .  error
    118  1.10  rillig .endif
    119  1.10  rillig 
    120  1.10  rillig # The above side effect of undefining a variable from a certain scope can be
    121  1.10  rillig # further combined with the otherwise undocumented implementation detail that
    122  1.10  rillig # the argument of an '.if' directive is evaluated in cmdline scope.  Putting
    123  1.10  rillig # these together makes it possible to undefine variables from the cmdline
    124  1.10  rillig # scope, something that is not possible in a straight-forward way.
    125  1.10  rillig .MAKEFLAGS: CMDLINE=cmdline
    126  1.10  rillig .if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
    127  1.10  rillig .  error
    128  1.10  rillig .endif
    129  1.10  rillig # Now the cmdline variable got undefined.
    130  1.10  rillig .if ${CMDLINE} != "cmdline"
    131  1.10  rillig .  error
    132  1.10  rillig .endif
    133  1.10  rillig # At this point, it still looks as if the cmdline variable were defined,
    134  1.10  rillig # since the value of CMDLINE is still "cmdline".  That impression is only
    135  1.10  rillig # superficial though, the cmdline variable is actually deleted.  To
    136  1.10  rillig # demonstrate this, it is now possible to override its value using a global
    137  1.10  rillig # variable, something that was not possible before:
    138  1.10  rillig CMDLINE=	global
    139  1.10  rillig .if ${CMDLINE} != "global"
    140  1.10  rillig .  error
    141  1.10  rillig .endif
    142  1.10  rillig # Now undefine that global variable again, to get back to the original value.
    143  1.10  rillig .undef CMDLINE
    144  1.10  rillig .if ${CMDLINE} != "cmdline"
    145  1.10  rillig .  error
    146  1.10  rillig .endif
    147  1.10  rillig # What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
    148  1.10  rillig # target in the cmdline scope, that same variable was exported to the
    149  1.10  rillig # environment, see Var_SetWithFlags.
    150  1.10  rillig .unexport CMDLINE
    151  1.10  rillig .if ${CMDLINE} != "cmdline"
    152  1.10  rillig .  error
    153  1.10  rillig .endif
    154  1.10  rillig # The above '.unexport' has no effect since UnexportVar requires a global
    155  1.10  rillig # variable of the same name to be defined, otherwise nothing is unexported.
    156  1.10  rillig CMDLINE=	global
    157  1.10  rillig .unexport CMDLINE
    158  1.10  rillig .undef CMDLINE
    159  1.10  rillig .if ${CMDLINE} != "cmdline"
    160  1.10  rillig .  error
    161  1.10  rillig .endif
    162  1.10  rillig # This still didn't work since there must not only be a global variable, the
    163  1.10  rillig # variable must be marked as exported as well, which it wasn't before.
    164  1.10  rillig CMDLINE=	global
    165  1.10  rillig .export CMDLINE
    166  1.10  rillig .unexport CMDLINE
    167  1.10  rillig .undef CMDLINE
    168  1.10  rillig .if ${CMDLINE:Uundefined} != "undefined"
    169  1.10  rillig .  error
    170  1.10  rillig .endif
    171  1.10  rillig # Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
    172  1.10  rillig # traces from the environment are gone as well.  To do that, a global variable
    173  1.10  rillig # had to be defined and exported, something that is far from obvious.  To
    174  1.10  rillig # recap, here is the essence of the above story:
    175  1.10  rillig .MAKEFLAGS: CMDLINE=cmdline	# have a cmdline + environment variable
    176  1.10  rillig .if ${:U:@CMDLINE@@}}		# undefine cmdline, keep environment
    177  1.10  rillig .endif
    178  1.10  rillig CMDLINE=	global		# needed for deleting the environment
    179  1.10  rillig .export CMDLINE			# needed for deleting the environment
    180  1.10  rillig .unexport CMDLINE		# delete the environment
    181  1.10  rillig .undef CMDLINE			# delete the global helper variable
    182  1.10  rillig .if ${CMDLINE:Uundefined} != "undefined"
    183  1.10  rillig .  error			# 'CMDLINE' is gone now from all scopes
    184  1.10  rillig .endif
    185  1.10  rillig 
    186  1.10  rillig 
    187  1.10  rillig # TODO: Actually trigger the undefined behavior (use after free) that was
    188  1.10  rillig #  already suspected in Var_Parse, in the comment 'the value of the variable
    189  1.10  rillig #  must not change'.
    190