Module Name:    src
Committed By:   rillig
Date:           Mon Jun 19 17:30:56 UTC 2023

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

Log Message:
make: clean up code for skipping files with multiple-inclusion guard

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.347 -r1.348 src/usr.bin/make/cond.c
cvs rdiff -u -r1.700 -r1.701 src/usr.bin/make/parse.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.347 src/usr.bin/make/cond.c:1.348
--- src/usr.bin/make/cond.c:1.347	Mon Jun 19 12:53:57 2023
+++ src/usr.bin/make/cond.c	Mon Jun 19 17:30:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.347 2023/06/19 12:53:57 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.348 2023/06/19 17:30:56 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -92,7 +92,7 @@
 #include "dir.h"
 
 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: cond.c,v 1.347 2023/06/19 12:53:57 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.348 2023/06/19 17:30:56 rillig Exp $");
 
 /*
  * Conditional expressions conform to this grammar:
@@ -1238,13 +1238,14 @@ Cond_EvalLine(const char *line)
 }
 
 static bool
-skip_identifier(const char **pp)
+ParseVarnameGuard(const char **pp, const char **varname)
 {
 	const char *p = *pp;
 
 	if (ch_isalpha(*p) || *p == '_') {
 		while (ch_isalnum(*p) || *p == '_')
 			p++;
+		*varname = *pp;
 		*pp = p;
 		return true;
 	}
@@ -1258,33 +1259,26 @@ skip_identifier(const char **pp)
 char *
 Cond_ExtractGuard(const char *line)
 {
-	const char *p = line, *dir, *varname;
-	size_t dir_len;
+	const char *p = line, *varname;
+	Substring dir;
 
 	if (!skip_string(&p, "."))
 		return NULL;
 	cpp_skip_hspace(&p);
 
-	dir = p;
+	dir.start = p;
 	while (ch_isalpha(*p))
 		p++;
-	dir_len = (size_t)(p - dir);
+	dir.end = p;
 	cpp_skip_hspace(&p);
 
-	if (dir_len == 2 && memcmp(dir, "if", 2) == 0) {
-		if (!skip_string(&p, "!defined("))
-			return NULL;
-		varname = p;
-		skip_identifier(&p);
-		if (p > varname && strcmp(p, ")") == 0)
-			return bmake_strsedup(varname, p);
-	}
-	if (dir_len == 6 && memcmp(dir, "ifndef", 6) == 0) {
-		varname = p;
-		skip_identifier(&p);
-		if (p > varname && *p == '\0')
-			return bmake_strsedup(varname, p);
-	}
+	if (Substring_Equals(dir, "if"))
+		return skip_string(&p, "!defined(")
+		    && ParseVarnameGuard(&p, &varname) && strcmp(p, ")") == 0
+		    ? bmake_strsedup(varname, p) : NULL;
+	if (Substring_Equals(dir, "ifndef"))
+		return ParseVarnameGuard(&p, &varname) && *p == '\0'
+		    ? bmake_strsedup(varname, p) : NULL;
 	return NULL;
 }
 

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.700 src/usr.bin/make/parse.c:1.701
--- src/usr.bin/make/parse.c:1.700	Mon Jun 19 12:53:57 2023
+++ src/usr.bin/make/parse.c	Mon Jun 19 17:30:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.700 2023/06/19 12:53:57 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.701 2023/06/19 17:30:56 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -105,7 +105,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.700 2023/06/19 12:53:57 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.701 2023/06/19 17:30:56 rillig Exp $");
 
 /* Detects a multiple-inclusion guard in a makefile. */
 typedef enum {
@@ -1210,6 +1210,18 @@ FindInQuotPath(const char *file)
 	return fullname;
 }
 
+static bool
+SkipGuarded(const char *fullname)
+{
+	char *guard = HashTable_FindValue(&guards, fullname);
+	if (guard != NULL && GNode_ValueDirect(SCOPE_GLOBAL, guard) != NULL) {
+		DEBUG2(PARSE, "Skipping '%s' because '%s' is already set\n",
+		    fullname, guard);
+		return true;
+	}
+	return false;
+}
+
 /*
  * Handle one of the .[-ds]include directives by remembering the current file
  * and pushing the included file on the stack.  After the included file has
@@ -1225,7 +1237,6 @@ IncludeFile(const char *file, bool isSys
 {
 	Buffer buf;
 	char *fullname;		/* full pathname of file */
-	char *guardVarname;
 	int fd;
 
 	fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
@@ -1245,13 +1256,8 @@ IncludeFile(const char *file, bool isSys
 		return;
 	}
 
-	guardVarname = HashTable_FindValue(&guards, fullname);
-	if (guardVarname != NULL
-	    && GNode_ValueDirect(SCOPE_GLOBAL, guardVarname) != NULL) {
-		DEBUG2(PARSE, "Skipping '%s' because '%s' is already set\n",
-		    fullname, guardVarname);
+	if (SkipGuarded(fullname))
 		return;
-	}
 
 	if ((fd = open(fullname, O_RDONLY)) == -1) {
 		if (!silent)

Reply via email to