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