Commit e964a556 by Jason Thorpe Committed by Jason Thorpe

c-common.c (c_common_post_options): Warn if -Wformat-zero-length is used without -Wformat.

* 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.

From-SVN: r53592
parent d92b4486
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> 2002-05-18 Kazu Hirata <kazu@cs.umass.edu>
* timevar.c: Fix formatting. * timevar.c: Fix formatting.
......
...@@ -4311,6 +4311,8 @@ c_common_post_options () ...@@ -4311,6 +4311,8 @@ c_common_post_options ()
warning ("-Wformat-y2k ignored without -Wformat"); warning ("-Wformat-y2k ignored without -Wformat");
if (warn_format_extra_args && !warn_format) if (warn_format_extra_args && !warn_format)
warning ("-Wformat-extra-args ignored without -Wformat"); 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) if (warn_format_nonliteral && !warn_format)
warning ("-Wformat-nonliteral ignored without -Wformat"); warning ("-Wformat-nonliteral ignored without -Wformat");
if (warn_format_security && !warn_format) if (warn_format_security && !warn_format)
......
...@@ -414,6 +414,10 @@ extern int warn_format_y2k; ...@@ -414,6 +414,10 @@ extern int warn_format_y2k;
extern int warn_format_extra_args; extern int warn_format_extra_args;
/* Warn about zero-length formats. */
extern int warn_format_zero_length;
/* Warn about non-literal format arguments. */ /* Warn about non-literal format arguments. */
extern int warn_format_nonliteral; extern int warn_format_nonliteral;
......
...@@ -474,6 +474,7 @@ c_decode_option (argc, argv) ...@@ -474,6 +474,7 @@ c_decode_option (argc, argv)
{ "div-by-zero", &warn_div_by_zero }, { "div-by-zero", &warn_div_by_zero },
{ "float-equal", &warn_float_equal }, { "float-equal", &warn_float_equal },
{ "format-extra-args", &warn_format_extra_args }, { "format-extra-args", &warn_format_extra_args },
{ "format-zero-length", &warn_format_zero_length },
{ "format-nonliteral", &warn_format_nonliteral }, { "format-nonliteral", &warn_format_nonliteral },
{ "format-security", &warn_format_security }, { "format-security", &warn_format_security },
{ "format-y2k", &warn_format_y2k }, { "format-y2k", &warn_format_y2k },
......
...@@ -44,6 +44,10 @@ int warn_format_y2k; ...@@ -44,6 +44,10 @@ int warn_format_y2k;
int warn_format_extra_args; int warn_format_extra_args;
/* Warn about zero-length formats. */
int warn_format_zero_length;
/* Warn about non-literal format arguments. */ /* Warn about non-literal format arguments. */
int warn_format_nonliteral; int warn_format_nonliteral;
...@@ -61,6 +65,7 @@ set_Wformat (setting) ...@@ -61,6 +65,7 @@ set_Wformat (setting)
warn_format = setting; warn_format = setting;
warn_format_y2k = setting; warn_format_y2k = setting;
warn_format_extra_args = setting; warn_format_extra_args = setting;
warn_format_zero_length = setting;
if (setting != 1) if (setting != 1)
{ {
warn_format_nonliteral = setting; warn_format_nonliteral = setting;
...@@ -1361,8 +1366,9 @@ check_format_info (status, info, params) ...@@ -1361,8 +1366,9 @@ check_format_info (status, info, params)
&& res.number_other == 0 && warn_format_extra_args) && res.number_other == 0 && warn_format_extra_args)
status_warning (status, "unused arguments in $-style format"); status_warning (status, "unused arguments in $-style format");
if (res.number_empty > 0 && res.number_non_literal == 0 if (res.number_empty > 0 && res.number_non_literal == 0
&& res.number_other == 0) && res.number_other == 0 && warn_format_zero_length)
status_warning (status, "zero-length format string"); status_warning (status, "zero-length %s format string",
format_types[info->format_type].name);
if (res.number_wide > 0) if (res.number_wide > 0)
status_warning (status, "format is a wide character string"); status_warning (status, "format is a wide character string");
......
...@@ -1844,9 +1844,9 @@ Options,,Options Controlling C Dialect}. ...@@ -1844,9 +1844,9 @@ Options,,Options Controlling C Dialect}.
@option{-Wformat} is included in @option{-Wall}. For more control over some @option{-Wformat} is included in @option{-Wall}. For more control over some
aspects of format checking, the options @option{-Wno-format-y2k}, aspects of format checking, the options @option{-Wno-format-y2k},
@option{-Wno-format-extra-args}, @option{-Wformat-nonliteral}, @option{-Wno-format-extra-args}, @option{-Wno-format-zero-length},
@option{-Wformat-security} and @option{-Wformat=2} are available, but are @option{-Wformat-nonliteral}, @option{-Wformat-security}, and
not included in @option{-Wall}. @option{-Wformat=2} are available, but are not included in @option{-Wall}.
@item -Wno-format-y2k @item -Wno-format-y2k
@opindex Wno-format-y2k @opindex Wno-format-y2k
...@@ -1867,6 +1867,11 @@ in the case of @code{scanf} formats, this option will suppress the ...@@ -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 warning if the unused arguments are all pointers, since the Single
Unix Specification says that such unused arguments are allowed. 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 @item -Wformat-nonliteral
@opindex Wformat-nonliteral @opindex Wformat-nonliteral
If @option{-Wformat} is specified, also warn if the format string is not a If @option{-Wformat} is specified, also warn if the format string is not a
......
/* 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 ("");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment