Module Name: src
Committed By: rillig
Date: Mon Sep 14 06:44:50 UTC 2020
Modified Files:
src/distrib/sets/lists/tests: mi
src/usr.bin/make/unit-tests: Makefile
Added Files:
src/usr.bin/make/unit-tests: cond-undef-lint.exp cond-undef-lint.mk
Log Message:
make(1): add test for undefined expressions in conditions in lint mode
To generate a diff of this commit:
cvs rdiff -u -r1.923 -r1.924 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.141 -r1.142 src/usr.bin/make/unit-tests/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.bin/make/unit-tests/cond-undef-lint.exp \
src/usr.bin/make/unit-tests/cond-undef-lint.mk
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.923 src/distrib/sets/lists/tests/mi:1.924
--- src/distrib/sets/lists/tests/mi:1.923 Mon Sep 14 06:22:59 2020
+++ src/distrib/sets/lists/tests/mi Mon Sep 14 06:44:50 2020
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.923 2020/09/14 06:22:59 rillig Exp $
+# $NetBSD: mi,v 1.924 2020/09/14 06:44:50 rillig Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -4603,6 +4603,8 @@
./usr/tests/usr.bin/make/unit-tests/cond-token-string.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/cond-token-var.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/cond-token-var.mk tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/cond-undef-lint.exp tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/cond-undef-lint.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/cond1.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/cond1.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/cond2.exp tests-usr.bin-tests compattestfile,atf
Index: src/usr.bin/make/unit-tests/Makefile
diff -u src/usr.bin/make/unit-tests/Makefile:1.141 src/usr.bin/make/unit-tests/Makefile:1.142
--- src/usr.bin/make/unit-tests/Makefile:1.141 Mon Sep 14 06:22:59 2020
+++ src/usr.bin/make/unit-tests/Makefile Mon Sep 14 06:44:50 2020
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.141 2020/09/14 06:22:59 rillig Exp $
+# $NetBSD: Makefile,v 1.142 2020/09/14 06:44:50 rillig Exp $
#
# Unit tests for make(1)
#
@@ -67,6 +67,7 @@ TESTS+= cond-token-number
TESTS+= cond-token-plain
TESTS+= cond-token-string
TESTS+= cond-token-var
+TESTS+= cond-undef-lint
TESTS+= cond1
TESTS+= cond2
TESTS+= counter
Added files:
Index: src/usr.bin/make/unit-tests/cond-undef-lint.exp
diff -u /dev/null src/usr.bin/make/unit-tests/cond-undef-lint.exp:1.1
--- /dev/null Mon Sep 14 06:44:50 2020
+++ src/usr.bin/make/unit-tests/cond-undef-lint.exp Mon Sep 14 06:44:50 2020
@@ -0,0 +1,8 @@
+make: "cond-undef-lint.mk" line 20: Variable "UNDEF" is undefined
+make: "cond-undef-lint.mk" line 35: Variable "UNDEF" is undefined
+make: "cond-undef-lint.mk" line 35: Variable "VAR." is undefined
+make: "cond-undef-lint.mk" line 42: Variable "VAR.defined" is undefined
+make: "cond-undef-lint.mk" line 66: Variable "NESTED_UNDEF" is undefined
+make: Fatal errors encountered -- cannot continue
+make: stopped in unit-tests
+exit status 1
Index: src/usr.bin/make/unit-tests/cond-undef-lint.mk
diff -u /dev/null src/usr.bin/make/unit-tests/cond-undef-lint.mk:1.1
--- /dev/null Mon Sep 14 06:44:50 2020
+++ src/usr.bin/make/unit-tests/cond-undef-lint.mk Mon Sep 14 06:44:50 2020
@@ -0,0 +1,68 @@
+# $NetBSD: cond-undef-lint.mk,v 1.1 2020/09/14 06:44:50 rillig Exp $
+#
+# Tests for defined and undefined variables in .if conditions, in lint mode.
+#
+# As of 2020-09-14, lint mode contains experimental code for printing
+# accurate error messages in case of undefined variables, instead of the
+# wrong "Malformed condition".
+
+.MAKEFLAGS: -dL
+
+# DEF is defined, UNDEF is not.
+DEF= defined
+
+# An expression based on a defined variable is fine.
+.if !${DEF}
+. error
+.endif
+
+# Since the condition fails to evaluate, neither of the branches is taken.
+.if ${UNDEF}
+. error
+.else
+. error
+.endif
+
+# The variable name depends on the undefined variable, which is probably a
+# mistake. The variable UNDEF, as used here, can be easily turned into
+# an expression that is always defined, using the :U modifier.
+#
+# The outer expression does not generate an error message since there was
+# already an error evaluating this variable's name.
+#
+# TODO: Suppress the error message "Variable VAR. is undefined". That part
+# of the expression must not be evaluated at all.
+.if ${VAR.${UNDEF}}
+. error
+.else
+. error
+.endif
+
+# The variable VAR.defined is not defined and thus generates an error message.
+.if ${VAR.${DEF}}
+. error
+.else
+. error
+.endif
+
+
+# Variables that are referenced indirectly may be undefined in a condition.
+#
+# A practical example for this is CFLAGS, which consists of CWARNS, COPTS
+# and a few others. Just because these nested variables are not defined,
+# this does not make the condition invalid.
+#
+# The crucial point is that at the point where the variable appears in the
+# condition, there is no way to influence the definedness of the nested
+# variables. In particular, there is no modifier that would turn undefined
+# nested variables into empty strings, as an equivalent to the :U modifier.
+INDIRECT= ${NESTED_UNDEF} ${NESTED_DEF}
+NESTED_DEF= nested-defined
+
+# Since NESTED_UNDEF is not controllable at this point, it must not generate
+# an error message. This condition should generate no error message at all.
+#
+# TODO: Suppress the error message.
+.if !${INDIRECT}
+. error
+.endif