cond-token-var.mk revision 1.10 1 # $NetBSD: cond-token-var.mk,v 1.10 2025/01/10 23:00:38 rillig Exp $
2 #
3 # Tests for expressions in .if conditions.
4 #
5 # Note the fine distinction between a variable and an expression.
6 # A variable has a name and a value. To access the value, one writes an
7 # expression of the form ${VAR}. This is a simple
8 # expression. Expressions can get more complicated by adding
9 # variable modifiers such as in ${VAR:Mpattern}.
10 #
11 # XXX: Strictly speaking, variable modifiers should be called expression
12 # modifiers instead since they only modify the expression, not the variable.
13 # Well, except for the assignment modifiers, these do indeed change the value
14 # of the variable.
15
16 D= defined
17 DEF= defined
18
19
20 # A defined variable may appear on either side of the comparison.
21 .if ${DEF} == ${DEF}
22 # expect+1: ok
23 . info ok
24 .else
25 . error
26 .endif
27
28 # A variable that appears on the left-hand side must be defined.
29 # expect+1: Malformed conditional '${UNDEF} == ${DEF}'
30 .if ${UNDEF} == ${DEF}
31 . error
32 .endif
33
34 # A variable that appears on the right-hand side must be defined.
35 # expect+1: Malformed conditional '${DEF} == ${UNDEF}'
36 .if ${DEF} == ${UNDEF}
37 . error
38 .endif
39
40 # A defined variable may appear as an expression of its own.
41 .if ${DEF}
42 .endif
43
44 # An undefined variable on its own generates a parse error.
45 # expect+1: Malformed conditional '${UNDEF}'
46 .if ${UNDEF}
47 .endif
48
49 # The :U modifier turns an undefined expression into a defined expression.
50 # Since the expression is defined now, it doesn't generate any parse error.
51 .if ${UNDEF:U}
52 .endif
53
54
55 # The same as above, for single-letter variables without braces or
56 # parentheses.
57
58 # A defined variable may appear on either side of the comparison.
59 .if $D == $D
60 .endif
61
62 # A variable on the left-hand side must be defined.
63 # FIXME: Replace "Malformed" with "Undefined variable".
64 # expect+1: Malformed conditional '$U == $D'
65 .if $U == $D
66 .endif
67
68 # A variable on the right-hand side must be defined.
69 # FIXME: Replace "Malformed" with "Undefined variable".
70 # expect+1: Malformed conditional '$D == $U'
71 .if $D == $U
72 .endif
73
74 # A defined variable may appear as an expression of its own.
75 .if $D
76 .endif
77
78 # An undefined variable without a comparison operator generates a parse error.
79 # FIXME: Replace "Malformed" with "Undefined variable".
80 # expect+1: Malformed conditional '$U'
81 .if $U
82 .endif
83
84
85 # If the value of the expression is a number, it is compared against
86 # zero.
87 .if ${:U0}
88 . error
89 .endif
90 .if !${:U1}
91 . error
92 .endif
93
94 # If the value of the expression is not a number, any non-empty
95 # value evaluates to true, even if there is only whitespace.
96 .if ${:U}
97 . error
98 .endif
99 .if !${:U }
100 . error
101 .endif
102 .if !${:Uanything}
103 . error
104 .endif
105
106 .MAKEFLAGS: -dv
107 # FIXME: Replace "Malformed" with "Undefined variable".
108 # expect+1: Malformed conditional 'x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3}'
109 .if x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3}
110 .endif
111
112 # FIXME: Replace "Malformed" with "Undefined variable".
113 # expect+1: Malformed conditional 'x${DEF}y == "${UNDEF2}" || 0x${UNDEF3}'
114 .if x${DEF}y == "${UNDEF2}" || 0x${UNDEF3}
115 .endif
116
117 # FIXME: Replace "Malformed" with "Undefined variable".
118 # expect+1: Malformed conditional 'x${DEF}y == "${DEF}" || 0x${UNDEF3}'
119 .if x${DEF}y == "${DEF}" || 0x${UNDEF3}
120 .endif
121 .MAKEFLAGS: -d0
122