]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wuninitialized for mem-inits and empty classes [PR19808]
authorMarek Polacek <polacek@redhat.com>
Fri, 19 Nov 2021 19:22:10 +0000 (14:22 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 23 Nov 2021 20:02:08 +0000 (15:02 -0500)
This fixes a bogus -Wuninitialized warning: there's nothing to initialize
in empty classes, so don't add them into our uninitialized set.

PR c++/19808

gcc/cp/ChangeLog:

* init.c (emit_mem_initializers): Don't add is_really_empty_class
members into uninitialized.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wuninitialized-28.C: Make a class nonempty.
* g++.dg/warn/Wuninitialized-29.C: Likewise.
* g++.dg/warn/Wuninitialized-31.C: New test.

gcc/cp/init.c
gcc/testsuite/g++.dg/warn/Wuninitialized-28.C
gcc/testsuite/g++.dg/warn/Wuninitialized-29.C
gcc/testsuite/g++.dg/warn/Wuninitialized-31.C [new file with mode: 0644]

index 975f2eda29dfb13653aa245191deba47c85f1d14..2a4512e462a97ba9db809a8ea5f31d38a4f07530 100644 (file)
@@ -1470,7 +1470,8 @@ emit_mem_initializers (tree mem_inits)
     for (tree f = next_initializable_field (TYPE_FIELDS (current_class_type));
         f != NULL_TREE;
         f = next_initializable_field (DECL_CHAIN (f)))
-      if (!DECL_ARTIFICIAL (f))
+      if (!DECL_ARTIFICIAL (f)
+         && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
        uninitialized.add (f);
 
   if (mem_inits
index 7dbbf8719ec51d31cf10d15da12c837d3231a0af..816249c2b9c85535814401cdc1cd529640f1c100 100644 (file)
@@ -47,6 +47,7 @@ struct F {
 };
 
 struct bar {
+  int a;
   bar() {}
   bar(bar&) {}
 };
index bc742997441aa386462843d75de44efbd15e0857..da81abf07c9301f0fc1d2b45e3ad9a930ff2e785 100644 (file)
@@ -47,6 +47,7 @@ struct F {
 };
 
 struct bar {
+  int a;
   bar() {}
   bar(bar&) {}
 };
diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-31.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-31.C
new file mode 100644 (file)
index 0000000..e22b150
--- /dev/null
@@ -0,0 +1,73 @@
+// PR c++/19808
+// { dg-do compile }
+// { dg-options "-Wuninitialized" }
+
+class AllocatorWithCleanup {
+public:
+  int *allocate(int);
+};
+class SecBlock {
+  SecBlock() : m_ptr(m_alloc.allocate(0)) {} // { dg-bogus "uninitialized" }
+  AllocatorWithCleanup m_alloc;
+  int *m_ptr;
+};
+
+struct A {
+  int *allocate(int);
+};
+
+struct B {
+  int : 0;
+  int *allocate(int);
+};
+
+struct C : B {
+};
+
+struct D {
+  char arr[0];
+  int *allocate(int);
+};
+
+struct E { };
+
+struct F {
+  E arr[10];
+  int *allocate(int);
+};
+
+struct G {
+  E e;
+  int *allocate(int);
+};
+
+struct H {
+  virtual void foo ();
+  int *allocate(int);
+};
+
+template<typename T>
+struct X {
+  X() : m_ptr(t.allocate(0)) {} // { dg-bogus "uninitialized" }
+  T t;
+  int *m_ptr;
+};
+
+struct V {
+  int a;
+  int *allocate(int);
+};
+
+struct Z {
+  Z() : m_ptr(v.allocate(0)) {} // { dg-warning "uninitialized" }
+  V v;
+  int *m_ptr;
+};
+
+X<A> x1;
+X<B> x2;
+X<C> x3;
+X<D> x4;
+X<F> x5;
+X<G> x6;
+X<H> x7;