]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Rust: Support --keyword option with a qualified name for macros.
authorBruno Haible <bruno@clisp.org>
Mon, 11 May 2026 15:08:06 +0000 (17:08 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 11 May 2026 15:19:29 +0000 (17:19 +0200)
Reported by Lucas Baudin <lbaudin@gnome.org>
at <https://savannah.gnu.org/bugs/?68297>.

* gettext-tools/src/x-rust.c (extract_from_function_call_like): Use the
accumulated qualified name also for macro invocations.
(extract_from_node): Allow generic identifiers also for macro invocations.
* gettext-tools/tests/xgettext-rust-9: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/x-rust.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/xgettext-rust-9 [new file with mode: 0755]

index 0e23ac91bff801b575257ef072782da28e4641bb..a774f97fc78139ca314a48a974fab74d1c6a13b8 100644 (file)
@@ -729,8 +729,9 @@ extract_from_function_call (TSNode callee_node,
 
 /* Extracts messages from a function call like syntax in a macro invocation,
    consisting of
-     - CALLEE_NODE: a tree node of type 'identifier', or NULL for a mere
-       parenthesized expression,
+     - CALLEE_NODE: a tree node of type 'identifier' or
+       a tree node of type 'scoped_identifier' with at least 1 named child,
+       or NULL for a mere parenthesized expression,
      - CALLEE_NAME: a freshly allocated string, representing the qualified name
        of the node sequence of which CALLEE_NODE is the last one,
      - ARGS_NODE: a tree node of type 'token_tree'.
@@ -913,7 +914,7 @@ extract_from_function_call_like (TSNode *callee_node,
                           TSNode identifier_node = ts_node_child (args_node, prev2_token_in_same_arg);
                           extract_from_function_call_like (
                                              &identifier_node,
-                                             generic_identifier_name (identifier_node),
+                                             xsd_copy (sd_readonly (prev2_token_qualifiedname)),
                                              true,
                                              arg_node,
                                              arg_region,
@@ -1026,7 +1027,7 @@ extract_from_function_call_like (TSNode *callee_node,
                   TSNode identifier_node = ts_node_child (args_node, prev2_token_in_same_arg);
                   extract_from_function_call_like (
                                      &identifier_node,
-                                     generic_identifier_name (identifier_node),
+                                     xsd_copy (sd_readonly (prev2_token_qualifiedname)),
                                      true,
                                      arg_node,
                                      arg_region,
@@ -1138,7 +1139,7 @@ extract_from_node (TSNode node,
       if (! ts_node_eq (ts_node_child_by_field_id (node, ts_field_macro),
                         callee_node))
         abort ();
-      if (ts_node_symbol (callee_node) == ts_symbol_identifier)
+      if (is_generic_identifier (callee_node))
         {
           /* We have to search for the args_node.
              It is not always = ts_node_named_child (node, 1),
index 67987c5b41b41061e6319752acaab3879c8e75e2..3cb2e3bf5e7c8785433f617aacbb1987ddf8a247 100644 (file)
@@ -170,6 +170,7 @@ TESTS = gettext-1 gettext-2 \
        xgettext-ruby-1 xgettext-ruby-2 \
        xgettext-rust-1 xgettext-rust-2 xgettext-rust-3 xgettext-rust-4 \
        xgettext-rust-5 xgettext-rust-6 xgettext-rust-7 xgettext-rust-8 \
+       xgettext-rust-9 \
        xgettext-rust-stackovfl-1 xgettext-rust-stackovfl-2 \
        xgettext-rust-stackovfl-3 xgettext-rust-stackovfl-4 \
        xgettext-scheme-1 xgettext-scheme-2 xgettext-scheme-3 \
diff --git a/gettext-tools/tests/xgettext-rust-9 b/gettext-tools/tests/xgettext-rust-9
new file mode 100755 (executable)
index 0000000..f4287c2
--- /dev/null
@@ -0,0 +1,82 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Rust support: --keyword option with a composed macro name.
+
+cat <<\EOF > xg-rs-9.rs
+gettext! ("Test 1");
+translate! ("Test 2");
+foo! ("Test 3");
+bar! ("Test 4");
+baz! ("Test 5");
+ResourceBundles::gettext! ("Test 11");
+ResourceBundles::translate! ("Test 12");
+i18n::translate! ("Test 13");
+bar::baz! ("Test 14");
+ResourceBundles::i18n::translate! ("Test 21");
+foo::bar::baz! ("Test 22");
+foo :: bar :: baz ! ("Test 23");
+
+println!(gettext! ("PTest 1"));
+println!(translate! ("PTest 2"));
+println!(foo! ("PTest 3"));
+println!(bar! ("PTest 4"));
+println!(baz! ("PTest 5"));
+println!(ResourceBundles::gettext! ("PTest 11"));
+println!(ResourceBundles::translate! ("PTest 12"));
+println!(i18n::translate! ("PTest 13"));
+println!(bar::baz! ("PTest 14"));
+println!(ResourceBundles::i18n::translate! ("PTest 21"));
+println!(foo::bar::baz! ("PTest 22"));
+println!(foo :: bar :: baz ! ("PTest 23"));
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location \
+            --keyword='gettext!' --keyword='i18n::translate!' --keyword='foo::bar::baz!' \
+            -d xg-rs-9.tmp xg-rs-9.rs || Exit 1
+LC_ALL=C tr -d '\r' < xg-rs-9.tmp.po > xg-rs-9.po || Exit 1
+
+cat <<\EOF > xg-rs-9.ok
+msgid "Test 1"
+msgstr ""
+
+msgid "Test 11"
+msgstr ""
+
+msgid "Test 13"
+msgstr ""
+
+msgid "Test 21"
+msgstr ""
+
+msgid "Test 22"
+msgstr ""
+
+msgid "Test 23"
+msgstr ""
+
+msgid "PTest 1"
+msgstr ""
+
+msgid "PTest 11"
+msgstr ""
+
+msgid "PTest 13"
+msgstr ""
+
+msgid "PTest 21"
+msgstr ""
+
+msgid "PTest 22"
+msgstr ""
+
+msgid "PTest 23"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-rs-9.ok xg-rs-9.po
+result=$?
+
+exit $result