linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 06/15] options: move helpers up
Date: Sun,  5 Jul 2020 15:02:11 +0200	[thread overview]
Message-ID: <20200705130220.26230-7-luc.vanoostenryck@gmail.com> (raw)
Message-ID: <20200705130211.qWGV8P4vZuFg5YZQAm1kbI_2qVgXor41k2vAr-Wdu4Q@z> (raw)
In-Reply-To: <20200705130220.26230-1-luc.vanoostenryck@gmail.com>

The helpers for parsing the options are often situated just above the
first function using them. As result, these helpers can be found a bit
everywhere in the code, it's messy and doesn't help to reuse these helpers.

So, move all these helpers to the top.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 lib.c | 87 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 44 insertions(+), 43 deletions(-)

diff --git a/lib.c b/lib.c
index c27773097127..4868154fa3de 100644
--- a/lib.c
+++ b/lib.c
@@ -364,6 +364,15 @@ void add_pre_buffer(const char *fmt, ...)
 ////////////////////////////////////////////////////////////////////////////////
 // Helpers for option parsing
 
+static const char *match_option(const char *arg, const char *prefix)
+{
+	unsigned int n = strlen(prefix);
+	if (strncmp(arg, prefix, n) == 0)
+		return arg + n;
+	return NULL;
+}
+
+
 struct val_map {
 	const char *name;
 	int val;
@@ -438,14 +447,6 @@ end:
 }
 
 
-static const char *match_option(const char *arg, const char *prefix)
-{
-	unsigned int n = strlen(prefix);
-	if (strncmp(arg, prefix, n) == 0)
-		return arg + n;
-	return NULL;
-}
-
 #define OPT_INVERSE	1
 #define OPT_VAL		2
 struct flag {
@@ -497,6 +498,41 @@ static int handle_switches(const char *ori, const char *opt, const struct flag *
 	return 0;
 }
 
+static char **handle_onoff_switch(char *arg, char **next, const struct flag flags[])
+{
+	int flag = FLAG_ON;
+	char *p = arg + 1;
+	unsigned i;
+
+	// Prefixes "no" and "no-" mean to turn warning off.
+	if (p[0] == 'n' && p[1] == 'o') {
+		p += 2;
+		if (p[0] == '-')
+			p++;
+		flag = FLAG_FORCE_OFF;
+	}
+
+	for (i = 0; flags[i].name; i++) {
+		if (!strcmp(p,flags[i].name)) {
+			*flags[i].flag = flag;
+			return next;
+		}
+	}
+
+	// Unknown.
+	return NULL;
+}
+
+static void handle_onoff_switch_finalize(const struct flag flags[])
+{
+	unsigned i;
+
+	for (i = 0; flags[i].name; i++) {
+		if (*flags[i].flag == FLAG_FORCE_OFF)
+			*flags[i].flag = FLAG_OFF;
+	}
+}
+
 static int handle_switch_setval(const char *arg, const char *opt, const struct flag *flag, int options)
 {
 	*(flag->flag) = flag->mask;
@@ -529,31 +565,6 @@ static int opt_##NAME(const char *arg, const char *opt, TYPE *ptr, int flag)	\
 OPT_NUMERIC(ullong, unsigned long long, strtoull)
 OPT_NUMERIC(uint, unsigned int, strtoul)
 
-static char **handle_onoff_switch(char *arg, char **next, const struct flag flags[])
-{
-	int flag = FLAG_ON;
-	char *p = arg + 1;
-	unsigned i;
-
-	// Prefixes "no" and "no-" mean to turn warning off.
-	if (p[0] == 'n' && p[1] == 'o') {
-		p += 2;
-		if (p[0] == '-')
-			p++;
-		flag = FLAG_FORCE_OFF;
-	}
-
-	for (i = 0; flags[i].name; i++) {
-		if (!strcmp(p,flags[i].name)) {
-			*flags[i].flag = flag;
-			return next;
-		}
-	}
-
-	// Unknown.
-	return NULL;
-}
-
 ////////////////////////////////////////////////////////////////////////////////
 // Option parsing
 
@@ -858,16 +869,6 @@ static char **handle_switch_d(char *arg, char **next)
 }
 
 
-static void handle_onoff_switch_finalize(const struct flag flags[])
-{
-	unsigned i;
-
-	for (i = 0; flags[i].name; i++) {
-		if (*flags[i].flag == FLAG_FORCE_OFF)
-			*flags[i].flag = FLAG_OFF;
-	}
-}
-
 static void handle_switch_W_finalize(void)
 {
 	handle_onoff_switch_finalize(warnings);
-- 
2.27.0


  parent reply	other threads:[~2020-07-05 13:02 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-05 13:02 [PATCH 00/15] tidy-up options / reorganize lib.c Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 01/15] options: let handle_onoff_switch() use null terminated arrays Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 02/15] options: move -Wsparse-all's processing out of handle_onoff_switch() Luc Van Oostenryck
2020-07-05 13:02   ` Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 03/15] options: move on top the definition of warning type enums Luc Van Oostenryck
2020-07-05 13:02   ` Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 04/15] options: make Wsparse_error less special Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 05/15] options: handle_onoff_switch() can handle any flags, not only warnings Luc Van Oostenryck
2020-07-05 13:02 ` Luc Van Oostenryck [this message]
2020-07-05 13:02   ` [PATCH 06/15] options: move helpers up Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 07/15] options: alphasort the handle_switch_[a-zA_Z]() Luc Van Oostenryck
2020-07-05 13:02   ` Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 08/15] options: avoid spaces between function name and arguments list Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 09/15] options: move declaration of tabstop out of "token.h" Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 10/15] options: add a small helper: handle_switch_finalize() Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 11/15] options: move option parsing in a separate file Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 12/15] options: keep the options sorted Luc Van Oostenryck
2020-07-05 13:02   ` Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 13/15] cleanup: move predefines in a separate file Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 14/15] cleanup: move parsing helpers to parse.c Luc Van Oostenryck
2020-07-05 17:27   ` Linus Torvalds
2020-07-05 20:45     ` Luc Van Oostenryck
2020-07-05 13:02 ` [PATCH 15/15] cleanup: move hexval() to utils.c Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200705130220.26230-7-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).