Module Name: src Committed By: rillig Date: Thu Jul 2 15:14:38 UTC 2020
Modified Files: src/usr.bin/make: make.h nonints.h var.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.108 -r1.109 src/usr.bin/make/make.h cvs rdiff -u -r1.75 -r1.76 src/usr.bin/make/nonints.h cvs rdiff -u -r1.227 -r1.228 src/usr.bin/make/var.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/make.h diff -u src/usr.bin/make/make.h:1.108 src/usr.bin/make/make.h:1.109 --- src/usr.bin/make/make.h:1.108 Fri Jun 19 21:17:48 2020 +++ src/usr.bin/make/make.h Thu Jul 2 15:14:38 2020 @@ -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 str2Lst_Append(Lst, char *, const ch 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 { \ Index: src/usr.bin/make/nonints.h diff -u src/usr.bin/make/nonints.h:1.75 src/usr.bin/make/nonints.h:1.76 --- src/usr.bin/make/nonints.h:1.75 Sat Apr 25 18:20:57 2020 +++ src/usr.bin/make/nonints.h Thu Jul 2 15:14:38 2020 @@ -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(void); 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); Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.227 src/usr.bin/make/var.c:1.228 --- src/usr.bin/make/var.c:1.227 Thu Jul 2 13:04:09 2020 +++ src/usr.bin/make/var.c Thu Jul 2 15:14:38 2020 @@ -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 @@ GNode *VAR_CMD; /* variabl #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 @@ ApplyModifiers(char *nstr, const char *t */ /* 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 @@ Var_Parse(const char *str, GNode *ctxt, *----------------------------------------------------------------------- */ 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 */