diff options
author | Joseph Myers <jsm28@cam.ac.uk> | 2000-10-17 07:52:06 +0100 |
---|---|---|
committer | Joseph Myers <jsm28@gcc.gnu.org> | 2000-10-17 07:52:06 +0100 |
commit | 74ff46299b5d4b97dc736fc77fb3a2618c119e85 (patch) | |
tree | 0d78db7f736f6806e242f06d781a29dd0b8f8ae2 /gcc | |
parent | 1d3b0e2c196a53b8d0e0b8ecabf5b110f666c442 (diff) | |
download | gcc-74ff46299b5d4b97dc736fc77fb3a2618c119e85.zip gcc-74ff46299b5d4b97dc736fc77fb3a2618c119e85.tar.gz gcc-74ff46299b5d4b97dc736fc77fb3a2618c119e85.tar.bz2 |
c-common.h (warn_missing_format_attribute): New variable.
* c-common.h (warn_missing_format_attribute): New variable.
* c-decl.c (warn_missing_format_attribute): New variable.
(c_decode_option): Decode -Wmissing-format-attribute and
-Wno-missing-format-attribute.
* c-common.c (check_function_format): If
-Wmissing-format-attribute, give a warning where a vprintf or
vscanf function is called by a function without its own printf or
scanf attribute.
* toplev.c (documented_lang_options): Add
-Wmissing-format-attribute.
* invoke.texi: Document -Wmissing-format-attribute.
cp:
* decl2.c (warn_missing_format_attribute): New variable.
(lang_decode_option): Decode -Wmissing-format-attribute.
testsuite:
* gcc.dg/format-miss-1.c: New test.
From-SVN: r36897
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 14 | ||||
-rw-r--r-- | gcc/c-common.c | 18 | ||||
-rw-r--r-- | gcc/c-common.h | 4 | ||||
-rw-r--r-- | gcc/c-decl.c | 8 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/cp/decl2.c | 6 | ||||
-rw-r--r-- | gcc/invoke.texi | 10 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/format-miss-1.c | 42 | ||||
-rw-r--r-- | gcc/toplev.c | 3 |
10 files changed, 113 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a93203a..791233c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,17 @@ +2000-10-17 Joseph S. Myers <jsm28@cam.ac.uk> + + * c-common.h (warn_missing_format_attribute): New variable. + * c-decl.c (warn_missing_format_attribute): New variable. + (c_decode_option): Decode -Wmissing-format-attribute and + -Wno-missing-format-attribute. + * c-common.c (check_function_format): If + -Wmissing-format-attribute, give a warning where a vprintf or + vscanf function is called by a function without its own printf or + scanf attribute. + * toplev.c (documented_lang_options): Add + -Wmissing-format-attribute. + * invoke.texi: Document -Wmissing-format-attribute. + 2000-10-17 Marc Espie <espie@openbsd.org> * invoke.texi (-shared): Insist on requiring code generation flags diff --git a/gcc/c-common.c b/gcc/c-common.c index 3b88f18..db28750 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -1926,7 +1926,9 @@ record_international_format (name, assembler_name, format_num) NAME is the function identifier. ASSEMBLER_NAME is the function's assembler identifier. (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.) - PARAMS is the list of argument values. */ + PARAMS is the list of argument values. Also, if -Wmissing-format-attribute, + warn for calls to vprintf or vscanf in functions with no such format + attribute themselves. */ void check_function_format (status, name, assembler_name, params) @@ -1946,6 +1948,20 @@ check_function_format (status, name, assembler_name, params) { /* Yup; check it. */ check_format_info (status, info, params); + if (warn_missing_format_attribute && info->first_arg_num == 0 + && (format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT)) + { + function_format_info *info2; + for (info2 = function_format_list; info2; info2 = info2->next) + if ((info2->assembler_name + ? (info2->assembler_name == DECL_ASSEMBLER_NAME (current_function_decl)) + : (info2->name == DECL_NAME (current_function_decl))) + && info2->format_type == info->format_type) + break; + if (info2 == NULL) + warning ("function might be possible candidate for `%s' format attribute", + format_types[info->format_type].name); + } break; } } diff --git a/gcc/c-common.h b/gcc/c-common.h index 18eb5fb..3781adf 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -337,6 +337,10 @@ extern int flag_const_strings; extern int warn_format; +/* Warn about functions which might be candidates for format attributes. */ + +extern int warn_missing_format_attribute; + /* Nonzero means do some things the same way PCC does. */ extern int flag_traditional; diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 419d85f..f95cccc 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -416,6 +416,10 @@ int warn_cast_qual; int warn_bad_function_cast; +/* Warn about functions which might be candidates for format attributes. */ + +int warn_missing_format_attribute; + /* Warn about traditional constructs whose meanings changed in ANSI C. */ int warn_traditional; @@ -716,6 +720,10 @@ c_decode_option (argc, argv) warn_missing_noreturn = 1; else if (!strcmp (p, "-Wno-missing-noreturn")) warn_missing_noreturn = 0; + else if (!strcmp (p, "-Wmissing-format-attribute")) + warn_missing_format_attribute = 1; + else if (!strcmp (p, "-Wno-missing-format-attribute")) + warn_missing_format_attribute = 0; else if (!strcmp (p, "-Wpointer-arith")) warn_pointer_arith = 1; else if (!strcmp (p, "-Wno-pointer-arith")) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 262405c..b9c74ad 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2000-10-17 Joseph S. Myers <jsm28@cam.ac.uk> + + * decl2.c (warn_missing_format_attribute): New variable. + (lang_decode_option): Decode -Wmissing-format-attribute. + 2000-10-16 Mark Mitchell <mark@codesourcery.com> * typeck.c (qualify_type): Remove. diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 93c1be4..4f6b57e 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -290,6 +290,10 @@ int warn_float_equal = 0; int warn_format; +/* Warn about functions which might be candidates for format attributes. */ + +int warn_missing_format_attribute; + /* Warn about a subscript that has type char. */ int warn_char_subscripts; @@ -751,6 +755,8 @@ lang_decode_option (argc, argv) warn_float_equal = setting; else if (!strcmp (p, "format")) warn_format = setting; + else if (!strcmp (p, "missing-format-attribute")) + warn_missing_format_attribute = setting; else if (!strcmp (p, "conversion")) warn_conversion = setting; else if (!strcmp (p, "parentheses")) diff --git a/gcc/invoke.texi b/gcc/invoke.texi index 52586dd..47d4fd5 100644 --- a/gcc/invoke.texi +++ b/gcc/invoke.texi @@ -2012,6 +2012,16 @@ be taken to manually verify functions actually do not ever return before adding the @code{noreturn} attribute, otherwise subtle code generation bugs could be introduced. +@item -Wmissing-format-attribute +If @samp{-Wformat} is enabled, also warn about functions which might be +candidates for @code{format} attributes. Note these are only possible +candidates, not absolute ones. GCC will guess that @code{format} +attributes might be appropriate for any function that calls a function +like @code{vprintf} or @code{vscanf}, but this might not always be the +case, and some functions for which @code{format} attributes are +appropriate may not be detected. This option has no effect unless +@samp{-Wformat} is enabled (possibly by @samp{-Wall}). + @item -Wpacked Warn if a structure is given the packed attribute, but the packed attribute has no effect on the layout or size of the structure. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c78c097..04e3adb 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2000-10-17 Joseph S. Myers <jsm28@cam.ac.uk> + + * gcc.dg/format-miss-1.c: New test. + 2000-10-16 Jakub Jelinek <jakub@redhat.com> * gcc.c-torture/execute/20001013-1.c: New test. diff --git a/gcc/testsuite/gcc.dg/format-miss-1.c b/gcc/testsuite/gcc.dg/format-miss-1.c new file mode 100644 index 0000000..a5d467a --- /dev/null +++ b/gcc/testsuite/gcc.dg/format-miss-1.c @@ -0,0 +1,42 @@ +/* Test for warnings for missing format attributes. */ +/* Origin: Joseph Myers <jsm28@cam.ac.uk> */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu99 -Wformat -Wmissing-format-attribute" } */ + +#include <stdarg.h> + +extern int vprintf (const char *restrict, va_list); +extern int vscanf (const char *restrict, va_list); + +void +foo (const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + vprintf (fmt, ap); /* { dg-warning "candidate" "printf attribute warning" } */ + va_end (ap); +} + +void +bar (const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + vscanf (fmt, ap); /* { dg-warning "candidate" "scanf attribute warning" } */ + va_end (ap); +} + +__attribute__((__format__(__printf__, 1, 2))) void +foo2 (const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + vprintf (fmt, ap); + va_end (ap); +} + +void +vfoo (const char *fmt, va_list arg) +{ + vprintf (fmt, arg); /* { dg-warning "candidate" "printf attribute warning 2" } */ +} diff --git a/gcc/toplev.c b/gcc/toplev.c index 15e1f65..cccdb35 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1207,6 +1207,9 @@ documented_lang_options[] = { "-Wmissing-noreturn", "Warn about functions which might be candidates for attribute noreturn" }, { "-Wno-missing-noreturn", "" }, + { "-Wmissing-format-attribute", + "Warn about functions which might be candidates for format attributes" }, + { "-Wno-missing-format-attribute", "" }, { "-Wcast-qual", "Warn about casts which discard qualifiers"}, { "-Wno-cast-qual", "" }, { "-Wchar-subscripts", "Warn about subscripts whose type is 'char'"}, |