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 Dir_MakeFlags simpler



details:   https://anonhg.NetBSD.org/src/rev/861917e0b4d8
branches:  trunk
changeset: 1012840:861917e0b4d8
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Aug 12 03:05:57 2020 +0000

description:
make(1): make Dir_MakeFlags simpler

This avoids unnecessary string allocations, especially for long lists.

There is no unit test to cover this code.  Since this is an obscure,
undocumented part of make, I wasn't able to come up with a unit test.
Therefore I resorted to write a separate testing program to verify the
expected results.

$ cat <<'EOF' >dir_test.c
#include <stdio.h>

#include "make.h"
#include "dir.h"

int main(int argc, char **argv)
{
    int i;
    Lst lst;
    char *flags;

    lst = Lst_Init(FALSE);
    for (i = 1; i < argc; i++)
        Dir_AddDir(lst, argv[i]);
    flags = Dir_MakeFlags("-I", lst);

    printf("%s\n", flags);
    free(flags);
    return 0;
}
EOF

# Needs some trivial patches to Makefile and main.c
$ make PROG=dir_test EXTRA_SRCS=dir_test.c EXTRA_CPPFLAGS=-DNO_MAIN \
       COPTS.main.c=-Wno-unused-function NOMAN=1

$ ./dir_test a b c

$ mkdir a b c
$ ./dir_test a b c
 -Ia -Ib -Ic
$ rmdir a b c

diffstat:

 usr.bin/make/dir.c |  24 ++++++++++--------------
 1 files changed, 10 insertions(+), 14 deletions(-)

diffs (60 lines):

diff -r 9305b8031b4e -r 861917e0b4d8 usr.bin/make/dir.c
--- a/usr.bin/make/dir.c        Tue Aug 11 19:46:56 2020 +0000
+++ b/usr.bin/make/dir.c        Wed Aug 12 03:05:57 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $  */
+/*     $NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 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.87 2020/08/10 19:53:19 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 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.87 2020/08/10 19:53:19 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1723,26 +1723,22 @@
 char *
 Dir_MakeFlags(const char *flag, Lst path)
 {
-    char *str;                 /* the string which will be returned */
-    char *s1, *s2;             /* the current directory preceded by 'flag' */
+    Buffer buf;
     LstNode ln;                        /* the node of the current directory */
-    Path *p;                   /* the structure describing the current
-                                * directory */
 
-    str = bmake_strdup("");
+    Buf_Init(&buf, 0);
 
     if (Lst_Open(path) == SUCCESS) {
        while ((ln = Lst_Next(path)) != NULL) {
-           p = (Path *)Lst_Datum(ln);
-           s2 = str_concat2(flag, p->name);
-           str = str_concat3(s1 = str, " ", s2);
-           free(s1);
-           free(s2);
+           Path *p = (Path *)Lst_Datum(ln);
+           Buf_AddStr(&buf, " ");
+           Buf_AddStr(&buf, flag);
+           Buf_AddStr(&buf, p->name);
        }
        Lst_Close(path);
     }
 
-    return str;
+    return Buf_Destroy(&buf, FALSE);
 }
 
 /*-



Home | Main Index | Thread Index | Old Index