Patch to add option -Wmissing-format-attribute

Joseph S. Myers jsm28@cam.ac.uk
Fri Sep 29 15:46:00 GMT 2000


This patch adds an option -Wmissing-format-attribute to detect
functions which it might be appropriate to add format attributes to
(similar to -Wmissing-noreturn).

Bootstrapped with no regressions on i686-pc-linux-gnu.  OK to commit?

gcc/ChangeLog:
2000-09-29  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.

gcc/cp/ChangeLog:
2000-09-29  Joseph S. Myers  <jsm28@cam.ac.uk>

	* decl2.c (warn_missing_format_attribute): New variable.
	(lang_decode_option): Decode -Wmissing-format-attribute.

gcc/testsuite/ChangeLog:
2000-09-29  Joseph S. Myers  <jsm28@cam.ac.uk>

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

--- c-common.h.orig	Tue Sep 19 08:34:30 2000
+++ c-common.h	Fri Sep 29 18:57:43 2000
@@ -333,6 +333,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;
--- c-decl.c.orig	Wed Sep 20 19:45:51 2000
+++ c-decl.c	Fri Sep 29 18:49:37 2000
@@ -407,6 +407,10 @@ int warn_bad_function_cast;
 
 int warn_missing_noreturn;
 
+/* 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;
@@ -703,6 +707,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"))
--- c-common.c.orig	Wed Sep 27 10:53:48 2000
+++ c-common.c	Fri Sep 29 19:17:24 2000
@@ -1702,7 +1702,9 @@ record_international_format (name, assem
    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)
@@ -1722,6 +1724,20 @@ check_function_format (status, name, ass
 	{
 	  /* Yup; check it.  */
 	  check_format_info (status, info, params);
+	  if (warn_missing_format_attribute && info->first_arg_num == 0
+	      && info->format_type != strftime_format_type)
+	    {
+	      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;
 	}
     }
--- toplev.c.orig	Tue Sep 26 08:12:15 2000
+++ toplev.c	Fri Sep 29 18:45:37 2000
@@ -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'"},
--- invoke.texi.orig	Thu Sep 28 18:07:40 2000
+++ invoke.texi	Fri Sep 29 18:59:12 2000
@@ -1966,6 +1966,16 @@
 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.  
--- cp/decl2.c.orig	Thu Sep 21 22:09:38 2000
+++ cp/decl2.c	Fri Sep 29 18:53:05 2000
@@ -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"))
--- testsuite/gcc.dg/format-miss-1.c.orig	Fri Sep 11 11:31:59 1998
+++ testsuite/gcc.dg/format-miss-1.c	Fri Sep 29 20:57:28 2000
@@ -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" } */
+}

-- 
Joseph S. Myers
jsm28@cam.ac.uk



More information about the Gcc-patches mailing list