aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-02-16 12:09:30 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-02-17 18:38:50 +0100
commitd2e557a42b55e1014219a3e0e5726fc74392a272 (patch)
tree5bef3133d4b5e266b215a86d2e6501070cdbb541
parent54985f5b2db36aff19255fd196032bd068caa2ba (diff)
downloadsparse-dev-d2e557a42b55e1014219a3e0e5726fc74392a272.tar.gz
builtin: extract eval_args() from arguments_choose()
Almost the same code is needed for others builtins, only with another function name a arguments number. So, extract this into a generic helper. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--builtin.c49
1 files changed, 30 insertions, 19 deletions
diff --git a/builtin.c b/builtin.c
index f5ea06ab..9aa109d1 100644
--- a/builtin.c
+++ b/builtin.c
@@ -51,6 +51,35 @@ static int evaluate_pure_unop(struct expression *expr)
return 1;
}
+/*
+ * eval_args - check the number of arguments and evaluate them.
+ */
+static int eval_args(struct expression *expr, int n)
+{
+ struct expression *arg;
+ struct symbol *sym;
+ const char *msg;
+ int rc = 1;
+
+ FOR_EACH_PTR(expr->args, arg) {
+ if (n-- == 0) {
+ msg = "too many arguments";
+ goto error;
+ }
+ if (!evaluate_expression(arg))
+ rc = 0;
+ } END_FOR_EACH_PTR(arg);
+ if (n > 0) {
+ msg = "not enough arguments";
+ goto error;
+ }
+ return rc;
+
+error:
+ sym = expr->fn->ctype;
+ expression_error(expr, "%s for %s", msg, show_ident(sym->ident));
+ return 0;
+}
static int evaluate_expect(struct expression *expr)
{
@@ -61,25 +90,7 @@ static int evaluate_expect(struct expression *expr)
static int arguments_choose(struct expression *expr)
{
- struct expression_list *arglist = expr->args;
- struct expression *arg;
- int i = 0;
-
- FOR_EACH_PTR (arglist, arg) {
- if (!evaluate_expression(arg))
- return 0;
- i++;
- } END_FOR_EACH_PTR(arg);
- if (i < 3) {
- sparse_error(expr->pos,
- "not enough arguments for __builtin_choose_expr");
- return 0;
- } if (i > 3) {
- sparse_error(expr->pos,
- "too many arguments for __builtin_choose_expr");
- return 0;
- }
- return 1;
+ return eval_args(expr, 3);
}
static int evaluate_choose(struct expression *expr)