From patchwork Wed Oct 25 18:55:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1855188 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=S2cnH0YM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SFyp70Dthz23jn for ; Thu, 26 Oct 2023 05:56:45 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C97E83858022 for ; Wed, 25 Oct 2023 18:56:43 +0000 (GMT) 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 8B5723858D33 for ; Wed, 25 Oct 2023 18:56:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B5723858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8B5723858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698260194; cv=none; b=ROwOMep6NlRoW9hZ6zNnxMFY8jg1v9hnC98Wd3oyjKrPnuSj4lGLowmVSDI8BrRBoNB+IKSJ/DH7dlImQGYISn1rUoFRYgwJAd+OeX1RfJxjKeWhrjkBZzIfoY8K8m+PiNh7apVAy0SIe49thtIjCu7hHNu8d5MSFvr6aGYcxHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698260194; c=relaxed/simple; bh=U0gXtTmmf6eAK1a/ymJgK5VzreMt6MvrQzxQrq1zci4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DRYRRmU6rkjw8iqKcqRMf5Jl2K3V3nf/WVY4ykj6sWoXpLT0JpbVNKDzQ3sfpfHEfY/a+Z2yVcCoHBWhU8rqyDrnWhMoyN/ZU5UeFJ/wKqFL0xf/Ek4U8UhrbjHQKHmKzv7ZNr3x8d7xmHYWQI2+G8zvPg+BRLAQpBDMFRLM6OM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698260192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Y/OWJZkUQNmZr7Ir3Z8BDrE4KIk4tBQqPOGp9X9kid0=; b=S2cnH0YMswjHO7zolngM/PDbe2nwd44mqtgVXjGR6Cvs6PpF3lH00pfNFmjONGR925hAmC HnTjT+DIqrzwwokWivsHeY9vPImn+AO2tD1GxckRQWdkuH9/hr8neLMbTLY51rI0Qmq0Mx SIgr6Boo7e1oIO8YTIREsEh/+wltSc0= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-50-IgmYoKGBM-OxU-4Q3HZTNA-1; Wed, 25 Oct 2023 14:56:30 -0400 X-MC-Unique: IgmYoKGBM-OxU-4Q3HZTNA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-77892f2ee51so16899085a.1 for ; Wed, 25 Oct 2023 11:56:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698260190; x=1698864990; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Y/OWJZkUQNmZr7Ir3Z8BDrE4KIk4tBQqPOGp9X9kid0=; b=kIN/b3ddjqGsQ2UzgE7o+LP/5Eo3f4V0LAAGRRY47kPgXz6jkWvuOF/ZEnqyHHAGqG lJkgoiYA+YmdoOKldvAL9Cj7B99ASF+oc1YldkbzTheGhpUOsHVBGrVRZ/RvEdFac4m0 Z5/PhCI6GNz3A7d9ZTmno4ejgvDR5HMlkAKkDFk5PeayeAnzbdNx0MCOWxQDce9dKEKa LIWWUMaIgCoq2ObkNq0GmK/VwuPj3kPWkNH7gmqE/8EGcS5Xzvy1os7B3O0RF1e2Lu5d 2R2tjDTAfuGLldWBpqvmgcNbDgBYk/mo7EzzNSQYm/jmtPbIGNQJnu0jHkEhV9x9YlmN CG2g== X-Gm-Message-State: AOJu0YxZxzx8iLNWmzMhlsk+yFoOYELjg7DJvdAx5ZWDD3k466YimlwO lXE3r/ahIsYfBMEMPcGyZF/goYfRuririHSsh5t2ub/yNpzTntRDgCs89dn32dcNzZjFquBfQwy JjIO1Uheg5CZdT99CeJgL75I678jSYU5Bo95IwqQ4XeY+XqhaPUShAxxAhLW2EOPkw+wgiNd1kG M= X-Received: by 2002:a05:620a:4724:b0:774:2a7f:fa24 with SMTP id bs36-20020a05620a472400b007742a7ffa24mr19336183qkb.25.1698260189735; Wed, 25 Oct 2023 11:56:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvR1rv5JNe6wm5ydmKFpXmoIf5HwEzpPgTA+iQHmq7PDP/U3Y8maWy0E2Mc2zKuVTtYqmDIw== X-Received: by 2002:a05:620a:4724:b0:774:2a7f:fa24 with SMTP id bs36-20020a05620a472400b007742a7ffa24mr19336156qkb.25.1698260189270; Wed, 25 Oct 2023 11:56:29 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id a24-20020a05620a103800b00767da10efb6sm4412606qkk.97.2023.10.25.11.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 11:56:28 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: more ahead-of-time -Wparentheses warnings Date: Wed, 25 Oct 2023 14:55:56 -0400 Message-ID: <20231025185556.844718-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.482.g2e8e77cbac MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- Now that we don't have to worry about looking thruogh NON_DEPENDENT_EXPR, we can easily extend the -Wparentheses warning in convert_for_assignment to consider (non-dependent) templated assignment operator expressions as well, like r14-4111-g6e92a6a2a72d3b did in maybe_convert_cond. gcc/cp/ChangeLog: * cp-tree.h (is_assignment_op_expr_p): Declare. * semantics.cc (is_assignment_op_expr_p): Generalize to return true for assignment operator expression, not just one that have been resolved to an operator overload. (maybe_convert_cond): Remove now-redundant checks around is_assignment_op_expr_p. * typeck.cc (convert_for_assignment): Look through implicit INDIRECT_REF in -Wparentheses warning logic, and generalize to use is_assignment_op_expr_p. gcc/testsuite/ChangeLog: * g++.dg/warn/Wparentheses-13.C: Strengthen by not requiring that the templates are instantiated for any of the -Wparentheses warnings to be issued. * g++.dg/warn/Wparentheses-23.C: Likewise. * g++.dg/warn/Wparentheses-32.C: Remove xfails. --- gcc/cp/cp-tree.h | 1 + gcc/cp/semantics.cc | 22 +++++++++++---------- gcc/cp/typeck.cc | 7 ++++--- gcc/testsuite/g++.dg/warn/Wparentheses-13.C | 2 -- gcc/testsuite/g++.dg/warn/Wparentheses-23.C | 3 --- gcc/testsuite/g++.dg/warn/Wparentheses-32.C | 8 ++++---- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 30fe716b109..c90ef883e52 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7875,6 +7875,7 @@ extern tree lambda_regenerating_args (tree); extern tree most_general_lambda (tree); extern tree finish_omp_target (location_t, tree, tree, bool); extern void finish_omp_target_clauses (location_t, tree, tree *); +extern bool is_assignment_op_expr_p (tree); /* in tree.cc */ extern int cp_tree_operand_length (const_tree); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 72ec72de690..4b0038a4fc7 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -840,15 +840,20 @@ finish_goto_stmt (tree destination) return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); } -/* Returns true if CALL is a (possibly wrapped) CALL_EXPR or AGGR_INIT_EXPR - to operator= () that is written as an operator expression. */ -static bool -is_assignment_op_expr_p (tree call) +/* Returns true if T corresponds to an assignment operator expression. */ + +bool +is_assignment_op_expr_p (tree t) { - if (call == NULL_TREE) + if (t == NULL_TREE) return false; - call = extract_call_expr (call); + if (TREE_CODE (t) == MODIFY_EXPR + || (TREE_CODE (t) == MODOP_EXPR + && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)) + return true; + + tree call = extract_call_expr (t); if (call == NULL_TREE || call == error_mark_node || !CALL_EXPR_OPERATOR_SYNTAX (call)) @@ -882,10 +887,7 @@ maybe_convert_cond (tree cond) cond = convert_from_reference (cond); tree inner = REFERENCE_REF_P (cond) ? TREE_OPERAND (cond, 0) : cond; - if ((TREE_CODE (inner) == MODIFY_EXPR - || (TREE_CODE (inner) == MODOP_EXPR - && TREE_CODE (TREE_OPERAND (inner, 1)) == NOP_EXPR) - || is_assignment_op_expr_p (inner)) + if (is_assignment_op_expr_p (inner) && warn_parentheses && !warning_suppressed_p (inner, OPT_Wparentheses) && warning_at (cp_expr_loc_or_input_loc (inner), diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 3b719326d76..0585b4a6bf0 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10338,16 +10338,17 @@ convert_for_assignment (tree type, tree rhs, /* If -Wparentheses, warn about a = b = c when a has type bool and b does not. */ + tree inner_rhs = REFERENCE_REF_P (rhs) ? TREE_OPERAND (rhs, 0) : rhs; if (warn_parentheses && TREE_CODE (type) == BOOLEAN_TYPE - && TREE_CODE (rhs) == MODIFY_EXPR - && !warning_suppressed_p (rhs, OPT_Wparentheses) + && is_assignment_op_expr_p (inner_rhs) + && !warning_suppressed_p (inner_rhs, OPT_Wparentheses) && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE && (complain & tf_warning) && warning_at (rhs_loc, OPT_Wparentheses, "suggest parentheses around assignment used as " "truth value")) - suppress_warning (rhs, OPT_Wparentheses); + suppress_warning (inner_rhs, OPT_Wparentheses); if (complain & tf_warning) warn_for_address_or_pointer_of_packed_member (type, rhs); diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-13.C b/gcc/testsuite/g++.dg/warn/Wparentheses-13.C index 22a139f23a4..d6438942c28 100644 --- a/gcc/testsuite/g++.dg/warn/Wparentheses-13.C +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-13.C @@ -65,5 +65,3 @@ bar (T) d = (a = a); foo (27); } - -template void bar (int); // { dg-message "required" } diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-23.C b/gcc/testsuite/g++.dg/warn/Wparentheses-23.C index f1749c2b8da..bd7195e5023 100644 --- a/gcc/testsuite/g++.dg/warn/Wparentheses-23.C +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-23.C @@ -114,8 +114,5 @@ bar4 (T) return (a = a); } -template void bar (int); // { dg-message "required" } -template bool bar1 (int); // { dg-message "required" } template bool bar2 (int); -template bool bar3 (int); // { dg-message "required" } template bool bar4 (int); diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-32.C b/gcc/testsuite/g++.dg/warn/Wparentheses-32.C index 719a9d9e73a..b03515afd55 100644 --- a/gcc/testsuite/g++.dg/warn/Wparentheses-32.C +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-32.C @@ -21,8 +21,8 @@ void f() { if (z1 = z2) { } // { dg-warning "parentheses" } bool b; - b = m = n; // { dg-warning "parentheses" "" { xfail *-*-* } } - b = x1 = x2; // { dg-warning "parentheses" "" { xfail *-*-* } } - b = y1 = y2; // { dg-warning "parentheses" "" { xfail *-*-* } } - b = z1 = z2; // { dg-warning "parentheses" "" { xfail *-*-* } } + b = m = n; // { dg-warning "parentheses" } + b = x1 = x2; // { dg-warning "parentheses" } + b = y1 = y2; // { dg-warning "parentheses" } + b = z1 = z2; // { dg-warning "parentheses" } }