xref: /freebsd/contrib/bmake/unit-tests/directive-for-errors.mk (revision 226192822cddc30cacecd55bccb48f39c653058c)
1*22619282SSimon J. Gerraty# $NetBSD: directive-for-errors.mk,v 1.13 2024/07/06 10:14:35 rillig Exp $
206b9b3e0SSimon J. Gerraty#
306b9b3e0SSimon J. Gerraty# Tests for error handling in .for loops.
406b9b3e0SSimon J. Gerraty
5c1d01b5fSSimon J. Gerraty
606b9b3e0SSimon J. Gerraty# A .for directive must be followed by whitespace, everything else results
706b9b3e0SSimon J. Gerraty# in a parse error.
8c1d01b5fSSimon J. Gerraty# expect+1: Unknown directive "fori"
906b9b3e0SSimon J. Gerraty.fori in 1 2 3
10*22619282SSimon J. Gerraty# expect+1: warning: <>
11c1d01b5fSSimon J. Gerraty.  warning <${i}>
12*22619282SSimon J. Gerraty# expect+1: for-less endfor
1306b9b3e0SSimon J. Gerraty.endfor
14c1d01b5fSSimon J. Gerraty
1506b9b3e0SSimon J. Gerraty
1606b9b3e0SSimon J. Gerraty# A slash is not whitespace, therefore this is not parsed as a .for loop.
1706b9b3e0SSimon J. Gerraty#
1806b9b3e0SSimon J. Gerraty# XXX: The error message is misleading though.  As of 2020-12-31, it says
19c1d01b5fSSimon J. Gerraty# 'Unknown directive "for"', but that directive is actually known.  This is
2006b9b3e0SSimon J. Gerraty# because ForEval does not detect the .for loop as such, so parsing
21b0c40a00SSimon J. Gerraty# continues in ParseLine > ParseDependencyLine > ParseDependency >
22b0c40a00SSimon J. Gerraty# ParseDependencyTargets > ParseErrorNoDependency, and there the directive
2306b9b3e0SSimon J. Gerraty# name is parsed a bit differently.
24c1d01b5fSSimon J. Gerraty# expect+1: Unknown directive "for"
2506b9b3e0SSimon J. Gerraty.for/i in 1 2 3
26*22619282SSimon J. Gerraty# expect+1: warning: <>
27c1d01b5fSSimon J. Gerraty.  warning <${i}>
28*22619282SSimon J. Gerraty# expect+1: for-less endfor
2906b9b3e0SSimon J. Gerraty.endfor
3006b9b3e0SSimon J. Gerraty
31c1d01b5fSSimon J. Gerraty
32c1d01b5fSSimon J. Gerraty# Before for.c 1.173 from 2023-05-08, the variable name could be an arbitrary
33c1d01b5fSSimon J. Gerraty# word, it only needed to be separated by whitespace.  Even '$' and '\' were
34c1d01b5fSSimon J. Gerraty# valid variable names, which was not useful in practice.
3506b9b3e0SSimon J. Gerraty#
36c1d01b5fSSimon J. Gerraty# The '$$' was not replaced with the values '1' or '3' from the .for loop,
37c1d01b5fSSimon J. Gerraty# instead it was kept as-is, and when the .info directive expanded its
38d5e0a182SSimon J. Gerraty# argument, each '$$' got replaced with a single '$'.  The "long
39c1d01b5fSSimon J. Gerraty# expression" ${$} got replaced though, even though this would be a parse
40c1d01b5fSSimon J. Gerraty# error everywhere outside a .for loop.
4106b9b3e0SSimon J. Gerraty${:U\$}=	dollar		# see whether the "variable" '$' is local
4206b9b3e0SSimon J. Gerraty${:U\\}=	backslash	# see whether the "variable" '\' is local
43c1d01b5fSSimon J. Gerraty# expect+1: invalid character '$' in .for loop variable name
448d5c8e21SSimon J. Gerraty.for a b $ \ in 1 2 3 4
4506b9b3e0SSimon J. Gerraty.  info Dollar $$ ${$} $($) and backslash $\ ${\} $(\).
4606b9b3e0SSimon J. Gerraty.endfor
4706b9b3e0SSimon J. Gerraty
4806b9b3e0SSimon J. Gerraty# If there are no variables, there is no point in expanding the .for loop
49c1d01b5fSSimon J. Gerraty# since this would end up in an endless loop, consuming 0 of the 3 values in
50c1d01b5fSSimon J. Gerraty# each iteration.
51c1d01b5fSSimon J. Gerraty# expect+1: no iteration variables in for
5206b9b3e0SSimon J. Gerraty.for in 1 2 3
5306b9b3e0SSimon J. Gerraty# XXX: This should not be reached.  It should be skipped, as already done
5406b9b3e0SSimon J. Gerraty# when the number of values is not a multiple of the number of variables,
5506b9b3e0SSimon J. Gerraty# see below.
5606b9b3e0SSimon J. Gerraty.  warning Should not be reached.
5706b9b3e0SSimon J. Gerraty.endfor
5806b9b3e0SSimon J. Gerraty
59c1d01b5fSSimon J. Gerraty
6006b9b3e0SSimon J. Gerraty# There are 3 variables and 5 values.  These 5 values cannot be split evenly
6106b9b3e0SSimon J. Gerraty# among the variables, therefore the loop is not expanded at all, it is
62c1d01b5fSSimon J. Gerraty# skipped instead.
63c1d01b5fSSimon J. Gerraty# expect+1: Wrong number of words (5) in .for substitution list with 3 variables
6406b9b3e0SSimon J. Gerraty.for a b c in 1 2 3 4 5
6506b9b3e0SSimon J. Gerraty.  warning Should not be reached.
6606b9b3e0SSimon J. Gerraty.endfor
6706b9b3e0SSimon J. Gerraty
68c1d01b5fSSimon J. Gerraty
6906b9b3e0SSimon J. Gerraty# The list of values after the 'in' may be empty, no matter if this emptiness
70d5e0a182SSimon J. Gerraty# comes from an expanded expression or from a syntactically empty line.
7106b9b3e0SSimon J. Gerraty.for i in
7206b9b3e0SSimon J. Gerraty.  info Would be reached if there were items to loop over.
7306b9b3e0SSimon J. Gerraty.endfor
7406b9b3e0SSimon J. Gerraty
75c1d01b5fSSimon J. Gerraty
7606b9b3e0SSimon J. Gerraty# A missing 'in' should parse the .for loop but skip the body.
77c1d01b5fSSimon J. Gerraty# expect+1: missing `in' in for
78c1d01b5fSSimon J. Gerraty.for i over k
7906b9b3e0SSimon J. Gerraty# XXX: As of 2020-12-31, this line is reached once.
8006b9b3e0SSimon J. Gerraty.  warning Should not be reached.
8106b9b3e0SSimon J. Gerraty.endfor
8206b9b3e0SSimon J. Gerraty
83c1d01b5fSSimon J. Gerraty
8406b9b3e0SSimon J. Gerraty# A malformed modifier should be detected and skip the body of the loop.
8506b9b3e0SSimon J. Gerraty#
8606b9b3e0SSimon J. Gerraty# XXX: As of 2020-12-31, Var_Subst doesn't report any errors, therefore
8706b9b3e0SSimon J. Gerraty# the loop body is expanded as if no error had happened.
88*22619282SSimon J. Gerraty# expect+1: while evaluating "${:U3:Z} 4" with value "3": Unknown modifier "Z"
8906b9b3e0SSimon J. Gerraty.for i in 1 2 ${:U3:Z} 4
90*22619282SSimon J. Gerraty# expect+3: warning: Should not be reached.
91*22619282SSimon J. Gerraty# expect+2: warning: Should not be reached.
92*22619282SSimon J. Gerraty# expect+1: warning: Should not be reached.
9306b9b3e0SSimon J. Gerraty.  warning Should not be reached.
9406b9b3e0SSimon J. Gerraty.endfor
95