linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Geoff Johnstone" <geoffsheep.johnstonefrog@googlemail.com>
To: Josh Triplett <josh@kernel.org>
Cc: linux-sparse@vger.kernel.org
Subject: Re: four sparse patches
Date: Sat, 12 Apr 2008 11:57:23 +0100	[thread overview]
Message-ID: <32e600e90804120357k5923dd47xfcb253bf1c390225@mail.gmail.com> (raw)
In-Reply-To: <47F750CA.1090307@kernel.org>

[-- Attachment #1: Type: text/plain, Size: 999 bytes --]

>  Regarding Wmix-decl-code.diff, I agree that that warning definitely
>  needs an option controlling it. but GCC already has that option and
>  calls it "-Wdeclaration-after-statement", so matching GCC's name
>  seems potentially useful. (However, I can imagine corner cases
>  where it might prove problematic, such as wanting to pass that
>  option to GCC and not Sparse or vice versa.) Also, I agree that the
>  default should depend on the C standard in use, and I see no
>  compatibility reason why the warning should remain for code that
>  explicitly asks for C99.  Thus, I haven't applied this version of
>  the patch.

I've attached a revised version of the patch that:
 - Renames the option to -Wdeclaration-after-statement, as per GCC
   (wasn't hitherto aware of that gcc option).
 - Defaults based on chosen C dialect.
 - Adds a few tests. (I'll do a separate patch for tests for the
   incomplete struct patch.)
 - Was made wrt the git trunk at about 11:30 UTC on 12th April.

Geoff.

[-- Attachment #2: Wdeclaration-after-statement.diff --]
[-- Type: application/octet-stream, Size: 4944 bytes --]

This adds -W[no-]declaration-after-statement, which makes warnings about
declarations after statements a command-line option. (The code to implement
the warning was already in there via a #define; the patch just exposes it
at runtime.) Rationale: C99 allows them, C89 doesn't.

Signed-off-by: Geoff Johnstone <geoffSHEEP.johnstoneFROG@googlemail.com>

diff --git a/lib.c b/lib.c
index 75afdb7..0abcc9a 100644
--- a/lib.c
+++ b/lib.c
@@ -208,6 +208,7 @@ int Wtransparent_union = 1;
 int Wtypesign = 0;
 int Wundef = 0;
 int Wuninitialized = 1;
+int Wdeclarationafterstatement = -1;
 
 int dbg_entry = 0;
 int dbg_dead = 0;
@@ -372,6 +373,7 @@ static const struct warning {
 	{ "typesign", &Wtypesign },
 	{ "undef", &Wundef },
 	{ "uninitialized", &Wuninitialized },
+	{ "declaration-after-statement", &Wdeclarationafterstatement },
 };
 
 enum {
@@ -456,6 +458,28 @@ static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
 static void handle_switch_W_finalize(void)
 {
 	handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
+
+	/* default Wdeclarationafterstatement based on the C dialect */
+	if (-1 == Wdeclarationafterstatement)
+	{
+		switch (standard)
+		{
+			case STANDARD_C89:
+			case STANDARD_C94:
+				Wdeclarationafterstatement = 1;
+				break;
+
+			case STANDARD_C99:
+			case STANDARD_GNU89:
+			case STANDARD_GNU99:
+				Wdeclarationafterstatement = 0;
+				break;
+
+			default:
+				assert (0);
+		}
+
+	}
 }
 
 static void handle_switch_v_finalize(void)
diff --git a/lib.h b/lib.h
index cef4ab8..42a3396 100644
--- a/lib.h
+++ b/lib.h
@@ -108,6 +108,7 @@ extern int Wtransparent_union;
 extern int Wtypesign;
 extern int Wundef;
 extern int Wuninitialized;
+extern int Wdeclarationafterstatement;
 
 extern int dbg_entry;
 extern int dbg_dead;
diff --git a/parse.c b/parse.c
index 6255737..8346fe2 100644
--- a/parse.c
+++ b/parse.c
@@ -28,8 +28,6 @@
 #include "expression.h"
 #include "target.h"
 
-#define warn_on_mixed (1)
-
 static struct symbol_list **function_symbol_list;
 struct symbol_list *function_computed_target_list;
 struct statement_list *function_computed_goto_list;
@@ -1810,7 +1808,7 @@ static struct token * statement_list(struct token *token, struct statement_list
 			stmt = alloc_statement(token->pos, STMT_DECLARATION);
 			token = external_declaration(token, &stmt->declaration);
 		} else {
-			seen_statement = warn_on_mixed;
+			seen_statement = Wdeclarationafterstatement;
 			token = statement(token, &stmt);
 		}
 		add_statement(list, stmt);
diff --git a/sparse.1 b/sparse.1
index 7a39d85..0f9131d 100644
--- a/sparse.1
+++ b/sparse.1
@@ -112,6 +112,16 @@ Sparse issues these warnings by default.  To turn them off, use
 \fB\-Wno\-decl\fR.
 .
 .TP
+.B \-Wdeclaration-after-statement
+Warn about declarations that are not at the start of a block.
+
+These declarations are permitted in C99 but not in C89.
+
+Sparse issues these warnings by default only when the C dialect is
+C89 (i.e. -ansi or -std=c89).  To turn them off, use
+\fB\-Wno\-declaration\-after\-statement\fR.
+.
+.TP
 .B \-Wdefault\-bitfield\-sign
 Warn about any bitfield with no explicit signedness.
 
diff --git a/validation/declaration-after-statement-ansi.c b/validation/declaration-after-statement-ansi.c
new file mode 100644
index 0000000..333410b
--- /dev/null
+++ b/validation/declaration-after-statement-ansi.c
@@ -0,0 +1,12 @@
+static void func (int i)
+{
+	i;
+	int j = i;
+}
+/*
+ * check-name: declaration after statement (ANSI)
+ * check-command: sparse -ansi $file
+ * check-error-start
+declaration-after-statement-ansi.c:4:2: warning: mixing declarations and code
+ * check-error-end
+ */
diff --git a/validation/declaration-after-statement-c89.c b/validation/declaration-after-statement-c89.c
new file mode 100644
index 0000000..78632cd
--- /dev/null
+++ b/validation/declaration-after-statement-c89.c
@@ -0,0 +1,12 @@
+static void func (int i)
+{
+	i;
+	int j = i;
+}
+/*
+ * check-name: declaration after statement (C89)
+ * check-command: sparse -std=c89 $file
+ * check-error-start
+declaration-after-statement-c89.c:4:2: warning: mixing declarations and code
+ * check-error-end
+ */
diff --git a/validation/declaration-after-statement-c99.c b/validation/declaration-after-statement-c99.c
new file mode 100644
index 0000000..dd36e6e
--- /dev/null
+++ b/validation/declaration-after-statement-c99.c
@@ -0,0 +1,9 @@
+static void func (int i)
+{
+	i;
+	int j = i;
+}
+/*
+ * check-name: declaration after statement (C99)
+ * check-command: sparse -std=c99 $file
+ */
diff --git a/validation/declaration-after-statement-default.c b/validation/declaration-after-statement-default.c
new file mode 100644
index 0000000..c3fe2cd
--- /dev/null
+++ b/validation/declaration-after-statement-default.c
@@ -0,0 +1,9 @@
+static void func (int i)
+{
+	i;
+	int j = i;
+}
+/*
+ * check-name: declaration after statement (default)
+ * check-command: sparse $file
+ */

  reply	other threads:[~2008-04-12 10:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9356e4460803300752y6c1416bfh680b68fd3c42c19a@mail.gmail.com>
     [not found] ` <47F07F78.5030405@kernel.org>
     [not found]   ` <9356e4460803311222v4b09df5fk76268fb5c7e71971@mail.gmail.com>
2008-03-31 19:36     ` four sparse patches Geoff Johnstone
2008-04-05 10:13       ` Josh Triplett
2008-04-12 10:57         ` Geoff Johnstone [this message]
2008-04-21 19:12           ` Josh Triplett

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=32e600e90804120357k5923dd47xfcb253bf1c390225@mail.gmail.com \
    --to=geoffsheep.johnstonefrog@googlemail.com \
    --cc=josh@kernel.org \
    --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).