OSDN Git Service

* c-common.c (c_common_post_options): Warn if -Wformat-zero-length
authorthorpej <thorpej@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 May 2002 19:02:03 +0000 (19:02 +0000)
committerthorpej <thorpej@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 May 2002 19:02:03 +0000 (19:02 +0000)
is used without -Wformat.
* c-common.h (warn_format_zero_length): Declare extern.
* c-decl.c (warn_options): Add "format-zero-length".
* c-format.c (warn_format_zero_length): Declare.
(set_Wformat): Set warn_format_zero_length for -Wformat.
(check_format_info): Only warn about zero-length formats if
warn_format_zero_length is true.  Include the format type
name in the warning message.
* doc/invoke.texi: Document -Wformat-zero-length.
* testsuite/gcc.dg/format/zero-length-1.c: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@53592 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-common.c
gcc/c-common.h
gcc/c-decl.c
gcc/c-format.c
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/format/zero-length-1.c [new file with mode: 0644]

index 8e3b2c0..9951dc1 100644 (file)
@@ -1,3 +1,17 @@
+2002-05-18  Jason Thorpe  <thorpej@wasabisystems.com>
+
+       * c-common.c (c_common_post_options): Warn if -Wformat-zero-length
+       is used without -Wformat.  
+       * c-common.h (warn_format_zero_length): Declare extern.
+       * c-decl.c (warn_options): Add "format-zero-length".
+       * c-format.c (warn_format_zero_length): Declare.
+       (set_Wformat): Set warn_format_zero_length for -Wformat.
+       (check_format_info): Only warn about zero-length formats if
+       warn_format_zero_length is true.  Include the format type
+       name in the warning message.
+       * doc/invoke.texi: Document -Wformat-zero-length.
+       * testsuite/gcc.dg/format/zero-length-1.c: New test.
+
 2002-05-18  Kazu Hirata  <kazu@cs.umass.edu>
 
        * timevar.c: Fix formatting.
index 5a87899..d851c27 100644 (file)
@@ -4311,6 +4311,8 @@ c_common_post_options ()
     warning ("-Wformat-y2k ignored without -Wformat");
   if (warn_format_extra_args && !warn_format)
     warning ("-Wformat-extra-args ignored without -Wformat");
+  if (warn_format_zero_length && !warn_format)
+    warning ("-Wformat-zero-length ignored without -Wformat");
   if (warn_format_nonliteral && !warn_format)
     warning ("-Wformat-nonliteral ignored without -Wformat");
   if (warn_format_security && !warn_format)
index 7c5c30d..8666095 100644 (file)
@@ -414,6 +414,10 @@ extern int warn_format_y2k;
 
 extern int warn_format_extra_args;
 
+/* Warn about zero-length formats.  */
+
+extern int warn_format_zero_length;
+
 /* Warn about non-literal format arguments.  */
 
 extern int warn_format_nonliteral;
index 854cd73..8825db1 100644 (file)
@@ -474,6 +474,7 @@ c_decode_option (argc, argv)
     { "div-by-zero", &warn_div_by_zero },
     { "float-equal", &warn_float_equal },
     { "format-extra-args", &warn_format_extra_args },
+    { "format-zero-length", &warn_format_zero_length },
     { "format-nonliteral", &warn_format_nonliteral },
     { "format-security", &warn_format_security },
     { "format-y2k", &warn_format_y2k },
index 470ccf7..dc73f4d 100644 (file)
@@ -44,6 +44,10 @@ int warn_format_y2k;
 
 int warn_format_extra_args;
 
+/* Warn about zero-length formats.  */
+
+int warn_format_zero_length;
+
 /* Warn about non-literal format arguments.  */
 
 int warn_format_nonliteral;
@@ -61,6 +65,7 @@ set_Wformat (setting)
   warn_format = setting;
   warn_format_y2k = setting;
   warn_format_extra_args = setting;
+  warn_format_zero_length = setting;
   if (setting != 1)
     {
       warn_format_nonliteral = setting;
@@ -1361,8 +1366,9 @@ check_format_info (status, info, params)
       && res.number_other == 0 && warn_format_extra_args)
     status_warning (status, "unused arguments in $-style format");
   if (res.number_empty > 0 && res.number_non_literal == 0
-      && res.number_other == 0)
-    status_warning (status, "zero-length format string");
+      && res.number_other == 0 && warn_format_zero_length)
+    status_warning (status, "zero-length %s format string",
+                   format_types[info->format_type].name);
 
   if (res.number_wide > 0)
     status_warning (status, "format is a wide character string");
index 1190c97..855fcd9 100644 (file)
@@ -1844,9 +1844,9 @@ Options,,Options Controlling C Dialect}.
 
 @option{-Wformat} is included in @option{-Wall}.  For more control over some
 aspects of format checking, the options @option{-Wno-format-y2k},
-@option{-Wno-format-extra-args}, @option{-Wformat-nonliteral},
-@option{-Wformat-security} and @option{-Wformat=2} are available, but are
-not included in @option{-Wall}.
+@option{-Wno-format-extra-args}, @option{-Wno-format-zero-length},
+@option{-Wformat-nonliteral}, @option{-Wformat-security}, and
+@option{-Wformat=2} are available, but are not included in @option{-Wall}.
 
 @item -Wno-format-y2k
 @opindex Wno-format-y2k
@@ -1867,6 +1867,11 @@ in the case of @code{scanf} formats, this option will suppress the
 warning if the unused arguments are all pointers, since the Single
 Unix Specification says that such unused arguments are allowed.
 
+@item -Wno-format-zero-length
+@opindex Wno-format-zero-length
+If @option{-Wformat} is specified, do not warn about zero-length formats.
+The C standard specifies that zero-length formats are allowed.
+
 @item -Wformat-nonliteral
 @opindex Wformat-nonliteral
 If @option{-Wformat} is specified, also warn if the format string is not a
diff --git a/gcc/testsuite/gcc.dg/format/zero-length-1.c b/gcc/testsuite/gcc.dg/format/zero-length-1.c
new file mode 100644 (file)
index 0000000..4cf37f3
--- /dev/null
@@ -0,0 +1,15 @@
+/* Test the -Wno-format-zero-length option, which suppresses warnings
+   about zero-length formats.  */
+/* Origin: Jason Thorpe <thorpej@wasabisystems.com> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic -Wformat -Wno-format-zero-length" } */
+
+#include "format.h"
+
+void
+foo (void)
+{
+  /* See ISO/IEC 9899:1990 (E) subclause 7.9.6.1 (pages 131-134).  */
+  /* Zero-length format strings are allowed.  */
+  printf ("");
+}