cond-short.mk revision 1.16 1 1.16 rillig # $NetBSD: cond-short.mk,v 1.16 2021/03/14 11:49:37 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Demonstrates that in conditions, the right-hand side of an && or ||
4 1.2 rillig # is only evaluated if it can actually influence the result.
5 1.12 rillig # This is called 'short-circuit evaluation' and is the usual evaluation
6 1.12 rillig # mode in most programming languages. A notable exception is Ada, which
7 1.12 rillig # distinguishes between the operators 'And', 'And Then', 'Or', 'Or Else'.
8 1.1 rillig #
9 1.13 rillig # Before 2020-06-28, the right-hand side of an && or || operator was always
10 1.13 rillig # evaluated, which was wrong. In cond.c 1.69 and var.c 1.197 on 2015-10-11,
11 1.13 rillig # Var_Parse got a new parameter named 'wantit'. Since then it would have been
12 1.13 rillig # possible to skip evaluation of irrelevant variable expressions and only
13 1.13 rillig # parse them. They were still evaluated though, the only difference to
14 1.13 rillig # relevant variable expressions was that in the irrelevant variable
15 1.13 rillig # expressions, undefined variables were allowed.
16 1.16 rillig #
17 1.16 rillig # See also:
18 1.16 rillig # var-eval-short.mk, for short-circuited variable modifiers
19 1.2 rillig
20 1.16 rillig # The && operator:
21 1.1 rillig
22 1.1 rillig .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
23 1.1 rillig .endif
24 1.1 rillig
25 1.1 rillig .if 1 && ${echo "expected and" 1>&2 :L:sh}
26 1.1 rillig .endif
27 1.1 rillig
28 1.2 rillig .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
29 1.2 rillig .endif
30 1.2 rillig
31 1.2 rillig .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
32 1.2 rillig .endif
33 1.2 rillig
34 1.2 rillig .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
35 1.2 rillig .endif
36 1.2 rillig
37 1.2 rillig .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
38 1.2 rillig .endif
39 1.2 rillig
40 1.4 rillig # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
41 1.4 rillig # The whole !empty condition is only parsed and then discarded.
42 1.3 rillig VAR= ${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
43 1.3 rillig VAR13= ${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
44 1.3 rillig .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
45 1.3 rillig .endif
46 1.3 rillig
47 1.3 rillig VAR= ${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
48 1.3 rillig VAR23= ${VAR${:U22${echo "expected VAR23" 1>&2 :L:sh}}}
49 1.3 rillig .if 1 && !empty(VAR${:U23${echo "expected U23 condition" 1>&2 :L:sh}})
50 1.3 rillig .endif
51 1.5 rillig VAR= # empty again, for the following tests
52 1.3 rillig
53 1.5 rillig # The :M modifier is only parsed, not evaluated.
54 1.5 rillig # Before 2020-07-02, it was wrongly evaluated.
55 1.4 rillig .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
56 1.4 rillig .endif
57 1.4 rillig
58 1.5 rillig .if 1 && !empty(VAR:M${:U${echo "expected M pattern" 1>&2 :L:sh}})
59 1.5 rillig .endif
60 1.5 rillig
61 1.6 rillig .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
62 1.6 rillig .endif
63 1.6 rillig
64 1.6 rillig .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
65 1.6 rillig .endif
66 1.6 rillig
67 1.6 rillig .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
68 1.6 rillig .endif
69 1.6 rillig
70 1.6 rillig .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
71 1.6 rillig .endif
72 1.6 rillig
73 1.6 rillig .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
74 1.6 rillig .endif
75 1.6 rillig
76 1.6 rillig .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
77 1.6 rillig .endif
78 1.6 rillig
79 1.8 rillig # Irrelevant assignment modifiers are skipped as well.
80 1.8 rillig .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
81 1.8 rillig .endif
82 1.8 rillig .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
83 1.8 rillig .endif
84 1.8 rillig .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
85 1.8 rillig .endif
86 1.8 rillig .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
87 1.8 rillig .endif
88 1.8 rillig .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
89 1.10 rillig . warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
90 1.8 rillig .endif
91 1.8 rillig
92 1.16 rillig # The || operator:
93 1.2 rillig
94 1.1 rillig .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
95 1.1 rillig .endif
96 1.1 rillig
97 1.1 rillig .if 0 || ${echo "expected or" 1>&2 :L:sh}
98 1.1 rillig .endif
99 1.1 rillig
100 1.2 rillig .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
101 1.2 rillig .endif
102 1.2 rillig
103 1.2 rillig .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
104 1.2 rillig .endif
105 1.2 rillig
106 1.2 rillig .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
107 1.2 rillig .endif
108 1.2 rillig
109 1.2 rillig .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
110 1.2 rillig .endif
111 1.2 rillig
112 1.2 rillig # Unreachable nested conditions are skipped completely as well.
113 1.1 rillig
114 1.1 rillig .if 0
115 1.1 rillig . if ${echo "unexpected nested and" 1>&2 :L:sh}
116 1.1 rillig . endif
117 1.1 rillig .endif
118 1.1 rillig
119 1.1 rillig .if 1
120 1.1 rillig .elif ${echo "unexpected nested or" 1>&2 :L:sh}
121 1.1 rillig .endif
122 1.1 rillig
123 1.7 sjg # make sure these do not cause complaint
124 1.7 sjg #.MAKEFLAGS: -dc
125 1.7 sjg
126 1.12 rillig # TODO: Rewrite this whole section and check all the conditions and variables.
127 1.12 rillig # Several of the assumptions are probably wrong here.
128 1.12 rillig # TODO: replace 'x=' with '.info' or '.error'.
129 1.11 rillig V42= 42
130 1.11 rillig iV1= ${V42}
131 1.11 rillig iV2= ${V66}
132 1.7 sjg
133 1.7 sjg .if defined(V42) && ${V42} > 0
134 1.11 rillig x= Ok
135 1.7 sjg .else
136 1.11 rillig x= Fail
137 1.7 sjg .endif
138 1.14 rillig x!= echo 'defined(V42) && $${V42} > 0: $x' >&2; echo
139 1.9 rillig
140 1.13 rillig # With cond.c 1.76 from 2020-07-03, the following condition triggered a
141 1.13 rillig # warning: "String comparison operator should be either == or !=".
142 1.13 rillig # This was because the variable expression ${iV2} was defined, but the
143 1.13 rillig # contained variable V66 was undefined. The left-hand side of the comparison
144 1.13 rillig # therefore evaluated to the string "${V66}", which is obviously not a number.
145 1.13 rillig #
146 1.13 rillig # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
147 1.13 rillig # comparisons. Instead, they are only parsed and then discarded.
148 1.13 rillig #
149 1.13 rillig # At that time, there was not enough debug logging to see the details in the
150 1.13 rillig # -dA log. To actually see it, add debug logging at the beginning and end of
151 1.13 rillig # Var_Parse.
152 1.7 sjg .if defined(V66) && ( ${iV2} < ${V42} )
153 1.11 rillig x= Fail
154 1.7 sjg .else
155 1.11 rillig x= Ok
156 1.7 sjg .endif
157 1.13 rillig # XXX: This condition doesn't match the one above. The quotes are missing
158 1.13 rillig # above. This is a crucial detail since without quotes, the variable
159 1.13 rillig # expression ${iV2} evaluates to "${V66}", and with quotes, it evaluates to ""
160 1.13 rillig # since undefined variables are allowed and expand to an empty string.
161 1.14 rillig x!= echo 'defined(V66) && ( "$${iV2}" < $${V42} ): $x' >&2; echo
162 1.9 rillig
163 1.7 sjg .if 1 || ${iV1} < ${V42}
164 1.11 rillig x= Ok
165 1.7 sjg .else
166 1.11 rillig x= Fail
167 1.7 sjg .endif
168 1.14 rillig x!= echo '1 || $${iV1} < $${V42}: $x' >&2; echo
169 1.9 rillig
170 1.13 rillig # With cond.c 1.76 from 2020-07-03, the following condition triggered a
171 1.13 rillig # warning: "String comparison operator should be either == or !=".
172 1.13 rillig # This was because the variable expression ${iV2} was defined, but the
173 1.13 rillig # contained variable V66 was undefined. The left-hand side of the comparison
174 1.13 rillig # therefore evaluated to the string "${V66}", which is obviously not a number.
175 1.13 rillig #
176 1.13 rillig # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
177 1.13 rillig # comparisons. Instead, they are only parsed and then discarded.
178 1.13 rillig #
179 1.13 rillig # At that time, there was not enough debug logging to see the details in the
180 1.13 rillig # -dA log. To actually see it, add debug logging at the beginning and end of
181 1.13 rillig # Var_Parse.
182 1.7 sjg .if 1 || ${iV2:U2} < ${V42}
183 1.11 rillig x= Ok
184 1.7 sjg .else
185 1.11 rillig x= Fail
186 1.7 sjg .endif
187 1.14 rillig x!= echo '1 || $${iV2:U2} < $${V42}: $x' >&2; echo
188 1.9 rillig
189 1.7 sjg # the same expressions are fine when the lhs is expanded
190 1.7 sjg # ${iV1} expands to 42
191 1.7 sjg .if 0 || ${iV1} <= ${V42}
192 1.11 rillig x= Ok
193 1.7 sjg .else
194 1.11 rillig x= Fail
195 1.7 sjg .endif
196 1.14 rillig x!= echo '0 || $${iV1} <= $${V42}: $x' >&2; echo
197 1.9 rillig
198 1.7 sjg # ${iV2:U2} expands to 2
199 1.7 sjg .if 0 || ${iV2:U2} < ${V42}
200 1.11 rillig x= Ok
201 1.7 sjg .else
202 1.11 rillig x= Fail
203 1.7 sjg .endif
204 1.14 rillig x!= echo '0 || $${iV2:U2} < $${V42}: $x' >&2; echo
205 1.7 sjg
206 1.15 rillig # The right-hand side of the '&&' is irrelevant since the left-hand side
207 1.15 rillig # already evaluates to false. Before cond.c 1.79 from 2020-07-09, it was
208 1.15 rillig # expanded nevertheless, although with a small modification: undefined
209 1.15 rillig # variables may be used in these expressions without generating an error.
210 1.12 rillig .if defined(UNDEF) && ${UNDEF} != "undefined"
211 1.12 rillig . error
212 1.12 rillig .endif
213 1.12 rillig
214 1.1 rillig all:
215