]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Improve the c-format guessing heuristic.
authorBruno Haible <bruno@clisp.org>
Mon, 13 Feb 2006 13:09:17 +0000 (13:09 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:12:59 +0000 (12:12 +0200)
23 files changed:
gettext-tools/src/ChangeLog
gettext-tools/src/format-awk.c
gettext-tools/src/format-c.c
gettext-tools/src/format-csharp.c
gettext-tools/src/format-elisp.c
gettext-tools/src/format-gcc-internal.c
gettext-tools/src/format-java.c
gettext-tools/src/format-librep.c
gettext-tools/src/format-lisp.c
gettext-tools/src/format-pascal.c
gettext-tools/src/format-perl-brace.c
gettext-tools/src/format-perl.c
gettext-tools/src/format-php.c
gettext-tools/src/format-python.c
gettext-tools/src/format-qt.c
gettext-tools/src/format-scheme.c
gettext-tools/src/format-sh.c
gettext-tools/src/format-tcl.c
gettext-tools/src/format-ycp.c
gettext-tools/src/format.h
gettext-tools/src/xgettext.c
gettext-tools/tests/ChangeLog
gettext-tools/tests/Makefile.am

index 56de0e849e7fdcff3d492e1f8518e02a85071f2b..588f18629d60b41c27f14e29f384106b1e359af0 100644 (file)
@@ -1,3 +1,35 @@
+2006-02-12  Bruno Haible  <bruno@clisp.org>
+
+       Reduce the number of false positives produced by the c-format guessing
+       heuristics.
+       * format.h (struct formatstring_parser): Add 'is_unlikely_intentional'
+       field.
+       * format-awk.c (formatstring_awk): Update.
+       * format-c.c (struct spec): Add 'unlikely_unintentional' field.
+       (format_parse): Set unlikely_unintentional to true when encountering
+       a "%...%" directive other than "%%".
+       (format_is_unlikely_intentional): New function.
+       (formatstring_c, formatstring_objc): Use it.
+       * format-csharp.c (formatstring_csharp): Update.
+       * format-elisp.c (formatstring_elisp): Update.
+       * format-gcc-internal.c (formatstring_gcc_internal): Update.
+       * format-java.c (formatstring_java): Update.
+       * format-librep.c (formatstring_librep): Update.
+       * format-lisp.c (formatstring_lisp): Update.
+       * format-pascal.c (formatstring_pascal): Update.
+       * format-perl.c (formatstring_perl): Update.
+       * format-perl-brace.c (formatstring_perl_brace): Update.
+       * format-php.c (format_php): Update.
+       * format-python.c (formatstring_python): Update.
+       * format-qt.c (formatstring_qt): Update.
+       * format-scheme.c (formatstring_scheme): Update
+       * format-sh.c (formatstring_sh): Update.
+       * format-tcl.c (formatstring_tcl): Update.
+       * format-ycp.c (formatstring_ycp, formatstring_smalltalk): Update.
+       * xgettext.c (remember_a_message, remember_a_message_plural): Don't
+       mark the message as c-format if the validity as c-format looks
+       unintentional.
+
 2006-02-12  Bruno Haible  <bruno@clisp.org>
 
        * msgattrib.c (main): Update year in --version output.
index b290f40ecd30612431182f53476d83dcf61e0c74..d540da69e4192237f746139810d1ebc4179950b7 100644 (file)
@@ -1,5 +1,5 @@
 /* awk format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2002.
 
    This program is free software; you can redistribute it and/or modify
@@ -537,6 +537,7 @@ struct formatstring_parser formatstring_awk =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index be12d2485a8f0d6f86a4cdb1eb64f8bfa0928dc1..e31bebff3382b0f6cc5e3dc9212f7add7b4bb313 100644 (file)
@@ -1,5 +1,5 @@
 /* C format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -152,6 +152,7 @@ struct spec
   unsigned int unnumbered_arg_count;
   unsigned int allocated;
   struct unnumbered_arg *unnumbered;
+  bool unlikely_intentional;
   unsigned int sysdep_directives_count;
   const char **sysdep_directives;
 };
@@ -190,6 +191,7 @@ format_parse (const char *format, bool translated, bool objc_extensions,
   spec.allocated = 0;
   numbered = NULL;
   spec.unnumbered = NULL;
+  spec.unlikely_intentional = false;
   spec.sysdep_directives_count = 0;
   spec.sysdep_directives = NULL;
 
@@ -608,6 +610,13 @@ format_parse (const char *format, bool translated, bool objc_extensions,
            switch (*format)
              {
              case '%':
+               /* Programmers writing _("%2%") most often will not want to
+                  use this string as a c-format string, but rather as a
+                  literal or as a different kind of format string.  */
+               if (format[-1] != '%')
+                 spec.unlikely_intentional = true;
+               type = FAT_NONE;
+               break;
              case 'm': /* glibc extension */
                type = FAT_NONE;
                break;
@@ -820,6 +829,14 @@ format_free (void *descr)
   free (spec);
 }
 
+static bool
+format_is_unlikely_intentional (void *descr)
+{
+  struct spec *spec = (struct spec *) descr;
+
+  return spec->unlikely_intentional;
+}
+
 static int
 format_get_number_of_directives (void *descr)
 {
@@ -867,6 +884,7 @@ struct formatstring_parser formatstring_c =
   format_c_parse,
   format_free,
   format_get_number_of_directives,
+  format_is_unlikely_intentional,
   format_check
 };
 
@@ -876,6 +894,7 @@ struct formatstring_parser formatstring_objc =
   format_objc_parse,
   format_free,
   format_get_number_of_directives,
+  format_is_unlikely_intentional,
   format_check
 };
 
index 8e71fb1cd68856f8d0d8aa68f7bd165de83dbefc..966ede2633ca93af13c8728167be227d97e8acde 100644 (file)
@@ -1,5 +1,5 @@
 /* C# format strings.
-   Copyright (C) 2003-2004 Free Software Foundation, Inc.
+   Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -206,6 +206,7 @@ struct formatstring_parser formatstring_csharp =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 4f5005c42e568497c3b353d6921f31e300dafed5..17c2e62e15026193b7fa7d9a7a3cb5598cc403d4 100644 (file)
@@ -1,5 +1,5 @@
 /* Emacs Lisp format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2002.
 
    This program is free software; you can redistribute it and/or modify
@@ -385,6 +385,7 @@ struct formatstring_parser formatstring_elisp =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 7d3be080fb6887344e58fabc02cd221245055bd8..43713aed8c633dbcb8ee2c087acc249e7c268aab 100644 (file)
@@ -1,5 +1,5 @@
 /* GCC internal format strings.
-   Copyright (C) 2003-2005 Free Software Foundation, Inc.
+   Copyright (C) 2003-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -357,6 +357,7 @@ struct formatstring_parser formatstring_gcc_internal =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 4574574910bf0f7703f81d52d2d97987be8e8519..b7f6de1936fa888e498eaf9fb051b12b2767d82b 100644 (file)
@@ -1,5 +1,5 @@
 /* Java format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -777,6 +777,7 @@ struct formatstring_parser formatstring_java =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index bc51379035762e0236ae7bd31bc71454f4ce072f..e64b92c6e73bd99f18bf587935373e189c88da76 100644 (file)
@@ -1,5 +1,5 @@
 /* librep format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -349,6 +349,7 @@ struct formatstring_parser formatstring_librep =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 25c39c85286ac67e471abd8b55474eec52eefb67..4b746b2ec94c702736963c94e728346467ce2892 100644 (file)
@@ -1,5 +1,5 @@
 /* Lisp format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -3370,6 +3370,7 @@ struct formatstring_parser formatstring_lisp =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index cb4b88ff6ab85e3a1f33e9a9675bedde092ffc2a..c863e30a759dfd67f7c06a0b1fa235fbd14953b2 100644 (file)
@@ -1,5 +1,5 @@
 /* Object Pascal format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -438,6 +438,7 @@ struct formatstring_parser formatstring_pascal =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 974375498f4b0a216a04afcf5beafed742025fe4..1d3877b9bc6a97931d8280bcf70b2449b959a125 100644 (file)
@@ -1,5 +1,5 @@
 /* Perl brace format strings.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -213,6 +213,7 @@ struct formatstring_parser formatstring_perl_brace =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index b1d15d394aa61219ce811aa5ca7b1b5bdfed3845..20d6632a1f1ce345a5333ac1bdc959f3aafbd431 100644 (file)
@@ -1,5 +1,5 @@
 /* Perl format strings.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -615,6 +615,7 @@ struct formatstring_parser formatstring_perl =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index c32f3f5835f34b5722f395c7f1cca56e2085b063..434808e1f7e6e2f1a1e5b1cf19d452e2b68f8391 100644 (file)
@@ -1,5 +1,5 @@
 /* PHP format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2002.
 
    This program is free software; you can redistribute it and/or modify
@@ -385,6 +385,7 @@ struct formatstring_parser formatstring_php =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 610cbf04c36e16f7a84efd2f621b5530ae31c3ca..b86de591b85458fce53c4a551aee89dad9eadcb3 100644 (file)
@@ -1,5 +1,5 @@
 /* Python format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -507,6 +507,7 @@ struct formatstring_parser formatstring_python =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 7f395bec86e012f2936d329207957af74e6ecf54..f4da230d00f7aa405d4c13686a7bd2ea11172e75 100644 (file)
@@ -1,5 +1,5 @@
 /* Qt format strings.
-   Copyright (C) 2003-2004 Free Software Foundation, Inc.
+   Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -145,6 +145,7 @@ struct formatstring_parser formatstring_qt =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index b71d65b62e2b77fa6d4d95b8884e5140b076600b..7497a9284548ef0408d211caae428c4e0176e3d6 100644 (file)
@@ -1,5 +1,5 @@
 /* Scheme format strings.
-   Copyright (C) 2001-2005 Free Software Foundation, Inc.
+   Copyright (C) 2001-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -3308,6 +3308,7 @@ struct formatstring_parser formatstring_scheme =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index d09f0be93a7134118bea38748a7175f0e0d90773..176177fc574699252b0b0ae5936209a834e115e7 100644 (file)
@@ -1,5 +1,5 @@
 /* Shell format strings.
-   Copyright (C) 2003-2004 Free Software Foundation, Inc.
+   Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -314,6 +314,7 @@ struct formatstring_parser formatstring_sh =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 14e234c88977873a19832acb1ccc2afb3ee8c901..800283204041d069026edf282529917a7cea40b6 100644 (file)
@@ -1,5 +1,5 @@
 /* Tcl format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2002.
 
    This program is free software; you can redistribute it and/or modify
@@ -424,6 +424,7 @@ struct formatstring_parser formatstring_tcl =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index 62f4fab22af9272cafeada59d73664fd51f55635..cf9797deafee1ec8bcdf5c58d56b75ac1c92d97c 100644 (file)
@@ -1,5 +1,5 @@
 /* YCP and Smalltalk format strings.
-   Copyright (C) 2001-2004 Free Software Foundation, Inc.
+   Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -146,6 +146,7 @@ struct formatstring_parser formatstring_ycp =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
@@ -155,6 +156,7 @@ struct formatstring_parser formatstring_smalltalk =
   format_parse,
   format_free,
   format_get_number_of_directives,
+  NULL,
   format_check
 };
 
index a62a5e05146782cd6d36871dc92fd9179cbf8fd4..27bb4afb588e04d3f3c082f8f767af0531dd9c4f 100644 (file)
@@ -1,5 +1,5 @@
 /* Format strings.
-   Copyright (C) 2001-2005 Free Software Foundation, Inc.
+   Copyright (C) 2001-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -58,6 +58,11 @@ struct formatstring_parser
      A string that can be output literally has 0 format directives.  */
   int (*get_number_of_directives) (void *descr);
 
+  /* Return true if the format string, although valid, contains directives that
+     make it appear unlikely that the string was meant as a format string.
+     A NULL function is equivalent to a function that always returns false.  */
+  bool (*is_unlikely_intentional) (void *descr);
+
   /* Verify that the argument types/names in msgid_descr and those in
      msgstr_descr are the same (if equality=true), or (if equality=false)
      that those of msgid_descr extend those of msgstr_descr (i.e.
index c0fa97cb535cb9556986dd1b9795e31a4b05bf2c..9da84765577bb9321c4dbd58b9871dfdcbfc3bd5 100644 (file)
@@ -2079,7 +2079,9 @@ meta information, not the empty string.\n")));
                 "xgettext: no-c-format" anywhere where a translator wishes
                 to use a percent sign.  So, the msgfmt checking will not be
                 perfect.  Oh well.  */
-             if (parser->get_number_of_directives (descr) > 0)
+             if (parser->get_number_of_directives (descr) > 0
+                 && !(parser->is_unlikely_intentional != NULL
+                      && parser->is_unlikely_intentional (descr)))
                is_format[i] = possible;
 
              parser->free (descr);
@@ -2181,7 +2183,9 @@ remember_a_message_plural (message_ty *mp, char *string,
            if (descr != NULL)
              {
                /* Same heuristic as in remember_a_message.  */
-               if (parser->get_number_of_directives (descr) > 0)
+               if (parser->get_number_of_directives (descr) > 0
+                   && !(parser->is_unlikely_intentional != NULL
+                        && parser->is_unlikely_intentional (descr)))
                  mp->is_format[i] = possible;
 
                parser->free (descr);
index 443925cec577f9f1cdd4a9709f0ff11487474e78..3fbed63303d8a8478b03b20297874542686a179b 100644 (file)
@@ -1,3 +1,8 @@
+2006-02-12  Bruno Haible  <bruno@clisp.org>
+
+       * xgettext-c-12: New file.
+       * Makefile.am (TESTS): Add it.
+
 2005-11-21  Bruno Haible  <bruno@clisp.org>
 
        * format-c-3: Put all non-option arguments after all option arguments,
index 328c19c37aa5d6d9ed6428c2e96ed3d37b7b4084..6031e07a2722daa270f4c876708c9ff0ac2d58ec 100644 (file)
@@ -1,5 +1,5 @@
 ## Makefile for the gettext-tools/tests subdirectory of GNU gettext
-## Copyright (C) 1995-1997, 2001-2005 Free Software Foundation, Inc.
+## Copyright (C) 1995-1997, 2001-2006 Free Software Foundation, Inc.
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -64,7 +64,7 @@ TESTS = gettext-1 gettext-2 gettext-3 gettext-4 gettext-5 gettext-6 gettext-7 \
        xgettext-awk-1 \
        xgettext-c-1 xgettext-c-2 xgettext-c-3 xgettext-c-4 xgettext-c-5 \
        xgettext-c-6 xgettext-c-7 xgettext-c-8 xgettext-c-9 xgettext-c-10 \
-       xgettext-c-11 \
+       xgettext-c-11 xgettext-c-12 \
        xgettext-csharp-1 xgettext-csharp-2 xgettext-csharp-3 \
        xgettext-csharp-4 xgettext-csharp-5 \
        xgettext-elisp-1 \