Module Name:    src
Committed By:   rillig
Date:           Sun Nov 28 18:58:58 UTC 2021

Modified Files:
        src/usr.bin/make: Makefile make.c make.h suff.c
Removed Files:
        src/usr.bin/make: enum.c enum.h

Log Message:
make: replace bloated bit-set-to-string code with simple code

It was a nice idea to implement a bit-set using an enum type and have a
generic ToString function for them.  In the end, the implementation
involved really heavy preprocessor magic and was probably difficult to
understand.  Replace all the code with a few bits of straight-forward
preprocessor magic that can be readily understood by just looking 5
lines around, instead of digging through 130 lines of lengthy macro
definitions.

Curiously, this reduces the binary size even though the 3 ToString
functions now have a few lines of duplicate code and there are more
explicit function calls.

The ToString functions are only seldom used, so the additional memory
allocation is acceptable.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.116 -r1.117 src/usr.bin/make/Makefile
cvs rdiff -u -r1.15 -r0 src/usr.bin/make/enum.c
cvs rdiff -u -r1.19 -r0 src/usr.bin/make/enum.h
cvs rdiff -u -r1.244 -r1.245 src/usr.bin/make/make.c
cvs rdiff -u -r1.266 -r1.267 src/usr.bin/make/make.h
cvs rdiff -u -r1.351 -r1.352 src/usr.bin/make/suff.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/Makefile
diff -u src/usr.bin/make/Makefile:1.116 src/usr.bin/make/Makefile:1.117
--- src/usr.bin/make/Makefile:1.116	Sat Jul 31 09:30:17 2021
+++ src/usr.bin/make/Makefile	Sun Nov 28 18:58:58 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.116 2021/07/31 09:30:17 rillig Exp $
+#	$NetBSD: Makefile,v 1.117 2021/11/28 18:58:58 rillig Exp $
 #	@(#)Makefile	5.2 (Berkeley) 12/28/90
 
 PROG=	make
@@ -7,7 +7,6 @@ SRCS+=  buf.c
 SRCS+=  compat.c
 SRCS+=  cond.c
 SRCS+=  dir.c
-SRCS+=  enum.c
 SRCS+=  for.c
 SRCS+=  hash.c
 SRCS+=  job.c

Index: src/usr.bin/make/make.c
diff -u src/usr.bin/make/make.c:1.244 src/usr.bin/make/make.c:1.245
--- src/usr.bin/make/make.c:1.244	Sun Apr  4 10:05:08 2021
+++ src/usr.bin/make/make.c	Sun Nov 28 18:58:58 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.c,v 1.244 2021/04/04 10:05:08 rillig Exp $	*/
+/*	$NetBSD: make.c,v 1.245 2021/11/28 18:58:58 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -104,7 +104,7 @@
 #include "job.h"
 
 /*	"@(#)make.c	8.1 (Berkeley) 6/6/93"	*/
-MAKE_RCSID("$NetBSD: make.c,v 1.244 2021/04/04 10:05:08 rillig Exp $");
+MAKE_RCSID("$NetBSD: make.c,v 1.245 2021/11/28 18:58:58 rillig Exp $");
 
 /* Sequence # to detect recursion. */
 static unsigned int checked_seqno = 1;
@@ -138,35 +138,93 @@ make_abort(GNode *gn, int lineno)
 	abort();
 }
 
-ENUM_FLAGS_RTTI_31(GNodeType,
-    OP_DEPENDS, OP_FORCE, OP_DOUBLEDEP,
-/* OP_OPMASK is omitted since it combines other flags */
-    OP_OPTIONAL, OP_USE, OP_EXEC, OP_IGNORE,
-    OP_PRECIOUS, OP_SILENT, OP_MAKE, OP_JOIN,
-    OP_MADE, OP_SPECIAL, OP_USEBEFORE, OP_INVISIBLE,
-    OP_NOTMAIN, OP_PHONY, OP_NOPATH, OP_WAIT,
-    OP_NOMETA, OP_META, OP_NOMETA_CMP, OP_SUBMAKE,
-    OP_TRANSFORM, OP_MEMBER, OP_LIB, OP_ARCHV,
-    OP_HAS_COMMANDS, OP_SAVE_CMDS, OP_DEPS_FOUND, OP_MARK);
-
-ENUM_FLAGS_RTTI_9(GNodeFlags,
-    REMAKE, CHILDMADE, FORCE, DONE_WAIT,
-    DONE_ORDER, FROM_DEPEND, DONE_ALLSRC, CYCLE,
-    DONECYCLE);
+static void
+Buf_AddFlag(Buffer *buf, bool flag, const char *name)
+{
+	if (flag) {
+		if (buf->len > 0)
+			Buf_AddByte(buf, '|');
+		Buf_AddBytes(buf, name, strlen(name));
+	}
+}
+
+static const char *
+GNodeType_ToString(GNodeType type, void **freeIt)
+{
+	Buffer buf;
+
+	Buf_InitSize(&buf, 32);
+#define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != 0, #flag)
+	ADD(OP_DEPENDS);
+	ADD(OP_FORCE);
+	ADD(OP_DOUBLEDEP);
+	ADD(OP_OPTIONAL);
+	ADD(OP_USE);
+	ADD(OP_EXEC);
+	ADD(OP_IGNORE);
+	ADD(OP_PRECIOUS);
+	ADD(OP_SILENT);
+	ADD(OP_MAKE);
+	ADD(OP_JOIN);
+	ADD(OP_MADE);
+	ADD(OP_SPECIAL);
+	ADD(OP_USEBEFORE);
+	ADD(OP_INVISIBLE);
+	ADD(OP_NOTMAIN);
+	ADD(OP_PHONY);
+	ADD(OP_NOPATH);
+	ADD(OP_WAIT);
+	ADD(OP_NOMETA);
+	ADD(OP_META);
+	ADD(OP_NOMETA_CMP);
+	ADD(OP_SUBMAKE);
+	ADD(OP_TRANSFORM);
+	ADD(OP_MEMBER);
+	ADD(OP_LIB);
+	ADD(OP_ARCHV);
+	ADD(OP_HAS_COMMANDS);
+	ADD(OP_SAVE_CMDS);
+	ADD(OP_DEPS_FOUND);
+	ADD(OP_MARK);
+#undef ADD
+	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
+}
+
+static const char *
+GNodeFlags_ToString(GNodeFlags flags, void **freeIt)
+{
+	Buffer buf;
+
+	Buf_InitSize(&buf, 32);
+#define ADD(flag) Buf_AddFlag(&buf, (flags & (flag)) != 0, #flag)
+	ADD(REMAKE);
+	ADD(CHILDMADE);
+	ADD(FORCE);
+	ADD(DONE_WAIT);
+	ADD(DONE_ORDER);
+	ADD(FROM_DEPEND);
+	ADD(DONE_ALLSRC);
+	ADD(CYCLE);
+	ADD(DONECYCLE);
+#undef ADD
+	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
+}
 
 void
 GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
 		    const char *suffix)
 {
-	char type_buf[GNodeType_ToStringSize];
-	char flags_buf[GNodeFlags_ToStringSize];
+	void *type_freeIt = NULL;
+	void *flags_freeIt = NULL;
 
 	fprintf(f, "%s%s, type %s, flags %s%s",
 	    prefix,
 	    GNodeMade_Name(gn->made),
-	    GNodeType_ToString(type_buf, gn->type),
-	    GNodeFlags_ToString(flags_buf, gn->flags),
+	    GNodeType_ToString(gn->type, &type_freeIt),
+	    GNodeFlags_ToString(gn->flags, &flags_freeIt),
 	    suffix);
+	free(type_freeIt);
+	free(flags_freeIt);
 }
 
 bool

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.266 src/usr.bin/make/make.h:1.267
--- src/usr.bin/make/make.h:1.266	Sat Nov 27 22:04:02 2021
+++ src/usr.bin/make/make.h	Sun Nov 28 18:58:58 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.h,v 1.266 2021/11/27 22:04:02 rillig Exp $	*/
+/*	$NetBSD: make.h,v 1.267 2021/11/28 18:58:58 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -163,7 +163,6 @@ typedef unsigned char bool;
 #endif
 
 #include "lst.h"
-#include "enum.h"
 #include "make_malloc.h"
 #include "str.h"
 #include "hash.h"

Index: src/usr.bin/make/suff.c
diff -u src/usr.bin/make/suff.c:1.351 src/usr.bin/make/suff.c:1.352
--- src/usr.bin/make/suff.c:1.351	Sat Jul 31 09:30:17 2021
+++ src/usr.bin/make/suff.c	Sun Nov 28 18:58:58 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.351 2021/07/31 09:30:17 rillig Exp $	*/
+/*	$NetBSD: suff.c,v 1.352 2021/11/28 18:58:58 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -115,7 +115,7 @@
 #include "dir.h"
 
 /*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
-MAKE_RCSID("$NetBSD: suff.c,v 1.351 2021/07/31 09:30:17 rillig Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.352 2021/11/28 18:58:58 rillig Exp $");
 
 typedef List SuffixList;
 typedef ListNode SuffixListNode;
@@ -170,9 +170,6 @@ typedef enum SuffixFlags {
 
 } SuffixFlags;
 
-ENUM_FLAGS_RTTI_3(SuffixFlags,
-    SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
-
 typedef List SuffixListList;
 
 /*
@@ -2131,15 +2128,39 @@ PrintSuffNames(const char *prefix, Suffi
 }
 
 static void
+Buf_AddFlag(Buffer *buf, bool flag, const char *name)
+{
+	if (flag) {
+		if (buf->len > 0)
+			Buf_AddByte(buf, '|');
+		Buf_AddBytes(buf, name, strlen(name));
+	}
+}
+
+static const char *
+SuffixFlags_ToString(SuffixFlags flags, void **freeIt)
+{
+	Buffer buf;
+
+	Buf_InitSize(&buf, 32);
+#define ADD(flag) Buf_AddFlag(&buf, (flags & (flag)) != 0, #flag)
+	ADD(SUFF_INCLUDE);
+	ADD(SUFF_LIBRARY);
+	ADD(SUFF_NULL);
+#undef ADD
+	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
+}
+
+static void
 Suffix_Print(Suffix *suff)
 {
 	debug_printf("# \"%s\" (num %d, ref %d)",
 	    suff->name, suff->sNum, suff->refCount);
 	if (suff->flags != 0) {
-		char flags_buf[SuffixFlags_ToStringSize];
-
+		void *flags_freeIt = NULL;
 		debug_printf(" (%s)",
-		    SuffixFlags_ToString(flags_buf, suff->flags));
+		    SuffixFlags_ToString(suff->flags, &flags_freeIt));
+		free(flags_freeIt);
 	}
 	debug_printf("\n");
 

Reply via email to