From 053876cdbe8057210e6f4da4eec2df58f92ccd4c Mon Sep 17 00:00:00 2001 From: Tom Honermann Date: Tue, 2 Aug 2022 14:36:02 -0400 Subject: preprocessor/106426: Treat u8 character literals as unsigned in char8_t modes. This patch corrects handling of UTF-8 character literals in preprocessing directives so that they are treated as unsigned types in char8_t enabled C++ modes (C++17 with -fchar8_t or C++20 without -fno-char8_t). Previously, UTF-8 character literals were always treated as having the same type as ordinary character literals (signed or unsigned dependent on target or use of the -fsigned-char or -funsigned char options). PR preprocessor/106426 gcc/c-family/ChangeLog: * c-opts.cc (c_common_post_options): Assign cpp_opts->unsigned_utf8char subject to -fchar8_t, -fsigned-char, and/or -funsigned-char. gcc/testsuite/ChangeLog: * g++.dg/ext/char8_t-char-literal-1.C: Check signedness of u8 literals. * g++.dg/ext/char8_t-char-literal-2.C: Check signedness of u8 literals. libcpp/ChangeLog: * charset.cc (narrow_str_to_charconst): Set signedness of CPP_UTF8CHAR literals based on unsigned_utf8char. * include/cpplib.h (cpp_options): Add unsigned_utf8char. * init.cc (cpp_create_reader): Initialize unsigned_utf8char. --- libcpp/charset.cc | 4 ++-- libcpp/include/cpplib.h | 4 ++-- libcpp/init.cc | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'libcpp') diff --git a/libcpp/charset.cc b/libcpp/charset.cc index ca8b7cf..12e3163 100644 --- a/libcpp/charset.cc +++ b/libcpp/charset.cc @@ -1960,8 +1960,8 @@ narrow_str_to_charconst (cpp_reader *pfile, cpp_string str, /* Multichar constants are of type int and therefore signed. */ if (i > 1) unsigned_p = 0; - else if (type == CPP_UTF8CHAR && !CPP_OPTION (pfile, cplusplus)) - unsigned_p = 1; + else if (type == CPP_UTF8CHAR) + unsigned_p = CPP_OPTION (pfile, unsigned_utf8char); else unsigned_p = CPP_OPTION (pfile, unsigned_char); diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 3eba6f7..f9c042d 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -581,8 +581,8 @@ struct cpp_options ints and target wide characters, respectively. */ size_t precision, char_precision, int_precision, wchar_precision; - /* True means chars (wide chars) are unsigned. */ - bool unsigned_char, unsigned_wchar; + /* True means chars (wide chars, UTF-8 chars) are unsigned. */ + bool unsigned_char, unsigned_wchar, unsigned_utf8char; /* True if the most significant byte in a word has the lowest address in memory. */ diff --git a/libcpp/init.cc b/libcpp/init.cc index f4ab83d..0242da5 100644 --- a/libcpp/init.cc +++ b/libcpp/init.cc @@ -231,6 +231,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table, CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int); CPP_OPTION (pfile, unsigned_char) = 0; CPP_OPTION (pfile, unsigned_wchar) = 1; + CPP_OPTION (pfile, unsigned_utf8char) = 1; CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */ /* Default to no charset conversion. */ -- cgit v1.1