Module Name:    src
Committed By:   rillig
Date:           Sun Sep 13 20:21:24 UTC 2020

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: opt-debug-lint.exp opt-debug-lint.mk

Log Message:
make(1): in lint mode, report undefined variables in conditions


To generate a diff of this commit:
cvs rdiff -u -r1.517 -r1.518 src/usr.bin/make/var.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/opt-debug-lint.exp
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/opt-debug-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/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.517 src/usr.bin/make/var.c:1.518
--- src/usr.bin/make/var.c:1.517	Sun Sep 13 19:46:23 2020
+++ src/usr.bin/make/var.c	Sun Sep 13 20:21:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.517 2020/09/13 19:46:23 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.518 2020/09/13 20:21:24 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -121,7 +121,7 @@
 #include    "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.517 2020/09/13 19:46:23 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.518 2020/09/13 20:21:24 rillig Exp $");
 
 #define VAR_DEBUG_IF(cond, fmt, ...)	\
     if (!(DEBUG(VAR) && (cond)))	\
@@ -3560,11 +3560,25 @@ Var_Parse(const char **pp, GNode *ctxt, 
 		    free(varname);
 		    *out_val = pstr;
 		    return VPE_OK;
-		} else {
+		}
+
+		if ((eflags & VARE_UNDEFERR) && DEBUG(LINT)) {
+		    Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined",
+				varname);
 		    free(varname);
-		    *out_val = eflags & VARE_UNDEFERR ? var_Error : varNoError;
-		    return eflags & VARE_UNDEFERR ? VPE_UNDEF_SILENT : VPE_OK;
+		    *out_val = var_Error;
+		    return VPE_UNDEF_MSG;
 		}
+
+		if (eflags & VARE_UNDEFERR) {
+		    free(varname);
+		    *out_val = var_Error;
+		    return VPE_UNDEF_SILENT;
+		}
+
+		free(varname);
+		*out_val = varNoError;
+		return VPE_OK;
 	    }
 
 	    /* The variable expression is based on an undefined variable.

Index: src/usr.bin/make/unit-tests/opt-debug-lint.exp
diff -u src/usr.bin/make/unit-tests/opt-debug-lint.exp:1.3 src/usr.bin/make/unit-tests/opt-debug-lint.exp:1.4
--- src/usr.bin/make/unit-tests/opt-debug-lint.exp:1.3	Sun Sep 13 19:46:23 2020
+++ src/usr.bin/make/unit-tests/opt-debug-lint.exp	Sun Sep 13 20:21:24 2020
@@ -1,4 +1,5 @@
 make: "opt-debug-lint.mk" line 18: Variable "X" is undefined
+make: "opt-debug-lint.mk" line 40: Variable "UNDEF" is undefined
 make: Fatal errors encountered -- cannot continue
 make: stopped in unit-tests
 exit status 1

Index: src/usr.bin/make/unit-tests/opt-debug-lint.mk
diff -u src/usr.bin/make/unit-tests/opt-debug-lint.mk:1.2 src/usr.bin/make/unit-tests/opt-debug-lint.mk:1.3
--- src/usr.bin/make/unit-tests/opt-debug-lint.mk:1.2	Sun Sep 13 19:28:46 2020
+++ src/usr.bin/make/unit-tests/opt-debug-lint.mk	Sun Sep 13 20:21:24 2020
@@ -1,4 +1,4 @@
-# $NetBSD: opt-debug-lint.mk,v 1.2 2020/09/13 19:28:46 rillig Exp $
+# $NetBSD: opt-debug-lint.mk,v 1.3 2020/09/13 20:21:24 rillig Exp $
 #
 # Tests for the -dL command line option, which runs additional checks
 # to catch common mistakes, such as unclosed variable expressions.
@@ -32,5 +32,14 @@
 .  error
 .endif
 
+# Since 2020-09-13, Var_Parse properly reports errors for undefined variables,
+# but only in lint mode.  Before, it had only silently returned var_Error,
+# hoping for the caller to print an error message.  This resulted in the
+# well-known "Malformed conditional" error message, even though the
+# conditional was well-formed and the only error was an undefined variable.
+.if ${UNDEF}
+.  error
+.endif
+
 all:
 	@:;

Reply via email to