c++: -Wuninitialized for mem-inits and empty classes [PR19808]

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.
This commit is contained in:
Marek Polacek 2021-11-19 14:22:10 -05:00
parent 7b7318faf7
commit 4b1d3d8d73
4 changed files with 77 additions and 1 deletions

View 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

View file

@ -47,6 +47,7 @@ struct F {
};
struct bar {
int a;
bar() {}
bar(bar&) {}
};

View file

@ -47,6 +47,7 @@ struct F {
};
struct bar {
int a;
bar() {}
bar(bar&) {}
};

View file

@ -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;