From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 CB9B83858430 for ; Wed, 20 Dec 2023 22:54:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB9B83858430 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 CB9B83858430 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=1703112882; cv=none; b=BZBRFOpzfqattASUcNBhkv07/+NfpBeDKZE9Rdp4iohVhW65g7jkP/NH8pD2Jziqe34af4KZOeXEPAgLKrecqjoOxhskeM+pcjBioKXp5ZdMiSXY2A5r5ClwletsT8Dkta5pc6jE7aAYsMeNnwSZTbb8JqGHd8DSjPLwelqQ+3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703112882; c=relaxed/simple; bh=AOyImVszD2Nm15n174gDEfrGFxgXzEpx7lBUAjao+ds=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=NS1Ky3Ttg7n/VzCz5+ZaBlM9z66RBcK76RgyginabesizpiLxnNMgRdfGWVV9O9e08LxAWZxekfHzIbbRoUHpIYz2ZMXKW8R58JzV5z8JbLqMClrBEIx2NqjXEve7juyG9YAqiQQ471HG0/MvoTNzSE/APhMC/2DI0iCiVw336g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703112879; 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: in-reply-to:in-reply-to:references:references; bh=DURD1TFInOMjhdXeKnO9xrvGP9k8rXAeD5RtNeVeJz0=; b=Gm1VHistq22Xo6BKMimYXb+iUOhhyARvc7iLC9eoj+mSB7IQIIiBZ75tuMFkl45f8OQntr 6vbj6p5WJ4idZMn9raTxadJQ7YxgM4lYaIM/kkUvX7xQR0G1r/MKFa/I/gg1rkHCq3Nl9N NWuU23I1GDT9u5sy4UgtFq5V9FHVhsM= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-73w6ezRbMayQCfOL995rPw-1; Wed, 20 Dec 2023 17:54:38 -0500 X-MC-Unique: 73w6ezRbMayQCfOL995rPw-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-42781bfcba2so1875281cf.0 for ; Wed, 20 Dec 2023 14:54:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703112877; x=1703717677; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DURD1TFInOMjhdXeKnO9xrvGP9k8rXAeD5RtNeVeJz0=; b=GUFcsjcVCBODMywIOUpIufZ05j3nlmF9e4KIeSZOnMvZhq3PYToJmwngBxvtCKUBYl /D260Qi9aHKv0S7oZvCqsx4rIby0zkd64UTPlwB5z0uv0mt2WOG8iUP7IZ3/jZdmV04y HJU4wCIH11iz8OSjWEFxpX9H1og4QwVrQLS07Hk66wDIYM02PIzOV8PmoQkz8rPZ4Amn bXoU1fFqbmFZaYZukz3vFOjW7fhes074G6uOXARmrRDPjosXx2tlEY4af5mjOdVqa/Ka nEOTRKdIEM5GMIpNR/F/SC6tgqLjD4Kza2quwpSTA2vFnQa7uSapCmO/zMYQ0SNpeVMh 7okQ== X-Gm-Message-State: AOJu0YxACNsH93/nWzzMO3CJiQtcjiL18aOa7NpHcYHjFWw/lKdMkZWC aWR2L/UrQ2z862kUoF3YRZJD+X+p21eqBW1Zr5iuoa1z/Kjwi7fROdLaLrB7hXPAO5ZgXPtE7da g7xtbwHDeB5oqTsmjJOCu43YZmQ== X-Received: by 2002:a05:622a:1a1f:b0:425:a508:a2c2 with SMTP id f31-20020a05622a1a1f00b00425a508a2c2mr27549615qtb.84.1703112876694; Wed, 20 Dec 2023 14:54:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYR+BmrVdSp5wcVsbBwGYeYJ1e4kL5l9T1ay2ZDF0kRi2nZwKVILQFNb8X0dmlIrSUMGw5Ig== X-Received: by 2002:a05:622a:1a1f:b0:425:a508:a2c2 with SMTP id f31-20020a05622a1a1f00b00425a508a2c2mr27549603qtb.84.1703112876327; Wed, 20 Dec 2023 14:54:36 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id fc16-20020a05622a489000b0042389b32e97sm282864qtb.53.2023.12.20.14.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 14:54:35 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 20 Dec 2023 17:54:34 -0500 (EST) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: fix -Wparentheses with boolean-like class types In-Reply-To: Message-ID: <042bc476-25b4-52b0-0927-323e61e5a26a@idea> References: <20231220220749.632100-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.8 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 20 Dec 2023, Jason Merrill wrote: > On 12/20/23 17:07, Patrick Palka wrote: > > Bootstrap and regtesting in progress on x86_64-pc-linux-gnu, does this > > look OK for trunk if successful? > > > > -- >8 -- > > > > Since r14-4977-g0f2e2080685e75 the -Wparentheses warning now undesirably > > warns on the idiom > > > > Wparentheses-34.C:9:14: warning: suggest parentheses around assignment used > > as truth value [-Wparentheses] > > 9 | b = v[i] = true; > > | ^~~~ > > > > where v has type std::vector. That commit intended to only extend > > the existing diagnostics so that they happen in a template context as > > well, but the refactoring of is_assignment_op_expr_p caused us for this > > particular -Wparentheses warning (from convert_for_assignment) to now > > consider user-defined operator= instead of just built-in operator=. And > > since std::vector is really a bitset, whose operator[] returns a > > class type with such a user-defined operator= (taking bool), we now warn. > > > > But arguably "boolish" class types should be treated like ordinary bool > > as far as the warning is concerned. To that end this patch relaxes the > > warning for such types, specifically when the (class) type can be > > (implicitly) converted to a and assigned from a bool. This should cover > > at least implementations of std::vector::reference. > > > > gcc/cp/ChangeLog: > > > > * cp-tree.h (maybe_warn_unparenthesized_assignment): Add > > 'nested_p' bool parameter. > > * semantics.cc (is_assignment_op_expr_p): Add 'rhs' bool > > parameter and set it accordingly. > > (class_type_is_boolish_cache): Define. > > (class_type_is_boolish): Define. > > (maybe_warn_unparenthesized_assignment): Add 'nested_p' > > bool parameter. Relax the warning for nested assignments > > to boolean-like class types. > > (maybe_convert_cond): Pass nested_p=false to > > maybe_warn_unparenthesized_assignment. > > * typeck.cc (convert_for_assignment): Pass nested_p=true to > > maybe_warn_unparenthesized_assignment. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/warn/Wparentheses-34.C: New test. > > --- > > gcc/cp/cp-tree.h | 2 +- > > gcc/cp/semantics.cc | 106 ++++++++++++++++++-- > > gcc/cp/typeck.cc | 2 +- > > gcc/testsuite/g++.dg/warn/Wparentheses-34.C | 31 ++++++ > > 4 files changed, 129 insertions(+), 12 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/warn/Wparentheses-34.C > > > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index 1979572c365..97065cccf3d 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -7928,7 +7928,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 void maybe_warn_unparenthesized_assignment (tree, tsubst_flags_t); > > +extern void maybe_warn_unparenthesized_assignment (tree, bool, > > tsubst_flags_t); > > extern tree cp_check_pragma_unroll (location_t, tree); > > /* in tree.cc */ > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > > index 64839b1ac87..92acd560fa4 100644 > > --- a/gcc/cp/semantics.cc > > +++ b/gcc/cp/semantics.cc > > @@ -839,10 +839,11 @@ finish_goto_stmt (tree destination) > > return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); > > } > > -/* Returns true if T corresponds to an assignment operator expression. > > */ > > +/* Returns true if T corresponds to an assignment operator expression, > > + and sets *LHS to its left-hand-side operand if so. */ > > static bool > > -is_assignment_op_expr_p (tree t) > > +is_assignment_op_expr_p (tree t, tree *lhs) > > { > > if (t == NULL_TREE) > > return false; > > @@ -850,7 +851,10 @@ is_assignment_op_expr_p (tree t) > > if (TREE_CODE (t) == MODIFY_EXPR > > || (TREE_CODE (t) == MODOP_EXPR > > && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)) > > - return true; > > + { > > + *lhs = TREE_OPERAND (t, 0); > > + return true; > > + } > > tree call = extract_call_expr (t); > > if (call == NULL_TREE > > @@ -859,26 +863,107 @@ is_assignment_op_expr_p (tree t) > > return false; > > tree fndecl = cp_get_callee_fndecl_nofold (call); > > - return fndecl != NULL_TREE > > - && DECL_ASSIGNMENT_OPERATOR_P (fndecl) > > - && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR); > > + if (fndecl != NULL_TREE > > + && DECL_ASSIGNMENT_OPERATOR_P (fndecl) > > + && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR)) > > + { > > + *lhs = get_nth_callarg (call, 0); > > + return true; > > + } > > + > > + return false; > > } > > +static GTY((deletable)) hash_map > > *class_type_is_boolish_cache; > > + > > +/* Return true if the class type TYPE can be converted to and assigned > > + from a boolean. */ > > + > > +static bool > > +class_type_is_boolish (tree type) > > +{ > > + type = TYPE_MAIN_VARIANT (type); > > + if (!COMPLETE_TYPE_P (type)) > > + return false; > > + > > + if (bool *r = hash_map_safe_get (class_type_is_boolish_cache, type)) > > + return *r; > > + > > + bool has_bool_conversion = false; > > + bool has_bool_assignment = false; > > + > > + tree ops = lookup_conversions (type); > > + for (; ops; ops = TREE_CHAIN (ops)) > > + { > > + tree op = TREE_VALUE (ops); > > + if (!DECL_NONCONVERTING_P (op) > > + && TREE_CODE (TREE_TYPE (DECL_NAME (op))) == BOOLEAN_TYPE) > > + { > > + has_bool_conversion = true; > > + break; > > + } > > + } > > + > > + if (!has_bool_conversion) > > + { > > + hash_map_safe_put (class_type_is_boolish_cache, type, false); > > + return false; > > + } > > + > > + ops = lookup_fnfields (type, assign_op_identifier, /*protect=*/0, > > tf_none); > > + for (tree op : ovl_range (BASELINK_FUNCTIONS (ops))) > > + { > > + op = STRIP_TEMPLATE (op); > > + if (TREE_CODE (op) != FUNCTION_DECL) > > + continue; > > + tree parm = DECL_CHAIN (DECL_ARGUMENTS (op)); > > + tree parm_type = non_reference (TREE_TYPE (parm)); > > + if (TREE_CODE (parm_type) == BOOLEAN_TYPE) > > + { > > + has_bool_assignment = true; > > + break; > > + } > > + } > > + > > + bool boolish = has_bool_conversion && has_bool_assignment; > > + hash_map_safe_put (class_type_is_boolish_cache, type, boolish); > > + return boolish; > > +} > > + > > + > > /* Maybe warn about an unparenthesized 'a = b' (appearing in a > > - boolean context where 'a == b' might have been intended). */ > > + boolean context where 'a == b' might have been intended). > > + NESTED_P is true if T appears as the RHS of another assignment. */ > > void > > -maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain) > > +maybe_warn_unparenthesized_assignment (tree t, bool nested_p, > > + tsubst_flags_t complain) > > { > > t = STRIP_REFERENCE_REF (t); > > + tree lhs; > > if ((complain & tf_warning) > > && warn_parentheses > > - && is_assignment_op_expr_p (t) > > + && is_assignment_op_expr_p (t, &lhs) > > /* A parenthesized expression would've had this warning > > suppressed by finish_parenthesized_expr. */ > > && !warning_suppressed_p (t, OPT_Wparentheses)) > > { > > + /* In a = b = c, don't warn if b is a boolean-like class type. */ > > + if (nested_p) > > + { > > + /* The caller already checked this. */ > > + gcc_checking_assert (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE); > > It seems awkward to check BOOLEAN_TYPE in convert_for_assignment and bool-ish > type here. Can we check both in one place or the other? Sounds good, it's easy enough to check both in maybe_warn_unparenthesized_assignment. Like so? I've opted to preserve the original slightly stronger form of the check which really checks that (b = c) has bool type rather than b having bool type, to avoid introducing new warnings. - >8 -- Subject: [PATCH] c++: fix -Wparentheses with bool-like class types Since r14-4977-g0f2e2080685e75 the -Wparentheses warning now undesirably warns on the idiom Wparentheses-34.C:9:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 9 | b = v[i] = true; | ^~~~ where v has type std::vector. That commit intended to only extend the existing diagnostics so that they happen in a template context as well, but the refactoring of is_assignment_op_expr_p caused us for this particular -Wparentheses warning (from convert_for_assignment) to now consider user-defined operator= instead of just built-in operator=. And since std::vector is really a bitset, whose operator[] returns a class type with such a user-defined operator= (taking bool), we now warn. But arguably "boolish" class types should be treated like ordinary bool as far as the warning is concerned. To that end this patch relaxes the warning for such types, specifically when the (class) type can be (implicitly) converted to and assigned from bool. This criterion at least covers implementations of std::vector::reference. gcc/cp/ChangeLog: * cp-tree.h (maybe_warn_unparenthesized_assignment): Add 'nested_p' bool parameter. * semantics.cc (is_assignment_op_expr_p): Add 'rhs' bool parameter and set it accordingly. (class_type_is_boolish_cache): Define. (class_type_is_boolish): Define. (maybe_warn_unparenthesized_assignment): Add 'nested_p' bool parameter. Suppress the warning for nested assignments to bool and bool-like class types. (maybe_convert_cond): Pass nested_p=false to maybe_warn_unparenthesized_assignment. * typeck.cc (convert_for_assignment): Pass nested_p=true to maybe_warn_unparenthesized_assignment. Remove now redundant check for 'rhs' having bool type. gcc/testsuite/ChangeLog: * g++.dg/warn/Wparentheses-34.C: New test. --- gcc/cp/cp-tree.h | 2 +- gcc/cp/semantics.cc | 107 ++++++++++++++++++-- gcc/cp/typeck.cc | 7 +- gcc/testsuite/g++.dg/warn/Wparentheses-34.C | 31 ++++++ 4 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wparentheses-34.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1979572c365..97065cccf3d 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7928,7 +7928,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 void maybe_warn_unparenthesized_assignment (tree, tsubst_flags_t); +extern void maybe_warn_unparenthesized_assignment (tree, bool, tsubst_flags_t); extern tree cp_check_pragma_unroll (location_t, tree); /* in tree.cc */ diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 64839b1ac87..6aa1dc46665 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -839,10 +839,11 @@ finish_goto_stmt (tree destination) return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); } -/* Returns true if T corresponds to an assignment operator expression. */ +/* Returns true if T corresponds to an assignment operator expression, + and sets *LHS to its left-hand-side operand if so. */ static bool -is_assignment_op_expr_p (tree t) +is_assignment_op_expr_p (tree t, tree *lhs) { if (t == NULL_TREE) return false; @@ -850,7 +851,10 @@ is_assignment_op_expr_p (tree t) if (TREE_CODE (t) == MODIFY_EXPR || (TREE_CODE (t) == MODOP_EXPR && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)) - return true; + { + *lhs = TREE_OPERAND (t, 0); + return true; + } tree call = extract_call_expr (t); if (call == NULL_TREE @@ -859,26 +863,108 @@ is_assignment_op_expr_p (tree t) return false; tree fndecl = cp_get_callee_fndecl_nofold (call); - return fndecl != NULL_TREE - && DECL_ASSIGNMENT_OPERATOR_P (fndecl) - && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR); + if (fndecl != NULL_TREE + && DECL_ASSIGNMENT_OPERATOR_P (fndecl) + && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR)) + { + *lhs = get_nth_callarg (call, 0); + return true; + } + + return false; } +static GTY((deletable)) hash_map *class_type_is_boolish_cache; + +/* Return true if the class type TYPE can be converted to and assigned + from bool. */ + +static bool +class_type_is_boolish (tree type) +{ + type = TYPE_MAIN_VARIANT (type); + if (!COMPLETE_TYPE_P (type)) + return false; + + if (bool *r = hash_map_safe_get (class_type_is_boolish_cache, type)) + return *r; + + bool has_bool_assignment = false; + bool has_bool_conversion = false; + + tree ops = lookup_fnfields (type, assign_op_identifier, + /*protect=*/0, tf_none); + for (tree op : ovl_range (BASELINK_FUNCTIONS (ops))) + { + op = STRIP_TEMPLATE (op); + if (TREE_CODE (op) != FUNCTION_DECL) + continue; + tree parm = DECL_CHAIN (DECL_ARGUMENTS (op)); + tree parm_type = non_reference (TREE_TYPE (parm)); + if (TREE_CODE (parm_type) == BOOLEAN_TYPE) + { + has_bool_assignment = true; + break; + } + } + + if (!has_bool_assignment) + { + hash_map_safe_put (class_type_is_boolish_cache, type, false); + return false; + } + + ops = lookup_conversions (type); + for (; ops; ops = TREE_CHAIN (ops)) + { + tree op = TREE_VALUE (ops); + if (!DECL_NONCONVERTING_P (op) + && TREE_CODE (DECL_CONV_FN_TYPE (op)) == BOOLEAN_TYPE) + { + has_bool_conversion = true; + break; + } + } + + bool boolish = has_bool_assignment && has_bool_conversion; + hash_map_safe_put (class_type_is_boolish_cache, type, boolish); + return boolish; +} + + /* Maybe warn about an unparenthesized 'a = b' (appearing in a - boolean context where 'a == b' might have been intended). */ + boolean context where 'a == b' might have been intended). + NESTED_P is true if T appears as the RHS of another assignment. */ void -maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain) +maybe_warn_unparenthesized_assignment (tree t, bool nested_p, + tsubst_flags_t complain) { t = STRIP_REFERENCE_REF (t); + tree lhs; if ((complain & tf_warning) && warn_parentheses - && is_assignment_op_expr_p (t) + && is_assignment_op_expr_p (t, &lhs) /* A parenthesized expression would've had this warning suppressed by finish_parenthesized_expr. */ && !warning_suppressed_p (t, OPT_Wparentheses)) { + if (nested_p) + { + /* In ... = a = b, don't warn if a is a bool or bool-like class. */ + if (TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE) + return; + + if (TREE_CODE (lhs) == ADDR_EXPR) + lhs = TREE_OPERAND (lhs, 0); + + tree lhs_type = non_reference (TREE_TYPE (lhs)); + if (CLASS_TYPE_P (lhs_type) + && class_type_is_boolish (lhs_type)) + return; + } + warning_at (cp_expr_loc_or_input_loc (t), OPT_Wparentheses, "suggest parentheses around assignment used as truth value"); suppress_warning (t, OPT_Wparentheses); @@ -903,7 +989,8 @@ maybe_convert_cond (tree cond) if (warn_sequence_point && !processing_template_decl) verify_sequence_points (cond); - maybe_warn_unparenthesized_assignment (cond, tf_warning_or_error); + maybe_warn_unparenthesized_assignment (cond, /*nested_p=*/false, + tf_warning_or_error); /* Do the conversion. */ cond = convert_from_reference (cond); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index a6e2f4ee7da..2f94dcb7938 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10377,11 +10377,8 @@ convert_for_assignment (tree type, tree rhs, } } - /* If -Wparentheses, warn about a = b = c when a has type bool and b - does not. */ - if (TREE_CODE (type) == BOOLEAN_TYPE - && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE) - maybe_warn_unparenthesized_assignment (rhs, complain); + if (TREE_CODE (type) == BOOLEAN_TYPE) + maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain); if (complain & tf_warning) warn_for_address_of_packed_member (type, rhs); diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-34.C b/gcc/testsuite/g++.dg/warn/Wparentheses-34.C new file mode 100644 index 00000000000..2100c8a193d --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-34.C @@ -0,0 +1,31 @@ +// Verify our -Wparentheses warning handles "boolish" class types +// such as std::vector's reference type the same as ordinary +// bool. +// { dg-additional-options "-Wparentheses" } + +#include + +void f(std::vector v, int i) { + bool b; + b = v[i] = true; + b = v[i] = v[i+1]; + + if (v[i] = 42) { } // { dg-message "parentheses" } + if (v[i] = v[i+1]) { } // { dg-message "parentheses" } + + if ((v[i] = 42)) { } + if ((v[i] = v[i+1])) { } +} + +template +void ft(std::vector v, int i) { + bool b; + b = v[i] = true; + b = v[i] = v[i+1]; + + if (v[i] = 42) { } // { dg-message "parentheses" } + if (v[i] = v[i+1]) { } // { dg-message "parentheses" } + + if ((v[i] = 42)) { } + if ((v[i] = v[i+1])) { } +} -- 2.43.0.174.g055bb6e996