This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] fix warning about duplicate 'const'
On Wed, Mar 10, 2004 at 11:20:03AM +0100, Gabriel Dos Reis wrote:
> I would suggest the existing practice (in nearby language) for this:
>
> (1) reject "direct" duplicate cv-qualifiers, .e.g.
>
> const const int i = 90; // ERROR
Perhaps you meant pedwarn, not error?
The following has been through the test suite, and doesn't warn
for *any* duplicated qualifiers. I chose this because that's
what C99 does, and that's easier to document and more forward
compatible.
r~
* c-decl.c (grokdeclarator): Don't warn for duplicate qualifiers
except for pedantic c90 mode.
Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.483
diff -u -p -r1.483 c-decl.c
--- c-decl.c 9 Mar 2004 23:39:14 -0000 1.483
+++ c-decl.c 10 Mar 2004 22:15:28 -0000
@@ -3372,7 +3372,7 @@ grokdeclarator (tree declarator, tree de
{
if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
{
- if (!flag_isoc99)
+ if (pedantic && !flag_isoc99)
pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
}
else
@@ -3629,12 +3629,15 @@ grokdeclarator (tree declarator, tree de
volatilep
= !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
inlinep = !! (specbits & (1 << (int) RID_INLINE));
- if (constp > 1 && ! flag_isoc99)
- pedwarn ("duplicate `const'");
- if (restrictp > 1 && ! flag_isoc99)
- pedwarn ("duplicate `restrict'");
- if (volatilep > 1 && ! flag_isoc99)
- pedwarn ("duplicate `volatile'");
+ if (pedantic && !flag_isoc99)
+ {
+ if (constp > 1)
+ pedwarn ("duplicate `const'");
+ if (restrictp > 1)
+ pedwarn ("duplicate `restrict'");
+ if (volatilep > 1)
+ pedwarn ("duplicate `volatile'");
+ }
if (! flag_gen_aux_info && (TYPE_QUALS (type)))
type = TYPE_MAIN_VARIANT (type);
type_quals = ((constp ? TYPE_QUAL_CONST : 0)
@@ -4087,12 +4090,15 @@ grokdeclarator (tree declarator, tree de
if (erred)
error ("invalid type modifier within pointer declarator");
- if (constp > 1 && ! flag_isoc99)
- pedwarn ("duplicate `const'");
- if (volatilep > 1 && ! flag_isoc99)
- pedwarn ("duplicate `volatile'");
- if (restrictp > 1 && ! flag_isoc99)
- pedwarn ("duplicate `restrict'");
+ if (pedantic && !flag_isoc99)
+ {
+ if (constp > 1)
+ pedwarn ("duplicate `const'");
+ if (volatilep > 1)
+ pedwarn ("duplicate `volatile'");
+ if (restrictp > 1)
+ pedwarn ("duplicate `restrict'");
+ }
type_quals = ((constp ? TYPE_QUAL_CONST : 0)
| (restrictp ? TYPE_QUAL_RESTRICT : 0)
Index: testsuite/gcc.dg/c90-dupqual-1.c
===================================================================
RCS file: testsuite/gcc.dg/c90-dupqual-1.c
diff -N testsuite/gcc.dg/c90-dupqual-1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/c90-dupqual-1.c 10 Mar 2004 22:15:29 -0000
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+typedef const int CI;
+const const int c1; /* { dg-error "duplicate" } */
+const CI c2; /* { dg-error "duplicate" } */
+const CI *c3; /* { dg-error "duplicate" } */
+
+typedef volatile int VI;
+volatile volatile int v1; /* { dg-error "duplicate" } */
+volatile VI v2; /* { dg-error "duplicate" } */
+volatile VI *v3; /* { dg-error "duplicate" } */
Index: testsuite/gcc.dg/c99-dupqual-1.c
===================================================================
RCS file: testsuite/gcc.dg/c99-dupqual-1.c
diff -N testsuite/gcc.dg/c99-dupqual-1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/c99-dupqual-1.c 10 Mar 2004 22:15:29 -0000
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+typedef const int CI;
+const const int c1; /* { dg-bogus "duplicate" } */
+const CI c2; /* { dg-bogus "duplicate" } */
+const CI *c3; /* { dg-bogus "duplicate" } */
+
+typedef volatile int VI;
+volatile volatile int v1; /* { dg-bogus "duplicate" } */
+volatile VI v2; /* { dg-bogus "duplicate" } */
+volatile VI *v3; /* { dg-bogus "duplicate" } */
Index: testsuite/gcc.dg/gnu89-dupqual-1.c
===================================================================
RCS file: testsuite/gcc.dg/gnu89-dupqual-1.c
diff -N testsuite/gcc.dg/gnu89-dupqual-1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/gnu89-dupqual-1.c 10 Mar 2004 22:15:29 -0000
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-std=gnu89 -Werror" } */
+
+typedef const int CI;
+const const int c1; /* { dg-bogus "duplicate" } */
+const CI c2; /* { dg-bogus "duplicate" } */
+const CI *c3; /* { dg-bogus "duplicate" } */
+
+typedef volatile int VI;
+volatile volatile int v1; /* { dg-bogus "duplicate" } */
+volatile VI v2; /* { dg-bogus "duplicate" } */
+volatile VI *v3; /* { dg-bogus "duplicate" } */