]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Vala: Don't flag printf arguments as c-format.
authorBruno Haible <bruno@clisp.org>
Wed, 21 Feb 2024 10:21:31 +0000 (11:21 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 21 Feb 2024 10:27:07 +0000 (11:27 +0100)
Reported by Anders Jonsson <anders.jonsson@norsjovallen.se>
at <https://savannah.gnu.org/bugs/?64921>.

* gettext-tools/src/xgettext.c (xgettext_record_flag: Recognize also
"undecided-...-format".
* gettext-tools/src/x-vala.c (init_flag_table_vala): Flag the printf function as
'undecided-c-format', not 'c-format'.
* gettext-tools/tests/xgettext-vala-5: Use vprintf instead of printf.
* gettext-tools/tests/xgettext-vala-6: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.
* NEWS: Mention the fix.

NEWS
gettext-tools/src/x-vala.c
gettext-tools/src/xgettext.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/xgettext-vala-5
gettext-tools/tests/xgettext-vala-6 [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 035f2cda5ea8aed6fad7b651129034ea57e0f254..1a786e7d8391124edaf69b9949155d4a68ed0fac 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,10 @@ Version 0.22.5 - February 2024
   provided through <libintl.h> on native Windows and NetBSD now enable
   GCC's format string analysis (-Wformat).
 
+* Bug fixes:
+  - xgettext's processing of Vala files with printf method invocations has
+    been corrected (regression in 0.22).
+
 Version 0.22.4 - November 2023
 
 * Bug fixes:
index 6f548538754f0cb64b63133e7c03ec2c41e81ba5..8bba28162fd8e0b9107da2e0decf9cc0c1484153 100644 (file)
@@ -1,5 +1,5 @@
 /* xgettext Vala backend.
-   Copyright (C) 2013-2014, 2018-2023 Free Software Foundation, Inc.
+   Copyright (C) 2013-2014, 2018-2024 Free Software Foundation, Inc.
 
    This file was written by Daiki Ueno <ueno@gnu.org>, 2013.
 
@@ -145,7 +145,52 @@ init_flag_table_vala ()
   xgettext_record_flag ("N_:1:pass-c-format!Vala");
   xgettext_record_flag ("NC_:2:pass-c-format!Vala");
 
-  xgettext_record_flag ("printf:1:c-format!Vala");
+  /* In Vala, vprintf does not exist as a function, only as a method.
+     https://valadoc.org/?q=printf provides this list:
+
+     Method                                             Arguments
+
+     Posix.FILE.printf (posix)                          (string format, ...)
+     Posix.printf (posix)                               (string format, ...)
+     Alsa.Output.printf (alsa)                          (string format, ...)
+     Dazzle.Path.printf (libdazzle-1.0)                 ()
+     Dazzle.ShortcutChordTable.printf (libdazzle-1.0)   ()
+     string.printf (glib-2.0)                           (...)
+     GLib.FileStream.printf (glib-2.0)                  (string format, ...)
+     GLib.StringBuilder.printf (glib-2.0)               (string format, ...)
+     GLib.Variant.Variant.printf (glib-2.0)             (string format, ...)
+     FastCGI.FileStream.printf (fcgi)                   (string format, ...)
+     FastCGI.Stream.printf (fcgi)                       (string format, ...)
+     Purple.Stringref.printf (purple)                   (string format)
+     Gsl.Stream.printf (gsl)                            (string label, string file, int line, string reason)
+     Gsf.Output.printf (libgsf-1)                       (string format, ...)
+     GLib.OutputStream.printf (gio-2.0)                 (out size_t bytes_written, Cancellable? cancellable, string format, ...)
+     TokyoCabinet.XString.printf (tokyocabinet)         (string format, ...)
+     ZLib.GZFileStream.printf (zlib)                    (string format, ...)
+
+     Therefore, whenever the first argument is a string, it may be a format
+     string or a plain string.  We don't know.  Therefore we cannot enable
+     this flag.  Recognition of format strings that occur as a first argument
+     therefore relies on the heuristics.  */
+  /* Override the effect of
+       xgettext_record_flag ("printf:1:c-format");
+     in x-c.c.  */
+  xgettext_record_flag ("printf:1:undecided-c-format!Vala");
+
+  /* In Vala, vprintf does not exist as a function, only as a method.
+     https://valadoc.org/?q=vprintf provides this list:
+
+     Method                                             Arguments
+     string.vprintf (glib-2.0)                          (va_list args)
+     GLib.FileStream.vprintf (glib-2.0)                 (string format, va_list args)
+     GLib.StringBuilder.vprintf (glib-2.0)              (string format, va_list args)
+     FastCGI.FileStream.vprintf (fcgi)                  (string format, va_list args)
+     FastCGI.Stream.vprintf (fcgi)                      (string format, va_list args)
+     Gsf.Output.vprintf (libgsf-1)                      (string format, va_list args)
+     GLib.OutputStream.vprintf (gio-2.0)                (out size_t bytes_written, Cancellable? cancellable, string format, va_list args)
+
+     Therefore, whenever the first argument is a string, it must be a format
+     string.  */
   xgettext_record_flag ("vprintf:1:c-format!Vala");
 }
 
index 9fc61ab3eb2f03143646659312ba0805644d89ef..881ab15f28e10b36ec666578ef7452a3041b523f 100644 (file)
@@ -1,5 +1,5 @@
 /* Extracts strings from C source file to Uniforum style .po file.
-   Copyright (C) 1995-1998, 2000-2016, 2018-2023 Free Software Foundation, Inc.
+   Copyright (C) 1995-1998, 2000-2016, 2018-2024 Free Software Foundation, Inc.
    Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, April 1995.
 
    This program is free software: you can redistribute it and/or modify
@@ -1485,6 +1485,12 @@ xgettext_record_flag (const char *optionstring)
               n -= 11;
               value = impossible;
             }
+          else if (n >= 10 && memcmp (p, "undecided-", 10) == 0)
+            {
+              p += 10;
+              n -= 10;
+              value = undecided;
+            }
           else
             value = yes_according_to_context;
 
index 3c5f9422f44516251081b9b7f3b220f29c84b889..8ed0ffd35b2d7ffdef42c9496be08f6438fbb3a6 100644 (file)
@@ -1,5 +1,5 @@
 ## Makefile for the gettext-tools/tests subdirectory of GNU gettext
-## Copyright (C) 1995-1997, 2001-2010, 2012-2016, 2018-2023 Free Software Foundation, Inc.
+## Copyright (C) 1995-1997, 2001-2010, 2012-2016, 2018-2024 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
@@ -164,7 +164,7 @@ TESTS = gettext-1 gettext-2 \
        xgettext-tcl-stackovfl-1 xgettext-tcl-stackovfl-2 \
        xgettext-tcl-stackovfl-3 xgettext-tcl-stackovfl-4 \
        xgettext-vala-1 xgettext-vala-2 xgettext-vala-3 xgettext-vala-4 \
-       xgettext-vala-5 \
+       xgettext-vala-5 xgettext-vala-6 \
        xgettext-vala-stackovfl-1 xgettext-vala-stackovfl-2 \
        xgettext-ycp-1 xgettext-ycp-2 xgettext-ycp-3 xgettext-ycp-4 \
        xgettext-ycp-stackovfl-1 xgettext-ycp-stackovfl-2 \
index 44d4be33ef2aacf61664928fb97ead90cfd42ab8..d68239833c5af0db71fd81277b99b3ed461c2874 100755 (executable)
@@ -6,8 +6,8 @@
 : ${XGETTEXT=xgettext}
 
 cat <<\EOF > xg-vala-5.vala
-printf (_("blablaQ %d and %s"), a, b);
-printf (_("blablaQ {0} and {1}"), a, b);
+vprintf (_("blablaQ %d and %s"), args);
+vprintf (_("blablaQ {0} and {1}"), args);
 EOF
 
 ${XGETTEXT} --omit-header --no-location -o xg-vala-5.tmp xg-vala-5.vala || Exit 1
diff --git a/gettext-tools/tests/xgettext-vala-6 b/gettext-tools/tests/xgettext-vala-6
new file mode 100644 (file)
index 0000000..afd78e9
--- /dev/null
@@ -0,0 +1,44 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Vala support: printf and format strings.
+
+: ${XGETTEXT=xgettext}
+
+cat <<\EOF > xg-vala-6.vala
+"<b>%s</b>".printf(_("Hello 1"));
+_("Explanation: %s").printf(_("Hello 2"));
+Posix.printf(_("Hello 3"));
+Posix.printf(_("Hello 4 %s!"), _("Sir"));
+EOF
+
+${XGETTEXT} --omit-header --no-location -o xg-vala-6.tmp xg-vala-6.vala || Exit 1
+func_filter_POT_Creation_Date xg-vala-6.tmp xg-vala-6.po
+
+cat <<\EOF > xg-vala-6.ok
+msgid "Hello 1"
+msgstr ""
+
+#, c-format
+msgid "Explanation: %s"
+msgstr ""
+
+msgid "Hello 2"
+msgstr ""
+
+msgid "Hello 3"
+msgstr ""
+
+#, c-format
+msgid "Hello 4 %s!"
+msgstr ""
+
+msgid "Sir"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-vala-6.ok xg-vala-6.po
+result=$?
+
+exit $result