From: Bruno Haible Date: Sun, 4 Nov 2018 19:19:56 +0000 (+0100) Subject: xgettext: Add support for C11 string literals. X-Git-Tag: v0.20~269 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4c4d80817da274719cad9918bd13f44fdb0175b;p=thirdparty%2Fgettext.git xgettext: Add support for C11 string literals. * gettext-tools/src/x-c.c (phase5_get): Recognize the u8"..." syntax. * gettext-tools/tests/xgettext-c-25: New file. * gettext-tools/tests/Makefile.am (TESTS): Add it. * NEWS: Mention the change. --- diff --git a/NEWS b/NEWS index 219bc280d..ed54883ea 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,9 @@ - The .pot file in a 'po' directory is now erased by "make maintainer-clean". * Programming languages support: + - C, C++: + xgettext now supports strings in u8"..." syntax, as specified in C11 + and C++11. - C, C++: xgettext now supports 'p'/'P' exponent markers in number tokens, as specified in C99 and C++17. diff --git a/gettext-tools/src/x-c.c b/gettext-tools/src/x-c.c index 1b81dfd5f..d8d9d0fc0 100644 --- a/gettext-tools/src/x-c.c +++ b/gettext-tools/src/x-c.c @@ -1274,6 +1274,17 @@ phase5_get (token_ty *tp) case '5': case '6': case '7': case '8': case '9': continue; + case '"': + /* Recognize C11 / C++11 string literals. + See (for C) ISO 9899:2011 section 6.4.5 + and (for C++) ISO C++ 11 section 2.14.5 [lex.string]. + Note: The programmer who passes an UTF-8 encoded string to + gettext() or similar API functions will have to have called + bind_textdomain_codeset (DOMAIN, "UTF-8") first. */ + if (bufpos == 2 && buffer[0] == 'u' && buffer[1] == '8') + goto string_literal; + /* FALLTHROUGH */ + default: phase4_ungetc (c); break; @@ -1449,6 +1460,13 @@ phase5_get (token_ty *tp) return; case '"': + string_literal: + /* We could worry about the 'L' or 'u' or 'U' before wide string + constants, but since gettext's argument is a 'const char *', not + a 'const wchar_t *' (for 'L') nor a 'const char16_t *' (for 'u') + nor a 'const char32_t *' (for 'U'), the compiler would complain + about the argument not matching the prototype. Just pretend it + won't happen. */ { struct mixed_string_buffer *bp; @@ -1457,10 +1475,6 @@ phase5_get (token_ty *tp) logical_file_name, line_number); - /* We could worry about the 'L' before wide string constants, - but since gettext's argument is not a wide character string, - let the compiler complain about the argument not matching the - prototype. Just pretend it won't happen. */ for (;;) { c = phase7_getc (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index d79627a48..9bca237ca 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -82,7 +82,7 @@ TESTS = gettext-1 gettext-2 \ xgettext-c-6 xgettext-c-7 xgettext-c-8 xgettext-c-9 xgettext-c-10 \ xgettext-c-11 xgettext-c-12 xgettext-c-13 xgettext-c-14 xgettext-c-15 \ xgettext-c-16 xgettext-c-17 xgettext-c-18 xgettext-c-19 xgettext-c-20 \ - xgettext-c-21 xgettext-c-22 xgettext-c-23 xgettext-c-24 \ + xgettext-c-21 xgettext-c-22 xgettext-c-23 xgettext-c-24 xgettext-c-25 \ xgettext-csharp-1 xgettext-csharp-2 xgettext-csharp-3 \ xgettext-csharp-4 xgettext-csharp-5 xgettext-csharp-6 \ xgettext-csharp-7 xgettext-csharp-8 \ diff --git a/gettext-tools/tests/xgettext-c-25 b/gettext-tools/tests/xgettext-c-25 new file mode 100755 index 000000000..490791bce --- /dev/null +++ b/gettext-tools/tests/xgettext-c-25 @@ -0,0 +1,44 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test C support: C11 string literals. + +: ${XGETTEXT=xgettext} + +cat <<\EOF > xg-c-25.c +gettext("Choose a " u8"rosé wine à la carte"); +EOF + +${XGETTEXT} --from-code=ISO-8859-1 --no-location \ + -o xg-c-25.tmp xg-c-25.c || Exit 1 +grep -v 'POT-Creation-Date' < xg-c-25.tmp > xg-c-25.tmq || Exit 1 +LC_ALL=C tr -d '\r' < xg-c-25.tmq > xg-c-25.po || Exit 1 + +cat <<\EOF > xg-c-25.ok +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +msgid "Choose a rosé wine à la carte" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-c-25.ok xg-c-25.po +result=$? + +exit $result