Home | History | Annotate | Line # | Download | only in unit-tests
directive-for-errors.mk revision 1.4
      1 # $NetBSD: directive-for-errors.mk,v 1.4 2023/05/08 10:24:07 rillig Exp $
      2 #
      3 # Tests for error handling in .for loops.
      4 
      5 # A .for directive must be followed by whitespace, everything else results
      6 # in a parse error.
      7 .fori in 1 2 3
      8 .  warning ${i}
      9 .endfor
     10 
     11 # A slash is not whitespace, therefore this is not parsed as a .for loop.
     12 #
     13 # XXX: The error message is misleading though.  As of 2020-12-31, it says
     14 # "Unknown directive "for"", but that directive is actually known.  This is
     15 # because ForEval does not detect the .for loop as such, so parsing
     16 # continues in ParseLine > ParseDependencyLine > ParseDependency >
     17 # ParseDependencyTargets > ParseErrorNoDependency, and there the directive
     18 # name is parsed a bit differently.
     19 .for/i in 1 2 3
     20 .  warning ${i}
     21 .endfor
     22 
     23 # Before for.c 1.173 from 2023-05-08, the variable name could be an arbitrary
     24 # word, it only needed to be separated by whitespace.  Even '$' and '\' were
     25 # valid variable names, which was not useful in practice.
     26 #
     27 # The '$$' was not replaced with the values '1' or '3' from the .for loop,
     28 # instead it was kept as-is, and when the .info directive expanded its
     29 # argument, each '$$' got replaced with a single '$'.  The "long variable
     30 # expression" ${$} got replaced though, even though this would be a parse
     31 # error everywhere outside a .for loop.
     32 ${:U\$}=	dollar		# see whether the "variable" '$' is local
     33 ${:U\\}=	backslash	# see whether the "variable" '\' is local
     34 .for $ \ in 1 2 3 4
     35 .  info Dollar $$ ${$} $($) and backslash $\ ${\} $(\).
     36 .endfor
     37 
     38 # If there are no variables, there is no point in expanding the .for loop
     39 # since this would end up in an endless loop, consuming 0 of the 3 values in
     40 # each iteration.
     41 .for in 1 2 3
     42 # XXX: This should not be reached.  It should be skipped, as already done
     43 # when the number of values is not a multiple of the number of variables,
     44 # see below.
     45 .  warning Should not be reached.
     46 .endfor
     47 
     48 # There are 3 variables and 5 values.  These 5 values cannot be split evenly
     49 # among the variables, therefore the loop is not expanded at all, it is
     50 # skipped instead.
     51 .for a b c in 1 2 3 4 5
     52 .  warning Should not be reached.
     53 .endfor
     54 
     55 # The list of values after the 'in' may be empty, no matter if this emptiness
     56 # comes from an empty expansion or even from a syntactically empty line.
     57 .for i in
     58 .  info Would be reached if there were items to loop over.
     59 .endfor
     60 
     61 # A missing 'in' should parse the .for loop but skip the body.
     62 # expect+1: missing `in' in for
     63 .for i over k
     64 # XXX: As of 2020-12-31, this line is reached once.
     65 .  warning Should not be reached.
     66 .endfor
     67 
     68 # A malformed modifier should be detected and skip the body of the loop.
     69 #
     70 # XXX: As of 2020-12-31, Var_Subst doesn't report any errors, therefore
     71 # the loop body is expanded as if no error had happened.
     72 .for i in 1 2 ${:U3:Z} 4
     73 .  warning Should not be reached.
     74 .endfor
     75