[PATCH]: Add a new option -Wvariable-length-array

Seongbae Park seongbae.park@gmail.com
Wed Feb 7 00:40:00 GMT 2007


On 2/6/07, Seongbae Park <seongbae.park@gmail.com> wrote:
> On 2/6/07, Joseph S. Myers <joseph@codesourcery.com> wrote:
> > On Tue, 6 Feb 2007, Seongbae Park wrote:
> >
> > > This patch adds a new flag -Wvariable-length-array
> > > which issues a warning for any variable-length array usage
> > > regardless of C99 or pedantic.
> >
> > Your logic is the wrong way round: the checks for pedantic and consequent
> > pedwarns must come before the checks for the new option and consequent
> > warnings, since the diagnostics should be errors with
> > -Wvariable-length-array -pedantic-errors.
>
> Good point. I'll reverse the condition and make -pedantic/-pedantic-errors
> override -Wvariable-length-array.

Attached is the modified patch.
I've started a new bootstrap & regtest with the change.
Thanks for the comment
(and thanks to Tom Tromey for catching a spelling error in ChangeLog).

ChangeLog
2007-02-06  Seongbae Park <seongbae.park@gmail.com>

       * gcc/doc/invoke.texi (Wvariable-length-array): New warning.


gcc/ChangeLog
 2007-02-06  Seongbae Park <seongbae.park@gmail.com>
       * c-decl.c (grokdeclarator): Support new flag warn_vla.
       Use "variable length array" instead of "variable-sized array".
       * c.opt (Wvariable-length-array): New flag.


gcc/cp/ChangeLog
2007-02-06  Seongbae Park <seongbae.park@gmail.com>

       * decl.c (compute_array_index_type): New warning flag warn_vla.


gcc/testsuite/ChangeLog
2007-02-06  Seongbae Park <seongbae.park@gmail.com>

       * gcc.dg/wvariable-length-array-1.c: New test
       * g++.dg/warn/Wvariable-length-array-1.C: New test

-- 
#pragma ident "Seongbae Park, compiler, http://seongbae.blogspot.com"
-------------- next part --------------
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 121663)
+++ gcc/doc/invoke.texi	(working copy)
@@ -252,7 +252,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wsystem-headers  -Wtrigraphs  -Wundef  -Wuninitialized @gol
 -Wunknown-pragmas  -Wno-pragmas -Wunreachable-code @gol
 -Wunused  -Wunused-function  -Wunused-label  -Wunused-parameter @gol
--Wunused-value  -Wunused-variable  -Wvariadic-macros @gol
+-Wunused-value  -Wunused-variable  -Wvariable-length-array @gol
+-Wvariadic-macros @gol
 -Wvolatile-register-var  -Wwrite-strings}
 
 @item C-only Warning Options
@@ -3533,6 +3534,11 @@ the warning messages, use @option{-Wno-l
 @option{-Wlong-long} and @option{-Wno-long-long} are taken into account
 only when @option{-pedantic} flag is used.
 
+@item -Wvariable-length-array
+@opindex Wvariable-length-array
+@opindex Wno-variable-length-array
+Warn if a variable length array is used, independent of @option{-pedantic}.
+
 @item -Wvariadic-macros
 @opindex Wvariadic-macros
 @opindex Wno-variadic-macros
Index: gcc/testsuite/gcc.dg/wvariable-length-array-1.c
===================================================================
--- gcc/testsuite/gcc.dg/wvariable-length-array-1.c	(revision 0)
+++ gcc/testsuite/gcc.dg/wvariable-length-array-1.c	(revision 0)
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options -Wvariable-length-array } */
+
+void func(int i)
+{
+  int array[i]; /* { dg-warning "Variable length array 'array' is used" } */
+}
Index: gcc/testsuite/g++.dg/warn/Wvariable-length-array-1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wvariable-length-array-1.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wvariable-length-array-1.C	(revision 0)
@@ -0,0 +1,8 @@
+// Test whether -Wvariable-length-array catches the case.
+// { dg-do compile }
+// { dg-options -Wvariable-length-array }
+
+void foo (int i)
+{
+  int array[i]; // { dg-warning "Variable length array 'array' is used" }
+}
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 121663)
+++ gcc/cp/decl.c	(working copy)
@@ -6641,9 +6641,18 @@ compute_array_index_type (tree name, tre
   else if (pedantic)
     {
       if (name)
-	pedwarn ("ISO C++ forbids variable-size array %qD", name);
+	pedwarn ("ISO C++ forbids variable length array %qD", name);
       else
-	pedwarn ("ISO C++ forbids variable-size array");
+	pedwarn ("ISO C++ forbids variable length array");
+    }
+  else if (warn_vla)
+    {
+      if (name)
+	warning (OPT_Wvariable_length_array, 
+                 "Variable length array %qD is used", name);
+      else
+	warning (OPT_Wvariable_length_array, 
+                 "Variable length array is used");
     }
 
   if (processing_template_decl && !TREE_CONSTANT (size))
Index: gcc/c-decl.c
===================================================================
--- gcc/c-decl.c	(revision 121663)
+++ gcc/c-decl.c	(working copy)
@@ -4320,9 +4320,20 @@ grokdeclarator (const struct c_declarato
 				   "can%'t be evaluated",
 				   name);
 			else
-			  pedwarn ("ISO C90 forbids variable-size array %qs",
+                          pedwarn ("ISO C90 forbids variable length array %qs",
 				   name);
 		      }
+		    else if (warn_vla)
+                      {
+			if (TREE_CONSTANT (size))
+                          warning (OPT_Wvariable_length_array,
+                                   "The size of array %qs can"
+                                   "%'t be evaluated", name);
+                        else
+                          warning (OPT_Wvariable_length_array,
+                                   "Variable length array %qs is used",
+                                   name);
+                      }
 		  }
 
 		if (integer_zerop (size))
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 121663)
+++ gcc/c.opt	(working copy)
@@ -442,6 +442,10 @@ Wunused-macros
 C ObjC C++ ObjC++
 Warn about macros defined in the main file that are not used
 
+Wvariable-length-array
+C ObjC C++ ObjC++ Var(warn_vla)
+Warn if a variable length array is used
+
 Wvariadic-macros
 C ObjC C++ ObjC++
 Do not warn about using variadic macros when -pedantic


More information about the Gcc-patches mailing list