From d079c75c1a1e04b1677a6baaa24b3bc61b17d865 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 26 Apr 2020 23:35:16 +0200 Subject: [PATCH] xgettext: Define functions for the Ruby support. * gettext-tools/src/xg-message.h (decide_is_format, intersect_range, decide_do_wrap, decide_syntax_check): New declarations. * gettext-tools/src/xg-message.c (decide_is_format, intersect_range, decide_do_wrap, decide_syntax_check): New functions. extracted from remember_a_message. (remember_a_message): Invoke these functions. --- gettext-tools/src/xg-message.c | 192 +++++++++++++++++++-------------- gettext-tools/src/xg-message.h | 17 ++- 2 files changed, 127 insertions(+), 82 deletions(-) diff --git a/gettext-tools/src/xg-message.c b/gettext-tools/src/xg-message.c index 5b9d3577f..7b053798c 100644 --- a/gettext-tools/src/xg-message.c +++ b/gettext-tools/src/xg-message.c @@ -1,5 +1,5 @@ /* Extracting a message. Accumulating the message list. - Copyright (C) 2001-2019 Free Software Foundation, Inc. + Copyright (C) 2001-2020 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 @@ -107,6 +107,109 @@ set_format_flags_from_context (enum is_format is_format[NFORMATS], } +void +decide_is_format (message_ty *mp) +{ + size_t i; + + /* If it is not already decided, through programmer comments, whether the + msgid is a format string, examine the msgid. This is a heuristic. */ + for (i = 0; i < NFORMATS; i++) + { + if (mp->is_format[i] == undecided + && (formatstring_parsers[i] == current_formatstring_parser1 + || formatstring_parsers[i] == current_formatstring_parser2 + || formatstring_parsers[i] == current_formatstring_parser3) + /* But avoid redundancy: objc-format is stronger than c-format. */ + && !(i == format_c && possible_format_p (mp->is_format[format_objc])) + && !(i == format_objc && possible_format_p (mp->is_format[format_c])) + /* Avoid flagging a string as c-format when it's known to be a + qt-format or qt-plural-format or kde-format or boost-format + string. */ + && !(i == format_c + && (possible_format_p (mp->is_format[format_qt]) + || possible_format_p (mp->is_format[format_qt_plural]) + || possible_format_p (mp->is_format[format_kde]) + || possible_format_p (mp->is_format[format_kde_kuit]) + || possible_format_p (mp->is_format[format_boost]))) + /* Avoid flagging a string as kde-format when it's known to + be a kde-kuit-format string. */ + && !(i == format_kde + && possible_format_p (mp->is_format[format_kde_kuit])) + /* Avoid flagging a string as kde-kuit-format when it's + known to be a kde-format string. Note that this relies + on the fact that format_kde < format_kde_kuit, so a + string will be marked as kde-format if both are + undecided. */ + && !(i == format_kde_kuit + && possible_format_p (mp->is_format[format_kde]))) + { + struct formatstring_parser *parser = formatstring_parsers[i]; + char *invalid_reason = NULL; + void *descr = parser->parse (mp->msgid, false, NULL, &invalid_reason); + + if (descr != NULL) + { + /* msgid is a valid format string. We mark only those msgids + as format strings which contain at least one format directive + and thus are format strings with a high probability. We + don't mark strings without directives as format strings, + because that would force the programmer to add + "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 + && !(parser->is_unlikely_intentional != NULL + && parser->is_unlikely_intentional (descr))) + mp->is_format[i] = possible; + + parser->free (descr); + } + else + { + /* msgid is not a valid format string. */ + mp->is_format[i] = impossible; + free (invalid_reason); + } + } + } +} + +void +intersect_range (message_ty *mp, const struct argument_range *range) +{ + if (has_range_p (*range)) + { + if (has_range_p (mp->range)) + { + if (range->min < mp->range.min) + mp->range.min = range->min; + if (range->max > mp->range.max) + mp->range.max = range->max; + } + else + mp->range = *range; + } +} + +void +decide_do_wrap (message_ty *mp) +{ + /* By default we wrap. */ + mp->do_wrap = (mp->do_wrap == no ? no : yes); +} + +void +decide_syntax_check (message_ty *mp) +{ + size_t i; + + for (i = 0; i < NSYNTAXCHECKS; i++) + if (mp->do_syntax_check[i] == undecided) + mp->do_syntax_check[i] = default_syntax_check[i] == yes ? yes : no; +} + + static void warn_format_string (enum is_format is_format[NFORMATS], const char *string, lex_pos_ty *pos, const char *pretty_msgstr) @@ -418,91 +521,18 @@ meta information, not the empty string.\n"))); } } - /* If it is not already decided, through programmer comments, whether the - msgid is a format string, examine the msgid. This is a heuristic. */ for (i = 0; i < NFORMATS; i++) - { - if (is_format[i] == undecided - && (formatstring_parsers[i] == current_formatstring_parser1 - || formatstring_parsers[i] == current_formatstring_parser2 - || formatstring_parsers[i] == current_formatstring_parser3) - /* But avoid redundancy: objc-format is stronger than c-format. */ - && !(i == format_c && possible_format_p (is_format[format_objc])) - && !(i == format_objc && possible_format_p (is_format[format_c])) - /* Avoid flagging a string as c-format when it's known to be a - qt-format or qt-plural-format or kde-format or boost-format - string. */ - && !(i == format_c - && (possible_format_p (is_format[format_qt]) - || possible_format_p (is_format[format_qt_plural]) - || possible_format_p (is_format[format_kde]) - || possible_format_p (is_format[format_kde_kuit]) - || possible_format_p (is_format[format_boost]))) - /* Avoid flagging a string as kde-format when it's known to - be a kde-kuit-format string. */ - && !(i == format_kde - && possible_format_p (is_format[format_kde_kuit])) - /* Avoid flagging a string as kde-kuit-format when it's - known to be a kde-format string. Note that this relies - on the fact that format_kde < format_kde_kuit, so a - string will be marked as kde-format if both are - undecided. */ - && !(i == format_kde_kuit - && possible_format_p (is_format[format_kde]))) - { - struct formatstring_parser *parser = formatstring_parsers[i]; - char *invalid_reason = NULL; - void *descr = parser->parse (mp->msgid, false, NULL, &invalid_reason); + mp->is_format[i] = is_format[i]; + decide_is_format (mp); - if (descr != NULL) - { - /* msgid is a valid format string. We mark only those msgids - as format strings which contain at least one format directive - and thus are format strings with a high probability. We - don't mark strings without directives as format strings, - because that would force the programmer to add - "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 - && !(parser->is_unlikely_intentional != NULL - && parser->is_unlikely_intentional (descr))) - is_format[i] = possible; + intersect_range (mp, &range); - parser->free (descr); - } - else - { - /* msgid is not a valid format string. */ - is_format[i] = impossible; - free (invalid_reason); - } - } - mp->is_format[i] = is_format[i]; - } - - if (has_range_p (range)) - { - if (has_range_p (mp->range)) - { - if (range.min < mp->range.min) - mp->range.min = range.min; - if (range.max > mp->range.max) - mp->range.max = range.max; - } - else - mp->range = range; - } - - mp->do_wrap = do_wrap == no ? no : yes; /* By default we wrap. */ + mp->do_wrap = do_wrap; + decide_do_wrap (mp); for (i = 0; i < NSYNTAXCHECKS; i++) - { - if (do_syntax_check[i] == undecided) - do_syntax_check[i] = default_syntax_check[i] == yes ? yes : no; - - mp->do_syntax_check[i] = do_syntax_check[i]; - } + mp->do_syntax_check[i] = do_syntax_check[i]; + decide_syntax_check (mp); /* Warn about the use of non-reorderable format strings when the programming language also provides reorderable format strings. */ diff --git a/gettext-tools/src/xg-message.h b/gettext-tools/src/xg-message.h index 8def094e7..eedc9202a 100644 --- a/gettext-tools/src/xg-message.h +++ b/gettext-tools/src/xg-message.h @@ -1,5 +1,5 @@ /* Extracting a message. Accumulating the message list. - Copyright (C) 2001-2019 Free Software Foundation, Inc. + Copyright (C) 2001-2020 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 @@ -76,6 +76,21 @@ extern void remember_a_message_plural (message_ty *mp, refcounted_string_list_ty *comment, bool comment_is_utf8); +/* The following functions are used by remember_a_message. + Most extractors don't need to invoke them explicitly. */ + +/* Eliminates the 'undecided' values in mp->is_format. */ +extern void decide_is_format (message_ty *mp); + +/* Adds a range restriction to mp->range. */ +extern void intersect_range (message_ty *mp, const struct argument_range *range); + +/* Eliminates the 'undecided' value in mp->do_wrap. */ +extern void decide_do_wrap (message_ty *mp); + +/* Eliminates the 'undecided' values in mp->syntax_check. */ +extern void decide_syntax_check (message_ty *mp); + #ifdef __cplusplus } -- 2.47.3