From patchwork Tue Mar 28 18:37:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 76226 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2427943vqo; Tue, 28 Mar 2023 11:38:29 -0700 (PDT) X-Google-Smtp-Source: AKy350YiHxP4K3NcOZiX8iY87IzWOafTHTlvtY+ed42BAJ93J2niB5L/R3qHn3Pwjo9CzOn3Q64W X-Received: by 2002:a17:907:9728:b0:93e:5baa:d443 with SMTP id jg40-20020a170907972800b0093e5baad443mr20868538ejc.63.1680028708904; Tue, 28 Mar 2023 11:38:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680028708; cv=none; d=google.com; s=arc-20160816; b=wbuAebujHM1ob+VESYWPo9HQbZlbRbQsUULYhhb542D0s8IQhT83HcNOlitY7Usnov f2hOAyQxmBtfAeLal1psM97GVVI1mAQPPfAHqpONXoKKLnGPT800/9Erx9bKL7Zd3MPD DLEZwhpWnN+Ab88/dkI16A7xJdF0PfVJCkwFlDAtAWID0QmMff8x02M7Vg9L80JTu8Is /fLyttfeGRYpIB+PalRs/lp6h6mksDsi355GmCDhlOlWHLdXYru8w6u+tHxhGk9hi3Tr B2DLb9N8s2vXPJMyxbaKT6axstxnAFIUCY5/+VvPWOUwNQUfXaRy7+NWzwgROG8D9XVB 1FQg== 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:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=NI6KorZ3A+T2q1HbQRzAG3d+BpBmcZEpuGrll3NgNOc=; b=NdJ8+xZc1VOBi/Z+1gm82LGLWlT7y8bonRsxNQQOZUeND7QxCBuLF6Vv0xknC8e7Yn m5+iXYyDY/gqyka20Du9wQwk6jlwZv31EpenOWUVPI02cQ9Bt6gds9A8Jvu8BgNuLerY mZQJNPrdIZdcDkHOQDUFubfWpMgZLUCmxw3aNTJql5znGOrUKhbVlZbCIuYO25AnRpFr G0+CmKqYRR3t7gZqhxe5jCOI5mX/2kvySAIHUH7r3f6J4oueMP/jyMtn+SG1x3gPQdJo SEy9qZryKEbqxXctfxTIuJpUkUUozFOjFyHvECsRnL+y+DlkmiK9Ns1bOks3cMVEVoT6 FG8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vY2jz3Nw; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id js2-20020a17090797c200b0093b6e1440b3si19380337ejc.764.2023.03.28.11.38.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 11:38:28 -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=vY2jz3Nw; 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 CAE66385842B for ; Tue, 28 Mar 2023 18:38:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CAE66385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680028707; bh=NI6KorZ3A+T2q1HbQRzAG3d+BpBmcZEpuGrll3NgNOc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vY2jz3Nw25fLSw9MEUWYUMIcynm8jdEpsF94Y6+kDAel9Nc4bdGcEyRi2HYx0igLO p0W5+P4HNfSs5Lcd7zPqwP10/UeOJ7bdzzUWcwSFZi6pJA5c2BitxOWLAH0wGqj1MR GQu5gVAAvucRwEwBg9SjOHYDdeyWhH0aOgHYpdE4= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 8AC383858417 for ; Tue, 28 Mar 2023 18:37:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8AC383858417 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-421-jr-FBuqGMLSqwQgH4_RNqA-1; Tue, 28 Mar 2023 14:37:30 -0400 X-MC-Unique: jr-FBuqGMLSqwQgH4_RNqA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7815D8028B3 for ; Tue, 28 Mar 2023 18:37:30 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 500D01121330; Tue, 28 Mar 2023 18:37:30 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] Don't emit -Wxor-used-as-pow on macro expansions [PR107002] Date: Tue, 28 Mar 2023 14:37:28 -0400 Message-Id: <20230328183728.168042-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm 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?1761637783714822145?= X-GMAIL-MSGID: =?utf-8?q?1761637783714822145?= PR c/107002 reports an assertion failure from deep inside the diagnostic_shows_locus when attempting to print fix-it hints relating to -Wxor-used-as-pow. The case involves macro expansions with -ftrack-macro-expansion=0. It doesn't seem to make much sense to emit this warning for macro expansions, so this patch updates the warning not to (which seems to also be clang's behavior). The patch also adds some bulletproofing to diagnostic-show-locus.cc to be more robust against such invalid fix-it hints. Doing so fixes the ICE. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-6912-g22c3a6c3c11828. gcc/c-family/ChangeLog: PR c/107002 * c-common.h (check_for_xor_used_as_pow): Add "rhs_loc" param. * c-warn.cc (check_for_xor_used_as_pow): Add "rhs_loc" param. Reject cases where involving macro expansions. gcc/c/ChangeLog: PR c/107002 * c-typeck.cc (parser_build_binary_op): Update for new param of check_for_xor_used_as_pow. gcc/cp/ChangeLog: PR c/107002 * parser.cc (cp_parser_binary_expression): Update for new param of check_for_xor_used_as_pow. gcc/ChangeLog: PR c/107002 * diagnostic-show-locus.cc (column_range::column_range): Factor out assertion conditional into... (column_range::valid_p): ...this new function. (line_corrections::add_hint): Don't attempt to consolidate hints if it would lead to invalid column_range instances. gcc/testsuite/ChangeLog: PR c/107002 * c-c++-common/Wxor-used-as-pow-1.c: Add macro test. * c-c++-common/Wxor-used-as-pow-pr107002-0.c: New test. * c-c++-common/Wxor-used-as-pow-pr107002-1.c: New test. * c-c++-common/Wxor-used-as-pow-pr107002-2.c: New test. Signed-off-by: David Malcolm --- gcc/c-family/c-common.h | 2 +- gcc/c-family/c-warn.cc | 23 +++++++++++++++---- gcc/c/c-typeck.cc | 2 +- gcc/cp/parser.cc | 1 + gcc/diagnostic-show-locus.cc | 13 ++++++++--- .../c-c++-common/Wxor-used-as-pow-1.c | 4 ++++ .../Wxor-used-as-pow-pr107002-0.c | 9 ++++++++ .../Wxor-used-as-pow-pr107002-1.c | 5 ++++ .../Wxor-used-as-pow-pr107002-2.c | 5 ++++ 9 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-0.c create mode 100644 gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-1.c create mode 100644 gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-2.c diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index e128e3e7379..f96350b64af 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1524,7 +1524,7 @@ extern void warn_for_multistatement_macros (location_t, location_t, extern void check_for_xor_used_as_pow (location_t lhs_loc, tree lhs_val, location_t operator_loc, - tree rhs_val); + location_t rhs_loc, tree rhs_val); /* In c-attribs.cc. */ extern bool attribute_takes_identifier_p (const_tree); diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index ccbede95ea4..9ac43a1af6e 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -3835,14 +3835,15 @@ do_warn_array_compare (location_t location, tree_code code, tree op0, tree op1) } } -/* Given LHS_VAL ^ RHS_VAL, where LHS_LOC is the location of the LHS and - OPERATOR_LOC is the location of the ^, complain with -Wxor-used-as-pow - if it looks like the user meant exponentiation rather than xor. */ +/* Given LHS_VAL ^ RHS_VAL, where LHS_LOC is the location of the LHS, + OPERATOR_LOC is the location of the ^, and RHS_LOC the location of the + RHS, complain with -Wxor-used-as-pow if it looks like the user meant + exponentiation rather than xor. */ void check_for_xor_used_as_pow (location_t lhs_loc, tree lhs_val, location_t operator_loc, - tree rhs_val) + location_t rhs_loc, tree rhs_val) { /* Only complain if both args are non-negative integer constants that fit in uhwi. */ @@ -3859,6 +3860,20 @@ check_for_xor_used_as_pow (location_t lhs_loc, tree lhs_val, binary_op_rich_location loc (operator_loc, lhs_val, rhs_val, false); + /* Reject cases where we don't have 3 distinct locations. + This can happen e.g. due to macro expansion with + -ftrack-macro-expansion=0 */ + if (!(lhs_loc != operator_loc + && lhs_loc != rhs_loc + && operator_loc != rhs_loc)) + return; + + /* Reject cases in which any of the locations came from a macro. */ + if (from_macro_expansion_at (lhs_loc) + || from_macro_expansion_at (operator_loc) + || from_macro_expansion_at (rhs_loc)) + return; + /* If we issue fix-it hints with the warning then we will also issue a note suggesting how to suppress the warning with a different change. These proposed changes are incompatible. */ diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 45bacc06c47..7079d4ee145 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -4083,7 +4083,7 @@ parser_build_binary_op (location_t location, enum tree_code code, && arg2.m_decimal) check_for_xor_used_as_pow (arg1.get_location (), arg1.value, location, - arg2.value); + arg2.get_location (), arg2.value); return result; } diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index c54972be85a..e8efc32f2c2 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -10289,6 +10289,7 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p, (current.lhs.get_location (), tree_strip_any_location_wrapper (current.lhs), current.loc, + rhs.get_location (), tree_strip_any_location_wrapper (rhs)); overload = NULL; diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc index 301102779b9..0514815b51f 100644 --- a/gcc/diagnostic-show-locus.cc +++ b/gcc/diagnostic-show-locus.cc @@ -2232,8 +2232,7 @@ class column_range public: column_range (int start_, int finish_) : start (start_), finish (finish_) { - /* We must have either a range, or an insertion. */ - gcc_assert (start <= finish || finish == start - 1); + gcc_assert (valid_p (start, finish)); } bool operator== (const column_range &other) const @@ -2241,6 +2240,12 @@ public: return start == other.start && finish == other.finish; } + static bool valid_p (int start, int finish) + { + /* We must have either a range, or an insertion. */ + return (start <= finish || finish == start - 1); + } + int start; int finish; }; @@ -2470,7 +2475,9 @@ line_corrections::add_hint (const fixit_hint *hint) gcc_assert (printed_columns.start >= last_correction->m_printed_columns.start); - if (printed_columns.start <= last_correction->m_printed_columns.finish) + if (printed_columns.start <= last_correction->m_printed_columns.finish + && column_range::valid_p (last_correction->m_affected_bytes.finish + 1, + affected_bytes.start - 1)) { /* We have two hints for which the printed forms of the hints would touch or overlap, so we need to consolidate them to avoid diff --git a/gcc/testsuite/c-c++-common/Wxor-used-as-pow-1.c b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-1.c index 962902c3a05..33c418c2479 100644 --- a/gcc/testsuite/c-c++-common/Wxor-used-as-pow-1.c +++ b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-1.c @@ -55,3 +55,7 @@ int h10_3 = 0xa ^ 3; /* Don't complain if the RHS isn't literal decimal. */ int t2_x16 = 2^0x10; int h10_x3 = 10 ^ 0x3; + +/* Don't complain about uses in macros. */ +#define AMT (10^2) +int amt = AMT; diff --git a/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-0.c b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-0.c new file mode 100644 index 00000000000..37a1e301d12 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-0.c @@ -0,0 +1,9 @@ +/* Regression test for ICE seen in -Wxor-used-as-pow with + -ftrack-macro-expansion=0 in source-printing (fix-it-hints, + specifically). */ + +/* { dg-options "-ftrack-macro-expansion=0 -fdiagnostics-show-caret" } */ + +#define test(lower, higher, a, b, c, d) \ + char test##line[ (a higher b lower c higher d) == 0 ? -1 : 1]; +test (|, ^, 1, 2, 2, 1) diff --git a/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-1.c b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-1.c new file mode 100644 index 00000000000..10e2a0d55a3 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-1.c @@ -0,0 +1,5 @@ +/* { dg-options "-ftrack-macro-expansion=1 -fdiagnostics-show-caret" } */ + +#define test(lower, higher, a, b, c, d) \ + char test##line[ (a higher b lower c higher d) == 0 ? -1 : 1]; +test (|, ^, 1, 2, 2, 1) diff --git a/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-2.c b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-2.c new file mode 100644 index 00000000000..8e9a4bf0f55 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-2.c @@ -0,0 +1,5 @@ +/* { dg-options "-ftrack-macro-expansion=2 -fdiagnostics-show-caret" } */ + +#define test(lower, higher, a, b, c, d) \ + char test##line[ (a higher b lower c higher d) == 0 ? -1 : 1]; +test (|, ^, 1, 2, 2, 1)