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