Source-Changes-HG archive

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

[.joined/src/trunk]: .joined/src/usr.bin/make make: clean up handling of .for...



details:   https://anonhg.NetBSD.org/.joined/src/rev/538f000926e5
branches:  trunk
changeset: 359395:538f000926e5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 02 01:54:43 2022 +0000

description:
make: clean up handling of .for loops and .include directives

No functional change.

diffstat:

 usr.bin/make/for.c     |   37 ++++-------
 usr.bin/make/nonints.h |    8 +-
 usr.bin/make/parse.c   |  152 ++++++++++++------------------------------------
 3 files changed, 55 insertions(+), 142 deletions(-)

diffs (truncated from 412 to 300 lines):

diff -r 090552d18b1b -r 538f000926e5 usr.bin/make/for.c
--- a/usr.bin/make/for.c        Sun Jan 02 01:35:31 2022 +0000
+++ b/usr.bin/make/for.c        Sun Jan 02 01:54:43 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: for.c,v 1.153 2022/01/02 00:12:47 rillig Exp $ */
+/*     $NetBSD: for.c,v 1.154 2022/01/02 01:54:43 rillig Exp $ */
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -58,7 +58,7 @@
 #include "make.h"
 
 /*     "@(#)for.c      8.1 (Berkeley) 6/6/93"  */
-MAKE_RCSID("$NetBSD: for.c,v 1.153 2022/01/02 00:12:47 rillig Exp $");
+MAKE_RCSID("$NetBSD: for.c,v 1.154 2022/01/02 01:54:43 rillig Exp $");
 
 
 /* One of the variables to the left of the "in" in a .for loop. */
@@ -71,7 +71,6 @@
        Buffer body;            /* Unexpanded body of the loop */
        Vector /* of ForVar */ vars; /* Iteration variables */
        SubstringWords items;   /* Substitution items */
-       Buffer curBody;         /* Expanded body of the current iteration */
        unsigned int nextItem;  /* Where to continue iterating */
 } ForLoop;
 
@@ -88,7 +87,6 @@
        Buf_Init(&f->body);
        Vector_Init(&f->vars, sizeof(ForVar));
        SubstringWords_Init(&f->items);
-       Buf_Init(&f->curBody);
        f->nextItem = 0;
 
        return f;
@@ -106,7 +104,6 @@
        Vector_Done(&f->vars);
 
        SubstringWords_Free(f->items);
-       Buf_Done(&f->curBody);
 
        free(f);
 }
@@ -484,40 +481,32 @@
  * Compute the body for the current iteration by copying the unexpanded body,
  * replacing the expressions for the iteration variables on the way.
  */
-static char *
-ForReadMore(void *v_arg, size_t *out_len)
+bool
+For_NextIteration(ForLoop *f, Buffer *body)
 {
-       ForLoop *f = v_arg;
-
        if (f->nextItem == f->items.len) {
                /* No more iterations */
                ForLoop_Free(f);
-               return NULL;
+               return false;
        }
 
-       ForLoop_SubstBody(f, &f->curBody);
-       DEBUG1(FOR, "For: loop body:\n%s", f->curBody.data);
+       ForLoop_SubstBody(f, body);
+       DEBUG1(FOR, "For: loop body:\n%s", body->data);
        f->nextItem += (unsigned int)f->vars.len;
-
-       *out_len = f->curBody.len;
-       return f->curBody.data;
+       return true;
 }
 
 /* Run the .for loop, imitating the actions of an include file. */
 void
 For_Run(int lineno)
 {
+       Buffer buf;
        ForLoop *f = accumFor;
        accumFor = NULL;
 
-       if (f->items.len == 0) {
-               /*
-                * Nothing to expand - possibly due to an earlier syntax
-                * error.
-                */
+       if (f->items.len > 0) {
+               Buf_Init(&buf);
+               Parse_PushInput(NULL, lineno, buf, f);
+       } else
                ForLoop_Free(f);
-               return;
-       }
-
-       Parse_PushInput(NULL, lineno, ForReadMore, f);
 }
diff -r 090552d18b1b -r 538f000926e5 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Sun Jan 02 01:35:31 2022 +0000
+++ b/usr.bin/make/nonints.h    Sun Jan 02 01:54:43 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.228 2022/01/01 21:50:29 rillig Exp $     */
+/*     $NetBSD: nonints.h,v 1.229 2022/01/02 01:54:43 rillig Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -116,9 +116,11 @@
 void SearchPath_Free(SearchPath *);
 
 /* for.c */
+struct ForLoop;
 int For_Eval(const char *) MAKE_ATTR_USE;
 bool For_Accum(const char *) MAKE_ATTR_USE;
 void For_Run(int);
+bool For_NextIteration(struct ForLoop *, Buffer *);
 
 /* job.c */
 void JobReapChild(pid_t, int, bool);
@@ -141,13 +143,11 @@
 void Parse_Init(void);
 void Parse_End(void);
 
-typedef char *(*ReadMoreProc)(void *, size_t *);
-
 void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
 bool Parse_VarAssign(const char *, bool, GNode *) MAKE_ATTR_USE;
 void Parse_AddIncludeDir(const char *);
 void Parse_File(const char *, int);
-void Parse_PushInput(const char *, int, ReadMoreProc, void *);
+void Parse_PushInput(const char *, int, Buffer, struct ForLoop *);
 void Parse_MainName(GNodeList *);
 int Parse_NumErrors(void) MAKE_ATTR_USE;
 
diff -r 090552d18b1b -r 538f000926e5 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sun Jan 02 01:35:31 2022 +0000
+++ b/usr.bin/make/parse.c      Sun Jan 02 01:54:43 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.615 2022/01/02 00:07:20 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.616 2022/01/02 01:54:43 rillig Exp $       */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -110,7 +110,7 @@
 #include "pathnames.h"
 
 /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.615 2022/01/02 00:07:20 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.616 2022/01/02 01:54:43 rillig Exp $");
 
 /* types and constants */
 
@@ -119,6 +119,7 @@
  */
 typedef struct IFile {
        FStr name;              /* absolute or relative to the cwd */
+       /* TODO: merge with forLoop */
        bool fromForLoop;       /* simulated .include by the .for loop */
        int lineno;             /* current line number in file */
        int first_lineno;       /* line number of start of text */
@@ -126,19 +127,14 @@
        bool depending; /* state of doing_depend on EOF */
 
        /*
-        * The buffer from which the file's content is read.  The buffer
-        * always ends with '\n', the buffer is not null-terminated, that is,
-        * buf_end[0] is already out of bounds.
+        * The buffer from which the file's content or the body of the .for
+        * loop is read.  The buffer always ends with '\n'.
         */
-       char *buf_freeIt;
+       Buffer buf;
        char *buf_ptr;          /* next char to be read */
        char *buf_end;          /* buf_end[-1] == '\n' */
 
-       /* Function to read more data, with a single opaque argument. */
-       ReadMoreProc readMore;
-       void *readMoreArg;
-
-       struct loadedfile *lf;  /* loadedfile object, if any */
+       struct ForLoop *forLoop;
 } IFile;
 
 /*
@@ -315,52 +311,8 @@
     { ".WAIT",         SP_WAIT,        OP_NONE },
 };
 
-/* file loader */
-
-struct loadedfile {
-       char *buf;              /* contents buffer, not null-terminated */
-       size_t len;             /* length of contents */
-       bool used;              /* XXX: have we used the data yet */
-};
-
-/* XXX: What is the lifetime of the path? Who manages the memory? */
-static struct loadedfile *
-loadedfile_create(char *buf, size_t buflen)
-{
-       struct loadedfile *lf;
-
-       lf = bmake_malloc(sizeof *lf);
-       lf->buf = buf;
-       lf->len = buflen;
-       lf->used = false;
-       return lf;
-}
-
-static void
-loadedfile_destroy(struct loadedfile *lf)
-{
-       free(lf->buf);
-       free(lf);
-}
-
-/*
- * readMore() operation for loadedfile, as needed by the weird and twisted
- * logic below. Once that's cleaned up, we can get rid of lf->used.
- */
-static char *
-loadedfile_readMore(void *x, size_t *len)
-{
-       struct loadedfile *lf = x;
-
-       if (lf->used)
-               return NULL;
-
-       lf->used = true;
-       *len = lf->len;
-       return lf->buf;
-}
-
-static struct loadedfile *
+
+static Buffer
 loadfile(const char *path, int fd)
 {
        ssize_t n;
@@ -398,7 +350,7 @@
        if (!Buf_EndsWith(&buf, '\n'))
                Buf_AddByte(&buf, '\n');
 
-       return loadedfile_create(buf.data, buf.len);
+       return buf;
 }
 
 static void
@@ -1936,7 +1888,7 @@
 static void
 IncludeFile(const char *file, bool isSystem, bool depinc, bool silent)
 {
-       struct loadedfile *lf;
+       Buffer buf;
        char *fullname;         /* full pathname of file */
        char *newName;
        char *slash, *incdir;
@@ -2029,13 +1981,10 @@
                return;
        }
 
-       /* load it */
-       lf = loadfile(fullname, fd);
+       buf = loadfile(fullname, fd);
        (void)close(fd);
 
-       /* Start reading from this file next */
-       Parse_PushInput(fullname, 0, loadedfile_readMore, lf);
-       CurFile()->lf = lf;
+       Parse_PushInput(fullname, 0, buf, NULL);
        if (depinc)
                doing_depend = depinc;  /* only turn it on */
        free(fullname);
@@ -2215,12 +2164,10 @@
  * The given file is added to the includes stack.
  */
 void
-Parse_PushInput(const char *name, int lineno,
-              ReadMoreProc readMore, void *readMoreArg)
+Parse_PushInput(const char *name, int lineno, Buffer buf,
+               struct ForLoop *forLoop)
 {
        IFile *curFile;
-       char *buf;
-       size_t len;
        bool fromForLoop = name == NULL;
 
        if (fromForLoop)
@@ -2229,7 +2176,7 @@
                TrackInput(name);
 
        DEBUG3(PARSE, "Parse_PushInput: %s %s, line %d\n",
-           readMore == loadedfile_readMore ? "file" : ".for loop in",
+           !fromForLoop ? "file" : ".for loop in",
            name, lineno);
 
        curFile = Vector_Push(&includes);
@@ -2237,25 +2184,15 @@
        curFile->fromForLoop = fromForLoop;
        curFile->lineno = lineno;
        curFile->first_lineno = lineno;
-       curFile->readMore = readMore;
-       curFile->readMoreArg = readMoreArg;
-       curFile->lf = NULL;



Home | Main Index | Thread Index | Old Index