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: convert VarEvalFlags back into an enum, b...



details:   https://anonhg.NetBSD.org/src/rev/093602ae9ce6
branches:  trunk
changeset: 954298:093602ae9ce6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Apr 04 11:56:43 2021 +0000

description:
make: convert VarEvalFlags back into an enum, but not a bit-set

As was apparent in VarEvalFlags_ToString, a bit-set was not the best
data type since most of the flags were not freely combinable.  The two
flags that could be combined were keepDollar and keepUndef, but even
these have distinguished names in the debug log.

The downside of struct bit-fields is that they need extra helper
functions in C90 (see nonints.h).  Exchange these for a few helper
functions in var.c, to keep the code outside var.c simple.

No functional change.

diffstat:

 usr.bin/make/cond.c    |   14 +-
 usr.bin/make/nonints.h |   71 +++++----------
 usr.bin/make/parse.c   |   12 +-
 usr.bin/make/var.c     |  209 ++++++++++++++++++++++++++----------------------
 4 files changed, 151 insertions(+), 155 deletions(-)

diffs (truncated from 812 to 300 lines):

diff -r 0b5f4d875f81 -r 093602ae9ce6 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Sun Apr 04 11:47:54 2021 +0000
+++ b/usr.bin/make/cond.c       Sun Apr 04 11:56:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.260 2021/04/03 11:08:40 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.261 2021/04/04 11:56:43 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,7 +95,7 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.260 2021/04/03 11:08:40 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.261 2021/04/04 11:56:43 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -264,11 +264,11 @@
                         * so we don't need to do it. Nor do we return an
                         * error, though perhaps we should.
                         */
-                       VarEvalFlags eflags = doEval
+                       VarEvalMode emode = doEval
                            ? VARE_UNDEFERR
                            : VARE_PARSE_ONLY;
                        FStr nestedVal;
-                       (void)Var_Parse(&p, SCOPE_CMDLINE, eflags, &nestedVal);
+                       (void)Var_Parse(&p, SCOPE_CMDLINE, emode, &nestedVal);
                        /* TODO: handle errors */
                        Buf_AddStr(&argBuf, nestedVal.str);
                        FStr_Done(&nestedVal);
@@ -414,19 +414,19 @@
                      bool const doEval, bool const quoted,
                      Buffer *buf, FStr *const inout_str)
 {
-       VarEvalFlags eflags;
+       VarEvalMode emode;
        const char *nested_p;
        bool atStart;
        VarParseResult parseResult;
 
        /* if we are in quotes, an undefined variable is ok */
-       eflags = doEval && !quoted ? VARE_UNDEFERR
+       emode = doEval && !quoted ? VARE_UNDEFERR
            : doEval ? VARE_WANTRES
            : VARE_PARSE_ONLY;
 
        nested_p = par->p;
        atStart = nested_p == start;
-       parseResult = Var_Parse(&nested_p, SCOPE_CMDLINE, eflags, inout_str);
+       parseResult = Var_Parse(&nested_p, SCOPE_CMDLINE, emode, inout_str);
        /* TODO: handle errors */
        if (inout_str->str == var_Error) {
                if (parseResult == VPR_ERR) {
diff -r 0b5f4d875f81 -r 093602ae9ce6 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Sun Apr 04 11:47:54 2021 +0000
+++ b/usr.bin/make/nonints.h    Sun Apr 04 11:56:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.210 2021/04/04 10:13:09 rillig Exp $     */
+/*     $NetBSD: nonints.h,v 1.211 2021/04/04 11:56:43 rillig Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -318,39 +318,40 @@
 void Var_Init(void);
 void Var_End(void);
 
-typedef struct VarEvalFlags {
+typedef enum VarEvalMode {
 
        /*
-        * Expand and evaluate variables during parsing.
-        *
-        * Without this flag, the expression is only parsed, without
-        * evaluating any part of it.
+        * Only parse the expression but don't evaluate any part of it.
         *
-        * TODO: Document what Var_Parse and Var_Subst return when this flag
-        *  is not set.  As of 2021-03-15, they return unspecified,
-        *  inconsistent results.
+        * TODO: Document what Var_Parse and Var_Subst return in this mode.
+        *  As of 2021-03-15, they return unspecified, inconsistent results.
         */
-       bool wantRes: 1;
+       VARE_PARSE_ONLY,
+
+       /* Parse and evaluate the expression. */
+       VARE_WANTRES,
 
        /*
-        * Treat undefined variables as errors.
-        * Must only be used in combination with wantRes.
+        * Parse and evaluate the expression.  It is an error if a
+        * subexpression evaluates to undefined.
         */
-       bool undefErr: 1;
+       VARE_UNDEFERR,
 
        /*
-        * Keep '$$' as '$$' instead of reducing it to a single '$'.
+        * Parse and evaluate the expression.  Keep '$$' as '$$' instead of
+        * reducing it to a single '$'.  Subexpressions that evaluate to
+        * undefined expand to an empty string.
         *
         * Used in variable assignments using the ':=' operator.  It allows
         * multiple such assignments to be chained without accidentally
         * expanding '$$file' to '$file' in the first assignment and
         * interpreting it as '${f}' followed by 'ile' in the next assignment.
         */
-       bool keepDollar: 1;
+       VARE_EVAL_KEEP_DOLLAR,
 
        /*
-        * Keep undefined variables as-is instead of expanding them to an
-        * empty string.
+        * Parse and evaluate the expression.  Keep undefined variables as-is
+        * instead of expanding them to an empty string.
         *
         * Example for a ':=' assignment:
         *      CFLAGS = $(.INCLUDES)
@@ -359,36 +360,14 @@
         *      # way) is still undefined, the updated CFLAGS becomes
         *      # "-I.. $(.INCLUDES)".
         */
-       bool keepUndef: 1;
+       VARE_EVAL_KEEP_UNDEF,
 
        /*
-        * Without this padding, GCC 9.3.0 on NetBSD 9.99.80 generates larger
-        * code than necessary (1.2 kB), masking out the unused bits from the
-        * int (since that is the default representation of bool in make),
-        * even for initializers consisting entirely of constants.
+        * Parse and evaluate the expression.  Keep '$$' as '$$' and preserve
+        * undefined subexpressions.
         */
-       bool : 0;
-} VarEvalFlags;
-
-#if __STDC_VERSION__ >= 199901L
-#define VarEvalFlagsLiteral(wantRes, undefErr, keep) \
-       (VarEvalFlags) { wantRes, undefErr, keep, keep }
-#else
-MAKE_INLINE VarEvalFlags
-VarEvalFlagsLiteral(bool wantRes, bool undefErr, bool keep)
-{
-       VarEvalFlags eflags;
-       eflags.wantRes = wantRes;
-       eflags.undefErr = undefErr;
-       eflags.keepDollar = keep;
-       eflags.keepUndef = keep;
-       return eflags;
-}
-#endif
-#define VARE_PARSE_ONLY                VarEvalFlagsLiteral(false, false, false)
-#define VARE_WANTRES           VarEvalFlagsLiteral(true, false, false)
-#define VARE_UNDEFERR          VarEvalFlagsLiteral(true, true, false)
-#define VARE_KEEP_DOLLAR_UNDEF VarEvalFlagsLiteral(true, false, true)
+       VARE_KEEP_DOLLAR_UNDEF
+} VarEvalMode;
 
 typedef enum VarSetFlags {
        VAR_SET_NONE            = 0,
@@ -447,8 +426,8 @@
 bool Var_ExistsExpand(GNode *, const char *);
 FStr Var_Value(GNode *, const char *);
 const char *GNode_ValueDirect(GNode *, const char *);
-VarParseResult Var_Parse(const char **, GNode *, VarEvalFlags, FStr *);
-VarParseResult Var_Subst(const char *, GNode *, VarEvalFlags, char **);
+VarParseResult Var_Parse(const char **, GNode *, VarEvalMode, FStr *);
+VarParseResult Var_Subst(const char *, GNode *, VarEvalMode, char **);
 void Var_Stats(void);
 void Var_Dump(GNode *);
 void Var_ReexportVars(void);
diff -r 0b5f4d875f81 -r 093602ae9ce6 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sun Apr 04 11:47:54 2021 +0000
+++ b/usr.bin/make/parse.c      Sun Apr 04 11:56:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.556 2021/04/04 10:13:09 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.557 2021/04/04 11:56:43 rillig Exp $       */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -109,7 +109,7 @@
 #include "pathnames.h"
 
 /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.556 2021/04/04 10:13:09 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.557 2021/04/04 11:56:43 rillig Exp $");
 
 /* types and constants */
 
@@ -3075,13 +3075,13 @@
 }
 
 /*
- * dependency  -> target... op [source...]
+ * dependency  -> target... op [source...] [';' command]
  * op          -> ':' | '::' | '!'
  */
 static void
 ParseDependencyLine(char *line)
 {
-       VarEvalFlags eflags;
+       VarEvalMode emode;
        char *expanded_line;
        const char *shellcmd = NULL;
 
@@ -3130,8 +3130,8 @@
         * Var_Parse does not print any parse errors in such a case.
         * It simply returns the special empty string var_Error,
         * which cannot be detected in the result of Var_Subst. */
-       eflags = opts.strict ? VARE_WANTRES : VARE_UNDEFERR;
-       (void)Var_Subst(line, SCOPE_CMDLINE, eflags, &expanded_line);
+       emode = opts.strict ? VARE_WANTRES : VARE_UNDEFERR;
+       (void)Var_Subst(line, SCOPE_CMDLINE, emode, &expanded_line);
        /* TODO: handle errors */
 
        /* Need a fresh list for the target nodes */
diff -r 0b5f4d875f81 -r 093602ae9ce6 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Apr 04 11:47:54 2021 +0000
+++ b/usr.bin/make/var.c        Sun Apr 04 11:56:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.905 2021/04/04 11:47:54 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.906 2021/04/04 11:56:43 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.905 2021/04/04 11:47:54 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.906 2021/04/04 11:56:43 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -318,27 +318,15 @@
 
 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
 
-
-static const char *
-VarEvalFlags_ToString(VarEvalFlags eflags)
-{
-       if (!eflags.wantRes) {
-               assert(!eflags.undefErr);
-               assert(!eflags.keepDollar && !eflags.keepUndef);
-               return "parse-only";
-       }
-       if (eflags.undefErr) {
-               assert(!eflags.keepDollar && !eflags.keepUndef);
-               return "eval-defined";
-       }
-       if (eflags.keepDollar && eflags.keepUndef)
-               return "eval-keep-dollar-and-undefined";
-       if (eflags.keepDollar)
-               return "eval-keep-dollar";
-       if (eflags.keepUndef)
-               return "eval-keep-undefined";
-       return "eval";
-}
+static const char *VarEvalMode_Name[] = {
+       "parse-only",
+       "eval",
+       "eval-defined",
+       "eval-keep-dollar",
+       "eval-keep-undefined",
+       "eval-keep-dollar-and-undefined",
+};
+
 
 static Var *
 VarNew(FStr name, const char *value, bool fromEnv, bool readOnly)
@@ -1271,11 +1259,40 @@
        return v != NULL ? v->val.data : NULL;
 }
 
+static VarEvalMode
+VarEvalMode_WithoutKeepDollar(VarEvalMode emode)
+{
+       if (emode == VARE_KEEP_DOLLAR_UNDEF)
+               return VARE_EVAL_KEEP_UNDEF;
+       if (emode == VARE_EVAL_KEEP_DOLLAR)
+               return VARE_WANTRES;
+       return emode;
+}
+
+static VarEvalMode
+VarEvalMode_UndefOk(VarEvalMode emode)
+{
+       return emode == VARE_UNDEFERR ? VARE_WANTRES : emode;
+}
 
 static bool
-VarEvalFlags_ShouldEval(VarEvalFlags eflags)
+VarEvalMode_ShouldEval(VarEvalMode emode)
+{
+       return emode != VARE_PARSE_ONLY;
+}



Home | Main Index | Thread Index | Old Index