Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make/unit-tests make(1): add test for undefined vari...



details:   https://anonhg.NetBSD.org/src/rev/965452908ac6
branches:  trunk
changeset: 942024:965452908ac6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Nov 04 04:24:57 2020 +0000

description:
make(1): add test for undefined variables in command line arguments

The variable discardUndefined has an implicit negation in its name,
which makes it hard to understand.  Plus, most of the time it is true.
It's better to have a flag that is false most of the time and has a
positive name.

On the first attempt of inverting that variable, I stumbled upon
MainParseArgs, which initially leaves discardUndefined == FALSE, and
after handling the dashed options, sets it to TRUE.  This would make a
difference when more command line arguments would be added later via the
.MAKEFLAGS special target.

Upon further inspection, the only place where discardUndefined is used
is in VarAssign_EvalSubst in parse.c, and that place is not reachable
from any of the dashed options.  Therefore, discardUndefined could
already be set at the very beginning of MainParseArgs or even when
initializing the global variable itself, without any observable
difference.

Not even the ::= variable modifier could do anything about this since it
is not reachable from the dashed command line options as well, and in
addition, it expands its right-hand side in any case, always discarding
undefined variables.  Oh, these little inconsistencies everywhere.

diffstat:

 distrib/sets/lists/tests/mi                   |   4 ++-
 usr.bin/make/unit-tests/Makefile              |   3 +-
 usr.bin/make/unit-tests/cmdline-undefined.exp |  17 +++++++++++
 usr.bin/make/unit-tests/cmdline-undefined.mk  |  40 +++++++++++++++++++++++++++
 usr.bin/make/var.c                            |   6 ++-
 5 files changed, 66 insertions(+), 4 deletions(-)

diffs (127 lines):

diff -r b101269809b5 -r 965452908ac6 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Nov 04 03:37:51 2020 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Nov 04 04:24:57 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.960 2020/11/03 17:17:31 rillig Exp $
+# $NetBSD: mi,v 1.961 2020/11/04 04:24:57 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -4819,6 +4819,8 @@
 ./usr/tests/usr.bin/make/unit-tests/cmd-errors.mk                              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/cmd-interrupt.exp                          tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/cmd-interrupt.mk                           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/cmdline-undefined.exp                      tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/cmdline-undefined.mk                       tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/cmdline.exp                                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/cmdline.mk                                 tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/comment.exp                                        tests-usr.bin-tests     compattestfile,atf
diff -r b101269809b5 -r 965452908ac6 usr.bin/make/unit-tests/Makefile
--- a/usr.bin/make/unit-tests/Makefile  Wed Nov 04 03:37:51 2020 +0000
+++ b/usr.bin/make/unit-tests/Makefile  Wed Nov 04 04:24:57 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.187 2020/11/03 17:17:31 rillig Exp $
+# $NetBSD: Makefile,v 1.188 2020/11/04 04:24:57 rillig Exp $
 #
 # Unit tests for make(1)
 #
@@ -41,6 +41,7 @@
 TESTS+=                cmd-errors-lint
 TESTS+=                cmd-interrupt
 TESTS+=                cmdline
+TESTS+=                cmdline-undefined
 TESTS+=                comment
 TESTS+=                cond-cmp-numeric
 TESTS+=                cond-cmp-numeric-eq
diff -r b101269809b5 -r 965452908ac6 usr.bin/make/unit-tests/cmdline-undefined.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/unit-tests/cmdline-undefined.exp     Wed Nov 04 04:24:57 2020 +0000
@@ -0,0 +1,17 @@
+The = assignment operator
+make: "cmdline-undefined.mk" line 27: From the command line: Undefined is .
+make: "cmdline-undefined.mk" line 28: From .MAKEFLAGS '=': Undefined is .
+make: "cmdline-undefined.mk" line 29: From .MAKEFLAGS ':=': Undefined is .
+make: "cmdline-undefined.mk" line 33: From the command line: Undefined is now defined.
+make: "cmdline-undefined.mk" line 34: From .MAKEFLAGS '=': Undefined is now defined.
+make: "cmdline-undefined.mk" line 35: From .MAKEFLAGS ':=': Undefined is now defined.
+
+The := assignment operator
+make: "cmdline-undefined.mk" line 27: From the command line: Undefined is .
+make: "cmdline-undefined.mk" line 28: From .MAKEFLAGS '=': Undefined is .
+make: "cmdline-undefined.mk" line 29: From .MAKEFLAGS ':=': Undefined is .
+make: "cmdline-undefined.mk" line 33: From the command line: Undefined is now defined.
+make: "cmdline-undefined.mk" line 34: From .MAKEFLAGS '=': Undefined is now defined.
+make: "cmdline-undefined.mk" line 35: From .MAKEFLAGS ':=': Undefined is now defined.
+
+exit status 0
diff -r b101269809b5 -r 965452908ac6 usr.bin/make/unit-tests/cmdline-undefined.mk
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/unit-tests/cmdline-undefined.mk      Wed Nov 04 04:24:57 2020 +0000
@@ -0,0 +1,40 @@
+# $NetBSD: cmdline-undefined.mk,v 1.1 2020/11/04 04:24:57 rillig Exp $
+#
+# Tests for undefined variable expressions in the command line.
+
+all:
+       # When the command line is parsed during the initial call of
+       # MainParseArgs, discardUndefined is still FALSE, therefore preserving
+       # undefined subexpressions.  This makes sense because at that early
+       # stage, almost no variables are defined.  On the other hand, the '='
+       # assignment operator does not expand its right-hand side anyway.
+       @echo 'The = assignment operator'
+       @${.MAKE} -f ${MAKEFILE} print-undefined \
+               CMDLINE='Undefined is $${UNDEFINED}.'
+       @echo
+
+       # The interesting case is using the ':=' assignment operator, which
+       # expands its right-hand side.  But only those variables that are
+       # defined.
+       @echo 'The := assignment operator'
+       @${.MAKE} -f ${MAKEFILE} print-undefined \
+               CMDLINE:='Undefined is $${UNDEFINED}.'
+       @echo
+
+.if make(print-undefined)
+
+.MAKEFLAGS: MAKEFLAGS_ASSIGN='Undefined is $${UNDEFINED}.'
+.MAKEFLAGS: MAKEFLAGS_SUBST:='Undefined is $${UNDEFINED}.'
+
+.info From the command line: ${CMDLINE}
+.info From .MAKEFLAGS '=': ${MAKEFLAGS_ASSIGN}
+.info From .MAKEFLAGS ':=': ${MAKEFLAGS_SUBST}
+
+UNDEFINED?=    now defined
+
+.info From the command line: ${CMDLINE}
+.info From .MAKEFLAGS '=': ${MAKEFLAGS_ASSIGN}
+.info From .MAKEFLAGS ':=': ${MAKEFLAGS_SUBST}
+
+print-undefined:
+.endif
diff -r b101269809b5 -r 965452908ac6 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Wed Nov 04 03:37:51 2020 +0000
+++ b/usr.bin/make/var.c        Wed Nov 04 04:24:57 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.655 2020/11/04 03:37:51 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.656 2020/11/04 04:24:57 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -130,7 +130,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.655 2020/11/04 03:37:51 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.656 2020/11/04 04:24:57 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -2978,6 +2978,8 @@
     }
 
     delim = st->startc == '(' ? ')' : '}';
+    /* TODO: Add test for using the ::= modifier in a := assignment line.
+     * Probably st->eflags should be passed down without VARE_ASSIGN here. */
     res = ParseModifierPart(pp, delim, st->eflags, st, &val, NULL, NULL, NULL);
     if (res != VPR_OK)
        return AMR_CLEANUP;



Home | Main Index | Thread Index | Old Index