1# $NetBSD: cond-func.mk,v 1.14 2023/11/19 21:47:52 rillig Exp $
2#
3# Tests for those parts of the functions in .if conditions that are common
4# among several functions.
5#
6# The below test uses the 'defined' function since it has no side-effects.
7# The other functions would work equally well, except for 'empty', which
8# parses its argument differently from the other functions.
9#
10
11DEF=			defined
12${:UA B}=		variable name with spaces
13${:UVAR(value)}=	variable name with parentheses
14${:UVAR{value}}=	variable name with balanced braces
15
16# Really strange variable names must be given indirectly via another variable,
17# so that no unbalanced braces appear in the top-level expression.
18VARNAME_UNBALANCED_BRACES=	VAR{{{value
19${VARNAME_UNBALANCED_BRACES}=	variable name with unbalanced braces
20
21.if !defined(DEF)
22.  error
23.endif
24
25# Horizontal whitespace (space tab) after the opening parenthesis is ignored.
26.if !defined( 	DEF)
27.  error
28.endif
29
30# Horizontal whitespace (space tab) before the closing parenthesis is ignored.
31.if !defined(DEF 	)
32.  error
33.endif
34
35# The argument of a function must not directly contain whitespace.
36# expect+1: Missing closing parenthesis for defined()
37.if !defined(A B)
38.  error
39.endif
40
41# If necessary, the whitespace can be generated by an expression.
42.if !defined(${:UA B})
43.  error
44.endif
45
46# Characters that could be mistaken for operators must not appear directly
47# in a function argument.  As with whitespace, these can be generated
48# indirectly.
49#
50# It's not entirely clear why these characters are forbidden.
51# The most plausible reason seems to be typo detection.
52# expect+1: Missing closing parenthesis for defined()
53.if !defined(A&B)
54.  error
55.endif
56# expect+1: Missing closing parenthesis for defined()
57.if !defined(A|B)
58.  error
59.endif
60
61# Even parentheses may appear in variable names.
62# They must be balanced though.
63.if !defined(VAR(value))
64.  error
65.endif
66
67# Braces do not have any special meaning when parsing arguments.
68.if !defined(VAR{value})
69.  error
70.endif
71
72# Braces do not have any special meaning when parsing arguments.
73# They don't need to be balanced.
74.if !defined(VAR{{{value)
75.  error
76.endif
77
78# There may be spaces around the operators and parentheses, and even
79# inside the parentheses.  The spaces inside the parentheses are not
80# allowed for the 'empty' function (see cond-func-empty.mk), therefore
81# they are typically omitted for the other functions as well.
82.if ! defined ( DEF )
83.  error
84.endif
85
86# The following condition is interpreted as defined(A) && defined(B).
87# In lack of a function call expression, each kind of .if directive has a
88# default function that is called when a bare word is parsed.  For the plain
89# .if directive, this function is defined(); see "struct If ifs" in cond.c.
90.if A&B
91.  error
92.endif
93
94.if defined()
95.  error
96.else
97# expect+1: The empty variable is never defined.
98.  info The empty variable is never defined.
99.endif
100
101# The plain word 'defined' is interpreted as 'defined(defined)', see
102# CondParser_ComparisonOrLeaf.
103# That variable is not defined (yet).
104.if defined
105.  error
106.else
107# expect+1: A plain function name is parsed as defined(...).
108.  info A plain function name is parsed as defined(...).
109.endif
110
111# If a variable named 'defined' is actually defined, the bare word 'defined'
112# is interpreted as 'defined(defined)', and the condition evaluates to true.
113defined=	# defined but empty
114.if defined
115# expect+1: A plain function name is parsed as defined(...).
116.  info A plain function name is parsed as defined(...).
117.else
118.  error
119.endif
120
121# A plain symbol name may start with one of the function names, in this case
122# 'defined'.
123.if defined-var
124.  error
125.else
126# expect+1: Symbols may start with a function name.
127.  info Symbols may start with a function name.
128.endif
129
130defined-var=	# defined but empty
131.if defined-var
132# expect+1: Symbols may start with a function name.
133.  info Symbols may start with a function name.
134.else
135.  error
136.endif
137
138# expect+1: Missing closing parenthesis for defined()
139.if defined(
140.  error
141.else
142.  error
143.endif
144