Commit 26f6672d by Joseph Myers Committed by Joseph Myers

c-common.c (enum format_type): Add strfmon_format_type.

	* c-common.c (enum format_type): Add strfmon_format_type.
	(decl_attributes): Handle format attributes strfmon and
	__strfmon__.
	(FMT_FLAG_USE_DOLLAR, FMT_FLAG_ZERO_WIDTH_BAD,
	FMT_FLAG_EMPTY_PREC_OK): Define.
	(format_char_info): Update comment for flag_chars.
	(format_flag_spec): Add skip_next_char.
	(format_kind_info): Add left_precision_char.
	(printf_flag_specs, scanf_flag_specs, strftime_flag_specs,
	format_types): Update for these new structure members and flags.
	(time_char_table): Make const.
	(strfmon_length_specs, strfmon_flag_specs, strfmon_flag_pairs,
	monetary_char_table): New.
	(format_types): Add details of strfmon formats.
	(init_function_format_info): Create default attribute for strfmon.
	(check_format_info_main): Check the new flags.  Handle
	skip_next_char and left precision.
	* toplev.c (documented_lang_options): Update description of
	-Wformat.
	* extend.texi: Document strfmon format attributes.  Document
	attribute forms such as __printf__.  Clarify format_arg attribute
	documentation.
	* invoke.texi (-Wformat): Update for strfmon formats.

testsuite:
	* gcc.dg/format-strfmon-1.c: New test.

From-SVN: r38512
parent df7978d9
2000-12-28 Joseph S. Myers <jsm28@cam.ac.uk>
* c-common.c (enum format_type): Add strfmon_format_type.
(decl_attributes): Handle format attributes strfmon and
__strfmon__.
(FMT_FLAG_USE_DOLLAR, FMT_FLAG_ZERO_WIDTH_BAD,
FMT_FLAG_EMPTY_PREC_OK): Define.
(format_char_info): Update comment for flag_chars.
(format_flag_spec): Add skip_next_char.
(format_kind_info): Add left_precision_char.
(printf_flag_specs, scanf_flag_specs, strftime_flag_specs,
format_types): Update for these new structure members and flags.
(time_char_table): Make const.
(strfmon_length_specs, strfmon_flag_specs, strfmon_flag_pairs,
monetary_char_table): New.
(format_types): Add details of strfmon formats.
(init_function_format_info): Create default attribute for strfmon.
(check_format_info_main): Check the new flags. Handle
skip_next_char and left precision.
* toplev.c (documented_lang_options): Update description of
-Wformat.
* extend.texi: Document strfmon format attributes. Document
attribute forms such as __printf__. Clarify format_arg attribute
documentation.
* invoke.texi (-Wformat): Update for strfmon formats.
2000-12-28 Andreas Jaeger <aj@suse.de>
* expmed.c (store_bit_field): Fix last patch.
......
......@@ -1381,7 +1381,7 @@ hack ((union foo) x);
@cindex functions that behave like malloc
@cindex @code{volatile} applied to function
@cindex @code{const} applied to function
@cindex functions with @code{printf}, @code{scanf} or @code{strftime} style arguments
@cindex functions with @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon} style arguments
@cindex functions that are passed arguments in registers on the 386
@cindex functions that pop the argument stack on the 386
@cindex functions that do not pop the argument stack on the 386
......@@ -1505,8 +1505,9 @@ specifies that the @samp{const} must be attached to the return value.
@item format (@var{archetype}, @var{string-index}, @var{first-to-check})
@cindex @code{format} function attribute
The @code{format} attribute specifies that a function takes @code{printf},
@code{scanf}, or @code{strftime} style arguments which should be type-checked
against a format string. For example, the declaration:
@code{scanf}, @code{strftime} or @code{strfmon} style arguments which
should be type-checked against a format string. For example, the
declaration:
@smallexample
extern int
......@@ -1520,8 +1521,9 @@ for consistency with the @code{printf} style format string argument
@code{my_format}.
The parameter @var{archetype} determines how the format string is
interpreted, and should be either @code{printf}, @code{scanf}, or
@code{strftime}. The
interpreted, and should be @code{printf}, @code{scanf}, @code{strftime}
or @code{strfmon}. (You can also use @code{__printf__},
@code{__scanf__}, @code{__strftime__} or @code{__strfmon__}.) The
parameter @var{string-index} specifies which argument is the format
string argument (starting from 1), while @var{first-to-check} is the
number of the first argument to check against the format string. For
......@@ -1545,15 +1547,20 @@ for the standard library functions @code{printf}, @code{fprintf},
warnings are requested (using @samp{-Wformat}), so there is no need to
modify the header file @file{stdio.h}. In C99 mode, the functions
@code{snprintf}, @code{vsnprintf}, @code{vscanf}, @code{vfscanf} and
@code{vsscanf} are also checked.
@code{vsscanf} are also checked. Except in strictly conforming C
standard modes, the X/Open function @code{strfmon} is also checked.
@xref{C Dialect Options,,Options Controlling C Dialect}.
@item format_arg (@var{string-index})
@cindex @code{format_arg} function attribute
The @code{format_arg} attribute specifies that a function takes
@code{printf} or @code{scanf} style arguments, modifies it (for example,
to translate it into another language), and passes it to a @code{printf}
or @code{scanf} style function. For example, the declaration:
The @code{format_arg} attribute specifies that a function takes a format
string for a @code{printf}, @code{scanf}, @code{strftime} or
@code{strfmon} style function and modifies it (for example, to translate
it into another language), so the result can be passed to a
@code{printf}, @code{scanf}, @code{strftime} or @code{strfmon} style
function (with the remaining arguments to the format function the same
as they would have been for the unmodified string). For example, the
declaration:
@smallexample
extern char *
......@@ -1562,22 +1569,28 @@ my_dgettext (char *my_domain, const char *my_format)
@end smallexample
@noindent
causes the compiler to check the arguments in calls to
@code{my_dgettext} whose result is passed to a @code{printf},
@code{scanf}, or @code{strftime} type function for consistency with the
@code{printf} style format string argument @code{my_format}.
causes the compiler to check the arguments in calls to a @code{printf},
@code{scanf}, @code{strftime} or @code{strfmon} type function, whose
format string argument is a call to the @code{my_dgettext} function, for
consistency with the format string argument @code{my_format}. If the
@code{format_arg} attribute had not been specified, all the compiler
could tell in such calls to format functions would be that the format
string argument is not constant; this would generate a warning when
@code{-Wformat-nonliteral} is used, but the calls could not be checked
without the attribute.
The parameter @var{string-index} specifies which argument is the format
string argument (starting from 1).
The @code{format-arg} attribute allows you to identify your own
functions which modify format strings, so that GNU CC can check the
calls to @code{printf}, @code{scanf}, or @code{strftime} function whose
operands are a call to one of your own function. The compiler always
treats @code{gettext}, @code{dgettext}, and @code{dcgettext} in this
manner except when strict ISO C support is requested by @samp{-ansi} or
an appropriate @samp{-std} option, or @samp{-ffreestanding} is used.
@xref{C Dialect Options,,Options Controlling C Dialect}.
calls to @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon}
type function whose operands are a call to one of your own function.
The compiler always treats @code{gettext}, @code{dgettext}, and
@code{dcgettext} in this manner except when strict ISO C support is
requested by @samp{-ansi} or an appropriate @samp{-std} option, or
@samp{-ffreestanding} is used. @xref{C Dialect Options,,Options
Controlling C Dialect}.
@item no_instrument_function
@cindex @code{no_instrument_function} function attribute
......
......@@ -1596,7 +1596,11 @@ comment, or whenever a Backslash-Newline appears in a @samp{//} comment.
@item -Wformat
Check calls to @code{printf} and @code{scanf}, etc., to make sure that
the arguments supplied have types appropriate to the format string
specified.
specified, and that the conversions specified in the format string make
sense. This includes standard functions, and others specified by format
attributes (@pxref{Function Attributes}), in the @code{printf},
@code{scanf}, @code{strftime} and @code{strfmon} (an X/Open extension,
not in the C standard) families.
The formats are checked against the format features supported by GNU
libc version 2.2. These include all ISO C89 and C99 features, as well
......@@ -1605,8 +1609,9 @@ extensions. Other library implementations may not support all these
features; GCC does not support warning about features that go beyond a
particular library's limitations. However, if @samp{-pedantic} is used
with @samp{-Wformat}, warnings will be given about format features not
in the selected standard version. @xref{C Dialect Options,,Options
Controlling C Dialect}.
in the selected standard version (but not for @code{strfmon} formats,
since those are not in any version of the C standard). @xref{C Dialect
Options,,Options Controlling C Dialect}.
@samp{-Wformat} is included in @samp{-Wall}. For more control over some
aspects of format checking, the options @samp{-Wno-format-y2k},
......
2000-12-28 Joseph S. Myers <jsm28@cam.ac.uk>
* gcc.dg/format-strfmon-1.c: New test.
2000-12-27 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* gcc.c-torture/execute/stdio-opt-1.c: Test __builtin_ style too.
......
/* Test for strfmon format checking. */
/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
/* { dg-do compile } */
/* { dg-options "-std=gnu99 -Wformat" } */
typedef __SIZE_TYPE__ size_t;
/* Kludge to get something that may be ssize_t. */
#define unsigned signed
typedef __SIZE_TYPE__ ssize_t;
#undef unsigned
#define NULL ((void *)0)
extern ssize_t strfmon (char *restrict, size_t, const char *restrict, ...);
void
foo (char *s, size_t m, double d, long double ld)
{
/* Examples of valid formats from Austin Group draft 5. */
strfmon (s, m, "%n", d);
strfmon (s, m, "%11n", d);
strfmon (s, m, "%#5n", d);
strfmon (s, m, "%=*#5n", d);
strfmon (s, m, "%=0#5n", d);
strfmon (s, m, "%^#5n", d);
strfmon (s, m, "%^#5.0n", d);
strfmon (s, m, "%^#5.4n", d);
strfmon (s, m, "%(#5n", d);
strfmon (s, m, "%(!#5n", d);
/* Some more valid formats, including the GNU L length extension. */
strfmon (s, m, "abc%-11ndef%==i%%", d, d);
strfmon (s, m, "%%abc%-11ndef%==Li%=%i", d, ld, d);
strfmon (s, m, "%Li", ld);
strfmon (s, m, "%11Li", ld);
strfmon (s, m, "%#5Li", ld);
strfmon (s, m, "%=*#5Li", ld);
strfmon (s, m, "%=0#5Li", ld);
strfmon (s, m, "%^#5Li", ld);
strfmon (s, m, "%^#5.0Li", ld);
strfmon (s, m, "%^#5.4Li", ld);
strfmon (s, m, "%(#5Li", ld);
strfmon (s, m, "%(!#5Li", ld);
/* Formats with the wrong types used. */
strfmon (s, m, "%Ln", d); /* { dg-warning "format" "wrong type" } */
strfmon (s, m, "%n", ld); /* { dg-warning "format" "wrong type" } */
/* The + and ( flags cannot be used together. */
strfmon (s, m, "%+(i", d); /* { dg-warning "flag" "+ and ( flags" } */
strfmon (s, m, "%(+i", d); /* { dg-warning "flag" "+ and ( flags" } */
/* Although empty precision is OK for printf, it isn't here. */
strfmon (s, m, "%#.5n", d); /* { dg-warning "empty" "empty left precision" } */
strfmon (s, m, "%#5.n", d); /* { dg-warning "empty" "empty right precision" } */
/* However, zero is a valid value for width and precisions. */
strfmon (s, m, "%0#0.0n", d);
/* Test bogus %% constructions. */
strfmon (s, m, "%^%"); /* { dg-warning "format" "bogus %%" } */
strfmon (s, m, "%!%\n"); /* { dg-warning "format" "bogus %%" } */
strfmon (s, m, "%5%\n"); /* { dg-warning "format" "bogus %%" } */
strfmon (s, m, "%.5%\n"); /* { dg-warning "format" "bogus %%" } */
strfmon (s, m, "%#5%\n"); /* { dg-warning "format" "bogus %%" } */
/* Miscellaneous bogus formats. */
strfmon (s, m, "%n%n", d); /* { dg-warning "arguments" "too few args" } */
strfmon (s, m, ""); /* { dg-warning "zero-length" "empty" } */
strfmon (s, m, NULL); /* { dg-warning "null" "null format string" } */
strfmon (s, m, "%"); /* { dg-warning "trailing" "tailing %" } */
strfmon (s, m, "%n\0", d); /* { dg-warning "embedded" "embedded NUL" } */
strfmon (s, m, "%^^n", d); /* { dg-warning "repeated" "repeated flag" } */
}
......@@ -1226,7 +1226,7 @@ documented_lang_options[] =
{ "-Wno-comments", "" },
{ "-Wconversion", "Warn about possibly confusing type conversions" },
{ "-Wno-conversion", "" },
{ "-Wformat", "Warn about printf/scanf/strftime format anomalies" },
{ "-Wformat", "Warn about printf/scanf/strftime/strfmon format anomalies" },
{ "-Wno-format", "" },
{ "-Wformat-y2k", "" },
{ "-Wno-format-y2k",
......
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