Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): make list library code stricter



details:   https://anonhg.NetBSD.org/src/rev/45d90955b0c8
branches:  trunk
changeset: 942865:45d90955b0c8
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Aug 21 03:36:03 2020 +0000

description:
make(1): make list library code stricter

Up to now, the list library didn't distinguish between programming
mistakes (violations of invariants, illegal parameter values) and
actually interesting situations like "element not found in list".

The current code contains many branches for conditions that are neither
exercised by the unit tests nor by real-world usage.  There is no point
in keeping this unnecessary code.

The list functions will be migrated from their lenient variants to the
stricter variants in small parts, each function getting the S suffix
when it is made strict, to avoid any confusion about how strict a
particular function is.  When all functions have been migrated, they
will be renamed back to their original names.

While here, the comments of the functions are cleaned up since they
mention irrelevant implementation details in the API comments, as well
as "side effects" that are really main effects.

diffstat:

 usr.bin/make/compat.c |  10 +++---
 usr.bin/make/dir.c    |  10 +++---
 usr.bin/make/lst.c    |  68 ++++++++++++--------------------------------------
 usr.bin/make/lst.h    |   6 ++--
 usr.bin/make/make.c   |   8 +++---
 usr.bin/make/meta.c   |   4 +-
 usr.bin/make/suff.c   |  20 +++++++-------
 7 files changed, 46 insertions(+), 80 deletions(-)

diffs (truncated from 352 to 300 lines):

diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/compat.c
--- a/usr.bin/make/compat.c     Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/compat.c     Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $      */
+/*     $NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $      */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $";
+static char rcsid[] = "$NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)compat.c   8.2 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: compat.c,v 1.118 2020/08/01 14:47:49 rillig Exp $");
+__RCSID("$NetBSD: compat.c,v 1.119 2020/08/21 03:36:03 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -249,7 +249,7 @@
        return 0;
     }
     cmd = cmdStart;
-    Lst_Replace(cmdNode, cmdStart);
+    Lst_ReplaceS(cmdNode, cmdStart);
 
     if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
        (void)Lst_AtEnd(ENDNode->commands, cmdStart);
@@ -400,7 +400,7 @@
     free(mav);
     free(bp);
 
-    Lst_Replace(cmdNode, NULL);
+    Lst_ReplaceS(cmdNode, NULL);
 
 #ifdef USE_META
     if (useMeta) {
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/dir.c
--- a/usr.bin/make/dir.c        Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/dir.c        Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $  */
+/*     $NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)dir.c      8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: dir.c,v 1.93 2020/08/21 02:20:47 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.94 2020/08/21 03:36:03 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -429,7 +429,7 @@
 
        /* Remove old entry from openDirectories, but do not destroy. */
        ln = Lst_Member(openDirectories, dot);
-       (void)Lst_Remove(openDirectories, ln);
+       Lst_RemoveS(openDirectories, ln);
     }
 
     dot = Dir_AddDir(NULL, ".");
@@ -1719,7 +1719,7 @@
        LstNode ln;
 
        ln = Lst_Member(openDirectories, p);
-       (void)Lst_Remove(openDirectories, ln);
+       Lst_RemoveS(openDirectories, ln);
 
        Hash_DeleteTable(&p->files);
        free(p->name);
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/lst.c
--- a/usr.bin/make/lst.c        Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/lst.c        Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -32,15 +32,17 @@
  * SUCH DAMAGE.
  */
 
+#include <assert.h>
+
 #include "lst.h"
 #include "make_malloc.h"
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $";
+static char rcsid[] = "$NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: lst.c,v 1.7 2020/08/21 03:03:45 rillig Exp $");
+__RCSID("$NetBSD: lst.c,v 1.8 2020/08/21 03:36:03 rillig Exp $");
 #endif /* not lint */
 #endif
 
@@ -405,30 +407,16 @@
     return Lst_InsertAfter(l, end, d);
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Lst_Remove --
- *     Remove the given node from the given list.
- *
- * Results:
- *     SUCCESS or FAILURE.
- *
- * Side Effects:
- *     The list's firstPtr will be set to NULL if ln is the last
- *     node on the list. firsPtr and lastPtr will be altered if ln is
- *     either the first or last node, respectively, on the list.
- *
- *-----------------------------------------------------------------------
- */
-ReturnStatus
-Lst_Remove(Lst l, LstNode ln)
+/* Remove the given node from the given list.
+ * The datum stored in the node must be freed by the caller, if necessary. */
+void
+Lst_RemoveS(Lst l, LstNode ln)
 {
     List list = l;
     ListNode lNode = ln;
 
-    if (!LstValid(l) || !LstNodeValid(ln)) {
-       return FAILURE;
-    }
+    assert(LstValid(l));
+    assert(LstNodeValid(ln));
 
     /*
      * unlink it from the list
@@ -473,32 +461,13 @@
     } else {
        lNode->deleted = TRUE;
     }
-
-    return SUCCESS;
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Lst_Replace --
- *     Replace the datum in the given node with the new datum
- *
- * Results:
- *     SUCCESS or FAILURE.
- *
- * Side Effects:
- *     The datum field fo the node is altered.
- *
- *-----------------------------------------------------------------------
- */
-ReturnStatus
-Lst_Replace(LstNode ln, void *d)
+/* Replace the datum in the given node with the new datum. */
+void
+Lst_ReplaceS(LstNode ln, void *d)
 {
-    if (ln == NULL) {
-       return FAILURE;
-    } else {
-       (ln)->datum = d;
-       return SUCCESS;
-    }
+    ln->datum = d;
 }
 
 
@@ -1080,9 +1049,6 @@
     }
 
     rd = tln->datum;
-    if (Lst_Remove(l, tln) == FAILURE) {
-       return NULL;
-    } else {
-       return rd;
-    }
+    Lst_RemoveS(l, tln);
+    return rd;
 }
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/lst.h
--- a/usr.bin/make/lst.h        Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/lst.h        Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lst.h,v 1.23 2020/08/21 02:56:25 rillig Exp $  */
+/*     $NetBSD: lst.h,v 1.24 2020/08/21 03:36:03 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -122,9 +122,9 @@
 /* Place an element at the end of a lst. */
 ReturnStatus   Lst_AtEnd(Lst, void *);
 /* Remove an element */
-ReturnStatus   Lst_Remove(Lst, LstNode);
+void           Lst_RemoveS(Lst, LstNode);
 /* Replace a node with a new value */
-ReturnStatus   Lst_Replace(LstNode, void *);
+void           Lst_ReplaceS(LstNode, void *);
 /* Concatenate two lists */
 ReturnStatus   Lst_Concat(Lst, Lst, int);
 
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/make.c
--- a/usr.bin/make/make.c       Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/make.c       Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $        */
+/*     $NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $";
+static char rcsid[] = "$NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)make.c     8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: make.c,v 1.104 2020/08/21 02:20:47 rillig Exp $");
+__RCSID("$NetBSD: make.c,v 1.105 2020/08/21 03:36:03 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -548,7 +548,7 @@
      * whether to queue the parent or examine its children...
      */
     if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
-       Lst_Remove(pgn->children, ln);
+       Lst_RemoveS(pgn->children, ln);
        pgn->unmade--;
     }
     return 0;
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/meta.c
--- a/usr.bin/make/meta.c       Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/meta.c       Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.93 2020/08/21 02:20:47 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.94 2020/08/21 03:36:03 rillig Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -1341,7 +1341,7 @@
                                nln = Lst_FindFrom(missingFiles, Lst_Succ(ln),
                                                   p, path_match);
                                tp = Lst_Datum(ln);
-                               Lst_Remove(missingFiles, ln);
+                               Lst_RemoveS(missingFiles, ln);
                                free(tp);
                            } while ((ln = nln) != NULL);
                        }
diff -r b4869e0e928b -r 45d90955b0c8 usr.bin/make/suff.c
--- a/usr.bin/make/suff.c       Fri Aug 21 03:13:30 2020 +0000
+++ b/usr.bin/make/suff.c       Fri Aug 21 03:36:03 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $ */
+/*     $NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $";
+static char rcsid[] = "$NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)suff.c     8.4 (Berkeley) 3/21/94";
 #else
-__RCSID("$NetBSD: suff.c,v 1.97 2020/08/21 02:20:48 rillig Exp $");
+__RCSID("$NetBSD: suff.c,v 1.98 2020/08/21 03:36:03 rillig Exp $");



Home | Main Index | Thread Index | Old Index