Module Name:    src
Committed By:   rillig
Date:           Thu Dec 30 01:06:43 UTC 2021

Modified Files:
        src/usr.bin/make: cond.c

Log Message:
make: make ParseWord in condition parser simpler

Merge the two return values (bool, string) into a single return value.
As before, the caller cannot observe the difference between a parse
error and an empty word, both are handled in the same way.

In CondParser_ComparisonOrLeaf, the word cannot be empty since the
calling function CondParser_Token already handles all cases that could
lead to an empty word.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.317 -r1.318 src/usr.bin/make/cond.c

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.317 src/usr.bin/make/cond.c:1.318
--- src/usr.bin/make/cond.c:1.317	Thu Dec 30 00:22:20 2021
+++ src/usr.bin/make/cond.c	Thu Dec 30 01:06:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.317 2021/12/30 00:22:20 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.318 2021/12/30 01:06:43 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,7 +95,7 @@
 #include "dir.h"
 
 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: cond.c,v 1.317 2021/12/30 00:22:20 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.318 2021/12/30 01:06:43 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -218,16 +218,15 @@ CondParser_SkipWhitespace(CondParser *pa
  *	func says whether the argument belongs to an actual function, or
  *	NULL when parsing a bare word.
  *
- * Return false if there was a parse error or the argument was empty.
+ * Return NULL if there was a parse error or the argument was empty.
  */
-static bool
-ParseWord(CondParser *par, const char **pp, bool doEval, const char *func,
-	     char **out_arg)
+static char *
+ParseWord(CondParser *par, const char **pp, bool doEval, const char *func)
 {
 	const char *p = *pp;
 	Buffer argBuf;
 	int paren_depth;
-	bool ok;
+	char *res;
 
 	if (func != NULL)
 		p++;		/* Skip opening '(' - verified by caller */
@@ -270,8 +269,7 @@ ParseWord(CondParser *par, const char **
 		p++;
 	}
 
-	ok = argBuf.len > 0;
-	*out_arg = Buf_DoneData(&argBuf);
+	res = Buf_DoneData(&argBuf);
 
 	cpp_skip_hspace(&p);
 
@@ -283,11 +281,17 @@ ParseWord(CondParser *par, const char **
 		Parse_Error(PARSE_FATAL,
 		    "Missing closing parenthesis for %.*s()", len, func);
 		par->printedError = true;
-		return false;
+		free(res);
+		return NULL;
 	}
 
 	*pp = p;
-	return ok;
+
+	if (res[0] == '\0') {
+		free(res);
+		res = NULL;
+	}
+	return res;
 }
 
 /* Test whether the given variable is defined. */
@@ -738,8 +742,7 @@ CondParser_FuncCallEmpty(CondParser *par
 static bool
 CondParser_FuncCall(CondParser *par, bool doEval, Token *out_token)
 {
-	char *arg = NULL;
-	bool ok;
+	char *arg;
 	const char *p = par->p;
 	bool (*fn)(const char *);
 	const char *fn_name = p;
@@ -761,10 +764,10 @@ CondParser_FuncCall(CondParser *par, boo
 	if (*p != '(')
 		return false;
 
-	ok = ParseWord(par, &p, doEval, fn_name, &arg);
-	*out_token = ToToken(ok && doEval && fn(arg));
-
+	arg = ParseWord(par, &p, doEval, fn_name);
+	*out_token = ToToken(doEval && arg != NULL && fn(arg));
 	free(arg);
+
 	par->p = p;
 	return true;
 }
@@ -801,7 +804,9 @@ CondParser_ComparisonOrLeaf(CondParser *
 	 * XXX: Is it possible to have a variable expression evaluated twice
 	 *  at this point?
 	 */
-	(void)ParseWord(par, &cp, doEval, NULL, &arg);
+	arg = ParseWord(par, &cp, doEval, NULL);
+	assert(arg != NULL);
+
 	cp1 = cp;
 	cpp_skip_whitespace(&cp1);
 	if (*cp1 == '=' || *cp1 == '!' || *cp1 == '<' || *cp1 == '>')

Reply via email to