cond-short.mk revision 1.8 1 # $NetBSD: cond-short.mk,v 1.8 2020/07/19 21:03:55 rillig Exp $
2 #
3 # Demonstrates that in conditions, the right-hand side of an && or ||
4 # is only evaluated if it can actually influence the result.
5 #
6 # Between 2015-10-11 and 2020-06-28, the right-hand side of an && or ||
7 # operator was always evaluated, which was wrong.
8 #
9
10 # The && operator.
11
12 .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
13 .endif
14
15 .if 1 && ${echo "expected and" 1>&2 :L:sh}
16 .endif
17
18 .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
19 .endif
20
21 .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
22 .endif
23
24 .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
25 .endif
26
27 .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
28 .endif
29
30 # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
31 # The whole !empty condition is only parsed and then discarded.
32 VAR= ${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
33 VAR13= ${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
34 .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
35 .endif
36
37 VAR= ${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
38 VAR23= ${VAR${:U22${echo "expected VAR23" 1>&2 :L:sh}}}
39 .if 1 && !empty(VAR${:U23${echo "expected U23 condition" 1>&2 :L:sh}})
40 .endif
41 VAR= # empty again, for the following tests
42
43 # The :M modifier is only parsed, not evaluated.
44 # Before 2020-07-02, it was wrongly evaluated.
45 .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
46 .endif
47
48 .if 1 && !empty(VAR:M${:U${echo "expected M pattern" 1>&2 :L:sh}})
49 .endif
50
51 .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
52 .endif
53
54 .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
55 .endif
56
57 .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
58 .endif
59
60 .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
61 .endif
62
63 .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
64 .endif
65
66 .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
67 .endif
68
69 # Irrelevant assignment modifiers are skipped as well.
70 .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
71 .endif
72 .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
73 .endif
74 .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
75 .endif
76 .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
77 .endif
78 .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
79 .warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
80 .endif
81
82 # The || operator.
83
84 .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
85 .endif
86
87 .if 0 || ${echo "expected or" 1>&2 :L:sh}
88 .endif
89
90 .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
91 .endif
92
93 .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
94 .endif
95
96 .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
97 .endif
98
99 .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
100 .endif
101
102 # Unreachable nested conditions are skipped completely as well.
103
104 .if 0
105 . if ${echo "unexpected nested and" 1>&2 :L:sh}
106 . endif
107 .endif
108
109 .if 1
110 .elif ${echo "unexpected nested or" 1>&2 :L:sh}
111 .endif
112
113 # make sure these do not cause complaint
114 #.MAKEFLAGS: -dc
115
116 V42 = 42
117 iV1 = ${V42}
118 iV2 = ${V66}
119
120 .if defined(V42) && ${V42} > 0
121 x=Ok
122 .else
123 x=Fail
124 .endif
125 x!= echo 'defined(V42) && ${V42} > 0: $x' >&2; echo
126 # this one throws both String comparison operator and
127 # Malformed conditional with cond.c 1.78
128 # indirect iV2 would expand to "" and treated as 0
129 .if defined(V66) && ( ${iV2} < ${V42} )
130 x=Fail
131 .else
132 x=Ok
133 .endif
134 x!= echo 'defined(V66) && ( "${iV2}" < ${V42} ): $x' >&2; echo
135 # next two thow String comparison operator with cond.c 1.78
136 # indirect iV1 would expand to 42
137 .if 1 || ${iV1} < ${V42}
138 x=Ok
139 .else
140 x=Fail
141 .endif
142 x!= echo '1 || ${iV1} < ${V42}: $x' >&2; echo
143 .if 1 || ${iV2:U2} < ${V42}
144 x=Ok
145 .else
146 x=Fail
147 .endif
148 x!= echo '1 || ${iV2:U2} < ${V42}: $x' >&2; echo
149 # the same expressions are fine when the lhs is expanded
150 # ${iV1} expands to 42
151 .if 0 || ${iV1} <= ${V42}
152 x=Ok
153 .else
154 x=Fail
155 .endif
156 x!= echo '0 || ${iV1} <= ${V42}: $x' >&2; echo
157 # ${iV2:U2} expands to 2
158 .if 0 || ${iV2:U2} < ${V42}
159 x=Ok
160 .else
161 x=Fail
162 .endif
163 x!= echo '0 || ${iV2:U2} < ${V42}: $x' >&2; echo
164
165 all:
166 @:;:
167