xref: /freebsd/contrib/bmake/unit-tests/cond-undef-lint.mk (revision 1e4896b176ff664dc9c2fce5426bf2fdf8017a7d)
1# $NetBSD: cond-undef-lint.mk,v 1.2 2020/09/14 07:13:29 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.if ${VAR.${DEF}}
46.  error
47.else
48.  error
49.endif
50
51
52# Variables that are referenced indirectly may be undefined in a condition.
53#
54# A practical example for this is CFLAGS, which consists of CWARNS, COPTS
55# and a few others.  Just because these nested variables are not defined,
56# this does not make the condition invalid.
57#
58# The crucial point is that at the point where the variable appears in the
59# condition, there is no way to influence the definedness of the nested
60# variables.  In particular, there is no modifier that would turn undefined
61# nested variables into empty strings, as an equivalent to the :U modifier.
62INDIRECT=	${NESTED_UNDEF} ${NESTED_DEF}
63NESTED_DEF=	nested-defined
64
65# Since NESTED_UNDEF is not controllable at this point, it must not generate
66# an error message, and it doesn't do so, since 2020-09-14.
67.if !${INDIRECT}
68.  error
69.endif
70