Home | History | Annotate | Line # | Download | only in unit-tests
varmod-loop.mk revision 1.6
      1  1.6  rillig # $NetBSD: varmod-loop.mk,v 1.6 2020/11/03 18:21:36 rillig Exp $
      2  1.1  rillig #
      3  1.2  rillig # Tests for the :@var (a] ...${var}...@ variable modifier.
      4  1.1  rillig 
      5  1.2  rillig all: mod-loop-varname
      6  1.2  rillig all: mod-loop-resolve
      7  1.2  rillig all: mod-loop-varname-dollar
      8  1.2  rillig all: mod-loop-dollar
      9  1.1  rillig 
     10  1.2  rillig # In the :@ modifier, the name of the loop variable can even be generated
     11  1.2  rillig # dynamically.  There's no practical use-case for this, and hopefully nobody
     12  1.2  rillig # will ever depend on this, but technically it's possible.
     13  1.2  rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
     14  1.2  rillig mod-loop-varname:
     15  1.2  rillig 	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
     16  1.5  rillig 
     17  1.5  rillig 	# ":::" is a very creative variable name, unlikely in practice.
     18  1.2  rillig 	# The expression ${\:\:\:} would not work since backslashes can only
     19  1.2  rillig 	# be escaped in the modifiers, but not in the variable name.
     20  1.2  rillig 	@echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
     21  1.5  rillig 
     22  1.2  rillig 	# "@@" is another creative variable name.
     23  1.2  rillig 	@echo :${:U1 2 3:@\@\@@x${@@}y@}:
     24  1.5  rillig 
     25  1.2  rillig 	# Even "@" works as a variable name since the variable is installed
     26  1.2  rillig 	# in the "current" scope, which in this case is the one from the
     27  1.2  rillig 	# target.
     28  1.2  rillig 	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
     29  1.5  rillig 
     30  1.2  rillig 	# In extreme cases, even the backslash can be used as variable name.
     31  1.2  rillig 	# It needs to be doubled though.
     32  1.2  rillig 	@echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
     33  1.2  rillig 
     34  1.3  rillig 	# The variable name can technically be empty, and in this situation
     35  1.3  rillig 	# the variable value cannot be accessed since the empty variable is
     36  1.3  rillig 	# protected to always return an empty string.
     37  1.3  rillig 	@echo empty: :${:U1 2 3:@@x${}y@}:
     38  1.3  rillig 
     39  1.2  rillig # The :@ modifier resolves the variables a little more often than expected.
     40  1.2  rillig # In particular, it resolves _all_ variables from the context, and not only
     41  1.2  rillig # the loop variable (in this case v).
     42  1.2  rillig #
     43  1.2  rillig # The d means direct reference, the i means indirect reference.
     44  1.2  rillig RESOLVE=	${RES1} $${RES1}
     45  1.2  rillig RES1=		1d${RES2} 1i$${RES2}
     46  1.2  rillig RES2=		2d${RES3} 2i$${RES3}
     47  1.2  rillig RES3=		3
     48  1.2  rillig 
     49  1.2  rillig mod-loop-resolve:
     50  1.2  rillig 	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
     51  1.2  rillig 
     52  1.2  rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
     53  1.2  rillig # or two dollar signs, which were silently ignored.
     54  1.2  rillig # There's no point in allowing a dollar sign in that position.
     55  1.2  rillig mod-loop-varname-dollar:
     56  1.2  rillig 	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
     57  1.2  rillig 	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
     58  1.2  rillig 	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
     59  1.2  rillig 
     60  1.6  rillig # Demonstrate that it is possible to generate dollar signs using the
     61  1.2  rillig # :@ modifier.
     62  1.2  rillig #
     63  1.2  rillig # These are edge cases that could have resulted in a parse error as well
     64  1.2  rillig # since the $@ at the end could have been interpreted as a variable, which
     65  1.2  rillig # would mean a missing closing @ delimiter.
     66  1.2  rillig mod-loop-dollar:
     67  1.2  rillig 	@echo $@:${:U1:@word@${word}$@:Q}:
     68  1.2  rillig 	@echo $@:${:U2:@word@$${word}$$@:Q}:
     69  1.2  rillig 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
     70  1.2  rillig 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
     71  1.2  rillig 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
     72  1.2  rillig 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
     73  1.4  rillig 
     74  1.4  rillig # It may happen that there are nested :@ modifiers that use the same name for
     75  1.4  rillig # for the loop variable.  These modifiers influence each other.
     76  1.4  rillig #
     77  1.5  rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
     78  1.4  rillig # variable in the context of the expression and deleting it again after the
     79  1.4  rillig # loop.  This is different from the .for loops, which substitute the variable
     80  1.4  rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
     81  1.4  rillig #
     82  1.4  rillig # To make the behavior more predictable, the :@ modifier should restore the
     83  1.4  rillig # loop variable to the value it had before the loop.  This would result in
     84  1.4  rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
     85  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"
     86  1.4  rillig .  error
     87  1.4  rillig .endif
     88  1.5  rillig 
     89  1.5  rillig # During the loop, the variable is actually defined and nonempty.
     90  1.5  rillig # If the loop were implemented in the same way as the .for loop, the variable
     91  1.5  rillig # would be neither defined nor nonempty since all expressions of the form
     92  1.5  rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
     93  1.5  rillig .if defined(var)
     94  1.5  rillig .  error
     95  1.5  rillig .endif
     96  1.5  rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
     97  1.5  rillig     != "def nonempty"
     98  1.5  rillig .  error
     99  1.5  rillig .endif
    100  1.5  rillig .if defined(var)
    101  1.5  rillig .  error
    102  1.5  rillig .endif
    103