Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Ben Dooks <ben.dooks@codethink.co.uk>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 12/13] format-check: remove wrappers around type checking methods
Date: Wed, 14 Oct 2020 01:22:30 +0200
Message-ID: <20201013232231.10349-13-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201013232231.10349-1-luc.vanoostenryck@gmail.com>

Now that all types have their own checking functions, the
signature of the methods can be simplified and the wrappers
removed.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 verify-format.c | 86 +++++++------------------------------------------
 1 file changed, 12 insertions(+), 74 deletions(-)

diff --git a/verify-format.c b/verify-format.c
index fdfe9c22e858..753e59c167ce 100644
--- a/verify-format.c
+++ b/verify-format.c
@@ -54,10 +54,7 @@ enum length_mod {
 
 struct format_type {
 	const char	*format;
-	int		(*test)(struct format_type *fmt,
-				struct expression **expr,
-				struct symbol *ctype,
-				const char **typediff);
+	const char*	(*test)(struct format_type *fmt, struct symbol *source);
 	struct symbol	*type;
 };
 
@@ -149,27 +146,11 @@ static const char *check_printf_sint(struct format_type *fmt, struct symbol *sou
 	return check_printf_int(fmt, source, 1);
 }
 
-static int printf_fmt_sint(struct format_type *fmt,
-			      struct expression **expr,
-			      struct symbol *ctype,
-			      const char **typediff)
-{
-	return !(*typediff = check_printf_sint(fmt, ctype));
-}
-
 static const char *check_printf_uint(struct format_type *fmt, struct symbol *source)
 {
 	return check_printf_int(fmt, source, 0);
 }
 
-static int printf_fmt_uint(struct format_type *fmt,
-			      struct expression **expr,
-			      struct symbol *ctype,
-			      const char **typediff)
-{
-	return !(*typediff = check_printf_uint(fmt, ctype));
-}
-
 static const char *check_printf_float(struct format_type *fmt, struct symbol *source)
 {
 	const char *typediff = "different base types";
@@ -183,14 +164,6 @@ static const char *check_printf_float(struct format_type *fmt, struct symbol *so
 	return typediff;
 }
 
-static int printf_fmt_float(struct format_type *fmt,
-			      struct expression **expr,
-			      struct symbol *ctype,
-			      const char **typediff)
-{
-	return !(*typediff = check_printf_float(fmt, ctype));
-}
-
 // For 's' & 'S' specifiers
 static const char *check_printf_string(struct format_type *fmt, struct symbol *source)
 {
@@ -223,15 +196,6 @@ static const char *check_printf_string(struct format_type *fmt, struct symbol *s
 	return typediff;
 }
 
-static int printf_fmt_string(struct format_type *fmt,
-			     struct expression **expr,
-			     struct symbol *ctype,
-			     const char **typediff)
-{
-	return !(*typediff = check_printf_string(fmt, ctype));
-}
-
-
 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
 static inline const char *compare_pointer(struct symbol *target, struct symbol *source)
 {
@@ -258,14 +222,6 @@ static const char *check_printf_length(struct format_type *fmt, struct symbol *s
 	return compare_pointer(target, source);
 }
 
-static int printf_fmt_length(struct format_type *fmt,
-			      struct expression **expr,
-			      struct symbol *ctype,
-			      const char **typediff)
-{
-	return !(*typediff = check_printf_length(fmt, ctype));
-}
-
 // For 'p' specifiers
 static const char *check_printf_pointer(struct format_type *fmt, struct symbol *source)
 {
@@ -278,20 +234,6 @@ static const char *check_printf_pointer(struct format_type *fmt, struct symbol *
 	return NULL;
 }
 
-static int printf_fmt_pointer(struct format_type *fmt,
-			      struct expression **expr,
-			      struct symbol *ctype,
-			      const char **typediff)
-{
-	return !(*typediff = check_printf_pointer(fmt, ctype));
-}
-
-static struct format_type printf_fmt_ptr_ref = {
-	.format = "p",
-	.test = printf_fmt_pointer,
-	.type = &const_ptr_ctype,
-};
-
 static struct format_type *parse_printf_get_fmt(struct format_type *type,
 						const char *msg, const char **msgout)
 {
@@ -338,7 +280,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 
 	switch (*ptr++) {
 	case 'd': case 'i':
-		type->test = printf_fmt_sint;
+		type->test = check_printf_sint;
 		switch (szmod) {
 		case LEN_hh:
 		case LEN_h:
@@ -366,7 +308,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		}
 		break;
 	case 'u': case 'o': case 'x': case 'X':
-		type->test = printf_fmt_uint;
+		type->test = check_printf_uint;
 		switch (szmod) {
 		case LEN_hh:
 		case LEN_h:
@@ -387,6 +329,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 			type->type = size_t_ctype;
 			break;
 		case LEN_t:
+			type->test = check_printf_sint;
 			type->type = ptrdiff_ctype;
 			break;
 		default:
@@ -395,7 +338,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		break;
 	case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
 	case 'a': case 'A':
-		type->test = printf_fmt_float;
+		type->test = check_printf_float;
 		switch (szmod) {
 		case LEN_none:
 			type->type = &double_ctype;
@@ -408,7 +351,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		}
 		break;
 	case 'c':
-		type->test = printf_fmt_sint;	// FIXME: need its own check?
+		type->test = check_printf_sint;
 		switch (szmod) {
 		case LEN_none:
 			type->type = &int_ctype;
@@ -422,7 +365,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		}
 		break;
 	case 's':
-		type->test = printf_fmt_string;
+		type->test = check_printf_string;
 		switch (szmod) {
 		case LEN_none:
 			type->type = &const_string_ctype;
@@ -436,7 +379,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		}
 		break;
 	case 'p':
-		type->test = printf_fmt_pointer;
+		type->test = check_printf_pointer;
 		type->type = &const_ptr_ctype;
 		/* check for pointer being printed as hex explicitly */
 		if (*ptr == 'x' || *ptr == 'X') {
@@ -450,7 +393,7 @@ static struct format_type *parse_printf_get_fmt(struct format_type *type,
 		/* pointer to an de-referenced int/etc */
 		// todo - we should construct pointer to int/etc //
 		// also should not have any flags or widths for this
-		type->test = printf_fmt_length;
+		type->test = check_printf_length;
 		type->type = &const_ptr_ctype;
 		break;
 	default:
@@ -612,14 +555,9 @@ static int parse_format_printf(const char **fmtstring,
 	}
 
 	type = parse_printf_get_fmt(&ftype, fmt, &fmtpost);
-
-	if (!type && fmt[0] == 'p')
-		type = &printf_fmt_ptr_ref;	/* probably some extension */
-
 	if (type) {
 		struct symbol *ctype;
-		const char *typediff = "different types";
-		int ret;
+		const char *typediff;
 
 		*fmtstring = fmtpost;
 		expr = get_nth_expression(args, pos-1);
@@ -633,8 +571,8 @@ static int parse_format_printf(const char **fmtstring,
 		if (!ctype)
 			return -3;
 
-		ret = type->test(type, &expr, ctype, &typediff);
-		if (ret == 0) {
+		typediff = ftype.test(&ftype, ctype);
+		if (typediff) {
 			warning(expr->pos, "incorrect type in argument %d (%s)", pos, typediff);
 			info(expr->pos, "   expected %s", show_typename(type->type));
 			info(expr->pos, "   got %s", show_typename(ctype));
-- 
2.28.0


  parent reply index

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13 23:22 [PATCH 00/13] format-check: add specific type checking Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 01/13] format-check: void * is not OK for strings, fix the test Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 02/13] format-check: more complete parsing of the length & type modifiers Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 03/13] format-check: add helper type_class() Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 04/13] format-check: merge 'fmt_string' & 'string' Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 05/13] format-check: remove unneeded member: target Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 06/13] format-check: add a function to check to type of strings Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 07/13] format-check: add a function to check to type of 'n' arguments Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 08/13] format-check: add a function to check to type of pointers Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 09/13] format-check: remove printf_fmt_print_pointer() Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 10/13] format-check: add a function to check the type of floats Luc Van Oostenryck
2020-10-13 23:22 ` [PATCH 11/13] format-check: add a function to check the type of integers Luc Van Oostenryck
2020-10-13 23:22 ` Luc Van Oostenryck [this message]
2020-10-13 23:22 ` [PATCH 13/13] format-check: simplify calling of parse_printf_get_fmt() 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=20201013232231.10349-13-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=ben.dooks@codethink.co.uk \
    --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

Linux-Sparse Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sparse/0 linux-sparse/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sparse linux-sparse/ https://lore.kernel.org/linux-sparse \
		linux-sparse@vger.kernel.org
	public-inbox-index linux-sparse

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sparse


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git