diff options
author | Marek Polacek <polacek@redhat.com> | 2024-02-20 15:55:55 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2024-02-29 12:39:21 -0500 |
commit | b83f3cd3ff765fb82344b848b8a128763b7a4233 (patch) | |
tree | 6d8c54ef52070c02a52dde1a9c62bd6f898e8a93 | |
parent | bc0e18a960f9dff3e740f4d0cb5b25b3f68d920a (diff) |
c++: -Wuninitialized when binding a ref to uninit DM [PR113987]
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.
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.
-rw-r--r-- | gcc/cp/call.cc | 24 | ||||
-rw-r--r-- | gcc/cp/cp-tree.h | 1 | ||||
-rw-r--r-- | gcc/cp/init.cc | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wuninitialized-15.C | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wuninitialized-34.C | 32 |
5 files changed, 60 insertions, 3 deletions
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 04c3aa6cd91..06c2637d87a 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6843,6 +6843,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; } |