From: Bruno Haible Date: Wed, 21 Feb 2024 10:21:31 +0000 (+0100) Subject: xgettext: Vala: Don't flag printf arguments as c-format. X-Git-Tag: v0.22.5~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55082976f8598980fcd6f4b0ca97035e84197e66;p=thirdparty%2Fgettext.git xgettext: Vala: Don't flag printf arguments as c-format. Reported by Anders Jonsson at . * 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. --- diff --git a/NEWS b/NEWS index 035f2cda5..1a786e7d8 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,10 @@ Version 0.22.5 - February 2024 provided through 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: diff --git a/gettext-tools/src/x-vala.c b/gettext-tools/src/x-vala.c index 6f5485387..8bba28162 100644 --- a/gettext-tools/src/x-vala.c +++ b/gettext-tools/src/x-vala.c @@ -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 , 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"); } diff --git a/gettext-tools/src/xgettext.c b/gettext-tools/src/xgettext.c index 9fc61ab3e..881ab15f2 100644 --- a/gettext-tools/src/xgettext.c +++ b/gettext-tools/src/xgettext.c @@ -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 , 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; diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 3c5f9422f..8ed0ffd35 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -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 \ diff --git a/gettext-tools/tests/xgettext-vala-5 b/gettext-tools/tests/xgettext-vala-5 index 44d4be33e..d68239833 100755 --- a/gettext-tools/tests/xgettext-vala-5 +++ b/gettext-tools/tests/xgettext-vala-5 @@ -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 index 000000000..afd78e9e4 --- /dev/null +++ b/gettext-tools/tests/xgettext-vala-6 @@ -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 +"%s".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