From patchwork Mon Aug 28 14:00:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 137038 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp3348776vqm; Mon, 28 Aug 2023 07:01:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFkAJjBmaO+rBZFfVQnI0rNRzqe6enSENfq0l7nBQQBqR6e+UoApR3UZ/CkbfBuLLt4PutW X-Received: by 2002:a05:6512:33c1:b0:4ff:a23b:de27 with SMTP id d1-20020a05651233c100b004ffa23bde27mr5674719lfg.50.1693231309295; Mon, 28 Aug 2023 07:01:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693231309; cv=none; d=google.com; s=arc-20160816; b=mEV0+JGILjuX2366Hl2xypRXWBkEeBK0H/r67XY3nnIWfwasvlrkxEbMnZAuREA4+g /U+7cZwlcpBZ/ZPYo4rW+ceX6VsXWL694gmpg0qfdeK5J9MhvXcR8ZHf5daCsmmPSYy7 wr3LrQeQUUR0jT1vAeiRJXWBHxO5zniimvMx2g6xc56txxsSG22RaTjpXuZGNoN8T+T0 iQlVs/Afx276VJ0QmG66i4iEPvylZorJ+WoJQBwubATBFpnPnH471TcqcZwlsP6NqhXn +yoYzvJYMaUxW2L9jmwduXcawdkT9f1bS04Jk/nsEiw3XAL1AV9GkKBA4AHInpxfDuKr ekRQ== 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-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=B4hEDdeBKIwLZfhp2z62aJFf7kGKtrKxrZBAhqlND8g=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=Tcb4eQZDnFMw3nLXozvYmyDTtYHnUxcVHyy9zZTjLPVc70aaTpYTf+E39cPcH0mczL YSylIIOoQCD7CwQFLd4xnxxPqJzfSgeu1C7Y3a1jaXAO0v6Nnq5IGMrzX8yWM6Jiqizb Hk0eHWL1OeQYSgH5QGtuoKTa0MCsoRbVH2vmv8/NCx78ebQRPnfT5d1t2dK7tbkpDafb HHb5MVTI6kcxEn3FSvZmJy48O1nBXpGuyuevEKTuqMpxsgI+SfQ26dv+NgC53nvha2gI Cq6fj6uTJuV6KoXfoEyPP6mHthtmAjzC0k66CwOKlIoyZHEhMYmUZym2ufVmUq7NuAr/ zqJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PGYTdHwD; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id jt4-20020a170906dfc400b0098dc18c13f4si2616047ejc.1037.2023.08.28.07.01.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 07:01:49 -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=PGYTdHwD; 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 1C7A33858C41 for ; Mon, 28 Aug 2023 14:01:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C7A33858C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693231308; bh=B4hEDdeBKIwLZfhp2z62aJFf7kGKtrKxrZBAhqlND8g=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=PGYTdHwDict48pJ6sPoQo0q3k+BETzQJOnvztdalvtlj229BsLM8VbSbobmO2EJWa XQo8LSLtC9FCsgRr8xir23WTC/QxE1GRcyM/GWQQAV8owbQWsvysJJrXLcuqtR4FbI TW6JDFSwvCAjMTnPP1GuRtBz29PbhYCOUp3UEIAI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 88A4E3858D35 for ; Mon, 28 Aug 2023 14:00:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88A4E3858D35 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-lhjSLRD8N0WpDFT5zmm_Sg-1; Mon, 28 Aug 2023 10:00:50 -0400 X-MC-Unique: lhjSLRD8N0WpDFT5zmm_Sg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 57B0385CBF8 for ; Mon, 28 Aug 2023 14:00:50 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1E11540C2063; Mon, 28 Aug 2023 14:00:50 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37SE0mBh1504098 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 28 Aug 2023 16:00:48 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37SE0mSN1504097; Mon, 28 Aug 2023 16:00:48 +0200 Date: Mon, 28 Aug 2023 16:00:48 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libcpp, v2: Small incremental patch for P1854R4 [PR110341] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SCC_10_SHORT_WORD_LINES, SCC_20_SHORT_WORD_LINES, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775481713157976013 X-GMAIL-MSGID: 1775481713157976013 Hi! Sorry, testing revealed an unused uchar *outbuf; declaration breaking the build, here is the same patch with that one line removed, bootstrapped/regtested on x86_64-linux and i686-linux (on top of the earlier POR110341 patch). On Sat, Aug 26, 2023 at 01:11:06PM +0200, Jakub Jelinek via Gcc-patches wrote: > The following incremental patch to the PR110341 posted patch uses > a special conversion callback instead of conversion from host charset > (UTF-8/UTF-EBCDIC) to UTF-32, and also ignores all diagnostics from the > second cpp_interpret_string which should just count chars. The UTF-EBCDIC > is untested, but simple enough that it should just work. 2023-08-28 Jakub Jelinek PR c++/110341 * charset.cc (one_count_chars, convert_count_chars): New functions. (narrow_str_to_charconst): Call cpp_interpret_string with type rather than CPP_STRING32, temporarily override for that call pfile->cb.diagnostic to noop_diagnostic_cb and pfile->narrow_cset_desc.func to convert_count_chars and just compare str.len against str2.len. Jakub --- libcpp/charset.cc.jj 2023-08-25 17:14:14.098733396 +0200 +++ libcpp/charset.cc 2023-08-28 12:57:44.858858994 +0200 @@ -446,6 +446,73 @@ one_utf16_to_utf8 (iconv_t bigend, const return 0; } + +/* Special routine which just counts number of characters in the + string, what exactly is stored into the output doesn't matter + as long as it is one uchar per character. */ + +static inline int +one_count_chars (iconv_t, const uchar **inbufp, size_t *inbytesleftp, + uchar **outbufp, size_t *outbytesleftp) +{ + cppchar_t s = 0; + int rval; + + /* Check for space first, since we know exactly how much we need. */ + if (*outbytesleftp < 1) + return E2BIG; + +#if HOST_CHARSET == HOST_CHARSET_ASCII + rval = one_utf8_to_cppchar (inbufp, inbytesleftp, &s); + if (rval) + return rval; +#else + if (*inbytesleftp < 1) + return EINVAL; + static const uchar utf_ebcdic_map[256] = { + /* See table 4 in http://unicode.org/reports/tr16/tr16-7.2.html */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, + 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, + 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, + 9, 9, 9, 9, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, + 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, + 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, + 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 1, 3, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 4, + 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 5, 5, 5, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 6, 6, 7, 7, 0 + }; + rval = utf_ebcdic_map[**inbufp]; + if (rval == 9) + return EILSEQ; + if (rval == 0) + rval = 1; + if (rval >= 2) + { + if (*inbytesleftp < rval) + return EINVAL; + for (int i = 1; i < rval; ++i) + if (utf_ebcdic_map[(*inbufp)[i]] != 9) + return EILSEQ; + } + *inbytesleftp -= rval; + *inbufp += rval; +#endif + + **outbufp = ' '; + + *outbufp += 1; + *outbytesleftp -= 1; + return 0; +} + + /* Helper routine for the next few functions. The 'const' on one_conversion means that we promise not to modify what function is pointed to, which lets the inliner see through it. */ @@ -529,6 +596,15 @@ convert_utf32_utf8 (iconv_t cd, const uc return conversion_loop (one_utf32_to_utf8, cd, from, flen, to); } +/* Magic conversion which just counts characters from input, so + only to->len is significant. */ +static bool +convert_count_chars (iconv_t cd, const uchar *from, + size_t flen, struct _cpp_strbuf *to) +{ + return conversion_loop (one_count_chars, cd, from, flen, to); +} + /* Identity conversion, used when we have no alternative. */ static bool convert_no_conversion (iconv_t cd ATTRIBUTE_UNUSED, @@ -2623,15 +2699,22 @@ narrow_str_to_charconst (cpp_reader *pfi ill-formed. We need to count the number of c-chars and compare that to str.len. */ cpp_string str2 = { 0, 0 }; - if (cpp_interpret_string (pfile, &token->val.str, 1, &str2, - CPP_STRING32)) + bool (*saved_diagnostic_handler) (cpp_reader *, enum cpp_diagnostic_level, + enum cpp_warning_reason, rich_location *, + const char *, va_list *) + ATTRIBUTE_FPTR_PRINTF(5,0); + saved_diagnostic_handler = pfile->cb.diagnostic; + pfile->cb.diagnostic = noop_diagnostic_cb; + convert_f save_func = pfile->narrow_cset_desc.func; + pfile->narrow_cset_desc.func = convert_count_chars; + bool ret = cpp_interpret_string (pfile, &token->val.str, 1, &str2, type); + pfile->narrow_cset_desc.func = save_func; + pfile->cb.diagnostic = saved_diagnostic_handler; + if (ret) { - size_t width32 = converter_for_type (pfile, CPP_STRING32).width; - size_t nbwc = width32 / width; - size_t len = str2.len / nbwc; if (str2.text != token->val.str.text) free ((void *)str2.text); - if (str.len > len) + if (str.len > str2.len) { diagnosed = cpp_error (pfile, CPP_DL_PEDWARN,