This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Mixed declarations and code
- From: Michael Culbertson <Michael dot J dot Culbertson at wheaton dot edu>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sun, 6 Jul 2003 02:42:29 -0500 (CDT)
- Subject: Mixed declarations and code
Hello,
On the gnucash-devel list, someone recently mentioned the new feature in
gcc 3.3 that silently allows C code and declarations to be mixed (message
attached below). There was interest expressed in a switch (other than
-pedantic) that would turn back on a warning for when a declaration occurs
after code while compiling in C mode, since not all gnucash developers use
the newest version of gcc. So, I wrote a brief patch that adds
-Wmixed-declarations to this effect. Since I am not a gcc developer (this
is the first time I've peeked ``under the hood''), I don't know if this is
the best way (or even a good way) to accomplish the task, but I thought
I'd hand it off to you guys in case others would like such a switch.
Michael Culbertson
---------- Forwarded message ----------
Date: Wed, 2 Jul 2003 23:25:35 +0200
From: Christian Stimming <stimming@tuhh.de>
Subject: Newer gcc silently accepts mixing C Code and Declarations
To: gnucash-devel@gnucash.org
Message-ID: <200307022325.41280.stimming@tuhh.de>
Content-Type: Text/Plain; charset="us-ascii"
-----BEGIN PGP SIGNED MESSAGE-----
This is an issue that already showed up here and there occasionally, but I
just figured out it will become a major issue in the future:
The latest gcc (3.3) silently accepts mixing Code and Declarations in its C
code. Quoting from gcc info pages (gcc -> C Extensions -> Mixed
Declarations):
ISO C99 and ISO C++ allow declarations and code to be freely mixed
within compound statements. As an extension, GCC also allows this in
C89 mode. For example, you could do:
int i;
/* ... */
i++;
int j = i + 2;
(end quote)
However, in any gcc earlier than 3.3 (or any other compiler) this kind of
construct will simply issue a "syntax error" in the line with the second
declaration. It is therefore clear that this should be not used in gnucash's
code. BUT, unfortunately, gcc3.3 does not offer any command line switches to
turn off this particular gcc extension! I really dug through any available
information about gcc and tried and tried all sorts of -Wsomething, -std=c89,
- -ansi or whatever switches gcc has, but gcc3.3 keeps silently accepting this
construct. Which makes the behaviour here totally different between gcc3.3
and gcc3.2/1/... -- I really wonder why this is mentioned nowhere in the
release notes.
The only gcc switch that will issue a warning about this is "-pedantic". It
then says
warning: ISO C89 forbids mixed declarations and code
However, the "-pedantic" switch prints just a headache-causing long list of
warnings (including e.g. src/engine/Account.h:94: warning: comma at end of
enumerator list, and also warnings from <glib.h> and from g_assert()). This
switch is pretty useless for the current gnucash code.
So what do we do? First of all, if anybody happens to run a non-gcc3.3 and
discovers such a syntax error, then please don't start flaming the gcc3.3
folks but please simply go ahead and fix that. Second, does anyone know some
secret gcc switch that might enable again the detection of that problem? If
there is none, then, well, what do people think about making gnucash
- -pedantic proof... or do we simply live with those errors every now and then?
Cheers,
Christian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
iQCVAwUBPwNN1WXAi+BfhivFAQFWXQP/Xeu7uEHPEfC5C1mUfPdjWsgyuFziq7p+
jFgQCCC+oLVgXKk/aEIXSwce6ajIH6TW6y/yUMFXJGRDZx+HkxyxSruQCOWV6Fc3
Yo21Gkr5nncVBmdr3Hypr1pP2eNrsaInRNdoj/AjfxKJ/8zV614hIDSTAPNYi8Ed
UHhmJgYPkeQ=
=f5j/
-----END PGP SIGNATURE-----
------------------------------
--------- Begin GCC 3.3 Patch --------------------------
--- c-parse.c-orig Sat Jul 5 18:00:17 2003
+++ c-parse.c Sat Jul 5 18:01:00 2003
@@ -3991,7 +3991,7 @@
break;}
case 431:
#line 1929 "c-parse.y"
-{ if (pedantic && !flag_isoc99)
+{ if ((pedantic || warn_mixed_declarations) && !flag_isoc99)
pedwarn ("ISO C89 forbids mixed declarations and code"); ;
break;}
case 446:
--- c-parse.in-orig Sat Jul 5 17:59:49 2003
+++ c-parse.in Sat Jul 5 18:00:11 2003
@@ -2032,7 +2032,7 @@
lineno_stmt_decl_or_labels_ending_decl:
lineno_decl
| lineno_stmt_decl_or_labels_ending_stmt lineno_decl
- { if (pedantic && !flag_isoc99)
+ { if ((pedantic || warn_mixed_declarations) && !flag_isoc99)
pedwarn ("ISO C89 forbids mixed declarations and code"); }
| lineno_stmt_decl_or_labels_ending_decl lineno_decl
| lineno_stmt_decl_or_labels_ending_error lineno_decl
--- c-parse.y-orig Sat Jul 5 17:59:08 2003
+++ c-parse.y Sat Jul 5 17:59:40 2003
@@ -1926,7 +1926,7 @@
lineno_stmt_decl_or_labels_ending_decl:
lineno_decl
| lineno_stmt_decl_or_labels_ending_stmt lineno_decl
- { if (pedantic && !flag_isoc99)
+ { if ((pedantic || warn_mixed_declarations) && !flag_isoc99)
pedwarn ("ISO C89 forbids mixed declarations and code"); }
| lineno_stmt_decl_or_labels_ending_decl lineno_decl
| lineno_stmt_decl_or_labels_ending_error lineno_decl
--- flags.h-orig Sat Jul 5 17:58:30 2003
+++ flags.h Sat Jul 5 17:58:56 2003
@@ -189,6 +189,10 @@
extern int warn_strict_aliasing;
+/* Nonzero means warn about mixed declarations and code in C89 mode */
+
+extern int warn_mixed_declarations;
+
/* Nonzero if generating code to do profiling. */
extern int profile_flag;
--- toplev.c-orig Sat Jul 5 17:55:14 2003
+++ toplev.c Sun Jul 6 02:15:11 2003
@@ -1500,6 +1500,10 @@
int warn_strict_aliasing;
+/* Nonzero means warn about mixed declarations and code in C89 mode */
+
+int warn_mixed_declarations;
+
/* Likewise for -W. */
static const lang_independent_options W_options[] =
@@ -1547,7 +1551,9 @@
{"missing-noreturn", &warn_missing_noreturn, 1,
N_("Warn about functions which might be candidates for attribute noreturn") },
{"strict-aliasing", &warn_strict_aliasing, 1,
- N_ ("Warn about code which might break the strict aliasing rules") }
+ N_ ("Warn about code which might break the strict aliasing rules") },
+ {"mixed-declarations", &warn_mixed_declarations, 1,
+ N_("Warn about mixed declarations and code in C89 mode") }
};
void