Module Name:    src
Committed By:   rillig
Date:           Fri Sep  4 20:51:01 UTC 2020

Modified Files:
        src/usr.bin/make: cond.c
        src/usr.bin/make/unit-tests: cond-func-empty.mk

Log Message:
make(1): migrate get_mpt_arg to Var_ParsePP

This part is covered well by the unit tests.  When I forgot to decrement
the linePtr, several of them failed reliably.


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/usr.bin/make/cond.c
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/make/unit-tests/cond-func-empty.mk

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/cond.c
diff -u src/usr.bin/make/cond.c:1.110 src/usr.bin/make/cond.c:1.111
--- src/usr.bin/make/cond.c:1.110	Fri Sep  4 20:32:34 2020
+++ src/usr.bin/make/cond.c	Fri Sep  4 20:51:01 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.110 2020/09/04 20:32:34 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.111 2020/09/04 20:51:01 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: cond.c,v 1.110 2020/09/04 20:32:34 rillig Exp $";
+static char rcsid[] = "$NetBSD: cond.c,v 1.111 2020/09/04 20:51:01 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: cond.c,v 1.110 2020/09/04 20:32:34 rillig Exp $");
+__RCSID("$NetBSD: cond.c,v 1.111 2020/09/04 20:51:01 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -676,25 +676,16 @@ static int
 get_mpt_arg(Boolean doEval, const char **linePtr, char **argPtr,
 	    const char *func MAKE_ATTR_UNUSED)
 {
-    /*
-     * Use Var_Parse to parse the spec in parens and return
-     * TOK_TRUE if the resulting string is empty.
-     */
-    int length;
     void *val_freeIt;
     const char *val;
-    const char *cp = *linePtr;
+    int magic_res;
 
     /* We do all the work here and return the result as the length */
     *argPtr = NULL;
 
-    val = Var_Parse(cp - 1, VAR_CMD, doEval ? VARE_WANTRES : 0, &length,
-		    &val_freeIt);
-    /*
-     * Advance *linePtr to beyond the closing ). Note that
-     * we subtract one because 'length' is calculated from 'cp - 1'.
-     */
-    *linePtr = cp - 1 + length;
+    (*linePtr)--;		/* Make (*linePtr)[1] point to the '('. */
+    val = Var_ParsePP(linePtr, VAR_CMD, doEval ? VARE_WANTRES : 0, &val_freeIt);
+    /* If successful, *linePtr points beyond the closing ')' now. */
 
     if (val == var_Error) {
 	free(val_freeIt);
@@ -709,9 +700,9 @@ get_mpt_arg(Boolean doEval, const char *
      * For consistency with the other functions we can't generate the
      * true/false here.
      */
-    length = *val ? 2 : 1;
+    magic_res = *val != '\0' ? 2 : 1;
     free(val_freeIt);
-    return length;
+    return magic_res;
 }
 
 static Boolean

Index: src/usr.bin/make/unit-tests/cond-func-empty.mk
diff -u src/usr.bin/make/unit-tests/cond-func-empty.mk:1.4 src/usr.bin/make/unit-tests/cond-func-empty.mk:1.5
--- src/usr.bin/make/unit-tests/cond-func-empty.mk:1.4	Fri Sep  4 20:28:16 2020
+++ src/usr.bin/make/unit-tests/cond-func-empty.mk	Fri Sep  4 20:51:01 2020
@@ -1,4 +1,4 @@
-# $NetBSD: cond-func-empty.mk,v 1.4 2020/09/04 20:28:16 rillig Exp $
+# $NetBSD: cond-func-empty.mk,v 1.5 2020/09/04 20:51:01 rillig Exp $
 #
 # Tests for the empty() function in .if conditions, which tests a variable
 # expression for emptiness.
@@ -121,5 +121,17 @@ ${:U }=	space
 .  error
 .endif
 
+# Ensure that variable expressions that appear as part of the argument are
+# properly parsed.  Typical use cases for this are .for loops, which are
+# expanded to exactly these ${:U} expressions.
+#
+# If everything goes well, the argument expands to "WORD", and that variable
+# is defined at the beginning of this file.  The surrounding 'W' and 'D'
+# ensure that the parser in ParseEmptyArg has the correct position, both
+# before and after the call to Var_ParsePP.
+.if empty(W${:UOR}D)
+.  error
+.endif
+
 all:
 	@:;

Reply via email to