This is the mail archive of the gcc@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]

patch to suggest putc/fputs over printf("string") or printf("\n")


	Given the following examples of actual code from egcs/gcc/cccp.c:

 >         fprintf (stderr, "\n");
 >     fprintf (stderr, "#include \"...\" search starts here:\n");
 >           sprintf (directive, " __STDC__ 1");

Do people think that the warnings appearing below would be useful?

cccp.c:1635: warning: suggest using `putc' instead of `fprintf'
cccp.c:2058: warning: suggest using `fputs' instead of `fprintf'
cccp.c:10232: warning: suggest using `strcpy' instead of `sprintf'

	I wrote a patch to do this, it piggy backs on top of the
-Wformat code.  I bootstrapped egcs and found some interesting stats.
There were ~1730 of these warnings.  Around 220 were printing just one
character, i.e. should have used putc.  There were only 9 sprintf
cases which should have been strcpy.  The rest (~1500) were calls that
should have been fputs.

	I don't know if there is any serious performance win when
making this conversion, but it seems logical that the overhead of
printf > fputs > putc when printing simple strings or single
characters.  If someone has more concrete info on this topic I'd like
to hear about it.  I seem to recall hearing something in the distant
past, but in the context of performing automatic optimizations of
these calls. (?)

	Anyway, how's this patch?

		--Kaveh


Sun Jan 10 01:10:26 1999  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* c-common.c (check_format_info): Suggest replacing calls to
 	printf, fprintf and sprintf with putc/fputs or strcpy if no
	arguments appear after the format specifier.

--- orig/egcs-CVS19990109/gcc/c-common.c	Sat Jan  9 11:55:26 1999
+++ egcs-CVS19990109/gcc/c-common.c	Sun Jan 10 00:15:55 1999
@@ -1471,6 +1471,24 @@ check_format_info (info, params)
 	    warning ("embedded `\\0' in format");
 	  if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
 	    warning ("too many arguments for format");
+
+	  /* If we made it this far, we passed.  Check if putc/fputs
+	     would have been a better choice. */
+	  if ((!strcmp(IDENTIFIER_POINTER(info->name), "printf")
+	      || !strcmp(IDENTIFIER_POINTER(info->name), "fprintf"))
+	      && !first_fillin_param)
+	    {
+	      warning ("suggest using `%s' instead of `%s'",
+		       (format_length == 1 ? "putc" : "fputs"),
+		       IDENTIFIER_POINTER(info->name));
+	    }
+	  /* Likewise for strcpy instead of sprintf. */
+	  if (!strcmp(IDENTIFIER_POINTER(info->name), "sprintf")
+	      && !first_fillin_param)
+	    {
+	      warning ("suggest using `strcpy' instead of `%s'",
+		       IDENTIFIER_POINTER(info->name));
+	    }
 	  return;
 	}
       if (*format_chars++ != '%')


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