1 # $NetBSD: cond-op.mk,v 1.15 2021/12/10 23:12:44 rillig Exp $
3 # Tests for operators like &&, ||, ! in .if conditions.
9 # cond-op-parentheses.mk
11 # In make, && binds more tightly than ||, like in C.
12 # If make had the same precedence for both && and ||, like in the shell,
13 # the result would be different.
14 # If || were to bind more tightly than &&, the result would be different
20 # If make were to interpret the && and || operators like the shell, the
21 # previous condition would be interpreted as:
26 # The precedence of the ! operator is different from C though. It has a
27 # lower precedence than the comparison operators. Negating a condition
28 # does not need parentheses.
30 # This kind of condition looks so unfamiliar that it doesn't occur in
36 # This is how the above condition is actually interpreted.
37 .if !("word" == "word")
41 # TODO: Demonstrate that the precedence of the ! and == operators actually
42 # makes a difference. There is a simple example for sure, I just cannot
43 # wrap my head around it right now. See the truth table generator below
44 # for an example that doesn't require much thought.
46 # This condition is malformed because the '!' on the right-hand side must not
47 # appear unquoted. If any, it must be enclosed in quotes.
48 # In any case, it is not interpreted as a negation of an unquoted string.
49 # See CondParser_String.
54 # Surprisingly, the ampersand and pipe are allowed in bare strings.
55 # That's another opportunity for writing confusing code.
56 # See CondParser_String, which only has '!' in the list of stop characters.
57 .if "a&&b||c" != a&&b||c
61 # In the following malformed conditions, as soon as the parser sees the '$'
62 # after the '0' or the '1', it knows that the condition will be malformed.
63 # Therefore there is no point in evaluating the misplaced expression.
65 # Before cond.c 1.286 from 2021-12-10, the extra expression was evaluated
66 # nevertheless, since CondParser_Or and CondParser_And asked for the expanded
67 # next token, even though in this position of the condition, only comparison
68 # operators, TOK_AND, TOK_OR or TOK_RPAREN are allowed.
76 .if 0 ${ERR::=evaluated}
79 .if ${ERR:Uundefined} == undefined
80 . info A misplaced expression after 0 is not evaluated.
84 .if 1 ${ERR::=evaluated}
87 .if ${ERR:Uundefined} == undefined
88 . info A misplaced expression after 1 is not evaluated.
91 # Just in case that parsing should ever stop on the first error.
92 .info Parsing continues until here.
94 # Demonstration that '&&' has higher precedence than '||'.
95 .info A B C => (A || B) && C A || B && C A || (B && C)
99 . for r1 in ${ ($a || $b) && $c :?1:0}
100 . for r2 in ${ $a || $b && $c :?1:0}
101 . for r3 in ${ $a || ($b && $c) :?1:0}
102 . info $a $b $c => ${r1} ${r2} ${r3}
110 # This condition is obviously malformed. It is properly detected and also
111 # was properly detected before 2021-01-19, but only because the left hand
112 # side of the '&&' evaluated to true.
119 # This obviously malformed condition was not detected as such before cond.c
120 # 1.238 from 2021-01-19.
127 # This obviously malformed condition was not detected as such before cond.c
128 # 1.238 from 2021-01-19.
135 # This condition is obviously malformed. It is properly detected and also
136 # was properly detected before 2021-01-19, but only because the left hand
137 # side of the '||' evaluated to false.