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): add more type safety for enums



details:   https://anonhg.NetBSD.org/src/rev/edfd481d26e3
branches:  trunk
changeset: 935427:edfd481d26e3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Jul 02 15:14:38 2020 +0000

description:
make(1): add more type safety for enums

There are several types of flags for variables, and these cannot be
mixed.  To prevent accidental typos, these are defined in separate enum
types.  Clang warns about direct assignments between distinct types, but
not about mixing distinct types in binary expressions like A | B.  GCC
does not warn at all.

diffstat:

 usr.bin/make/make.h    |   6 +-----
 usr.bin/make/nonints.h |  13 ++++++++++---
 usr.bin/make/var.c     |  44 +++++++++++++++++++++++---------------------
 3 files changed, 34 insertions(+), 29 deletions(-)

diffs (140 lines):

diff -r 4860b66b2e52 -r edfd481d26e3 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Thu Jul 02 14:04:00 2020 +0000
+++ b/usr.bin/make/make.h       Thu Jul 02 15:14:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.108 2020/06/19 21:17:48 sjg Exp $   */
+/*     $NetBSD: make.h,v 1.109 2020/07/02 15:14:38 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -496,10 +496,6 @@
 int cached_lstat(const char *, void *);
 int cached_stat(const char *, void *);
 
-#define        VARF_UNDEFERR   1
-#define        VARF_WANTRES    2
-#define        VARF_ASSIGN     4
-
 #ifdef __GNUC__
 #define UNCONST(ptr)   ({              \
     union __unconst {                  \
diff -r 4860b66b2e52 -r edfd481d26e3 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Thu Jul 02 14:04:00 2020 +0000
+++ b/usr.bin/make/nonints.h    Thu Jul 02 15:14:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.75 2020/04/25 18:20:57 christos Exp $    */
+/*     $NetBSD: nonints.h,v 1.76 2020/07/02 15:14:38 rillig Exp $      */
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -178,13 +178,20 @@
 void Targ_Propagate_Wait(void);
 
 /* var.c */
+
+typedef enum {
+       VARF_UNDEFERR = 1,
+       VARF_WANTRES = 2,
+       VARF_ASSIGN = 4
+} Varf_Flags;
+
 void Var_Delete(const char *, GNode *);
 void Var_Set(const char *, const char *, GNode *, int);
 void Var_Append(const char *, const char *, GNode *);
 Boolean Var_Exists(const char *, GNode *);
 char *Var_Value(const char *, GNode *, char **);
-char *Var_Parse(const char *, GNode *, int, int *, void **);
-char *Var_Subst(const char *, const char *, GNode *, int);
+char *Var_Parse(const char *, GNode *, Varf_Flags, int *, void **);
+char *Var_Subst(const char *, const char *, GNode *, Varf_Flags);
 char *Var_GetTail(const char *);
 char *Var_GetHead(const char *);
 void Var_Init(void);
diff -r 4860b66b2e52 -r edfd481d26e3 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Thu Jul 02 14:04:00 2020 +0000
+++ b/usr.bin/make/var.c        Thu Jul 02 15:14:38 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.227 2020/07/02 13:04:09 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.228 2020/07/02 15:14:38 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.227 2020/07/02 13:04:09 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.228 2020/07/02 15:14:38 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.227 2020/07/02 13:04:09 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.228 2020/07/02 15:14:38 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -195,25 +195,27 @@
 #define FIND_GLOBAL    0x2   /* look in VAR_GLOBAL as well */
 #define FIND_ENV       0x4   /* look in the environment also */
 
+typedef enum {
+       VAR_IN_USE      = 1,    /* Variable's value is currently being used.
+                                * Used to avoid endless recursion */
+       VAR_FROM_ENV    = 2,    /* Variable comes from the environment */
+       VAR_JUNK        = 4,    /* Variable is a junk variable that
+                                * should be destroyed when done with
+                                * it. Used by Var_Parse for undefined,
+                                * modified variables */
+       VAR_KEEP        = 8,    /* Variable is VAR_JUNK, but we found
+                                * a use for it in some modifier and
+                                * the value is therefore valid */
+       VAR_EXPORTED    = 16,   /* Variable is exported */
+       VAR_REEXPORT    = 32,   /* Indicate if var needs re-export.
+                                * This would be true if it contains $'s */
+       VAR_FROM_CMD    = 64    /* Variable came from command line */
+} Var_Flags;
+
 typedef struct Var {
     char          *name;       /* the variable's name */
     Buffer       val;          /* its value */
-    int                  flags;        /* miscellaneous status flags */
-#define VAR_IN_USE     1           /* Variable's value currently being used.
-                                    * Used to avoid recursion */
-#define VAR_FROM_ENV   2           /* Variable comes from the environment */
-#define VAR_JUNK       4           /* Variable is a junk variable that
-                                    * should be destroyed when done with
-                                    * it. Used by Var_Parse for undefined,
-                                    * modified variables */
-#define VAR_KEEP       8           /* Variable is VAR_JUNK, but we found
-                                    * a use for it in some modifier and
-                                    * the value is therefore valid */
-#define VAR_EXPORTED   16          /* Variable is exported */
-#define VAR_REEXPORT   32          /* Indicate if var needs re-export.
-                                    * This would be true if it contains $'s
-                                    */
-#define VAR_FROM_CMD   64          /* Variable came from command line */
+    Var_Flags    flags;        /* miscellaneous status flags */
 }  Var;
 
 /*
@@ -3758,7 +3760,7 @@
  */
 /* coverity[+alloc : arg-*4] */
 char *
-Var_Parse(const char *str, GNode *ctxt, int flags,
+Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
          int *lengthPtr, void **freePtr)
 {
     const char    *tstr;       /* Pointer into str */
@@ -4107,7 +4109,7 @@
  *-----------------------------------------------------------------------
  */
 char *
-Var_Subst(const char *var, const char *str, GNode *ctxt, int flags)
+Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags)
 {
     Buffer       buf;              /* Buffer for forming things */
     char         *val;             /* Value to substitute for a variable */



Home | Main Index | Thread Index | Old Index