From patchwork Mon Jul 25 17:59:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Honermann X-Patchwork-Id: 166 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp1795925pxt; Mon, 25 Jul 2022 11:01:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uDstRvybSLA0NwdbEmCohAWrXNSlRl4p7RKHGA5BStAowTX5ky/xZ76efJ8A88xQGjNxy6 X-Received: by 2002:a05:6402:4390:b0:43b:e9d9:f9e4 with SMTP id o16-20020a056402439000b0043be9d9f9e4mr8938543edc.361.1658772064621; Mon, 25 Jul 2022 11:01:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658772064; cv=none; d=google.com; s=arc-20160816; b=Pwreh4QiGdPrS0HUybgOVjbTavHCTNY0Fmg3/wWfMZHn7H257hOS3kQQJVuzn9BzYN /ySHP+iasvLsZXSuXCJEWHBbKirPOEnWSJcEgyRsRXk4f7uONE/y151MnW75clyU/PHg ypflirj2EraoXgR/qbRjKZYw8bFwH4wtHUDO57fWdI9AU6OZ0aN39uT6zZ3Uyru6NjLb Up+e5AzxDH+URrVUr+5EU15DusVQ1DS7ZBWuJoujQmUolzG+EsvykFt1A0wpiGVbwyrZ wotxe2e/M3crycSPWvvW5deWDBF+bNXTRVKXRkVNDoBUcDZCFAHONIZwWejLrfVZ/HLx eiIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=YWb6Wm6MYPE5lj/6TvxmLNkfxs5oAtjaWyhbtfSt9zU=; b=XOny2ompMeJjS9PEOwltbZrjSIa19zDh8ueXVaKaSey71FHilRNv5nITfvdhebcHms AZJS5ffaF/iHo4E/HEp/CRnNIUfGdYYY2cora+nW1gfgrIk4ZSTcZ1Ck4FpIrhacQMPs +dSmgRjMks9JK02EmQc+EI+V1OSVxhnV4e/lR8CanpG/hjaD6U1gruSxtzPE8LBLwoYc ose+W5YxcV6PTodnp22O4BGAYGqThQdLCT7thpGnGjP1+rk7HgiWKdvVhcXQ9o1n7l0N y4n+BLt1h3Zd1cqQfYJ6JJPg3I0XIFO0eKjWnc5kAyyptcaxfASa+7fofrrxwnmwRtUN uZuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OppZqVoA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m13-20020a170906848d00b0072b13fa7314si8046609ejx.439.2022.07.25.11.01.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 11:01:04 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OppZqVoA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A1A51383A300 for ; Mon, 25 Jul 2022 18:00:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1A51383A300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658772056; bh=YWb6Wm6MYPE5lj/6TvxmLNkfxs5oAtjaWyhbtfSt9zU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=OppZqVoAta+NIhtMnyfhQbykO5Rm+qr/PrS55QS+O2IpvS+2cETMLCFHb42GVaq87 oA/rMPKNh0vZJDm2DxiTELzTI2tSiIfdQQVdusojw8L+XDluEhHo9NLxv49LW/ooMG sGdZ74919T2jaOaBW+y7NUKxdqe9SldbE8qsSh1I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp111.ord1d.emailsrvr.com (smtp111.ord1d.emailsrvr.com [184.106.54.111]) by sourceware.org (Postfix) with ESMTPS id 237B8383A35A for ; Mon, 25 Jul 2022 18:00:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 237B8383A35A X-Auth-ID: tom@honermann.net Received: by smtp6.relay.ord1d.emailsrvr.com (Authenticated sender: tom-AT-honermann.net) with ESMTPSA id 47E0BE0063; Mon, 25 Jul 2022 14:00:11 -0400 (EDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/3] c++/106426: Treat u8 character literals as unsigned in char8_t modes. Date: Mon, 25 Jul 2022 13:59:48 -0400 Message-Id: <20220725175948.1424695-4-tom@honermann.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220725175948.1424695-1-tom@honermann.net> References: <20220725175948.1424695-1-tom@honermann.net> MIME-Version: 1.0 X-Classification-ID: b7f150fc-14e1-4703-aa58-6102617532c5-4-1 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Honermann via Gcc-patches From: Tom Honermann Reply-To: Tom Honermann Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739348576071046361?= X-GMAIL-MSGID: =?utf-8?q?1739348576071046361?= 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). Fixes https://gcc.gnu.org/PR106426. 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. --- gcc/c-family/c-opts.cc | 1 + gcc/testsuite/g++.dg/ext/char8_t-char-literal-1.C | 6 +++++- gcc/testsuite/g++.dg/ext/char8_t-char-literal-2.C | 4 ++++ libcpp/charset.cc | 4 ++-- libcpp/include/cpplib.h | 4 ++-- libcpp/init.cc | 1 + 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index 108adc5caf8..02ce1e86cdb 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -1062,6 +1062,7 @@ c_common_post_options (const char **pfilename) /* char8_t support is implicitly enabled in C++20 and C2X. */ if (flag_char8_t == -1) flag_char8_t = (cxx_dialect >= cxx20) || flag_isoc2x; + cpp_opts->unsigned_utf8char = flag_char8_t ? 1 : cpp_opts->unsigned_char; if (flag_extern_tls_init) { diff --git a/gcc/testsuite/g++.dg/ext/char8_t-char-literal-1.C b/gcc/testsuite/g++.dg/ext/char8_t-char-literal-1.C index 8ed85ccfdcd..2994dd38516 100644 --- a/gcc/testsuite/g++.dg/ext/char8_t-char-literal-1.C +++ b/gcc/testsuite/g++.dg/ext/char8_t-char-literal-1.C @@ -1,6 +1,6 @@ // Test that UTF-8 character literals have type char if -fchar8_t is not enabled. // { dg-do compile } -// { dg-options "-std=c++17 -fno-char8_t" } +// { dg-options "-std=c++17 -fsigned-char -fno-char8_t" } template struct is_same @@ -10,3 +10,7 @@ template { static const bool value = true; }; static_assert(is_same::value, "Error"); + +#if u8'\0' - 1 > 0 +#error "UTF-8 character literals not signed in preprocessor" +#endif diff --git a/gcc/testsuite/g++.dg/ext/char8_t-char-literal-2.C b/gcc/testsuite/g++.dg/ext/char8_t-char-literal-2.C index 7861736689c..db4fe70046d 100644 --- a/gcc/testsuite/g++.dg/ext/char8_t-char-literal-2.C +++ b/gcc/testsuite/g++.dg/ext/char8_t-char-literal-2.C @@ -10,3 +10,7 @@ template { static const bool value = true; }; static_assert(is_same::value, "Error"); + +#if u8'\0' - 1 < 0 +#error "UTF-8 character literals not unsigned in preprocessor" +#endif diff --git a/libcpp/charset.cc b/libcpp/charset.cc index ca8b7cf7aa5..12e31632228 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 3eba6f74b57..f9c042db034 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 f4ab83d2145..0242da5f55c 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. */