Module Name:    src
Committed By:   rillig
Date:           Sat Sep 24 16:13:48 UTC 2022

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

Log Message:
make: clean up tracking of depth of nested .if directives

The variable cond_min_depth was redundant.  It was only accessed while
parsing the makefiles.  Merging it into struct IncludedFile removes the
possible confusion between cond_min_depth and including_cond_min_depth.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.341 -r1.342 src/usr.bin/make/cond.c
cvs rdiff -u -r1.306 -r1.307 src/usr.bin/make/make.h
cvs rdiff -u -r1.686 -r1.687 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.341 src/usr.bin/make/cond.c:1.342
--- src/usr.bin/make/cond.c:1.341	Sat Sep 24 10:26:31 2022
+++ src/usr.bin/make/cond.c	Sat Sep 24 16:13:48 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.341 2022/09/24 10:26:31 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.342 2022/09/24 16:13:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -81,13 +81,9 @@
  *			of one of the .if directives or the condition in a
  *			':?then:else' variable modifier.
  *
- *	Cond_PushMinDepth
- *	Cond_PopMinDepth
- *	Cond_ResetDepth
- *			Save and restore the nesting of the conditions, at
- *			the start and end of including another makefile, to
- *			ensure that in each makefile the conditional
- *			directives are well-balanced.
+ *	Cond_EndFile
+ *			At the end of reading a makefile, ensure that the
+ *			conditional directives are well-balanced.
  */
 
 #include <errno.h>
@@ -96,7 +92,7 @@
 #include "dir.h"
 
 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: cond.c,v 1.341 2022/09/24 10:26:31 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.342 2022/09/24 16:13:48 rillig Exp $");
 
 /*
  * Conditional expressions conform to this grammar:
@@ -179,8 +175,7 @@ typedef struct CondParser {
 
 static CondResult CondParser_Or(CondParser *par, bool);
 
-static unsigned int cond_depth = 0;	/* current .if nesting level */
-static unsigned int cond_min_depth = 0;	/* depth at makefile open */
+unsigned int cond_depth = 0;	/* current .if nesting level */
 
 /* Names for ComparisonOp. */
 static const char opname[][3] = { "<", "<=", ">", ">=", "==", "!=" };
@@ -1138,7 +1133,7 @@ Cond_EvalLine(const char *line)
 			    "The .endif directive does not take arguments");
 		}
 
-		if (cond_depth == cond_min_depth) {
+		if (cond_depth == CurFile_CondMinDepth()) {
 			Parse_Error(PARSE_FATAL, "if-less endif");
 			return CR_TRUE;
 		}
@@ -1168,7 +1163,7 @@ Cond_EvalLine(const char *line)
 				    "The .else directive "
 				    "does not take arguments");
 
-			if (cond_depth == cond_min_depth) {
+			if (cond_depth == CurFile_CondMinDepth()) {
 				Parse_Error(PARSE_FATAL, "if-less else");
 				return CR_TRUE;
 			}
@@ -1203,7 +1198,7 @@ Cond_EvalLine(const char *line)
 		return CR_ERROR;
 
 	if (isElif) {
-		if (cond_depth == cond_min_depth) {
+		if (cond_depth == CurFile_CondMinDepth()) {
 			Parse_Error(PARSE_FATAL, "if-less elif");
 			return CR_TRUE;
 		}
@@ -1256,36 +1251,14 @@ Cond_EvalLine(const char *line)
 	return res;
 }
 
-unsigned int
-Cond_PushMinDepth(void)
-{
-	unsigned int depth = cond_min_depth;
-
-	cond_min_depth = cond_depth;
-	return depth;
-}
-
 void
-Cond_PopMinDepth(unsigned int saved_depth)
+Cond_EndFile(void)
 {
-	unsigned int open_conds = cond_depth - cond_min_depth;
+	unsigned int open_conds = cond_depth - CurFile_CondMinDepth();
 
-	assert(saved_depth <= cond_depth);
 	if (open_conds != 0) {
 		Parse_Error(PARSE_FATAL, "%u open conditional%s",
 		    open_conds, open_conds == 1 ? "" : "s");
-		cond_depth = cond_min_depth;
+		cond_depth = CurFile_CondMinDepth();
 	}
-
-	cond_min_depth = saved_depth;
-}
-
-/*
- * When breaking out of a .for loop, restore cond_depth to where it was when
- * the loop started.
- */
-void
-Cond_ResetDepth(void)
-{
-	cond_depth = cond_min_depth;
 }

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.306 src/usr.bin/make/make.h:1.307
--- src/usr.bin/make/make.h:1.306	Sat Sep 24 10:26:31 2022
+++ src/usr.bin/make/make.h	Sat Sep 24 16:13:48 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.h,v 1.306 2022/09/24 10:26:31 rillig Exp $	*/
+/*	$NetBSD: make.h,v 1.307 2022/09/24 16:13:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -797,11 +797,10 @@ void Compat_MakeAll(GNodeList *);
 void Compat_Make(GNode *, GNode *);
 
 /* cond.c */
+extern unsigned int cond_depth;
 CondResult Cond_EvalCondition(const char *) MAKE_ATTR_USE;
 CondResult Cond_EvalLine(const char *) MAKE_ATTR_USE;
-unsigned int Cond_PushMinDepth(void) MAKE_ATTR_USE;
-void Cond_PopMinDepth(unsigned int);
-void Cond_ResetDepth(void);
+void Cond_EndFile(void);
 
 /* dir.c; see also dir.h */
 
@@ -864,6 +863,7 @@ void Parse_PushInput(const char *, unsig
 		     struct ForLoop *);
 void Parse_MainName(GNodeList *);
 int Parse_NumErrors(void) MAKE_ATTR_USE;
+unsigned int CurFile_CondMinDepth(void) MAKE_ATTR_USE;
 
 
 /* suff.c */

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.686 src/usr.bin/make/parse.c:1.687
--- src/usr.bin/make/parse.c:1.686	Sat Sep 24 16:09:04 2022
+++ src/usr.bin/make/parse.c	Sat Sep 24 16:13:48 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.686 2022/09/24 16:09:04 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.687 2022/09/24 16:13:48 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.686 2022/09/24 16:09:04 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.687 2022/09/24 16:13:48 rillig Exp $");
 
 /*
  * A file being read.
@@ -119,9 +119,8 @@ typedef struct IncludedFile {
 	unsigned forBodyReadLines; /* the number of physical lines that have
 				 * been read from the file above the body of
 				 * the .for loop */
-	unsigned int including_cond_min_depth; /* depth of nested 'if'
-				 * directives, at the point where the
-				 * including file was opened */
+	unsigned int condMinDepth; /* depth of nested 'if' directives, at the
+				 * beginning of the file */
 	bool depending;		/* state of doing_depend on EOF */
 
 	Buffer buf;		/* the file's content or the body of the .for
@@ -311,6 +310,12 @@ CurFile(void)
 	return GetInclude(includes.len - 1);
 }
 
+unsigned int
+CurFile_CondMinDepth(void)
+{
+	return CurFile()->condMinDepth;
+}
+
 static Buffer
 LoadFile(const char *path, int fd)
 {
@@ -2142,7 +2147,7 @@ Parse_PushInput(const char *name, unsign
 
 	curFile->buf_ptr = curFile->buf.data;
 	curFile->buf_end = curFile->buf.data + curFile->buf.len;
-	curFile->including_cond_min_depth = Cond_PushMinDepth();
+	curFile->condMinDepth = cond_depth;
 	SetParseFile(name);
 }
 
@@ -2275,11 +2280,7 @@ ParseEOF(void)
 		return true;
 	}
 
-	/*
-	 * Ensure the makefile (or .for loop) didn't have mismatched
-	 * conditionals.
-	 */
-	Cond_PopMinDepth(curFile->including_cond_min_depth);
+	Cond_EndFile();
 
 	FStr_Done(&curFile->name);
 	Buf_Done(&curFile->buf);
@@ -2672,7 +2673,7 @@ HandleBreak(void)
 	if (curFile->forLoop != NULL) {
 		/* pretend we reached EOF */
 		For_Break(curFile->forLoop);
-		Cond_ResetDepth();
+		cond_depth = CurFile_CondMinDepth();
 		ParseEOF();
 	} else
 		Parse_Error(PARSE_FATAL, "break outside of for loop");

Reply via email to