1# $NetBSD: cond-undef-lint.mk,v 1.3 2020/11/15 14:58:14 rillig Exp $
2#
3# Tests for defined and undefined variables in .if conditions, in lint mode.
4#
5# As of 2020-09-14, lint mode contains experimental code for printing
6# accurate error messages in case of undefined variables, instead of the
7# wrong "Malformed condition".
8#
9# See also:
10#	opt-debug-lint.mk
11
12.MAKEFLAGS: -dL
13
14# DEF is defined, UNDEF is not.
15DEF=		defined
16
17# An expression based on a defined variable is fine.
18.if !${DEF}
19.  error
20.endif
21
22# Since the condition fails to evaluate, neither of the branches is taken.
23.if ${UNDEF}
24.  error
25.else
26.  error
27.endif
28
29# The variable name depends on the undefined variable, which is probably a
30# mistake.  The variable UNDEF, as used here, can be easily turned into
31# an expression that is always defined, using the :U modifier.
32#
33# The outer expression does not generate an error message since there was
34# already an error evaluating this variable's name.
35#
36# TODO: Suppress the error message "Variable VAR. is undefined".  That part
37# of the expression must not be evaluated at all.
38.if ${VAR.${UNDEF}}
39.  error
40.else
41.  error
42.endif
43
44# The variable VAR.defined is not defined and thus generates an error message.
45#
46# TODO: This pattern looks a lot like CFLAGS.${OPSYS}, which is at least
47# debatable.  Or would any practical use of CFLAGS.${OPSYS} be via an indirect
48# expression, as in the next example?
49.if ${VAR.${DEF}}
50.  error
51.else
52.  error
53.endif
54
55
56# Variables that are referenced indirectly may be undefined in a condition.
57#
58# A practical example for this is CFLAGS, which consists of CWARNS, COPTS
59# and a few others.  Just because these nested variables are not defined,
60# this does not make the condition invalid.
61#
62# The crucial point is that at the point where the variable appears in the
63# condition, there is no way to influence the definedness of the nested
64# variables.  In particular, there is no modifier that would turn undefined
65# nested variables into empty strings, as an equivalent to the :U modifier.
66INDIRECT=	${NESTED_UNDEF} ${NESTED_DEF}
67NESTED_DEF=	nested-defined
68
69# Since NESTED_UNDEF is not controllable at this point, it must not generate
70# an error message, and it doesn't do so, since 2020-09-14.
71.if !${INDIRECT}
72.  error
73.endif
74