c-common.h (warn_missing_format_attribute): New variable.
authorJoseph Myers <jsm28@cam.ac.uk>
Tue, 17 Oct 2000 06:52:06 +0000 (07:52 +0100)
committerJoseph Myers <jsm28@gcc.gnu.org>
Tue, 17 Oct 2000 06:52:06 +0000 (07:52 +0100)
* 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

gcc/ChangeLog
gcc/c-common.c
gcc/c-common.h
gcc/c-decl.c
gcc/cp/ChangeLog
gcc/cp/decl2.c
gcc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/format-miss-1.c [new file with mode: 0644]
gcc/toplev.c

index a93203aa253fa8d078dbb0d247164fb61e4268f2..791233cc391337154cc21464e14c1aefa158686e 100644 (file)
@@ -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
index 3b88f187118946c86b3357210932a53f9394bfd9..db28750bc5698a94241b1df142a1ab13c08432d7 100644 (file)
@@ -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;
        }
     }
index 18eb5fb54f74f794cd37341a6378ba03240b3f6b..3781adffdad2155c02f03af5caef198ff21e749d 100644 (file)
@@ -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;
index 419d85f3c59f08d4edf836630f2528aac00e6029..f95cccc7b488f507dc25b594f74cc3f414da507c 100644 (file)
@@ -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"))
index 262405c1c4a4a5ac500cba11ef655041221c2093..b9c74aded94a2720c7f415848bf75f7f59d857d8 100644 (file)
@@ -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.
index 93c1be4b13d4e4fc75b75aa2a287f4ef70bf951b..4f6b57e15d78b77dbc2d85a02d9b002f814fae26 100644 (file)
@@ -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"))
index 52586dd044797bca997cb3ee03a44efeea5bb51b..47d4fd53e0f6715b119e70340175de5829b7bdf8 100644 (file)
@@ -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.  
index c78c0979e863e2509cfdf7c62547df0e84bde9e4..04e3adb6cab2ee398d5bc6c59897abfa7fe4b29c 100644 (file)
@@ -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 (file)
index 0000000..a5d467a
--- /dev/null
@@ -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" } */
+}
index 15e1f65dc62c086bd22918542d6c747df0fb3cd4..cccdb3521474fc5a65d72cba199351c089cc5f62 100644 (file)
@@ -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'"},