]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Emacs Lisp: Improve heuristic for format strings.
authorBruno Haible <bruno@clisp.org>
Sun, 1 Jun 2025 22:35:16 +0000 (00:35 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 00:22:37 +0000 (02:22 +0200)
* gettext-tools/src/format-elisp.c (struct spec): Add field
'likely_intentional_directives'.
(format_parse): Set it to the number of directives that don't contain a space.
(format_is_unlikely_intentional): New function.
(formatstring_elisp): Use it.
* gettext-tools/tests/format-elisp-3: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/format-elisp.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/format-elisp-3 [new file with mode: 0755]

index 438d7c6aaccd0e4c21b3d65a55df7839aa551584..5e0e54ce0b99264aea1ade5059fa18b1a6f041d5 100644 (file)
@@ -72,6 +72,11 @@ struct numbered_arg
 struct spec
 {
   unsigned int directives;
+  /* We consider a directive as "likely intentional" if it does not contain a
+     space.  This prevents xgettext from flagging strings like "100% complete"
+     as 'elisp-format' if they don't occur in a context that requires a format
+     string.  */
+  unsigned int likely_intentional_directives;
   unsigned int numbered_arg_count;
   struct numbered_arg *numbered;
 };
@@ -97,6 +102,7 @@ format_parse (const char *format, bool translated, char *fdi,
   unsigned int number;
 
   spec.directives = 0;
+  spec.likely_intentional_directives = 0;
   spec.numbered_arg_count = 0;
   spec.numbered = NULL;
   numbered_allocated = 0;
@@ -107,6 +113,7 @@ format_parse (const char *format, bool translated, char *fdi,
       {
         /* A directive.  */
         enum format_arg_type type;
+        bool likely_intentional = true;
 
         FDI_SET (format - 1, FMTDIR_START);
         spec.directives++;
@@ -133,7 +140,11 @@ format_parse (const char *format, bool translated, char *fdi,
         /* Parse flags.  */
         while (*format == ' ' || *format == '+' || *format == '-'
                || *format == '#' || *format == '0')
-          format++;
+          {
+            if (*format == ' ')
+              likely_intentional = false;
+            format++;
+          }
 
         /* Parse width.  */
         if (*format == '*')
@@ -231,6 +242,8 @@ format_parse (const char *format, bool translated, char *fdi,
             number++;
           }
 
+        if (likely_intentional)
+          spec.likely_intentional_directives++;
         FDI_SET (format, FMTDIR_END);
 
         format++;
@@ -311,6 +324,14 @@ format_get_number_of_directives (void *descr)
   return spec->directives;
 }
 
+static bool
+format_is_unlikely_intentional (void *descr)
+{
+  struct spec *spec = (struct spec *) descr;
+
+  return spec->likely_intentional_directives == 0;
+}
+
 static bool
 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
               formatstring_error_logger_t error_logger, void *error_logger_data,
@@ -395,7 +416,7 @@ struct formatstring_parser formatstring_elisp =
   format_parse,
   format_free,
   format_get_number_of_directives,
-  NULL,
+  format_is_unlikely_intentional,
   format_check
 };
 
index 602482c4879876daf587fab3789c691288c3ff62..b35ae6066f8df382cf80d2dcbab992a232b60b93 100644 (file)
@@ -201,7 +201,7 @@ TESTS = gettext-1 gettext-2 \
        format-c++-brace-1 format-c++-brace-2 \
        format-csharp-1 format-csharp-2 \
        format-d-1 format-d-2 format-d-3 \
-       format-elisp-1 format-elisp-2 \
+       format-elisp-1 format-elisp-2 format-elisp-3 \
        format-gcc-internal-1 format-gcc-internal-2 \
        format-gfc-internal-1 format-gfc-internal-2 \
        format-go-1 format-go-2 \
diff --git a/gettext-tools/tests/format-elisp-3 b/gettext-tools/tests/format-elisp-3
new file mode 100755 (executable)
index 0000000..5f66f99
--- /dev/null
@@ -0,0 +1,47 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test heuristic recognition of Emacs Lisp format strings like "100% complete".
+
+cat <<\EOF > f-el-3.el
+(_ "Test 1a is 100% complete")
+(format (_ "Test 1b is 100% complete") 120)
+(_ "Test 2a from 0% complete to 100% complete")
+(format (_ "Test 2b from 0% complete to 100% complete") 120 121)
+(_ "Test 3a of %s is 100% complete")
+(format (_ "Test 3b of %s is 100% complete") "foo" 120)
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d f-el-3.tmp f-el-3.el || Exit 1
+LC_ALL=C tr -d '\r' < f-el-3.tmp.po > f-el-3.po || Exit 1
+
+cat <<\EOF > f-el-3.ok
+msgid "Test 1a is 100% complete"
+msgstr ""
+
+#, elisp-format
+msgid "Test 1b is 100% complete"
+msgstr ""
+
+msgid "Test 2a from 0% complete to 100% complete"
+msgstr ""
+
+#, elisp-format
+msgid "Test 2b from 0% complete to 100% complete"
+msgstr ""
+
+#, elisp-format
+msgid "Test 3a of %s is 100% complete"
+msgstr ""
+
+#, elisp-format
+msgid "Test 3b of %s is 100% complete"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} f-el-3.ok f-el-3.po
+result=$?
+
+exit $result