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): reduce scope of variables



details:   https://anonhg.NetBSD.org/src/rev/e308b7ad7beb
branches:  trunk
changeset: 936731:e308b7ad7beb
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 02 18:23:00 2020 +0000

description:
make(1): reduce scope of variables

This groups the variables by topic and only introduces them when they
are actually needed.  The compiler doesn't care, but during a debugging
session this means fewer uninitialized variables.

In VarUniq, the variable i has been split into two.  All others remain
exactly as before, just with a smaller scope.

diffstat:

 usr.bin/make/var.c |  178 ++++++++++++++++++++++++----------------------------
 1 files changed, 81 insertions(+), 97 deletions(-)

diffs (truncated from 537 to 300 lines):

diff -r 26e62208aefe -r e308b7ad7beb usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Aug 02 17:10:54 2020 +0000
+++ b/usr.bin/make/var.c        Sun Aug 02 18:23:00 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.400 2020/08/02 17:10:54 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.401 2020/08/02 18:23:00 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.400 2020/08/02 17:10:54 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.401 2020/08/02 18:23:00 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.400 2020/08/02 17:10:54 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.401 2020/08/02 18:23:00 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -468,9 +468,6 @@
 Var_Export1(const char *name, VarExportFlags flags)
 {
     char tmp[BUFSIZ];
-    Var *v;
-    char *val = NULL;
-    int n;
     VarExportFlags parent = flags & VAR_EXPORT_PARENT;
 
     if (*name == '.')
@@ -490,12 +487,15 @@
            return 0;
        }
     }
-    v = VarFind(name, VAR_GLOBAL, 0);
+
+    Var *v = VarFind(name, VAR_GLOBAL, 0);
     if (v == NULL)
        return 0;
+
     if (!parent && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
        return 0;               /* nothing to do */
-    val = Buf_GetAllZ(&v->val, NULL);
+
+    char *val = Buf_GetAllZ(&v->val, NULL);
     if (!(flags & VAR_EXPORT_LITERAL) && strchr(val, '$')) {
        if (parent) {
            /*
@@ -513,7 +513,7 @@
             */
            return 0;
        }
-       n = snprintf(tmp, sizeof(tmp), "${%s}", name);
+       int n = snprintf(tmp, sizeof(tmp), "${%s}", name);
        if (n < (int)sizeof(tmp)) {
            val = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
            setenv(name, val, 1);
@@ -547,15 +547,13 @@
 void
 Var_ExportVars(void)
 {
-    char tmp[BUFSIZ];
-    char *val;
-
     /*
      * Several make's support this sort of mechanism for tracking
      * recursion - but each uses a different name.
      * We allow the makefiles to update MAKELEVEL and ensure
      * children see a correctly incremented value.
      */
+    char tmp[BUFSIZ];
     snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
     setenv(MAKE_LEVEL_ENV, tmp, 1);
 
@@ -568,7 +566,7 @@
        return;
     }
 
-    val = Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, VARE_WANTRES);
+    char *val = Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, VARE_WANTRES);
     if (*val) {
        char **av;
        char *as;
@@ -591,18 +589,12 @@
 void
 Var_Export(char *str, int isExport)
 {
-    char **av;
-    char *as;
-    VarExportFlags flags;
-    int ac;
-    int i;
-
     if (isExport && (!str || !str[0])) {
        var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
        return;
     }
 
-    flags = 0;
+    VarExportFlags flags = 0;
     if (strncmp(str, "-env", 4) == 0) {
        str += 4;
     } else if (strncmp(str, "-literal", 8) == 0) {
@@ -614,7 +606,11 @@
 
     char *val = Var_Subst(str, VAR_GLOBAL, VARE_WANTRES);
     if (*val) {
-       av = brk_string(val, &ac, FALSE, &as);
+       char *as;
+       int ac;
+       char **av = brk_string(val, &ac, FALSE, &as);
+
+       int i;
        for (i = 0; i < ac; i++) {
            const char *name = av[i];
            if (!name[1]) {
@@ -660,13 +656,12 @@
     char tmp[BUFSIZ];
     char *vlist;
     char *cp;
-    Boolean unexport_env;
     int n;
 
     vlist = NULL;
 
     str += strlen("unexport");
-    unexport_env = strncmp(str, "-env", 4) == 0;
+    Boolean unexport_env = strncmp(str, "-env", 4) == 0;
     if (unexport_env) {
        char **newenv;
 
@@ -748,7 +743,6 @@
 Var_Set_with_flags(const char *name, const char *val, GNode *ctxt,
                   VarSet_Flags flags)
 {
-    Var *v;
     char *name_freeIt = NULL;
 
     /*
@@ -770,6 +764,7 @@
        }
     }
 
+    Var *v;
     if (ctxt == VAR_GLOBAL) {
        v = VarFind(name, VAR_CMD, 0);
        if (v != NULL) {
@@ -895,8 +890,6 @@
 void
 Var_Append(const char *name, const char *val, GNode *ctxt)
 {
-    Var *v;
-    Hash_Entry *h;
     char *expanded_name = NULL;
 
     if (strchr(name, '$') != NULL) {
@@ -913,7 +906,7 @@
        name = expanded_name;
     }
 
-    v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD | FIND_ENV) : 0);
+    Var *v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD | FIND_ENV) : 0);
 
     if (v == NULL) {
        Var_Set(name, val, ctxt);
@@ -934,7 +927,7 @@
             * export other variables...)
             */
            v->flags &= ~VAR_FROM_ENV;
-           h = Hash_CreateEntry(&ctxt->context, name, NULL);
+           Hash_Entry *h = Hash_CreateEntry(&ctxt->context, name, NULL);
            Hash_SetValue(h, v);
        }
     }
@@ -1307,11 +1300,8 @@
 static void
 VarREError(int reerr, regex_t *pat, const char *str)
 {
-    char *errbuf;
-    int errlen;
-
-    errlen = regerror(reerr, pat, 0, 0);
-    errbuf = bmake_malloc(errlen);
+    int errlen = regerror(reerr, pat, 0, 0);
+    char *errbuf = bmake_malloc(errlen);
     regerror(reerr, pat, errbuf, errlen);
     Error("%s: %s", str, errbuf);
     free(errbuf);
@@ -1451,13 +1441,11 @@
               int last)
 {
     SepBuf buf;
+    SepBuf_Init(&buf, sep);
+
     char **av;                 /* word list */
     char *as;                  /* word list memory */
-    int ac, i;
-    int start, end, step;
-
-    SepBuf_Init(&buf, sep);
-
+    int ac;
     if (oneBigWord) {
        /* fake what brk_string() would do if there were only one word */
        ac = 1;
@@ -1482,6 +1470,7 @@
     /*
      * We avoid scanning more of the list than we need to.
      */
+    int start, end, step;
     if (first > last) {
        start = MIN(ac, first) - 1;
        end = MAX(0, last - 1);
@@ -1492,6 +1481,7 @@
        step = 1;
     }
 
+    int i;
     for (i = start; (step < 0) == (i >= end); i += step) {
        SepBuf_AddStr(&buf, av[i]);
        SepBuf_Sep(&buf);
@@ -1544,12 +1534,11 @@
     }
 
     SepBuf result;
+    SepBuf_Init(&result, sep);
+
     char **av;                 /* word list */
     char *as;                  /* word list memory */
-    int ac, i;
-
-    SepBuf_Init(&result, sep);
-
+    int ac;
     av = brk_string(str, &ac, FALSE, &as);
 
     if (DEBUG(VAR)) {
@@ -1557,6 +1546,7 @@
                str, ac);
     }
 
+    int i;
     for (i = 0; i < ac; i++) {
        modifyWord(av[i], &result, data);
        if (result.buf.count > 0)
@@ -1573,15 +1563,13 @@
 static int
 VarWordCompare(const void *a, const void *b)
 {
-    int r = strcmp(*(const char * const *)a, *(const char * const *)b);
-    return r;
+    return strcmp(*(const char * const *)a, *(const char * const *)b);
 }
 
 static int
 VarWordCompareReverse(const void *a, const void *b)
 {
-    int r = strcmp(*(const char * const *)b, *(const char * const *)a);
-    return r;
+    return strcmp(*(const char * const *)b, *(const char * const *)a);
 }
 
 /*-
@@ -1601,12 +1589,11 @@
 VarOrder(const char *str, const char otype)
 {
     Buffer buf;                        /* Buffer for the new string */
+    Buf_InitZ(&buf, 0);
+
     char **av;                 /* word list */
     char *as;                  /* word list memory */
-    int ac, i;
-
-    Buf_InitZ(&buf, 0);
-
+    int ac;
     av = brk_string(str, &ac, FALSE, &as);
 
     if (ac > 0) {
@@ -1624,6 +1611,8 @@
             * reasonable value for mod factor is 2 (the mod 1 will produce
             * 0 with probability 1).
             */
+           (void)0;
+           int i;
            for (i = ac - 1; i > 0; i--) {
                int rndidx = random() % (i + 1);
                char *t = av[i];
@@ -1633,6 +1622,7 @@
        }
     }
 
+    int i;
     for (i = 0; i < ac; i++) {



Home | Main Index | Thread Index | Old Index