public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: -Wuninitialized when binding a ref to uninit DM [PR113987]
Date: Thu, 22 Feb 2024 08:34:45 +0000	[thread overview]
Message-ID: <3dae04f1-90b7-4a03-b239-24df37c57cdf@redhat.com> (raw)
In-Reply-To: <20240221001517.478667-1-polacek@redhat.com>

On 2/20/24 19:15, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> -- >8 --
> This PR asks that our -Wuninitialized for mem-initializers does
> not warn when binding a reference to an uninitialized data member.
> We already check !INDIRECT_TYPE_P in find_uninit_fields_r, but
> that won't catch binding a parameter of a reference type to an
> uninitialized field, as in:
> 
>    struct S { S (int&); };
>    struct T {
>        T() : s(i) {}
>        S s;
>        int i;
>    };
> 
> This patch adds a new function to handle this case.

For type_build_ctor_call types like S, it's weird that we currently 
find_uninit_fields before building the initialization.  What if we move 
the check after the build_aggr_init so we have the actual initializer 
instead of just the expression?

> 	PR c++/113987
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (conv_binds_to_reference_parm_p): New.
> 	* cp-tree.h (conv_binds_to_reference_parm_p): Declare.
> 	* init.cc (find_uninit_fields_r): Call it.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wuninitialized-15.C: Turn dg-warning into dg-bogus.
> 	* g++.dg/warn/Wuninitialized-34.C: New test.
> ---
>   gcc/cp/call.cc                                | 24 ++++++++++++++
>   gcc/cp/cp-tree.h                              |  1 +
>   gcc/cp/init.cc                                |  3 +-
>   gcc/testsuite/g++.dg/warn/Wuninitialized-15.C |  3 +-
>   gcc/testsuite/g++.dg/warn/Wuninitialized-34.C | 32 +++++++++++++++++++
>   5 files changed, 60 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wuninitialized-34.C
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 1dac1470d3b..c40ef2e3028 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -14551,4 +14551,28 @@ maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
>   		"function was not considered");
>   }
>   
> +/* We're converting EXPR to TYPE.  If that conversion involves a conversion
> +   function and we're binding EXPR to a reference parameter of that function,
> +   return true.  */
> +
> +bool
> +conv_binds_to_reference_parm_p (tree type, tree expr)
> +{
> +  conversion_obstack_sentinel cos;
> +  conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
> +				       /*c_cast_p=*/false, LOOKUP_NORMAL,
> +				       tf_none);
> +  if (c && !c->bad_p && c->user_conv_p)
> +    for (; c; c = next_conversion (c))
> +      if (c->kind == ck_user)
> +	for (z_candidate *cand = c->cand; cand; cand = cand->next)
> +	  if (cand->viable == 1)
> +	    for (size_t i = 0; i < cand->num_convs; ++i)
> +	      if (cand->convs[i]->kind == ck_ref_bind
> +		  && conv_get_original_expr (cand->convs[i]) == expr)
> +		return true;
> +
> +  return false;
> +}
> +
>   #include "gt-cp-call.h"
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index 334c11396c2..ce2d85f1f86 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -6845,6 +6845,7 @@ extern void cp_warn_deprecated_use_scopes	(tree);
>   extern tree get_function_version_dispatcher	(tree);
>   extern bool any_template_arguments_need_structural_equality_p (tree);
>   extern void maybe_show_nonconverting_candidate	(tree, tree, tree, int);
> +extern bool conv_binds_to_reference_parm_p	(tree, tree);
>   
>   /* in class.cc */
>   extern tree build_vfield_ref			(tree, tree);
> diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
> index ac37330527e..1a341f7e606 100644
> --- a/gcc/cp/init.cc
> +++ b/gcc/cp/init.cc
> @@ -906,7 +906,8 @@ find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
>   	    warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
>   			"reference %qD is not yet bound to a value when used "
>   			"here", field);
> -	  else if (!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
> +	  else if ((!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
> +		   && !conv_binds_to_reference_parm_p (type, init))
>   	    warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
>   			"member %qD is used uninitialized", field);
>   	  *walk_subtrees = false;
> diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C
> index 89e90668c41..2fd33037bfd 100644
> --- a/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C
> +++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-15.C
> @@ -65,8 +65,7 @@ struct H {
>     G g;
>     A a2;
>     H() : g(a1) { }
> -  // ??? clang++ doesn't warn here
> -  H(int) : g(a2) { } // { dg-warning "member .H::a2. is used uninitialized" }
> +  H(int) : g(a2) { } // { dg-bogus "member .H::a2. is used uninitialized" }
>   };
>   
>   struct I {
> diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C
> new file mode 100644
> index 00000000000..28226d8032e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-34.C
> @@ -0,0 +1,32 @@
> +// PR c++/113987
> +// { dg-do compile }
> +// { dg-options "-Wuninitialized" }
> +
> +struct t1 {
> +    t1(int);
> +};
> +struct t2 {
> +    t2(int&, int = 0);
> +    t2(double&, int = 0);
> +};
> +struct t3 {
> +    t3(int&);
> +};
> +struct t4 {};
> +void f1(int&);
> +struct t {
> +    t() :
> +      v1(i),  // { dg-warning "is used uninitialized" }
> +      v2(i),
> +      v3(i),
> +      v4((f1(i), t4())),
> +      v5(i) {}
> +    t1 v1;
> +    t2 v2;
> +    t3 v3;
> +    t4 v4;
> +    t1 v5;
> +    int i;
> +    int j;
> +};
> +int main() { t v1; }
> 
> base-commit: ca5f509903315a56b2f306f8644544861a6525cb


  reply	other threads:[~2024-02-22  8:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21  0:15 Marek Polacek
2024-02-22  8:34 ` Jason Merrill [this message]
2024-02-22 19:28   ` Marek Polacek
2024-02-28 23:21     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3dae04f1-90b7-4a03-b239-24df37c57cdf@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).