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

Jeffrey A Law law@cygnus.com
Sun Jan 10 13:57:00 GMT 1999


  In message < r2d84msuns.fsf@happy.cygnus.com >you write:
  > "Kaveh R. Ghazi" <ghazi@caip.rutgers.edu> writes:
  > OK, remember, this is for gcc.  We have special attributes for
  > functions like gettext.  Therefore
  > 
  > 	printf (gettext ("some string `%s'"), 10)
  > 
  > would lead to a warning.  The gettext call is transparent and I expect
  > a warning about using printf instead of fputs for
  > 
  > 	printf (gettext ("some constant string"))
  > 
  > But there are also situations where the replacement returned by a
  > gettext call with an argument without format specifiers contains
  > format specifiers.  A call could look like this:
  > 
  > 	printf (gettext ("no format here"), some_variable)
  > 
  > The return value of the gettext call could be "value %d".
And in that case we can not and must not optimize.

If the format specifier is not a compile time constant we can't optimize. 
What's so hard about that?


  > Now one can of course say this is not used.  I don't agree with this
  > since some of the gettext extensions which will come sometime will use
  > dirty tricks like this.  The other objections could be that the
  > gettext attribute should not be taken into account and therefore is
  > not replaced.  But this is completely against the purpose of this
  > attribute: if we user gets a warning s/he can look at the case and
  > decide whether the use of printf is necessary.  If the compiler simply
  > silently rejects this case and does not substitute fputs for printf
  > the programmer might never learn about the missed opportunity for an
  > optimization.
So, you're going to give up an optimization that applies to a large number
of programs because it does not apply to gettext.  That is absurd.  

  > A possibility is of course to perform automatic conversion in the case
  > where no such problem exists and warn in the other case (when using
  > gettext etc).  But this is IMO too inconsistent, better always go with
  > the warning and let the programmer decide.  In this situation it is
  > necessary, though, to have a way to shut up the compiler if the way
  > the code is written is correct.  Somebody said #pragma?
It's not inconsistent at all.  A compiler tries to optimzie as much as it
can, but when it is not safe it does not optimize.  There's no inconsistency
in doing that.

jeff



More information about the Gcc-patches mailing list