]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/5645 (gcc warns that pure virtual class not explicitly initialized)
authorManuel López-Ibáñez <manu@gcc.gnu.org>
Thu, 14 Feb 2008 23:11:04 +0000 (18:11 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 14 Feb 2008 23:11:04 +0000 (18:11 -0500)
        PR c++/5645
        PR c++/11159
        * class.c (type_has_user_nondefault_constructor): New fn.
        * cp-tree.h: Declare it.
        * init.c (emit_mem_initializers): Use it for -W warning about
        missing base initializer.

Co-Authored-By: Jason Merrill <jason@redhat.com>
From-SVN: r132324

gcc/cp/ChangeLog
gcc/cp/class.c
gcc/cp/cp-tree.h
gcc/cp/init.c
gcc/testsuite/g++.dg/warn/Wreorder-1.C
gcc/testsuite/g++.dg/warn/pr11159.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/pr5645.C [new file with mode: 0644]

index 275ca4b1aebf2584ee2ee9ec080b7e31e8d41bda..36355125b417e3b63064efe96c828f1e30c8073c 100644 (file)
@@ -1,3 +1,13 @@
+2008-02-14  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
+           Jason Merrill  <jason@redhat.com>
+
+       PR c++/5645
+       PR c++/11159
+       * class.c (type_has_user_nondefault_constructor): New fn.
+       * cp-tree.h: Declare it.
+       * init.c (emit_mem_initializers): Use it for -W warning about
+       missing base initializer.
+
 2008-02-14  Paolo Carlini  <pcarlini@suse.de>
 
         PR c++/28743
index a5456c23324c67a2d744e176ac99fc680d78cd7c..1a76816c2294c445a559eab0547d729b740db117 100644 (file)
@@ -4039,6 +4039,28 @@ clone_constructors_and_destructors (tree t)
     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
 }
 
+/* Returns true iff class T has a user-defined constructor other than
+   the default constructor.  */
+
+bool
+type_has_user_nondefault_constructor (tree t)
+{
+  tree fns;
+
+  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
+    return false;
+
+  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    {
+      tree fn = OVL_CURRENT (fns);
+      if (!DECL_ARTIFICIAL (fn)
+         && skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn)) != NULL_TREE)
+       return true;
+    }
+
+  return false;
+}
+
 /* Remove all zero-width bit-fields from T.  */
 
 static void
index 27892b9c0fadcf73e39084aae617b821c8f16094..81e88613ff36d0ab95b07dc2af6e7b6083d0cb2d 100644 (file)
@@ -4157,6 +4157,7 @@ extern void determine_key_method          (tree);
 extern void check_for_override                 (tree, tree);
 extern void push_class_stack                   (void);
 extern void pop_class_stack                    (void);
+extern bool type_has_user_nondefault_constructor (tree);
 
 /* in cvt.c */
 extern tree convert_to_reference               (tree, tree, int, int, tree);
index 040b335147259c877dcc36fc944f6804bca08cb3..3e6db24713759f3081b2c2375dda563946e1f8b0 100644 (file)
@@ -829,12 +829,13 @@ emit_mem_initializers (tree mem_inits)
       tree subobject = TREE_PURPOSE (mem_inits);
       tree arguments = TREE_VALUE (mem_inits);
 
-      /* If these initializations are taking place in a copy
-        constructor, the base class should probably be explicitly
-        initialized.  */
+      /* If these initializations are taking place in a copy constructor,
+        the base class should probably be explicitly initialized if there
+        is a user-defined constructor in the base class (other than the
+        default constructor, which will be called anyway).  */
       if (extra_warnings && !arguments
          && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
-         && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
+         && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
        warning (OPT_Wextra, "%Jbase class %q#T should be explicitly initialized in the "
                 "copy constructor",
                 current_function_decl, BINFO_TYPE (subobject));
index bd8113a2a47c0afeaae770756bae59937fc9bebf..940ef4a464f7114217a81e967bd15ba0460245d9 100644 (file)
@@ -10,5 +10,5 @@ struct T {
 
 struct U : virtual public S, virtual public T {
   U () : T (), S () {}     // { dg-warning "" }
-  U (const U&) : S () {}   // { dg-warning "copy" }
+  U (const U&) : S () {}
 };
diff --git a/gcc/testsuite/g++.dg/warn/pr11159.C b/gcc/testsuite/g++.dg/warn/pr11159.C
new file mode 100644 (file)
index 0000000..ed4107a
--- /dev/null
@@ -0,0 +1,37 @@
+// PR c++/11159 : erroneous warning in copy ctor with virtual inheritance
+// { dg-do compile }
+// { dg-options "-Wall -Wextra" }
+struct A
+{
+  A ();
+};
+
+struct B : virtual A
+{
+  B ();
+};
+
+struct C : virtual A
+{
+  C ();
+};
+
+struct D : B, C
+{
+  D (D const&){}
+};
+
+template <typename Base>
+struct E : Base
+{
+  E ();
+
+  E (E const &)
+    : Base ()
+  {
+  };
+};
+
+E<C> foo;
+E<C> bar (foo);
+
diff --git a/gcc/testsuite/g++.dg/warn/pr5645.C b/gcc/testsuite/g++.dg/warn/pr5645.C
new file mode 100644 (file)
index 0000000..5ca61bd
--- /dev/null
@@ -0,0 +1,32 @@
+// PR5645: gcc warns that pure virtual class not explicitly initialized.  
+// { dg-do compile }
+// { dg-options "-Wall -Wextra" }
+
+class a {
+public:
+  virtual int f() = 0;
+  virtual int g() = 0;
+};
+
+class b : public a {
+public:
+  b();
+  b(const b& c);
+
+protected:
+  int i;
+};
+
+b::b() {}
+
+b::b(const b& c) { // { dg-bogus "base class .class a. should be explicitly initialized in the copy constructor" }
+  i = c.i;
+}
+
+struct X {};
+
+struct Y : X
+{
+  Y (Y const&) {}
+};
+