Message ID | 20240221001517.478667-1-polacek@redhat.com |
---|---|
State | New |
Headers | show |
Series | c++: -Wuninitialized when binding a ref to uninit DM [PR113987] | expand |
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
On Thu, Feb 22, 2024 at 08:34:45AM +0000, Jason Merrill wrote: > 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? Thanks. I've tried but unfortunately I'm not getting anywhere. One problem is that immediately after the find_uninit_fields call we may change the TREE_LIST in if (init && TREE_CODE (init) == TREE_LIST) //... so we'd have to cope with that somehow. Sinking find_uninit_fields into one of the conditions below looks like a complication. Another problem is that calling find_uninit_fields on the result of build_aggr_init call causes a bogus warning: we create something like E::E (&((struct F *) this)->e, ((struct F *) this)->a) and then warn that the this object is uninitialized. So I'm not sure if that fix would be simpler. Marek
On 2/22/24 14:28, Marek Polacek wrote: > On Thu, Feb 22, 2024 at 08:34:45AM +0000, Jason Merrill wrote: >> 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? > > Thanks. I've tried but unfortunately I'm not getting anywhere. One > problem is that immediately after the find_uninit_fields call we may > change the TREE_LIST in > > if (init && TREE_CODE (init) == TREE_LIST) > //... > > so we'd have to cope with that somehow. Sinking find_uninit_fields > into one of the conditions below looks like a complication. Another > problem is that calling find_uninit_fields on the result of > build_aggr_init call causes a bogus warning: we create something like > E::E (&((struct F *) this)->e, ((struct F *) this)->a) > and then warn that the this object is uninitialized. So I'm not sure > if that fix would be simpler. Fair enough, the patch is OK. Jason
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; }