Home | History | Annotate | Line # | Download | only in unit-tests
directive-for.mk revision 1.15
      1  1.15  rillig # $NetBSD: directive-for.mk,v 1.15 2022/10/01 09:23:04 rillig Exp $
      2   1.1  rillig #
      3   1.1  rillig # Tests for the .for directive.
      4   1.9  rillig #
      5   1.9  rillig # TODO: Describe naming conventions for the loop variables.
      6   1.9  rillig #	.for f in values
      7   1.9  rillig #	.for file in values
      8   1.9  rillig #	.for _FILE_ in values
      9   1.9  rillig #	.for .FILE. in values
     10   1.9  rillig #	.for _f_ in values
     11   1.1  rillig 
     12   1.1  rillig # Using the .for loop, lists of values can be produced.
     13   1.1  rillig # In simple cases, the :@var@${var}@ variable modifier can be used to
     14  1.11  rillig # achieve the same effects.
     15   1.1  rillig #
     16   1.1  rillig .undef NUMBERS
     17   1.1  rillig .for num in 1 2 3
     18   1.1  rillig NUMBERS+=	${num}
     19   1.1  rillig .endfor
     20   1.1  rillig .if ${NUMBERS} != "1 2 3"
     21   1.1  rillig .  error
     22   1.1  rillig .endif
     23   1.1  rillig 
     24   1.1  rillig # The .for loop also works for multiple iteration variables.
     25   1.9  rillig # This is something that the variable modifier :@ cannot do.
     26   1.1  rillig .for name value in VARNAME value NAME2 value2
     27   1.1  rillig ${name}=	${value}
     28   1.1  rillig .endfor
     29   1.1  rillig .if ${VARNAME} != "value" || ${NAME2} != "value2"
     30   1.1  rillig .  error
     31   1.1  rillig .endif
     32   1.1  rillig 
     33   1.1  rillig # The .for loop splits the items at whitespace, taking quotes into account,
     34   1.1  rillig # just like the :M or :S variable modifiers.
     35   1.1  rillig #
     36   1.1  rillig # Until 2012-06-03, it had split the items exactly at whitespace, without
     37   1.9  rillig # taking the quotes into account.  This had resulted in 10 words.
     38   1.1  rillig #
     39   1.1  rillig .undef WORDS
     40   1.1  rillig .for var in one t\ w\ o "three three" 'four four' `five six`
     41   1.1  rillig WORDS+=	counted
     42   1.1  rillig .endfor
     43   1.1  rillig .if ${WORDS:[#]} != 6
     44   1.1  rillig .  error
     45   1.1  rillig .endif
     46   1.1  rillig 
     47   1.1  rillig # In the body of the .for loop, the iteration variables can be accessed
     48   1.1  rillig # like normal variables, even though they are not really variables.
     49   1.1  rillig #
     50   1.1  rillig # Instead, the expression ${var} is transformed into ${:U1}, ${:U2} and so
     51   1.1  rillig # on, before the loop body is evaluated.
     52   1.1  rillig #
     53   1.1  rillig # A notable effect of this implementation technique is that the .for
     54   1.1  rillig # iteration variables and the normal global variables live in separate
     55   1.1  rillig # namespaces and do not influence each other.
     56   1.1  rillig #
     57   1.1  rillig var=	value before
     58   1.1  rillig var2=	value before
     59   1.1  rillig .for var var2 in 1 2 3 4
     60   1.1  rillig .endfor
     61   1.1  rillig .if ${var} != "value before"
     62   1.2  rillig .  warning After the .for loop, var must still have its original value.
     63   1.1  rillig .endif
     64   1.1  rillig .if ${var2} != "value before"
     65   1.2  rillig .  warning After the .for loop, var2 must still have its original value.
     66   1.1  rillig .endif
     67   1.1  rillig 
     68   1.1  rillig # Everything from the paragraph above also applies if the loop body is
     69   1.1  rillig # empty, even if there is no actual iteration since the loop items are
     70   1.1  rillig # also empty.
     71   1.1  rillig #
     72   1.1  rillig var=	value before
     73   1.1  rillig var2=	value before
     74   1.1  rillig .for var var2 in ${:U}
     75   1.1  rillig .endfor
     76   1.1  rillig .if ${var} != "value before"
     77   1.2  rillig .  warning After the .for loop, var must still have its original value.
     78   1.1  rillig .endif
     79   1.1  rillig .if ${var2} != "value before"
     80   1.2  rillig .  warning After the .for loop, var2 must still have its original value.
     81   1.1  rillig .endif
     82   1.1  rillig 
     83   1.1  rillig # Until 2008-12-21, the values of the iteration variables were simply
     84   1.1  rillig # inserted as plain text and then parsed as usual, which made it possible
     85   1.1  rillig # to achieve all kinds of strange effects.
     86   1.1  rillig #
     87   1.1  rillig # Before that date, the .for loop expanded to:
     88   1.1  rillig #	EXPANSION+= value
     89   1.1  rillig # Since that date, the .for loop expands to:
     90   1.1  rillig #	EXPANSION${:U+}= value
     91   1.1  rillig #
     92   1.6  rillig EXPANSION=		before
     93   1.6  rillig EXPANSION+ =		before
     94   1.1  rillig .for plus in +
     95   1.1  rillig EXPANSION${plus}=	value
     96   1.1  rillig .endfor
     97   1.1  rillig .if ${EXPANSION} != "before"
     98   1.1  rillig .  error This must be a make from before 2009.
     99   1.1  rillig .endif
    100   1.1  rillig .if ${EXPANSION+} != "value"
    101   1.1  rillig .  error This must be a make from before 2009.
    102   1.1  rillig .endif
    103   1.1  rillig 
    104   1.3  rillig # When the outer .for loop is expanded, it sees the expression ${i} and
    105   1.3  rillig # expands it.  The inner loop then has nothing more to expand.
    106   1.3  rillig .for i in outer
    107   1.3  rillig .  for i in inner
    108   1.3  rillig .    info ${i}
    109   1.3  rillig .  endfor
    110   1.3  rillig .endfor
    111   1.3  rillig 
    112   1.4  rillig # From https://gnats.netbsd.org/29985.
    113   1.4  rillig #
    114   1.4  rillig # Until 2008-12-21, the .for loop was expanded by replacing the variable
    115   1.4  rillig # value literally in the body.  This could lead to situations where the
    116   1.4  rillig # characters from the variable value were interpreted as markup rather than
    117   1.4  rillig # plain text.
    118   1.4  rillig #
    119   1.4  rillig # Until 2012-06-03, the .for loop had split the words at whitespace, without
    120   1.4  rillig # taking quotes into account.  This made it possible to have variable values
    121   1.4  rillig # like "a:\ a:\file.txt" that ended in a single backslash.  Since then, the
    122   1.4  rillig # variable values have been replaced with expressions of the form ${:U...},
    123   1.4  rillig # which are not interpreted as code anymore.
    124   1.4  rillig #
    125   1.4  rillig # As of 2020-09-22, a comment in for.c says that it may be possible to
    126   1.4  rillig # produce an "unwanted substitution", but there is no demonstration code yet.
    127   1.4  rillig #
    128   1.4  rillig # The above changes prevent a backslash at the end of a word from being
    129   1.4  rillig # interpreted as part of the code.  Because of this, the trailingBackslash
    130   1.4  rillig # hack in Var_Subst is no longer needed and as of 2020-09-22, has been
    131   1.4  rillig # removed.
    132   1.5  rillig .for path in a:\ a:\file.txt d:\\ d:\\file.txt
    133   1.4  rillig .  info ${path}
    134   1.4  rillig .endfor
    135   1.4  rillig 
    136   1.7  rillig # Ensure that braces and parentheses are properly escaped by the .for loop.
    137   1.7  rillig # Each line must print the same word 3 times.
    138  1.11  rillig # See ForLoop_SubstBody.
    139   1.7  rillig .for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{
    140   1.7  rillig .  info $v ${v} $(v)
    141   1.7  rillig .endfor
    142   1.7  rillig 
    143   1.8  rillig # As of 2020-10-25, the variable names may contain arbitrary characters,
    144   1.8  rillig # except for whitespace.  This allows for creative side effects. Hopefully
    145   1.8  rillig # nobody is misusing this "feature".
    146   1.8  rillig var=	outer
    147   1.8  rillig .for var:Q in value "quoted"
    148   1.8  rillig .  info ${var} ${var:Q} ${var:Q:Q}
    149   1.8  rillig .endfor
    150   1.8  rillig 
    151  1.10  rillig 
    152  1.10  rillig # XXX: A parse error or evaluation error in the items of the .for loop
    153  1.10  rillig # should skip the whole loop.  As of 2020-12-27, the loop is expanded twice.
    154  1.10  rillig .for var in word1 ${:Uword2:Z} word3
    155  1.10  rillig .  info XXX: Not reached ${var}
    156  1.10  rillig .endfor
    157  1.10  rillig 
    158  1.11  rillig 
    159  1.11  rillig # An empty list of variables to the left of the 'in' is a parse error.
    160  1.13  rillig .for in value			# expect+0: no iteration variables in for
    161  1.11  rillig # XXX: The loop body is evaluated once, even with the parse error above.
    162  1.13  rillig .  error			# expect+0: Missing argument for ".error"
    163  1.13  rillig .endfor				# expect+0: for-less endfor
    164  1.11  rillig 
    165  1.11  rillig # An empty list of iteration values to the right of the 'in' is accepted.
    166  1.11  rillig # Unlike in the shell, it is not a parse error.
    167  1.11  rillig .for var in
    168  1.11  rillig .  error
    169  1.11  rillig .endfor
    170  1.11  rillig 
    171  1.11  rillig # If the iteration values become empty after expanding the expressions, the
    172  1.11  rillig # body of the loop is not evaluated.  It is not a parse error.
    173  1.11  rillig .for var in ${:U}
    174  1.11  rillig .  error
    175  1.11  rillig .endfor
    176  1.11  rillig 
    177  1.11  rillig 
    178  1.11  rillig # The loop body can be empty.
    179  1.11  rillig .for var in 1 2 3
    180  1.11  rillig .endfor
    181  1.11  rillig 
    182  1.11  rillig 
    183  1.11  rillig # A mismatched .if inside a .for loop is detected each time when the loop body
    184  1.11  rillig # is processed.
    185  1.11  rillig .for var in value
    186  1.11  rillig .  if 0
    187  1.13  rillig .endfor				# expect+0: 1 open conditional
    188  1.11  rillig 
    189  1.11  rillig # If there are no iteration values, the loop body is not processed, and the
    190  1.11  rillig # check for mismatched conditionals is not performed.
    191  1.11  rillig .for var in ${:U}
    192  1.11  rillig .  if 0
    193  1.11  rillig .endfor
    194  1.11  rillig 
    195  1.11  rillig 
    196  1.11  rillig # When a .for without the corresponding .endfor occurs in an inactive branch
    197  1.11  rillig # of an .if, the .for directive is just skipped, it does not even need a
    198  1.11  rillig # corresponding .endfor.  In other words, the behavior of the parser depends
    199  1.11  rillig # on the actual values of the conditions in the .if clauses.
    200  1.11  rillig .if 0
    201  1.11  rillig .  for var in value		# does not need a corresponding .endfor
    202  1.11  rillig .endif
    203  1.13  rillig .endfor				# expect+0: for-less endfor
    204  1.13  rillig .endif				# expect+0: if-less endif
    205  1.11  rillig 
    206  1.11  rillig 
    207  1.11  rillig # When a .for without the corresponding .endfor occurs in an active branch of
    208  1.11  rillig # an .if, the parser just counts the number of .for and .endfor directives,
    209  1.11  rillig # without looking at any other directives.
    210  1.11  rillig .if 1
    211  1.11  rillig .  for var in value
    212  1.13  rillig .    endif			# expect+0: if-less endif
    213  1.11  rillig .  endfor			# no 'for-less endfor'
    214  1.11  rillig .endif				# no 'if-less endif'
    215  1.12  rillig 
    216  1.12  rillig 
    217  1.12  rillig # When make parses a .for loop, it assumes that there is no line break between
    218  1.12  rillig # the '.' and the 'for' or 'endfor', as there is no practical reason to break
    219  1.12  rillig # the line at this point.  When make scans the outer .for loop, it does not
    220  1.12  rillig # recognize the inner directives as such.  When make scans the inner .for
    221  1.12  rillig # loop, it recognizes the '.\n for' but does not recognize the '.\n endfor',
    222  1.12  rillig # as LK_FOR_BODY preserves the backslash-newline sequences.
    223  1.12  rillig .MAKEFLAGS: -df
    224  1.12  rillig .for outer in o
    225  1.12  rillig .\
    226  1.12  rillig    for inner in i
    227  1.12  rillig .\
    228  1.12  rillig    endfor
    229  1.12  rillig .endfor
    230  1.12  rillig .MAKEFLAGS: -d0
    231  1.14  rillig 
    232  1.14  rillig 
    233  1.14  rillig # When there is a variable definition 'scope=cmdline' from the command line
    234  1.14  rillig # (which has higher precedence than global variables) and a .for loop iterates
    235  1.14  rillig # over a variable of the same name, the expression '${scope}' expands to the
    236  1.14  rillig # value from the .for loop.  This is because when the body of the .for loop is
    237  1.14  rillig # expanded, the expression '${scope}' is textually replaced with ${:Uloop}',
    238  1.15  rillig # without resolving any other variable names (ForLoop_SubstBody).  Later, when
    239  1.15  rillig # the body of the .for loop is actually interpreted, the body text doesn't
    240  1.15  rillig # contain the word 'scope' anymore.
    241  1.14  rillig .MAKEFLAGS: scope=cmdline
    242  1.14  rillig .for scope in loop
    243  1.14  rillig .  if ${scope} != "loop"
    244  1.14  rillig .    error
    245  1.14  rillig .  endif
    246  1.14  rillig .endfor
    247