[RFC PATCH 06/28] cli: lil: Convert some defines to enums
Sean Anderson
seanga2 at gmail.com
Thu Jul 1 08:15:49 CEST 2021
This converts some defines to enums. This allows them to be documented more
easily, and helps the compiler give better errors for switch statements.
Signed-off-by: Sean Anderson <seanga2 at gmail.com>
---
common/cli_lil.c | 44 +++++++++++++++++++++++---------------------
include/cli_lil.h | 12 +++++++-----
2 files changed, 30 insertions(+), 26 deletions(-)
diff --git a/common/cli_lil.c b/common/cli_lil.c
index a6c77ee19c..3d1e6181f9 100644
--- a/common/cli_lil.c
+++ b/common/cli_lil.c
@@ -24,10 +24,6 @@
* overflows and is also useful when running through an automated fuzzer like AFL */
/*#define LIL_ENABLE_RECLIMIT 10000*/
-#define ERROR_NOERROR 0
-#define ERROR_DEFAULT 1
-#define ERROR_FIXHEAD 2
-
#define CALLBACKS 8
#define HASHMAP_CELLS 256
#define HASHMAP_CELLMASK 0xFF
@@ -97,7 +93,11 @@ struct lil {
struct lil_env *rootenv;
struct lil_env *downenv;
struct lil_value *empty;
- int error;
+ enum {
+ ERROR_NOERROR = 0,
+ ERROR_DEFAULT,
+ ERROR_FIXHEAD,
+ } error;
size_t err_head;
char *err_msg;
lil_callback_proc_t callback[CALLBACKS];
@@ -108,7 +108,12 @@ struct expreval {
const char *code;
size_t len, head;
ssize_t ival;
- int error;
+ enum {
+ EERR_NO_ERROR = 0,
+ EERR_SYNTAX_ERROR,
+ EERR_DIVISION_BY_ZERO,
+ EERR_INVALID_EXPRESSION,
+ } error;
};
static struct lil_value *next_word(struct lil *lil);
@@ -645,7 +650,7 @@ int lil_register(struct lil *lil, const char *name, lil_func_proc_t proc)
}
struct lil_var *lil_set_var(struct lil *lil, const char *name,
- struct lil_value *val, int local)
+ struct lil_value *val, enum lil_setvar local)
{
struct lil_var **nvar;
struct lil_env *env =
@@ -1222,11 +1227,6 @@ int lil_error(struct lil *lil, const char **msg, size_t *pos)
return 1;
}
-#define EERR_NO_ERROR 0
-#define EERR_SYNTAX_ERROR 1
-#define EERR_DIVISION_BY_ZERO 3
-#define EERR_INVALID_EXPRESSION 4
-
static void ee_expr(struct expreval *ee);
static int ee_invalidpunct(int ch)
@@ -1673,16 +1673,18 @@ struct lil_value *lil_eval_expr(struct lil *lil, struct lil_value *code)
ee_expr(&ee);
lil_free_value(code);
- if (ee.error) {
- switch (ee.error) {
- case EERR_DIVISION_BY_ZERO:
- lil_set_error(lil, "division by zero in expression");
- break;
- case EERR_SYNTAX_ERROR:
- lil_set_error(lil, "expression syntax error");
- break;
- }
+ switch (ee.error) {
+ case EERR_DIVISION_BY_ZERO:
+ lil_set_error(lil, "division by zero in expression");
return NULL;
+ case EERR_SYNTAX_ERROR:
+ lil_set_error(lil, "expression syntax error");
+ return NULL;
+ case EERR_INVALID_EXPRESSION:
+ lil_set_error(lil, "invalid expression");
+ return NULL;
+ case EERR_NO_ERROR:
+ break;
}
return lil_alloc_integer(ee.ival);
}
diff --git a/include/cli_lil.h b/include/cli_lil.h
index c72977ea5c..48735e0605 100644
--- a/include/cli_lil.h
+++ b/include/cli_lil.h
@@ -13,10 +13,12 @@
#define LIL_VERSION_STRING "0.1"
-#define LIL_SETVAR_GLOBAL 0
-#define LIL_SETVAR_LOCAL 1
-#define LIL_SETVAR_LOCAL_NEW 2
-#define LIL_SETVAR_LOCAL_ONLY 3
+enum lil_setvar {
+ LIL_SETVAR_GLOBAL = 0,
+ LIL_SETVAR_LOCAL,
+ LIL_SETVAR_LOCAL_NEW,
+ LIL_SETVAR_LOCAL_ONLY,
+};
#define LIL_CALLBACK_EXIT 0
#define LIL_CALLBACK_WRITE 1
@@ -98,7 +100,7 @@ struct lil_env *lil_push_env(struct lil *lil);
void lil_pop_env(struct lil *lil);
struct lil_var *lil_set_var(struct lil *lil, const char *name,
- struct lil_value *val, int local);
+ struct lil_value *val, enum lil_setvar local);
struct lil_value *lil_get_var(struct lil *lil, const char *name);
struct lil_value *lil_get_var_or(struct lil *lil, const char *name,
struct lil_value *defvalue);
--
2.32.0
More information about the U-Boot
mailing list