Home | History | Annotate | Line # | Download | only in unit-tests
varmod-loop.mk revision 1.10
      1  1.10  rillig # $NetBSD: varmod-loop.mk,v 1.10 2021/02/23 14:17:21 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.10  rillig 	# the variable value cannot be accessed since the empty "variable"
     38  1.10  rillig 	# is protected to always return an empty string.
     39   1.3  rillig 	@echo empty: :${:U1 2 3:@@x${}y@}:
     40   1.3  rillig 
     41  1.10  rillig 
     42  1.10  rillig # The :@ modifier resolves the variables from the replacement text once more
     43  1.10  rillig # than expected.  In particular, it resolves _all_ variables from the scope,
     44  1.10  rillig # and not only the loop variable (in this case v).
     45  1.10  rillig SRCS=		source
     46  1.10  rillig CFLAGS.source=	before
     47  1.10  rillig ALL_CFLAGS:=	${SRCS:@src@${CFLAGS.${src}}@}	# note the ':='
     48  1.10  rillig CFLAGS.source+=	after
     49  1.10  rillig .if ${ALL_CFLAGS} != "before"
     50  1.10  rillig .  error
     51  1.10  rillig .endif
     52  1.10  rillig 
     53  1.10  rillig 
     54  1.10  rillig # In the following example, the modifier ':@' expands the '$$' to '$'.  This
     55  1.10  rillig # means that when the resulting expression is evaluated, these resulting '$'
     56  1.10  rillig # will be interpreted as starting a subexpression.
     57   1.2  rillig #
     58   1.2  rillig # The d means direct reference, the i means indirect reference.
     59   1.2  rillig RESOLVE=	${RES1} $${RES1}
     60   1.2  rillig RES1=		1d${RES2} 1i$${RES2}
     61   1.2  rillig RES2=		2d${RES3} 2i$${RES3}
     62   1.2  rillig RES3=		3
     63   1.2  rillig 
     64  1.10  rillig # TODO: convert to '.if'.
     65   1.2  rillig mod-loop-resolve:
     66   1.2  rillig 	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
     67   1.2  rillig 
     68  1.10  rillig 
     69   1.2  rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
     70   1.2  rillig # or two dollar signs, which were silently ignored.
     71   1.2  rillig # There's no point in allowing a dollar sign in that position.
     72   1.2  rillig mod-loop-varname-dollar:
     73   1.2  rillig 	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
     74   1.2  rillig 	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
     75   1.2  rillig 	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
     76   1.2  rillig 
     77   1.6  rillig # Demonstrate that it is possible to generate dollar signs using the
     78   1.2  rillig # :@ modifier.
     79   1.2  rillig #
     80   1.2  rillig # These are edge cases that could have resulted in a parse error as well
     81   1.2  rillig # since the $@ at the end could have been interpreted as a variable, which
     82   1.2  rillig # would mean a missing closing @ delimiter.
     83   1.2  rillig mod-loop-dollar:
     84   1.2  rillig 	@echo $@:${:U1:@word@${word}$@:Q}:
     85   1.2  rillig 	@echo $@:${:U2:@word@$${word}$$@:Q}:
     86   1.2  rillig 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
     87   1.2  rillig 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
     88   1.2  rillig 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
     89   1.2  rillig 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
     90   1.4  rillig 
     91   1.4  rillig # It may happen that there are nested :@ modifiers that use the same name for
     92   1.4  rillig # for the loop variable.  These modifiers influence each other.
     93   1.4  rillig #
     94   1.5  rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
     95   1.9  rillig # variable in the scope of the expression and deleting it again after the
     96   1.4  rillig # loop.  This is different from the .for loops, which substitute the variable
     97   1.4  rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
     98   1.4  rillig #
     99   1.4  rillig # To make the behavior more predictable, the :@ modifier should restore the
    100   1.4  rillig # loop variable to the value it had before the loop.  This would result in
    101   1.4  rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
    102   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"
    103   1.4  rillig .  error
    104   1.4  rillig .endif
    105   1.5  rillig 
    106   1.5  rillig # During the loop, the variable is actually defined and nonempty.
    107   1.5  rillig # If the loop were implemented in the same way as the .for loop, the variable
    108   1.5  rillig # would be neither defined nor nonempty since all expressions of the form
    109   1.5  rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
    110   1.5  rillig .if defined(var)
    111   1.5  rillig .  error
    112   1.5  rillig .endif
    113   1.5  rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
    114   1.5  rillig     != "def nonempty"
    115   1.5  rillig .  error
    116   1.5  rillig .endif
    117   1.5  rillig .if defined(var)
    118   1.5  rillig .  error
    119   1.5  rillig .endif
    120   1.7  rillig 
    121   1.7  rillig # Assignment using the ':=' operator, combined with the :@var@ modifier
    122   1.7  rillig #
    123   1.7  rillig 8_DOLLARS=	$$$$$$$$
    124   1.7  rillig # This string literal is written with 8 dollars, and this is saved as the
    125   1.7  rillig # variable value.  But as soon as this value is evaluated, it goes through
    126   1.7  rillig # Var_Subst, which replaces each '$$' with a single '$'.  This could be
    127   1.7  rillig # prevented by VARE_KEEP_DOLLAR, but that flag is usually removed before
    128   1.7  rillig # expanding subexpressions.  See ApplyModifier_Loop and ParseModifierPart
    129   1.7  rillig # for examples.
    130   1.7  rillig #
    131   1.7  rillig .MAKEFLAGS: -dcp
    132   1.7  rillig USE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
    133   1.7  rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
    134   1.7  rillig .  error
    135   1.7  rillig .endif
    136   1.7  rillig #
    137   1.7  rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
    138   1.7  rillig # The ':=' assignment operator evaluates the variable value using the flag
    139   1.7  rillig # VARE_KEEP_DOLLAR, which means that some dollar signs are preserved, but not
    140   1.7  rillig # all.  The dollar signs in the top-level expression and in the indirect
    141   1.7  rillig # ${8_DOLLARS} are preserved.
    142   1.7  rillig #
    143   1.7  rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
    144   1.7  rillig # matter in which context it is evaluated.  What happens in detail is:
    145   1.7  rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
    146   1.7  rillig # Next, each word of the value is expanded on its own, and at this moment
    147   1.7  rillig # in ApplyModifier_Loop, the VARE_KEEP_DOLLAR flag is not passed down to
    148   1.7  rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
    149   1.7  rillig #
    150   1.7  rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
    151   1.7  rillig # modifier and are thus expanded with the flag VARE_KEEP_DOLLAR in action.
    152   1.7  rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
    153   1.7  rillig # "$$$$ $$$$$$$$ $$$$$$$$".
    154   1.7  rillig #
    155   1.7  rillig # The variable expression in the condition then expands this raw stored value
    156   1.7  rillig # once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
    157   1.7  rillig # longer take place since they had only been active during the evaluation of
    158   1.7  rillig # the variable assignment.
    159   1.7  rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
    160   1.7  rillig .  error
    161   1.7  rillig .endif
    162   1.7  rillig .MAKEFLAGS: -d0
    163  1.10  rillig 
    164  1.10  rillig # After looping over the words of the expression, the loop variable gets
    165  1.10  rillig # undefined.  The modifier ':@' uses an ordinary global variable for this,
    166  1.10  rillig # which is different from the '.for' loop, which replaces ${var} with
    167  1.10  rillig # ${:Uvalue} in the body of the loop.  This choice of implementation detail
    168  1.10  rillig # can be used for a nasty side effect.  The expression ${:U:@VAR@@} evaluates
    169  1.10  rillig # to an empty string, plus it undefines the variable 'VAR'.  This is the only
    170  1.10  rillig # possibility to undefine a global variable during evaluation.
    171  1.10  rillig GLOBAL=		before-global
    172  1.10  rillig RESULT:=	${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
    173  1.10  rillig .if ${RESULT} != "before-global  undefined"
    174  1.10  rillig .  error
    175  1.10  rillig .endif
    176  1.10  rillig 
    177  1.10  rillig # The above side effect of undefining a variable from a certain scope can be
    178  1.10  rillig # further combined with the otherwise undocumented implementation detail that
    179  1.10  rillig # the argument of an '.if' directive is evaluated in cmdline scope.  Putting
    180  1.10  rillig # these together makes it possible to undefine variables from the cmdline
    181  1.10  rillig # scope, something that is not possible in a straight-forward way.
    182  1.10  rillig .MAKEFLAGS: CMDLINE=cmdline
    183  1.10  rillig .if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
    184  1.10  rillig .  error
    185  1.10  rillig .endif
    186  1.10  rillig # Now the cmdline variable got undefined.
    187  1.10  rillig .if ${CMDLINE} != "cmdline"
    188  1.10  rillig .  error
    189  1.10  rillig .endif
    190  1.10  rillig # At this point, it still looks as if the cmdline variable were defined,
    191  1.10  rillig # since the value of CMDLINE is still "cmdline".  That impression is only
    192  1.10  rillig # superficial though, the cmdline variable is actually deleted.  To
    193  1.10  rillig # demonstrate this, it is now possible to override its value using a global
    194  1.10  rillig # variable, something that was not possible before:
    195  1.10  rillig CMDLINE=	global
    196  1.10  rillig .if ${CMDLINE} != "global"
    197  1.10  rillig .  error
    198  1.10  rillig .endif
    199  1.10  rillig # Now undefine that global variable again, to get back to the original value.
    200  1.10  rillig .undef CMDLINE
    201  1.10  rillig .if ${CMDLINE} != "cmdline"
    202  1.10  rillig .  error
    203  1.10  rillig .endif
    204  1.10  rillig # What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
    205  1.10  rillig # target in the cmdline scope, that same variable was exported to the
    206  1.10  rillig # environment, see Var_SetWithFlags.
    207  1.10  rillig .unexport CMDLINE
    208  1.10  rillig .if ${CMDLINE} != "cmdline"
    209  1.10  rillig .  error
    210  1.10  rillig .endif
    211  1.10  rillig # The above '.unexport' has no effect since UnexportVar requires a global
    212  1.10  rillig # variable of the same name to be defined, otherwise nothing is unexported.
    213  1.10  rillig CMDLINE=	global
    214  1.10  rillig .unexport CMDLINE
    215  1.10  rillig .undef CMDLINE
    216  1.10  rillig .if ${CMDLINE} != "cmdline"
    217  1.10  rillig .  error
    218  1.10  rillig .endif
    219  1.10  rillig # This still didn't work since there must not only be a global variable, the
    220  1.10  rillig # variable must be marked as exported as well, which it wasn't before.
    221  1.10  rillig CMDLINE=	global
    222  1.10  rillig .export CMDLINE
    223  1.10  rillig .unexport CMDLINE
    224  1.10  rillig .undef CMDLINE
    225  1.10  rillig .if ${CMDLINE:Uundefined} != "undefined"
    226  1.10  rillig .  error
    227  1.10  rillig .endif
    228  1.10  rillig # Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
    229  1.10  rillig # traces from the environment are gone as well.  To do that, a global variable
    230  1.10  rillig # had to be defined and exported, something that is far from obvious.  To
    231  1.10  rillig # recap, here is the essence of the above story:
    232  1.10  rillig .MAKEFLAGS: CMDLINE=cmdline	# have a cmdline + environment variable
    233  1.10  rillig .if ${:U:@CMDLINE@@}}		# undefine cmdline, keep environment
    234  1.10  rillig .endif
    235  1.10  rillig CMDLINE=	global		# needed for deleting the environment
    236  1.10  rillig .export CMDLINE			# needed for deleting the environment
    237  1.10  rillig .unexport CMDLINE		# delete the environment
    238  1.10  rillig .undef CMDLINE			# delete the global helper variable
    239  1.10  rillig .if ${CMDLINE:Uundefined} != "undefined"
    240  1.10  rillig .  error			# 'CMDLINE' is gone now from all scopes
    241  1.10  rillig .endif
    242  1.10  rillig 
    243  1.10  rillig 
    244  1.10  rillig # TODO: Actually trigger the undefined behavior (use after free) that was
    245  1.10  rillig #  already suspected in Var_Parse, in the comment 'the value of the variable
    246  1.10  rillig #  must not change'.
    247