xref: /freebsd/contrib/bmake/unit-tests/cond-op-parentheses.mk (revision 9f45a3c8c82ffead7044ae836d9257113c630d3b)
1*9f45a3c8SSimon J. Gerraty# $NetBSD: cond-op-parentheses.mk,v 1.5 2022/01/22 21:50:41 rillig Exp $
22c3632d1SSimon J. Gerraty#
3*9f45a3c8SSimon J. Gerraty# Tests for parentheses in .if conditions, which group expressions to override
4*9f45a3c8SSimon J. Gerraty# the precedence of the operators '!', '&&' and '||'.  Parentheses cannot be
5*9f45a3c8SSimon J. Gerraty# used to form arithmetic expressions such as '(3+4)' though.
62c3632d1SSimon J. Gerraty
7*9f45a3c8SSimon J. Gerraty# Contrary to the C family of programming languages, the outermost condition
8*9f45a3c8SSimon J. Gerraty# does not have to be enclosed in parentheses.
9*9f45a3c8SSimon J. Gerraty.if defined(VAR)
10*9f45a3c8SSimon J. Gerraty.  error
11*9f45a3c8SSimon J. Gerraty.elif !1
12*9f45a3c8SSimon J. Gerraty.  error
13*9f45a3c8SSimon J. Gerraty.endif
14*9f45a3c8SSimon J. Gerraty
15*9f45a3c8SSimon J. Gerraty# Parentheses cannot enclose numbers as there is no need for it.  Make does
16*9f45a3c8SSimon J. Gerraty# not implement any arithmetic functions in its condition parser.  If
17*9f45a3c8SSimon J. Gerraty# absolutely necessary, use expr(1).
18*9f45a3c8SSimon J. Gerraty# expect+1: String comparison operator must be either == or !=
19*9f45a3c8SSimon J. Gerraty.if 3 > (2)
20*9f45a3c8SSimon J. Gerraty.endif
21*9f45a3c8SSimon J. Gerraty# expect+1: Malformed conditional ((3) > 2)
22*9f45a3c8SSimon J. Gerraty.if (3) > 2
23*9f45a3c8SSimon J. Gerraty.endif
242c3632d1SSimon J. Gerraty
25e2eeea75SSimon J. Gerraty# Test for deeply nested conditions.
26e2eeea75SSimon J. Gerraty.if	((((((((((((((((((((((((((((((((((((((((((((((((((((((((	\
27e2eeea75SSimon J. Gerraty	((((((((((((((((((((((((((((((((((((((((((((((((((((((((	\
28e2eeea75SSimon J. Gerraty	1								\
29e2eeea75SSimon J. Gerraty	))))))))))))))))))))))))))))))))))))))))))))))))))))))))	\
30e2eeea75SSimon J. Gerraty	))))))))))))))))))))))))))))))))))))))))))))))))))))))))
31*9f45a3c8SSimon J. Gerraty# Parentheses can be nested at least to depth 112.  There is nothing special
32*9f45a3c8SSimon J. Gerraty# about this number though, much higher numbers work as well, at least on
33*9f45a3c8SSimon J. Gerraty# NetBSD.  The actual limit depends on the allowed call stack depth for C code
34*9f45a3c8SSimon J. Gerraty# of the platform.  Anyway, 112 should be enough for all practical purposes.
35e2eeea75SSimon J. Gerraty.else
36e2eeea75SSimon J. Gerraty.  error
37e2eeea75SSimon J. Gerraty.endif
38e2eeea75SSimon J. Gerraty
39dba7b0efSSimon J. Gerraty# An unbalanced opening parenthesis is a parse error.
40dba7b0efSSimon J. Gerraty.if (
41dba7b0efSSimon J. Gerraty.  error
42dba7b0efSSimon J. Gerraty.else
43dba7b0efSSimon J. Gerraty.  error
44dba7b0efSSimon J. Gerraty.endif
45dba7b0efSSimon J. Gerraty
46dba7b0efSSimon J. Gerraty# An unbalanced closing parenthesis is a parse error.
47dba7b0efSSimon J. Gerraty#
48*9f45a3c8SSimon J. Gerraty# Before cond.c 1.237 from 2021-01-19, CondParser_Term returned TOK_RPAREN
49*9f45a3c8SSimon J. Gerraty# even though the documentation of that function promised to only ever return
50*9f45a3c8SSimon J. Gerraty# TOK_TRUE, TOK_FALSE or TOK_ERROR.  In cond.c 1.241, the return type of that
51*9f45a3c8SSimon J. Gerraty# function was changed to a properly restricted enum type, to prevent this bug
52*9f45a3c8SSimon J. Gerraty# from occurring again.
53dba7b0efSSimon J. Gerraty.if )
54dba7b0efSSimon J. Gerraty.  error
55dba7b0efSSimon J. Gerraty.else
56dba7b0efSSimon J. Gerraty.  error
57dba7b0efSSimon J. Gerraty.endif
58dba7b0efSSimon J. Gerraty
592c3632d1SSimon J. Gerratyall:
602c3632d1SSimon J. Gerraty	@:;
61