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: Chris Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 2/2] ret-void: warn for implicit type
Date: Thu,  8 Jun 2017 04:47:36 +0200	[thread overview]
Message-ID: <20170608024736.49780-3-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170608024736.49780-1-luc.vanoostenryck@gmail.com>

Currently, no warning is given for symbols for which no
type is explicitely given. But for functions we received
a pointless warning like here under if the returned type
is effectively an int:
	warning: incorrect type in return expression (invalid types)
	    expected incomplete type
	    got int

Fix this by issuing the warning.
Also give an implicit type of int, as required by C89, to
avoid pointless warning about the expected incomplete type.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                        |  9 +++++++++
 validation/alias-mixed.c       |  2 +-
 validation/badtype2.c          |  1 +
 validation/implicit-ret-type.c | 15 +++++++++++++++
 validation/implicit-type.c     | 14 ++++++++++++++
 validation/typedef_shadow.c    |  1 +
 6 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 validation/implicit-ret-type.c
 create mode 100644 validation/implicit-type.c

diff --git a/parse.c b/parse.c
index b08d4acef..214547904 100644
--- a/parse.c
+++ b/parse.c
@@ -2920,6 +2920,11 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 			}
 		}
 	} else if (base_type && base_type->type == SYM_FN) {
+		if (base_type->ctype.base_type == &incomplete_ctype) {
+			warning(decl->pos, "'%s()' has implicit return type",
+				show_ident(decl->ident));
+			base_type->ctype.base_type = &int_ctype;
+		}
 		/* K&R argument declaration? */
 		if (lookup_type(token))
 			return parse_k_r_arguments(token, decl, list);
@@ -2931,6 +2936,10 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 	} else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
 		sparse_error(token->pos, "void declaration");
 	}
+	if (base_type == &incomplete_ctype) {
+		warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
+		decl->ctype.base_type = &int_ctype;;
+	}
 
 	for (;;) {
 		if (!is_typedef && match_op(token, '=')) {
diff --git a/validation/alias-mixed.c b/validation/alias-mixed.c
index 429304774..0cfbe36b8 100644
--- a/validation/alias-mixed.c
+++ b/validation/alias-mixed.c
@@ -15,7 +15,7 @@ static int bar(int *p)
 	return g == 1;
 }
 
-static test(void)
+static void test(void)
 {
 	foo(&g);
 	bar(&g);
diff --git a/validation/badtype2.c b/validation/badtype2.c
index 90a5fa1e4..49fec87ce 100644
--- a/validation/badtype2.c
+++ b/validation/badtype2.c
@@ -12,6 +12,7 @@ static undef foo(char *c)
 /*
  * check-name: missing type
  * check-error-start
+badtype2.c:2:8: warning: 'undef' has implicit type
 badtype2.c:2:14: error: Expected ; at end of declaration
 badtype2.c:2:14: error: got bar
 badtype2.c:3:14: error: Expected ; at end of declaration
diff --git a/validation/implicit-ret-type.c b/validation/implicit-ret-type.c
new file mode 100644
index 000000000..784a28531
--- /dev/null
+++ b/validation/implicit-ret-type.c
@@ -0,0 +1,15 @@
+fun(void);
+
+foo(void) { return 1; }
+static bar(void) { return 1; }
+
+/*
+ * check-name: implicit-ret-type.c
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+implicit-ret-type.c:1:1: warning: 'fun()' has implicit return type
+implicit-ret-type.c:3:1: warning: 'foo()' has implicit return type
+implicit-ret-type.c:4:8: warning: 'bar()' has implicit return type
+ * check-error-end
+ */
diff --git a/validation/implicit-type.c b/validation/implicit-type.c
new file mode 100644
index 000000000..724bab71e
--- /dev/null
+++ b/validation/implicit-type.c
@@ -0,0 +1,14 @@
+extern a;
+static b;
+c;
+
+/*
+ * check-name: implicit-type.c
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+implicit-type.c:1:8: warning: 'a' has implicit type
+implicit-type.c:2:8: warning: 'b' has implicit type
+implicit-type.c:3:1: warning: 'c' has implicit type
+ * check-error-end
+ */
diff --git a/validation/typedef_shadow.c b/validation/typedef_shadow.c
index c72cec72d..e52de80f2 100644
--- a/validation/typedef_shadow.c
+++ b/validation/typedef_shadow.c
@@ -6,6 +6,7 @@ static void f(int T)
 /*
  * check-name: typedef shadowing
  * check-error-start:
+typedef_shadow.c:4:16: warning: 'T' has implicit type
 typedef_shadow.c:4:18: error: Expected ; at end of declaration
 typedef_shadow.c:4:18: error: got a
  * check-error-end:
-- 
2.13.0


  parent reply	other threads:[~2017-06-08  2:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-08  2:47 [PATCH 0/2] warn on implicit type Luc Van Oostenryck
2017-06-08  2:47 ` [PATCH 1/2] ret-void: add test case for toplevel asm Luc Van Oostenryck
2017-06-08  2:47 ` Luc Van Oostenryck [this message]
2017-06-08  6:16 ` [PATCH 0/2] warn on implicit type Christopher Li
2017-06-08  7:25   ` 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=20170608024736.49780-3-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.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).