This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH]: PR c/22476 -Wmissing-format-attribute [take 4]


 > In principle it would be best for the options to be completely
 > independent.  That is, check_function_arguments would call
 > check_function_format if either option is enabled, and
 > check_function_format would then only call check_format_info if
 > -Wformat is enabled.  (Plus of course removal of the warning for
 > -Wmissing-format-attribute on its own, updating of the documentation
 > accordingly, removal of gcc.dg/format/opt-6.c which tests for the
 > warning and adding a test that -Wmissing-format-attribute on its own
 > enables the current warnings.)

This revision of my patch implements your suggestions above.
Bootstraped and tested on x86_64-unknown-linux-gnu, no regressions.

Okay for 4.2?  Okay for 4.1?

		Thanks,
		--Kaveh


2005-07-18  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	PR c/22476
	* c-common.c (check_function_arguments): Call
	'check_function_format' if either -Wformat or
	-Wmissing-format-attribute are specified.
	* c-format.c (check_function_format): Check -Wformat before
	calling 'check_format_info'.
	* c-opts.c (c_common_post_options): Don't warn for
	-Wmissing-format-attribute without -Wformat.
	* c-typeck.c (convert_for_assignment): Detect additional cases for
	-Wmissing-format-attribute.
	* doc/invoke.texi (-Wmissing-format-attribute): Document new
	behavior.

testsuite:
	* gcc.dg/format/miss-1.c, gcc.dg/format/miss-2.c: Don't
	specify -Wformat for these tests.
	* gcc.dg/format/miss-3.c, gcc.dg/format/miss-4.c,
	gcc.dg/format/miss-5.c, gcc.dg/format/miss-6.c: New.
	* gcc.dg/format/opt-6.c: Delete.
	
diff -rup orig/egcc-CVS20050718/gcc/c-common.c egcc-CVS20050718/gcc/c-common.c
--- orig/egcc-CVS20050718/gcc/c-common.c	2005-07-15 23:18:30.000000000 -0400
+++ egcc-CVS20050718/gcc/c-common.c	2005-07-18 04:56:10.000000000 -0400
@@ -5505,11 +5505,11 @@ check_function_arguments (tree attrs, tr
 
   /* Check for errors in format strings.  */
 
-  if (warn_format)
-    {
+  if (warn_format || warn_missing_format_attribute)
       check_function_format (attrs, params);
-      check_function_sentinel (attrs, params, typelist);
-    }
+
+  if (warn_format)
+    check_function_sentinel (attrs, params, typelist);
 }
 
 /* Generic argument checking recursion routine.  PARAM is the argument to
diff -rup orig/egcc-CVS20050718/gcc/c-format.c egcc-CVS20050718/gcc/c-format.c
--- orig/egcc-CVS20050718/gcc/c-format.c	2005-07-03 21:40:00.000000000 -0400
+++ egcc-CVS20050718/gcc/c-format.c	2005-07-18 04:59:20.000000000 -0400
@@ -864,7 +864,8 @@ check_function_format (tree attrs, tree 
 	  /* Yup; check it.  */
 	  function_format_info info;
 	  decode_format_attr (TREE_VALUE (a), &info, 1);
-	  check_format_info (&info, params);
+	  if (warn_format)
+	    check_format_info (&info, params);
 	  if (warn_missing_format_attribute && info.first_arg_num == 0
 	      && (format_types[info.format_type].flags
 		  & (int) FMT_FLAG_ARG_CONVERT))
diff -rup orig/egcc-CVS20050718/gcc/c-opts.c egcc-CVS20050718/gcc/c-opts.c
--- orig/egcc-CVS20050718/gcc/c-opts.c	2005-06-25 22:37:13.000000000 -0400
+++ egcc-CVS20050718/gcc/c-opts.c	2005-07-18 05:00:27.000000000 -0400
@@ -991,8 +991,6 @@ c_common_post_options (const char **pfil
 	       "-Wformat-nonliteral ignored without -Wformat");
       warning (OPT_Wformat_security,
 	       "-Wformat-security ignored without -Wformat");
-      warning (OPT_Wmissing_format_attribute,
-	       "-Wmissing-format-attribute ignored without -Wformat");
     }
 
   /* C99 requires special handling of complex multiplication and division;
diff -rup orig/egcc-CVS20050718/gcc/c-typeck.c egcc-CVS20050718/gcc/c-typeck.c
--- orig/egcc-CVS20050718/gcc/c-typeck.c	2005-07-16 10:38:02.000000000 -0400
+++ egcc-CVS20050718/gcc/c-typeck.c	2005-07-18 04:53:25.000000000 -0400
@@ -3793,6 +3793,55 @@ convert_for_assignment (tree type, tree 
         warning (OPT_Wc___compat, "request for implicit conversion from "
                  "%qT to %qT not permitted in C++", rhstype, type);
 
+      /* Check if the right-hand side has a format attribute but the
+	 left-hand side doesn't.  */
+      if (warn_missing_format_attribute)
+        {
+	  tree rattrs = TYPE_ATTRIBUTES (ttr), ra;
+	  for (ra = rattrs; ra; ra = TREE_CHAIN (ra))
+	    {
+	      if (is_attribute_p ("format", TREE_PURPOSE (ra)))
+		break;
+	    }
+	  if (ra)
+	    {
+	      tree lattrs = TYPE_ATTRIBUTES (ttl), la;
+	      for (la = lattrs; la; la = TREE_CHAIN (la))
+	      {
+		if (is_attribute_p ("format", TREE_PURPOSE (la)))
+		  break;
+	      }
+	      if (!la)
+		switch (errtype)
+		  {
+		  case ic_argpass:
+		  case ic_argpass_nonproto:
+		    warning (OPT_Wmissing_format_attribute,
+			     "argument %d of %qE might be "
+			     "a candidate for a format attribute",
+			     parmnum, rname);
+		    break;
+		  case ic_assign:
+		    warning (OPT_Wmissing_format_attribute,
+			     "assignment left-hand side might be "
+			     "a candidate for a format attribute");
+		    break;
+		  case ic_init:
+		    warning (OPT_Wmissing_format_attribute,
+			     "initialization left-hand side might be "
+			     "a candidate for a format attribute");
+		    break;
+		  case ic_return:
+		    warning (OPT_Wmissing_format_attribute,
+			     "return type might be "
+			     "a candidate for a format attribute");
+		    break;
+		  default:
+		    gcc_unreachable ();
+		  }
+	    }
+	}
+      
       /* Any non-function converts to a [const][volatile] void *
 	 and vice versa; otherwise, targets must be the same.
 	 Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
diff -rup orig/egcc-CVS20050718/gcc/doc/invoke.texi egcc-CVS20050718/gcc/doc/invoke.texi
--- orig/egcc-CVS20050718/gcc/doc/invoke.texi	2005-07-13 21:43:26.000000000 -0400
+++ egcc-CVS20050718/gcc/doc/invoke.texi	2005-07-18 05:01:55.000000000 -0400
@@ -3126,14 +3126,23 @@ hosted C environments.
 @item -Wmissing-format-attribute
 @opindex Wmissing-format-attribute
 @opindex Wformat
-If @option{-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
+Warn about function pointers which might be candidates for @code{format}
+attributes.  Note these are only possible candidates, not absolute ones.
+GCC will guess that function pointers with @code{format} attributes that
+are used in assignment, initialization, parameter passing or return
+statements should have a corresponding @code{format} attribute in the
+resulting type.  I.e.@: the left-hand side of the assignment or
+initialization, the type of the parameter variable, or the return type
+of the containing function respectively should also have a @code{format}
+attribute to avoid the warning.
+
+GCC will also warn about function definitions which might be
+candidates for @code{format} attributes.  Again, these are only
+possible candidates.  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
-@option{-Wformat} is enabled (possibly by @option{-Wall}).
+appropriate may not be detected.
 
 @item -Wno-multichar
 @opindex Wno-multichar
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-1.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-1.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-1.c	2001-01-07 05:44:59.000000000 -0500
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-1.c	2005-07-18 05:02:27.000000000 -0400
@@ -1,7 +1,7 @@
 /* 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" } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
 
 #include "format.h"
 
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-2.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-2.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-2.c	2001-01-07 05:44:59.000000000 -0500
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-2.c	2005-07-18 05:02:35.000000000 -0400
@@ -2,7 +2,7 @@
    relevant parameters for a format attribute; see c/1017.  */
 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
 /* { dg-do compile } */
-/* { dg-options "-std=gnu99 -Wformat -Wmissing-format-attribute" } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
 
 #include "format.h"
 
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-3.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-3.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-3.c	2005-07-18 05:04:35.000000000 -0400
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-3.c	2005-07-18 05:02:42.000000000 -0400
@@ -0,0 +1,26 @@
+/* Test warnings for missing format attributes on function pointers.  */
+/* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
+
+#include "format.h"
+
+typedef void (*noattr_t) (const char *, ...);
+typedef noattr_t __attribute__ ((__format__(__printf__, 1, 2))) attr_t;
+
+typedef void (*vnoattr_t) (const char *, va_list);
+typedef vnoattr_t __attribute__ ((__format__(__printf__, 1, 0))) vattr_t;
+
+void
+foo1 (noattr_t na, attr_t a, vnoattr_t vna, vattr_t va)
+{
+  noattr_t na1 = na;
+  noattr_t na2 = a; /* { dg-warning "candidate" "initialization warning" } */
+  attr_t a1 = na;
+  attr_t a2 = a;
+  
+  vnoattr_t vna1 = vna;
+  vnoattr_t vna2 = va; /* { dg-warning "candidate" "initialization warning" } */
+  vattr_t va1 = vna;
+  vattr_t va2 = va;
+}
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-4.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-4.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-4.c	2005-07-18 05:04:38.000000000 -0400
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-4.c	2005-07-18 05:02:46.000000000 -0400
@@ -0,0 +1,32 @@
+/* Test warnings for missing format attributes on function pointers.  */
+/* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
+
+#include "format.h"
+
+typedef void (*noattr_t) (const char *, ...);
+typedef noattr_t __attribute__ ((__format__(__printf__, 1, 2))) attr_t;
+
+typedef void (*vnoattr_t) (const char *, va_list);
+typedef vnoattr_t __attribute__ ((__format__(__printf__, 1, 0))) vattr_t;
+
+void
+foo1 (noattr_t na, attr_t a, vnoattr_t vna, vattr_t va)
+{
+  noattr_t na1, na2;
+  attr_t a1, a2;
+  
+  vnoattr_t vna1, vna2;
+  vattr_t va1, va2;
+
+  na1 = na;
+  na2 = a; /* { dg-warning "candidate" "assignment warning" } */
+  a1 = na;
+  a2 = a;
+  
+  vna1 = vna;
+  vna2 = va; /* { dg-warning "candidate" "assignment warning" } */
+  va1 = vna;
+  va1 = va;
+}
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-5.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-5.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-5.c	2005-07-18 05:04:40.000000000 -0400
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-5.c	2005-07-18 05:02:56.000000000 -0400
@@ -0,0 +1,48 @@
+/* Test warnings for missing format attributes on function pointers.  */
+/* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
+
+#include "format.h"
+
+typedef void (*noattr_t) (const char *, ...);
+typedef noattr_t __attribute__ ((__format__(__printf__, 1, 2))) attr_t;
+
+typedef void (*vnoattr_t) (const char *, va_list);
+typedef vnoattr_t __attribute__ ((__format__(__printf__, 1, 0))) vattr_t;
+
+noattr_t
+foo1 (noattr_t na, attr_t a, int i)
+{
+  if (i)
+    return na;
+  else
+    return a; /* { dg-warning "candidate" "return type warning" } */
+}
+
+attr_t
+foo2 (noattr_t na, attr_t a, int i)
+{
+  if (i)
+    return na;
+  else
+    return a;
+}
+
+vnoattr_t
+foo3 (vnoattr_t vna, vattr_t va, int i)
+{
+  if (i)
+    return vna;
+  else
+    return va; /* { dg-warning "candidate" "return type warning" } */
+}
+
+vattr_t
+foo4 (vnoattr_t vna, vattr_t va, int i)
+{
+  if (i)
+    return vna;
+  else
+    return va;
+}
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-6.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-6.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-6.c	2005-07-18 05:04:42.000000000 -0400
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/miss-6.c	2005-07-18 05:03:04.000000000 -0400
@@ -0,0 +1,31 @@
+/* Test warnings for missing format attributes on function pointers.  */
+/* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wmissing-format-attribute" } */
+
+#include "format.h"
+
+typedef void (*noattr_t) (const char *, ...);
+typedef noattr_t __attribute__ ((__format__(__printf__, 1, 2))) attr_t;
+
+typedef void (*vnoattr_t) (const char *, va_list);
+typedef vnoattr_t __attribute__ ((__format__(__printf__, 1, 0))) vattr_t;
+
+extern void foo1 (noattr_t);
+extern void foo2 (attr_t);
+extern void foo3 (vnoattr_t);
+extern void foo4 (vattr_t);
+
+void
+foo (noattr_t na, attr_t a, vnoattr_t vna, vattr_t va)
+{
+  foo1 (na);
+  foo1 (a); /* { dg-warning "candidate" "parameter passing warning" } */
+  foo2 (na);
+  foo2 (a);
+  
+  foo3 (vna);
+  foo3 (va); /* { dg-warning "candidate" "parameter passing warning" } */
+  foo4 (vna);
+  foo4 (va);
+}
diff -rup orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/opt-6.c egcc-CVS20050718/gcc/testsuite/gcc.dg/format/opt-6.c
--- orig/egcc-CVS20050718/gcc/testsuite/gcc.dg/format/opt-6.c	2004-11-27 21:16:57.000000000 -0500
+++ egcc-CVS20050718/gcc/testsuite/gcc.dg/format/opt-6.c	2005-07-18 05:04:15.000000000 -0400
@@ -1,7 +0,0 @@
-/* Test diagnostics for options used on their own without
-   -Wformat.  -Wmissing-format-attribute.  */
-/* Origin: Joseph Myers <joseph@codesourcery.com> */
-/* { dg-do compile } */
-/* { dg-options "-Wmissing-format-attribute" } */
-
-/* { dg-warning "warning: -Wmissing-format-attribute ignored without -Wformat" "ignored" { target *-*-* } 0 } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]