xref: /freebsd/contrib/bmake/unit-tests/varmod-ifelse.mk (revision fd0e6af5e49c3db034f1c45ff3f053f282e758c3)
1# $NetBSD: varmod-ifelse.mk,v 1.32 2024/07/05 20:01:52 rillig Exp $
2#
3# Tests for the ${cond:?then:else} variable modifier, which evaluates either
4# the then-expression or the else-expression, depending on the condition.
5#
6# The modifier was added on 1998-04-01.
7#
8# Until 2015-10-11, the modifier always evaluated both the "then" and the
9# "else" expressions.
10
11# TODO: Implementation
12
13# The variable name of the expression is expanded and then taken as the
14# condition.  In the below example it becomes:
15#
16#	bare words == "literal"
17#
18# This confuses the parser, which expects an operator instead of the bare
19# word "expression".  If the name were expanded lazily, everything would be
20# fine since the condition would be:
21#
22#	${:Ubare words} == "literal"
23#
24# Evaluating the variable name lazily would require additional code in
25# Var_Parse and ParseVarname, it would be more useful and predictable
26# though.
27# expect+2: while evaluating condition "bare words == "literal"": Bad condition
28# expect+1: Malformed conditional (${${:Ubare words} == "literal":?bad:bad})
29.if ${${:Ubare words} == "literal":?bad:bad}
30.  error
31.else
32.  error
33.endif
34
35# In a variable assignment, undefined variables are not an error.
36# Because of the early expansion, the whole condition evaluates to
37# ' == ""' though, which cannot be parsed because the left-hand side looks
38# empty.
39# expect+1: while evaluating condition " == """: Bad condition
40COND:=	${${UNDEF} == "":?bad-assign:bad-assign}
41
42# In a condition, undefined variables generate a "Malformed conditional"
43# error.  That error message is wrong though.  In lint mode, the correct
44# "Undefined variable" error message is generated.
45# The difference to the ':=' variable assignment is the additional
46# "Malformed conditional" error message.
47# expect+2: while evaluating condition " == """: Bad condition
48# expect+1: Malformed conditional (${${UNDEF} == "":?bad-cond:bad-cond})
49.if ${${UNDEF} == "":?bad-cond:bad-cond}
50.  error
51.else
52.  error
53.endif
54
55# When the :? is parsed, it is greedy.  The else branch spans all the
56# text, up until the closing character '}', even if the text looks like
57# another modifier.
58.if ${1:?then:else:Q} != "then"
59.  error
60.endif
61.if ${0:?then:else:Q} != "else:Q"
62.  error
63.endif
64
65# This line generates 2 error messages.  The first comes from evaluating the
66# malformed conditional "1 == == 2", which is reported as "Bad conditional
67# expression" by ApplyModifier_IfElse.  The expression containing that
68# conditional therefore returns a parse error from Var_Parse, and this parse
69# error propagates to CondEvalExpression, where the "Malformed conditional"
70# comes from.
71# expect+2: while evaluating condition "1 == == 2": Bad condition
72# expect+1: Malformed conditional (${1 == == 2:?yes:no} != "")
73.if ${1 == == 2:?yes:no} != ""
74.  error
75.else
76.  error
77.endif
78
79# If the "Bad conditional expression" appears in a quoted string literal, the
80# error message "Malformed conditional" is not printed, leaving only the "Bad
81# conditional expression".
82#
83# XXX: The left-hand side is enclosed in quotes.  This results in Var_Parse
84# being called without VARE_EVAL_DEFINED.  When ApplyModifier_IfElse
85# returns AMR_CLEANUP as result, Var_Parse returns varUndefined since the
86# value of the expression is still undefined.  CondParser_String is
87# then supposed to do proper error handling, but since varUndefined is local
88# to var.c, it cannot distinguish this return value from an ordinary empty
89# string.  The left-hand side of the comparison is therefore just an empty
90# string, which is obviously equal to the empty string on the right-hand side.
91#
92# XXX: The debug log for -dc shows a comparison between 1.0 and 0.0.  The
93# condition should be detected as being malformed before any comparison is
94# done since there is no well-formed comparison in the condition at all.
95.MAKEFLAGS: -dc
96# expect+1: while evaluating condition "1 == == 2": Bad condition
97.if "${1 == == 2:?yes:no}" != ""
98.  error
99.else
100# expect+1: warning: Oops, the parse error should have been propagated.
101.  warning Oops, the parse error should have been propagated.
102.endif
103.MAKEFLAGS: -d0
104
105# As of 2020-12-10, the variable "VAR" is first expanded, and the result of
106# this expansion is then taken as the condition.  To force the
107# expression in the condition to be evaluated at exactly the right point,
108# the '$' of the intended '${VAR}' escapes from the parser in form of the
109# expression ${:U\$}.  Because of this escaping, the variable "VAR" and thus
110# the condition ends up as "${VAR} == value", just as intended.
111#
112# This hack does not work for variables from .for loops since these are
113# expanded at parse time to their corresponding ${:Uvalue} expressions.
114# Making the '$' of the '${VAR}' expression indirect hides this expression
115# from the parser of the .for loop body.  See ForLoop_SubstVarLong.
116.MAKEFLAGS: -dc
117VAR=	value
118.if ${ ${:U\$}{VAR} == value:?ok:bad} != "ok"
119.  error
120.endif
121.MAKEFLAGS: -d0
122
123# On 2021-04-19, when building external/bsd/tmux with HAVE_LLVM=yes and
124# HAVE_GCC=no, the following conditional generated this error message:
125#
126#	make: Bad conditional expression 'string == "literal" && no >= 10'
127#	    in 'string == "literal" && no >= 10?yes:no'
128#
129# Despite the error message (which was not clearly marked with "error:"),
130# the build continued, for historical reasons, see main_Exit.
131#
132# The tricky detail here is that the condition that looks so obvious in the
133# form written in the makefile becomes tricky when it is actually evaluated.
134# This is because the condition is written in the place of the variable name
135# of the expression, and in an expression, the variable name is always
136# expanded first, before even looking at the modifiers.  This happens for the
137# modifier ':?' as well, so when CondEvalExpression gets to see the
138# expression, it already looks like this:
139#
140#	string == "literal" && no >= 10
141#
142# When parsing such an expression, the parser used to be strict.  It first
143# evaluated the left-hand side of the operator '&&' and then started parsing
144# the right-hand side 'no >= 10'.  The word 'no' is obviously a string
145# literal, not enclosed in quotes, which is OK, even on the left-hand side of
146# the comparison operator, but only because this is a condition in the
147# modifier ':?'.  In an ordinary directive '.if', this would be a parse error.
148# For strings, only the comparison operators '==' and '!=' are defined,
149# therefore parsing stopped at the '>', producing the 'Bad conditional
150# expression'.
151#
152# Ideally, the conditional expression would not be expanded before parsing
153# it.  This would allow to write the conditions exactly as seen below.  That
154# change has a high chance of breaking _some_ existing code and would need
155# to be thoroughly tested.
156#
157# Since cond.c 1.262 from 2021-04-20, make reports a more specific error
158# message in situations like these, pointing directly to the specific problem
159# instead of just saying that the whole condition is bad.
160STRING=		string
161NUMBER=		no		# not really a number
162# expect+1: no.
163.info ${${STRING} == "literal" && ${NUMBER} >= 10:?yes:no}.
164# expect+3: while evaluating condition "string == "literal" || no >= 10": Comparison with '>=' requires both operands 'no' and '10' to be numeric
165# expect+2: while evaluating condition "string == "literal" || no >= 10": Bad condition
166# expect+1: .
167.info ${${STRING} == "literal" || ${NUMBER} >= 10:?yes:no}.
168
169# The following situation occasionally occurs with MKINET6 or similar
170# variables.
171NUMBER=		# empty, not really a number either
172# expect+2: while evaluating condition "string == "literal" &&  >= 10": Bad condition
173# expect+1: .
174.info ${${STRING} == "literal" && ${NUMBER} >= 10:?yes:no}.
175# expect+2: while evaluating condition "string == "literal" ||  >= 10": Bad condition
176# expect+1: .
177.info ${${STRING} == "literal" || ${NUMBER} >= 10:?yes:no}.
178
179# CondParser_LeafToken handles [0-9-+] specially, treating them as a number.
180PLUS=		+
181ASTERISK=	*
182EMPTY=		# empty
183# "true" since "+" is not the empty string.
184# expect+1: <true>
185.info <${${PLUS}		:?true:false}>
186# "false" since the variable named "*" is not defined.
187# expect+1: <false>
188.info <${${ASTERISK}	:?true:false}>
189# syntax error since the condition is completely blank.
190# expect+2: while evaluating condition "	": Bad condition
191# expect+1: <>
192.info <${${EMPTY}	:?true:false}>
193
194
195# Since the condition of the '?:' modifier is expanded before being parsed and
196# evaluated, it is common practice to enclose expressions in quotes, to avoid
197# producing syntactically invalid conditions such as ' == value'.  This only
198# works if the expanded values neither contain quotes nor backslashes.  For
199# strings containing quotes or backslashes, the '?:' modifier should not be
200# used.
201PRIMES=	2 3 5 7 11
202.if ${1 2 3 4 5:L:@n@$n:${ ("${PRIMES:M$n}" != "") :?prime:not_prime}@} != \
203  "1:not_prime 2:prime 3:prime 4:not_prime 5:prime"
204.  error
205.endif
206
207# When parsing the modifier ':?', there are 3 possible cases:
208#
209#	1. The whole expression is only parsed.
210#	2. The expression is parsed and the 'then' branch is evaluated.
211#	3. The expression is parsed and the 'else' branch is evaluated.
212#
213# In all of these cases, the expression must be parsed in the same way,
214# especially when one of the branches contains unbalanced '{}' braces.
215#
216# At 2020-01-01, the expressions from the 'then' and 'else' branches were
217# parsed differently, depending on whether the branch was taken or not.  When
218# the branch was taken, the parser recognized that in the modifier ':S,}},,',
219# the '}}' were ordinary characters.  When the branch was not taken, the
220# parser only counted balanced '{' and '}', ignoring any escaping or other
221# changes in the interpretation.
222#
223# In var.c 1.285 from 2020-07-20, the parsing of the expressions changed so
224# that in both cases the expression is parsed in the same way, taking the
225# unbalanced braces in the ':S' modifiers into account.  This change was not
226# on purpose, the commit message mentioned 'has the same effect', which was a
227# wrong assumption.
228#
229# In var.c 1.323 from 2020-07-26, the unintended fix from var.c 1.285 was
230# reverted, still not knowing about the difference between regular parsing and
231# balanced-mode parsing.
232#
233# In var.c 1.1028 from 2022-08-08, there was another attempt at fixing this
234# inconsistency in parsing, but since that broke parsing of the modifier ':@',
235# it was reverted in var.c 1.1029 from 2022-08-23.
236#
237# In var.c 1.1047 from 2023-02-18, the inconsistency in parsing was finally
238# fixed.  The modifier ':@' now parses the body in balanced mode, while
239# everywhere else the modifier parts have their subexpressions parsed in the
240# same way, no matter whether they are evaluated or not.
241#
242# The modifiers ':@' and ':?' are similar in that they conceptually contain
243# text to be evaluated later or conditionally, still they parse that text
244# differently.  The crucial difference is that the body of the modifier ':@'
245# is always parsed using balanced mode.  The modifier ':?', on the other hand,
246# must parse both of its branches in the same way, no matter whether they are
247# evaluated or not.  Since balanced mode and standard mode are incompatible,
248# it's impossible to use balanced mode in the modifier ':?'.
249.MAKEFLAGS: -dc
250.if 0 && ${1:?${:Uthen0:S,}},,}:${:Uelse0:S,}},,}} != "not evaluated"
251# At 2020-01-07, the expression evaluated to 'then0,,}}', even though it was
252# irrelevant as the '0' had already been evaluated to 'false'.
253.  error
254.endif
255.if 1 && ${0:?${:Uthen1:S,}},,}:${:Uelse1:S,}},,}} != "else1"
256.  error
257.endif
258.if 2 && ${1:?${:Uthen2:S,}},,}:${:Uelse2:S,}},,}} != "then2"
259# At 2020-01-07, the whole expression evaluated to 'then2,,}}' instead of the
260# expected 'then2'.  The 'then' branch of the ':?' modifier was parsed
261# normally, parsing and evaluating the ':S' modifier, thereby treating the
262# '}}' as ordinary characters and resulting in 'then2'.  The 'else' branch was
263# parsed in balanced mode, ignoring that the inner '}}' were ordinary
264# characters.  The '}}' were thus interpreted as the end of the 'else' branch
265# and the whole expression.  This left the trailing ',,}}', which together
266# with the 'then2' formed the result 'then2,,}}'.
267.  error
268.endif
269
270
271# Since the condition is taken from the variable name of the expression, not
272# from its value, it is evaluated early.  It is possible though to construct
273# conditions that are evaluated lazily, at exactly the right point.  There is
274# no way to escape a '$' directly in the variable name, but there are
275# alternative ways to bring a '$' into the condition.
276#
277#	In an indirect condition using the ':U' modifier, each '$', ':' and
278#	'}' must be escaped as '\$', '\:' and '\}', respectively, but '{' must
279#	not be escaped.
280#
281#	In an indirect condition using a separate variable, each '$' must be
282#	escaped as '$$'.
283#
284# These two forms allow the variables to contain arbitrary characters, as the
285# condition parser does not see them.
286DELAYED=	two
287# expect+1: no
288.info ${ ${:U \${DELAYED\} == "one"}:?yes:no}
289# expect+1: yes
290.info ${ ${:U \${DELAYED\} == "two"}:?yes:no}
291INDIRECT_COND1=	$${DELAYED} == "one"
292# expect+1: no
293.info ${ ${INDIRECT_COND1}:?yes:no}
294INDIRECT_COND2=	$${DELAYED} == "two"
295# expect+1: yes
296.info ${ ${INDIRECT_COND2}:?yes:no}
297
298
299.MAKEFLAGS: -d0
300
301
302# In the modifier parts for the 'then' and 'else' branches, subexpressions are
303# parsed by inspecting the actual modifiers.  In 2008, 2015, 2020, 2022 and
304# 2023, the exact parsing algorithm switched a few times, counting balanced
305# braces instead of proper subexpressions, which meant that unbalanced braces
306# were parsed differently, depending on whether the branch was active or not.
307BRACES=	}}}
308NO=	${0:?${BRACES:S,}}},yes,}:${BRACES:S,}}},no,}}
309YES=	${1:?${BRACES:S,}}},yes,}:${BRACES:S,}}},no,}}
310BOTH=	<${YES}> <${NO}>
311.if ${BOTH} != "<yes> <no>"
312.  error
313.endif
314
315
316# expect+2: while evaluating then-branch of condition "1": while evaluating "${:X-then}:${:X-else}}" with value "": Unknown modifier "X-then"
317# expect+1: while evaluating else-branch of condition "1": while parsing "${:X-else}}": Unknown modifier "X-else"
318.if ${1:?${:X-then}:${:X-else}}
319.endif
320