You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Philip Martin <ph...@codematters.co.uk> on 2005/08/19 20:33:34 UTC

[PATCH] Tweak NLS for better gcc printf format checking

"Jonathan Gilbert" <o2...@sneakemail.com> writes:

> At 06:48 PM 19/08/2005 +0100, Philip Martin wrote:
>>Well, I haven't done much Subversion coding recently, but working on
>>r15828 leads me to make the following observations:
> [snip]
>>2. I18N
>>
>>   Using _("foo %s bar") defeats gcc's printf format string checking,
>>   if I insert '#define _(X) X' it starts working.  Is there some
>>   magic way to configure/build so that all the formats get checked?

I suspect --disable-nls is the required magic, at the expense of
needed a separate build to check formats.

> [snip]
>
> One approach would be to add an attribute to the declaration of '_',
> turning it into function:
>
> char *_(const char *msgid) __attribute__((__format_arg__(1),
> __always_inline__))
> {
>   return libintl_gettext(msgid);
> }
>
> This tells GCC that when '_' is passed as a format parameter to a
> printf-like function, the argument to '_' should be used for type-checking
> the arguments.

Looks promising, this seems to work

Index: configure.in
===================================================================
--- configure.in	(revision 15767)
+++ configure.in	(working copy)
@@ -299,9 +299,17 @@
 AH_BOTTOM(
 #define N_(x) x
 #ifdef ENABLE_NLS
+#ifndef SVN_GETTEXT
+#define SVN_GETTEXT
 #include <locale.h>
 #include <libintl.h>
-#define _(x) dgettext(PACKAGE_NAME, x)
+static char *_(const char *x)
+ __attribute__((__format_arg__(1), __always_inline__, __unused__));
+static char *_(const char *x)
+{
+  return dgettext(PACKAGE_NAME, x);
+}
+#endif /* SVN_GETTEXT */
 #else
 #define _(x) (x)
 #define gettext(x) (x)

I have to use a declaration/definition pair because my gcc doesn't
support attributes on definitions.  I have to use SVN_GETTEXT since
this ends up in svn_private_config.h which doesn't have include
guards (it's autoheader/autoconf generated). I have to use __unused__
because not all the files that include svn_private_config.h use _.

Compilers that don't support the gcc attributes may have problems.
__format_arg__ and __always_inline__ are probably OK, I suspect we
could drop __always_inline__ altogther.  A compiler that doesn't
support __unused__ may generate "defined bu not used" warnings, that
could be a problem.

What do people think?

-- 
Philip Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by Philip Martin <ph...@codematters.co.uk>.
"Peter N. Lundblad" <pe...@famlundblad.se> writes:

> Else I can live with this kludge, provided it doesn't impose other
> portability problems.

I think I'll just stop using -std=c89.  It's been in my config.nice
for ages, but Subversion stopped using it in r11054.

-- 
Philip Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by "Peter N. Lundblad" <pe...@famlundblad.se>.
On Sun, 21 Aug 2005, Philip Martin wrote:

> "Peter N. Lundblad" <pe...@famlundblad.se> writes:
>
> > I have something similar, *but* looking in the GCC manual it says that
> > gettext, dgettext and dcgettext are treated specially unless -ansi or some
> > -std option is specified. That's why it works for me.  (Look in the
> > section about the format_arg attribute).
>
> That's it!  I use -std=c89 to get warnings about inadvertent use of
> non-c89 features and that kills any warnings about _ format strings.
> With my patch I can get all the warnings.
>
>
I consider this a glibc bug that it depends of people not using -ansi (the
declaration of ngettext, for example, has these attribute declarations.).
And if we get "static function not used" warnings of other compilers, then
this is definitely not worth it (would someone want to test this?).

Else I can live with this kludge, provided it doesn't impose other
portability problems.

Regards,
//Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by Philip Martin <ph...@codematters.co.uk>.
"Peter N. Lundblad" <pe...@famlundblad.se> writes:

> I have something similar, *but* looking in the GCC manual it says that
> gettext, dgettext and dcgettext are treated specially unless -ansi or some
> -std option is specified. That's why it works for me.  (Look in the
> section about the format_arg attribute).

That's it!  I use -std=c89 to get warnings about inadvertent use of
non-c89 features and that kills any warnings about _ format strings.
With my patch I can get all the warnings.

-- 
Philip Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by "Peter N. Lundblad" <pe...@famlundblad.se>.
On Fri, 19 Aug 2005, Philip Martin wrote:

> "Peter N. Lundblad" <pe...@famlundblad.se> writes:
>
> > What GCC do you use?
>
> gcc version 4.0.1 20050610 (prerelease)
>
> > Format checking work on my GCC 4.0/glibc on Debina
> > testing.  That's because these magic attributes are used internally by
> > libintl.h.
>
> I'm using Debian/stable with libc6-dev_2.3.2.ds1-22.  My libintl.h has
>
> extern char *dgettext (__const char *__domainname, __const char *__msgid)
>      __THROW;
>
> so it doesn't check the formats.
>

I have something similar, *but* looking in the GCC manual it says that
gettext, dgettext and dcgettext are treated specially unless -ansi or some
-std option is specified. That's why it works for me.  (Look in the
section about the format_arg attribute).

Thansk,
//Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by Philip Martin <ph...@codematters.co.uk>.
"Peter N. Lundblad" <pe...@famlundblad.se> writes:

> What GCC do you use?

gcc version 4.0.1 20050610 (prerelease)

> Format checking work on my GCC 4.0/glibc on Debina
> testing.  That's because these magic attributes are used internally by
> libintl.h.

I'm using Debian/stable with libc6-dev_2.3.2.ds1-22.  My libintl.h has

extern char *dgettext (__const char *__domainname, __const char *__msgid)
     __THROW;

so it doesn't check the formats.

-- 
Philip Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Tweak NLS for better gcc printf format checking

Posted by "Peter N. Lundblad" <pe...@famlundblad.se>.
On Fri, 19 Aug 2005, Philip Martin wrote:

> "Jonathan Gilbert" <o2...@sneakemail.com> writes:
>
> > At 06:48 PM 19/08/2005 +0100, Philip Martin wrote:
> >>Well, I haven't done much Subversion coding recently, but working on
> >>r15828 leads me to make the following observations:
> > [snip]
> >>2. I18N
> >>
> >>   Using _("foo %s bar") defeats gcc's printf format string checking,
> >>   if I insert '#define _(X) X' it starts working.  Is there some
> >>   magic way to configure/build so that all the formats get checked?
>
> > [snip]
> >
> > One approach would be to add an attribute to the declaration of '_',
> > turning it into function:
> >
> > char *_(const char *msgid) __attribute__((__format_arg__(1),
> > __always_inline__))
> > {
> >   return libintl_gettext(msgid);
> > }
> >
> > This tells GCC that when '_' is passed as a format parameter to a
> > printf-like function, the argument to '_' should be used for type-checking
> > the arguments.
>
> Looks promising, this seems to work
>
[snip]

> What do people think?
>
>
What GCC do you use? Format checking work on my GCC 4.0/glibc on Debina
testing.  That's because these magic attributes are used internally by
libintl.h.

Regards,
//Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org