* [PATCH 1/9] option: add helper to parse/match command line options
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 2/9] option: rename 'struct warning' to 'struct flag' Luc Van Oostenryck
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
The goal of this helper is:
- to avoid to have to hardoce the length od the substring
- separate the suffix/option part from the whole argument
which help for error message.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 34 ++++++++++++++++++++--------------
1 file changed, 20 insertions(+), 14 deletions(-)
diff --git a/lib.c b/lib.c
index 73e9a2fe6..351da6f48 100644
--- a/lib.c
+++ b/lib.c
@@ -475,6 +475,13 @@ static void handle_arch_finalize(void)
handle_arch_msize_long_finalize();
}
+char *match_option(char *arg, const char *prefix)
+{
+ unsigned int n = strlen(prefix);
+ if (strncmp(arg, prefix, n) == 0)
+ return arg + n;
+ return NULL;
+}
static int handle_simple_switch(const char *arg, const char *name, int *flag)
{
@@ -720,11 +727,12 @@ static char **handle_switch_ftabstop(char *arg, char **next)
static char **handle_switch_fdump(char *arg, char **next)
{
- if (!strncmp(arg, "linearize", 9)) {
- arg += 9;
- if (*arg == '\0')
+ const char *opt;
+
+ if ((opt = match_option(arg, "linearize"))) {
+ if (*opt == '\0')
fdump_linearize = 1;
- else if (!strcmp(arg, "=only"))
+ else if (!strcmp(opt, "=only"))
fdump_linearize = 2;
else
goto err;
@@ -739,14 +747,15 @@ err:
static char **handle_switch_f(char *arg, char **next)
{
+ char *opt;
arg++;
- if (!strncmp(arg, "tabstop=", 8))
- return handle_switch_ftabstop(arg+8, next);
- if (!strncmp(arg, "dump-", 5))
- return handle_switch_fdump(arg+5, next);
- if (!strncmp(arg, "memcpy-max-count=", 17))
- return handle_switch_fmemcpy_max_count(arg+17, next);
+ if ((opt = match_option(arg, "tabstop=")))
+ return handle_switch_ftabstop(opt, next);
+ if ((opt = match_option(arg, "dump-")))
+ return handle_switch_fdump(opt, next);
+ if ((opt = match_option(arg, "memcpy-max-count=")))
+ return handle_switch_fmemcpy_max_count(opt, next);
/* handle switches w/ arguments above, boolean and only boolean below */
if (handle_simple_switch(arg, "mem-report", &fmem_report))
@@ -773,10 +782,7 @@ static char **handle_switch_a(char *arg, char **next)
static char **handle_switch_s(char *arg, char **next)
{
- if (!strncmp (arg, "std=", 4))
- {
- arg += 4;
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 2/9] option: rename 'struct warning' to 'struct flag'
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 1/9] option: add helper to parse/match command line options Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 3/9] option: let handle_simple_switch() handle an array of flags Luc Van Oostenryck
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
'struct warning' can be reused for flags other than warnings.
To avoid future confusion, rename it to something more general:
'struct flag' (which in its context, handling of compiler flags,
is clear enough).
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/lib.c b/lib.c
index 351da6f48..04f9de5a8 100644
--- a/lib.c
+++ b/lib.c
@@ -513,7 +513,7 @@ static char **handle_switch_o(char *arg, char **next)
return next;
}
-static const struct warning {
+static const struct flag {
const char *name;
int *flag;
} warnings[] = {
@@ -558,7 +558,7 @@ enum {
};
-static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
+static char **handle_onoff_switch(char *arg, char **next, const struct flag warnings[], int n)
{
int flag = WARNING_ON;
char *p = arg + 1;
@@ -600,7 +600,7 @@ static char **handle_switch_W(char *arg, char **next)
return next;
}
-static struct warning debugs[] = {
+static struct flag debugs[] = {
{ "entry", &dbg_entry},
{ "dead", &dbg_dead},
};
@@ -619,7 +619,7 @@ static char **handle_switch_v(char *arg, char **next)
return next;
}
-static struct warning dumps[] = {
+static struct flag dumps[] = {
{ "D", &dump_macro_defs},
};
@@ -633,7 +633,7 @@ static char **handle_switch_d(char *arg, char **next)
}
-static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
+static void handle_onoff_switch_finalize(const struct flag warnings[], int n)
{
unsigned i;
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 3/9] option: let handle_simple_switch() handle an array of flags
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 1/9] option: add helper to parse/match command line options Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 2/9] option: rename 'struct warning' to 'struct flag' Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 4/9] option: extract OPTION_NUMERIC() from handle_switch_fmemcpy_max_count() Luc Van Oostenryck
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
This was used to handle a single flag but we need something
more compact when we need to handle several flags.
So, adapt this helper so that it now takes an array of flags
instead of a single flag.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/lib.c b/lib.c
index 04f9de5a8..992fdb49a 100644
--- a/lib.c
+++ b/lib.c
@@ -483,7 +483,12 @@ char *match_option(char *arg, const char *prefix)
return NULL;
}
-static int handle_simple_switch(const char *arg, const char *name, int *flag)
+struct flag {
+ const char *name;
+ int *flag;
+};
+
+static int handle_simple_switch(const char *arg, const struct flag *flags)
{
int val = 1;
@@ -493,9 +498,11 @@ static int handle_simple_switch(const char *arg, const char *name, int *flag)
val = 0;
}
- if (strcmp(arg, name) == 0) {
- *flag = val;
- return 1;
+ for (; flags->name; flags++) {
+ if (strcmp(arg, flags->name) == 0) {
+ *flags->flag = val;
+ return 1;
+ }
}
// not handled
@@ -513,10 +520,7 @@ static char **handle_switch_o(char *arg, char **next)
return next;
}
-static const struct flag {
- const char *name;
- int *flag;
-} warnings[] = {
+static const struct flag warnings[] = {
{ "address", &Waddress },
{ "address-space", &Waddress_space },
{ "bitwise", &Wbitwise },
@@ -745,6 +749,11 @@ err:
die("error: unknown flag \"-fdump-%s\"", arg);
}
+static struct flag fflags[] = {
+ { "mem-report", &fmem_report },
+ { },
+};
+
static char **handle_switch_f(char *arg, char **next)
{
char *opt;
@@ -758,7 +767,7 @@ static char **handle_switch_f(char *arg, char **next)
return handle_switch_fmemcpy_max_count(opt, next);
/* handle switches w/ arguments above, boolean and only boolean below */
- if (handle_simple_switch(arg, "mem-report", &fmem_report))
+ if (handle_simple_switch(arg, fflags))
return next;
return next;
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 4/9] option: extract OPTION_NUMERIC() from handle_switch_fmemcpy_max_count()
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (2 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 3/9] option: let handle_simple_switch() handle an array of flags Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 5/9] option: add support for options with 'zero is infinity' Luc Van Oostenryck
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/lib.c b/lib.c
index 992fdb49a..5fda4b029 100644
--- a/lib.c
+++ b/lib.c
@@ -509,6 +509,25 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
return 0;
}
+#define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
+static int opt_##NAME(char *arg, const char *name, TYPE *ptr) \
+{ \
+ char *opt; \
+ char *end; \
+ TYPE val; \
+ \
+ if (!(opt = match_option(arg, name+2))) \
+ return 0; \
+ opt++; /* opt's last char is '=' */ \
+ val = FUNCTION(opt, &end, 0); \
+ if (*end != '\0' || end == opt) { \
+ die("error: missing argument to \"%s\"", name); \
+ } \
+ *ptr = val; \
+ return 1; \
+}
+
+
static char **handle_switch_o(char *arg, char **next)
{
if (!strcmp (arg, "o")) { // "-o foo"
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 5/9] option: add support for options with 'zero is infinity'
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (3 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 4/9] option: extract OPTION_NUMERIC() from handle_switch_fmemcpy_max_count() Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 6/9] option: add support for '-<some-option>=unlimited' Luc Van Oostenryck
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
For some options with a numerical value, it is sometimes desirable,
to easily specify we want the maximum possible value.
This patch allow to interpret '=0' as meaning the maximum value.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lib.c b/lib.c
index 5fda4b029..6352b4e93 100644
--- a/lib.c
+++ b/lib.c
@@ -509,8 +509,11 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
return 0;
}
+
+#define OPTNUM_ZERO_IS_INF 1
+
#define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
-static int opt_##NAME(char *arg, const char *name, TYPE *ptr) \
+static int opt_##NAME(char *arg, const char *name, TYPE *ptr, int flag) \
{ \
char *opt; \
char *end; \
@@ -523,6 +526,8 @@ static int opt_##NAME(char *arg, const char *name, TYPE *ptr) \
if (*end != '\0' || end == opt) { \
die("error: missing argument to \"%s\"", name); \
} \
+ if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
+ val = ~val; \
*ptr = val; \
return 1; \
}
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 6/9] option: add support for '-<some-option>=unlimited'
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (4 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 5/9] option: add support for options with 'zero is infinity' Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 7/9] option: use OPTION_NUMERIC() for handle_switch_fmemcpy_max_count() Luc Van Oostenryck
` (2 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
For some options with a numerical value, it is sometimes desirable,
when the value is used to limits something, to easily specify we
want to remove any limits.
This patch allow to use 'unlimited' for this by interpreting it as
the maximum value.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lib.c b/lib.c
index 6352b4e93..4c210d58f 100644
--- a/lib.c
+++ b/lib.c
@@ -511,6 +511,7 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
#define OPTNUM_ZERO_IS_INF 1
+#define OPTNUM_UNLIMITED 2
#define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
static int opt_##NAME(char *arg, const char *name, TYPE *ptr, int flag) \
@@ -524,6 +525,9 @@ static int opt_##NAME(char *arg, const char *name, TYPE *ptr, int flag) \
opt++; /* opt's last char is '=' */ \
val = FUNCTION(opt, &end, 0); \
if (*end != '\0' || end == opt) { \
+ if ((flag & OPTNUM_UNLIMITED) && !strcmp(opt, "unlimited")) \
+ val = ~val; \
+ else \
die("error: missing argument to \"%s\"", name); \
} \
if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 7/9] option: use OPTION_NUMERIC() for handle_switch_fmemcpy_max_count()
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (5 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 6/9] option: add support for '-<some-option>=unlimited' Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 8/9] option: constify match_option() Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 9/9] option: handle switches by table Luc Van Oostenryck
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 22 +++++-----------------
sparse.1 | 2 +-
2 files changed, 6 insertions(+), 18 deletions(-)
diff --git a/lib.c b/lib.c
index 4c210d58f..95601b002 100644
--- a/lib.c
+++ b/lib.c
@@ -536,6 +536,8 @@ static int opt_##NAME(char *arg, const char *name, TYPE *ptr, int flag) \
return 1; \
}
+OPT_NUMERIC(ullong, unsigned long long, strtoull)
+
static char **handle_switch_o(char *arg, char **next)
{
@@ -726,21 +728,6 @@ static char **handle_switch_O(char *arg, char **next)
return next;
}
-static char **handle_switch_fmemcpy_max_count(char *arg, char **next)
-{
- unsigned long long val;
- char *end;
-
- val = strtoull(arg, &end, 0);
- if (*end != '\0' || end == arg)
- die("error: missing argument to \"-fmemcpy-max-count=\"");
-
- if (val == 0)
- val = ~0ULL;
- fmemcpy_max_count = val;
- return next;
-}
-
static char **handle_switch_ftabstop(char *arg, char **next)
{
char *end;
@@ -791,8 +778,9 @@ static char **handle_switch_f(char *arg, char **next)
return handle_switch_ftabstop(opt, next);
if ((opt = match_option(arg, "dump-")))
return handle_switch_fdump(opt, next);
- if ((opt = match_option(arg, "memcpy-max-count=")))
- return handle_switch_fmemcpy_max_count(opt, next);
+ if (opt_ullong(arg, "-fmemcpy-max-count=", &fmemcpy_max_count,
+ OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED))
+ return next;
/* handle switches w/ arguments above, boolean and only boolean below */
if (handle_simple_switch(arg, fflags))
diff --git a/sparse.1 b/sparse.1
index b79c58767..bec8d6d73 100644
--- a/sparse.1
+++ b/sparse.1
@@ -369,7 +369,7 @@ Report some statistics about memory allocation used by the tool.
.TP
.B \-fmemcpy-max-count=COUNT
Set the limit for the warnings given by \fB-Wmemcpy-max-count\fR.
-A COUNT of 0, useless in itself, will effectively disable the warning.
+A COUNT of 'unlimited' or '0' will effectively disable the warning.
The default limit is 100000.
.
.TP
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 8/9] option: constify match_option()
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (6 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 7/9] option: use OPTION_NUMERIC() for handle_switch_fmemcpy_max_count() Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-08 10:10 ` [PATCH 9/9] option: handle switches by table Luc Van Oostenryck
8 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
Until now, match_option() needed a non-const argment and returned
a non-const result. Since it is no more needed, let's use const.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/lib.c b/lib.c
index 95601b002..3c9618a02 100644
--- a/lib.c
+++ b/lib.c
@@ -475,7 +475,7 @@ static void handle_arch_finalize(void)
handle_arch_msize_long_finalize();
}
-char *match_option(char *arg, const char *prefix)
+const char *match_option(const char *arg, const char *prefix)
{
unsigned int n = strlen(prefix);
if (strncmp(arg, prefix, n) == 0)
@@ -514,9 +514,9 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
#define OPTNUM_UNLIMITED 2
#define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
-static int opt_##NAME(char *arg, const char *name, TYPE *ptr, int flag) \
+static int opt_##NAME(const char *arg, const char *name, TYPE *ptr, int flag)\
{ \
- char *opt; \
+ const char *opt; \
char *end; \
TYPE val; \
\
@@ -728,7 +728,7 @@ static char **handle_switch_O(char *arg, char **next)
return next;
}
-static char **handle_switch_ftabstop(char *arg, char **next)
+static char **handle_switch_ftabstop(const char *arg, char **next)
{
char *end;
unsigned long val;
@@ -744,7 +744,7 @@ static char **handle_switch_ftabstop(char *arg, char **next)
return next;
}
-static char **handle_switch_fdump(char *arg, char **next)
+static char **handle_switch_fdump(const char *arg, char **next)
{
const char *opt;
@@ -771,7 +771,7 @@ static struct flag fflags[] = {
static char **handle_switch_f(char *arg, char **next)
{
- char *opt;
+ const char *opt;
arg++;
if ((opt = match_option(arg, "tabstop=")))
@@ -805,7 +805,7 @@ static char **handle_switch_a(char *arg, char **next)
return next;
}
-static char **handle_switch_s(char *arg, char **next)
+static char **handle_switch_s(const char *arg, char **next)
{
if ((arg = match_option(arg, "std="))) {
if (!strcmp (arg, "c89") ||
--
2.14.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 9/9] option: handle switches by table
2017-11-08 10:09 [PATCH 0/9] option parsing improvements Luc Van Oostenryck
` (7 preceding siblings ...)
2017-11-08 10:10 ` [PATCH 8/9] option: constify match_option() Luc Van Oostenryck
@ 2017-11-08 10:10 ` Luc Van Oostenryck
2017-11-09 18:42 ` Christopher Li
8 siblings, 1 reply; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-08 10:10 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
Currently, the parsing of options is often quite ad-hoc and thus:
- need a lot of code
- is not clear at all
Improve this by making this table driven.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 91 +++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 48 insertions(+), 43 deletions(-)
diff --git a/lib.c b/lib.c
index 3c9618a02..a03a94d69 100644
--- a/lib.c
+++ b/lib.c
@@ -483,13 +483,18 @@ const char *match_option(const char *arg, const char *prefix)
return NULL;
}
+
+#define OPT_INVERSE 1
struct flag {
const char *name;
int *flag;
+ int (*fun)(const char *arg, const char *opt, const struct flag *, int options);
+ unsigned long mask;
};
-static int handle_simple_switch(const char *arg, const struct flag *flags)
+static int handle_switches(const char *ori, const char *opt, const struct flag *flags)
{
+ const char *arg = opt;
int val = 1;
// Prefixe "no-" mean to turn flag off.
@@ -499,7 +504,22 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
}
for (; flags->name; flags++) {
- if (strcmp(arg, flags->name) == 0) {
+ const char *opt = match_option(arg, flags->name);
+ int rc;
+
+ if (!opt)
+ continue;
+
+ if (flags->fun) {
+ int options = 0;
+ if (!val)
+ options |= OPT_INVERSE;
+ if ((rc = flags->fun(ori, opt, flags, options)))
+ return rc;
+ }
+
+ // boolean flag
+ if (opt[0] == '\0' && flags->flag) {
*flags->flag = val;
return 1;
}
@@ -514,21 +534,17 @@ static int handle_simple_switch(const char *arg, const struct flag *flags)
#define OPTNUM_UNLIMITED 2
#define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
-static int opt_##NAME(const char *arg, const char *name, TYPE *ptr, int flag)\
+static int opt_##NAME(const char *arg, const char *opt, TYPE *ptr, int flag) \
{ \
- const char *opt; \
char *end; \
TYPE val; \
\
- if (!(opt = match_option(arg, name+2))) \
- return 0; \
- opt++; /* opt's last char is '=' */ \
val = FUNCTION(opt, &end, 0); \
if (*end != '\0' || end == opt) { \
if ((flag & OPTNUM_UNLIMITED) && !strcmp(opt, "unlimited")) \
val = ~val; \
else \
- die("error: missing argument to \"%s\"", name); \
+ die("error: wrong argument to \'%s\'", arg); \
} \
if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
val = ~val; \
@@ -728,62 +744,51 @@ static char **handle_switch_O(char *arg, char **next)
return next;
}
-static char **handle_switch_ftabstop(const char *arg, char **next)
+static int handle_ftabstop(const char *arg, const char *opt, const struct flag *flag, int options)
{
- char *end;
unsigned long val;
+ char *end;
- if (*arg == '\0')
- die("error: missing argument to \"-ftabstop=\"");
+ if (*opt == '\0')
+ die("error: missing argument to \"%s\"", arg);
/* we silently ignore silly values */
- val = strtoul(arg, &end, 10);
+ val = strtoul(opt, &end, 10);
if (*end == '\0' && 1 <= val && val <= 100)
tabstop = val;
- return next;
+ return 1;
}
-static char **handle_switch_fdump(const char *arg, char **next)
+static int handle_fdump_ir(const char *arg, const char *opt, const struct flag *flag, int options)
{
- const char *opt;
-
- if ((opt = match_option(arg, "linearize"))) {
- if (*opt == '\0')
- fdump_linearize = 1;
- else if (!strcmp(opt, "=only"))
- fdump_linearize = 2;
- else
- goto err;
- }
+ if (*opt == '\0')
+ fdump_linearize = 1;
+ else if (!strcmp(opt, "=only"))
+ fdump_linearize = 2;
+ else
+ die("error: wrong option \"%s\"", arg);
- /* ignore others flags */
- return next;
+ return 1;
+}
-err:
- die("error: unknown flag \"-fdump-%s\"", arg);
+static int handle_fmemcpy_max_count(const char *arg, const char *opt, const struct flag *flag, int options)
+{
+ opt_ullong(arg, opt, &fmemcpy_max_count, OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED);
+ return 1;
}
static struct flag fflags[] = {
- { "mem-report", &fmem_report },
+ { "dump-linearize", NULL, handle_fdump_ir },
+ { "mem-report", &fmem_report },
+ { "memcpy-max-count=", NULL, handle_fmemcpy_max_count },
+ { "tabstop=", NULL, handle_ftabstop },
{ },
};
static char **handle_switch_f(char *arg, char **next)
{
- const char *opt;
- arg++;
-
- if ((opt = match_option(arg, "tabstop=")))
- return handle_switch_ftabstop(opt, next);
- if ((opt = match_option(arg, "dump-")))
- return handle_switch_fdump(opt, next);
- if (opt_ullong(arg, "-fmemcpy-max-count=", &fmemcpy_max_count,
- OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED))
- return next;
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH 9/9] option: handle switches by table
2017-11-08 10:10 ` [PATCH 9/9] option: handle switches by table Luc Van Oostenryck
@ 2017-11-09 18:42 ` Christopher Li
2017-11-09 21:02 ` Luc Van Oostenryck
0 siblings, 1 reply; 13+ messages in thread
From: Christopher Li @ 2017-11-09 18:42 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Linux-Sparse
On Wed, Nov 8, 2017 at 6:10 PM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> Currently, the parsing of options is often quite ad-hoc and thus:
> - need a lot of code
> - is not clear at all
>
> Improve this by making this table driven.
Please start the patch with the greatest idea table driven option parsing.
Please also considering reducing the number of patches require
to get to the same code.
- const char *opt;
- arg++;
-
- if ((opt = match_option(arg, "tabstop=")))
- return handle_switch_ftabstop(opt, next);
- if ((opt = match_option(arg, "dump-")))
- return handle_switch_fdump(opt, next);
- if (opt_ullong(arg, "-fmemcpy-max-count=", &fmemcpy_max_count,
- OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED))
- return next;
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 9/9] option: handle switches by table
2017-11-09 18:42 ` Christopher Li
@ 2017-11-09 21:02 ` Luc Van Oostenryck
0 siblings, 0 replies; 13+ messages in thread
From: Luc Van Oostenryck @ 2017-11-09 21:02 UTC (permalink / raw)
To: Christopher Li; +Cc: Linux-Sparse
On Fri, Nov 10, 2017 at 02:42:48AM +0800, Christopher Li wrote:
> On Wed, Nov 8, 2017 at 6:10 PM, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> > Currently, the parsing of options is often quite ad-hoc and thus:
> > - need a lot of code
> > - is not clear at all
> >
> > Improve this by making this table driven.
>
> Please start the patch with the greatest idea table driven option parsing.
You don't seems to see that (most of) these patches are preparatory
steps to reach this goal.
If you think that it's simpler to review, test & debug once they are
all squashed in a single patch, fine, it's your opinion, not mine.
-- Luc
^ permalink raw reply [flat|nested] 13+ messages in thread