]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wuninitialized when binding a ref to uninit DM [PR113987]
authorMarek Polacek <polacek@redhat.com>
Tue, 20 Feb 2024 20:55:55 +0000 (15:55 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 29 Feb 2024 17:39:21 +0000 (12:39 -0500)
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.

gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/init.cc
gcc/testsuite/g++.dg/warn/Wuninitialized-15.C
gcc/testsuite/g++.dg/warn/Wuninitialized-34.C [new file with mode: 0644]

index 1dac1470d3beed05f7b54aef62413a28e12e7182..c40ef2e30281cdc19885705184990d93952fb176 100644 (file)
@@ -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"
index 04c3aa6cd917386da99a74dc9b427b320b31a7ee..06c2637d87a1ef5799d1db93ae98bacc049f79f1 100644 (file)
@@ -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);
index ac37330527e68beef9f9935bb6250392adfed0fa..1a341f7e606189a9865726174e340d6ce19d04e5 100644 (file)
@@ -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;
index 89e90668c41fc5ad35f50bcd1d7841e268699f7f..2fd33037bfdb511f68520e442688f829a5db3a11 100644 (file)
@@ -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 (file)
index 0000000..28226d8
--- /dev/null
@@ -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; }