This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch to deprecate casts as lvalues for C
- From: "Joseph S. Myers" <jsm at polyomino dot org dot uk>
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 20 Oct 2003 19:27:58 +0100 (BST)
- Subject: Patch to deprecate casts as lvalues for C
This patch deprecates the cast-as-lvalue extension for C (and so for
Objective-C), as previously discussed. (The deprecation warning
doesn't cover the case of a cast to the same type, where -pedantic
presently gives a hard error rather than a warning; I don't think it's
worth making the deprecation warning cover this case for a feature
that will be removed.) The patch to gcc-3.4/changes.html is at the
bottom.
Bootstrapped with no regressions on i686-pc-linux-gnu. OK to commit
(the obstack.h patch (required for bootstrap with this patch, as
obstack.h uses this unfortunate extension), and the libobjc patch)?
(codingconventions.html is silent on any upstream nature of obstack,
but the comments in obstack.c imply that the version used here is
forked from the glibc one.)
include:
2003-10-20 Joseph S. Myers <jsm@polyomino.org.uk>
* obstack.h (obstack_ptr_grow): Don't use a cast as an lvalue.
libobjc:
2003-10-20 Joseph S. Myers <jsm@polyomino.org.uk>
* objc/hash.h (hash_string): Don't use a cast as an lvalue.
gcc:
2003-10-20 Joseph S. Myers <jsm@polyomino.org.uk>
* c-typeck.c (pedantic_lvalue_warning): Unconditionally warn of
deprecation of casts as lvalues.
* doc/extend.texi: Deprecate casts as lvalues.
gcc/testsuite:
2003-10-20 Joseph S. Myers <jsm@polyomino.org.uk>
* gcc.dg/cast-lvalue-1.c: New test.
--- GCC/gcc/c-typeck.c.orig 2003-10-06 12:25:11.000000000 +0000
+++ GCC/gcc/c-typeck.c 2003-10-20 09:47:25.000000000 +0000
@@ -2585,19 +2585,20 @@
static void
pedantic_lvalue_warning (enum tree_code code)
{
- if (pedantic)
- switch (code)
- {
- case COND_EXPR:
+ switch (code)
+ {
+ case COND_EXPR:
+ if (pedantic)
pedwarn ("ISO C forbids use of conditional expressions as lvalues");
- break;
- case COMPOUND_EXPR:
+ break;
+ case COMPOUND_EXPR:
+ if (pedantic)
pedwarn ("ISO C forbids use of compound expressions as lvalues");
- break;
- default:
- pedwarn ("ISO C forbids use of cast expressions as lvalues");
- break;
- }
+ break;
+ default:
+ pedwarn ("use of cast expressions as lvalues is deprecated");
+ break;
+ }
}
/* Warn about storing in something that is `const'. */
--- GCC/gcc/doc/extend.texi.orig 2003-10-19 11:22:10.000000000 +0000
+++ GCC/gcc/doc/extend.texi 2003-10-20 09:48:26.000000000 +0000
@@ -1101,7 +1101,8 @@
(a ? b = 5 : (c = 5))
@end example
-A cast is a valid lvalue if its operand is an lvalue. A simple
+A cast is a valid lvalue if its operand is an lvalue. This extension
+is deprecated. A simple
assignment whose left-hand side is a cast works by converting the
right-hand side first to the specified type, then to the type of the
inner left-hand side expression. After this is stored, the value is
--- GCC/gcc/testsuite/gcc.dg/cast-lvalue-1.c 2002-08-26 16:21:36.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/cast-lvalue-1.c 2003-10-20 09:52:41.000000000 +0000
@@ -0,0 +1,12 @@
+/* Test for deprecation of casts as lvalues. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int x;
+
+void
+foo (void)
+{
+ (char) x = 1; /* { dg-warning "lvalue" "cast as lvalue deprecated" } */
+}
--- GCC/include/obstack.h.orig 2001-03-16 08:25:53.000000000 +0000
+++ GCC/include/obstack.h 2003-10-20 14:07:54.000000000 +0000
@@ -423,7 +423,8 @@
({ struct obstack *__o = (OBSTACK); \
if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
_obstack_newchunk (__o, sizeof (void *)); \
- *((void **)__o->next_free)++ = ((void *)datum); \
+ *(void **)__o->next_free = ((void *)datum); \
+ __o->next_free += sizeof (void *); \
(void) 0; })
# define obstack_int_grow(OBSTACK,datum) \
--- GCC/libobjc/objc/hash.h.orig 2003-10-04 07:41:01.000000000 +0000
+++ GCC/libobjc/objc/hash.h 2003-10-20 17:59:26.000000000 +0000
@@ -172,10 +172,10 @@
{
unsigned int ret = 0;
unsigned int ctr = 0;
+ const char *ckey = key;
-
- while (*(const char *) key) {
- ret ^= *((const char *) key)++ << ctr;
+ while (*ckey) {
+ ret ^= *ckey++ << ctr;
ctr = (ctr + 1) % sizeof (void *);
}
--- changes.html.orig 2003-10-15 08:04:56.000000000 +0000
+++ changes.html 2003-10-20 09:57:17.000000000 +0000
@@ -76,6 +76,16 @@
a correct implementation of <code>#import</code> and
<code>#pragma once</code>.
These two directives have therefore been un-deprecated.</li>
+ <li>The cast-as-lvalue extension has been removed for C++ and
+ deprecated for C and Objective-C. In particular,
+ code like this:
+ <pre>
+ int i;
+ (char) i = 5;
+ </pre>
+ <p>is no longer accepted for C++ and will not be accepted for
+ C and Objective-C in a future version.</p></li>
+
</ul>
<h3>C++</h3>
@@ -212,14 +222,6 @@
<code>f</code>. The default arguments for <code>g</code> must
be visible at the point where it is called.</p></li>
- <li>The cast-as-lvalue extension has been removed. In particular,
- code like this:
- <pre>
- int i;
- (char) i = 5;
- </pre>
- <p>is no longer accepted.</p></li>
-
<li>The C++ ABI Section 3.3.3 specifications for the array
construction routines <code>__cxa_vec_new2</code> and
<code>__cxa_vec_new3</code> were changed to return
--
Joseph S. Myers
jsm@polyomino.org.uk