Debian Codegraph @

gcc/testsuite/g++.dg/ipa/devirt-33.C

raw
1 /* Verify we do not devirtualize wrongly to __cxa_pure_virtual */
2
3 /* { dg-do run } */
4 /* { dg-options "-O2 -std=c++11" } */
5
6
7 inline void* operator new(__SIZE_TYPE__ s, void* buf) throw() {
8 return buf;
9 }
10
11 class A {
12
13 private:
14 struct Base {
15 virtual ~Base() {}
16 virtual Base *Clone(void *buf) const = 0;
17 virtual float *Allocate(__SIZE_TYPE__ count) = 0;
18 };
19
20 struct Value : Base {
21 virtual ~Value (){}
22 Base *Clone(void* buf) const override {
23 return new (buf) Value();
24 }
25
26 float *Allocate(__SIZE_TYPE__ count) override {
27 return new float[count];
28 }
29 };
30
31 public:
32 A() {
33 new (buffer_) Value();
34 }
35 A(const A& other) {
36 other.ptr()->Clone(buffer_);
37 }
38
39 float *Allocate() {
40 return ptr()->Allocate(100);
41 }
42 const Base *ptr() const { return reinterpret_cast<const Base*>(buffer_);}
43 Base *ptr() { return reinterpret_cast< Base*>(buffer_);}
44
45 private:
46 alignas(16) char buffer_[1024];
47 };
48
49
50 struct B {
51 B (const A& a) : a_(a) {
52 buff_ = a_.Allocate();
53 }
54
55 float *buff_;
56 A a_;
57 };
58
59 struct Dummy {
60 int i;
61 };
62
63 struct D : public Dummy {
64 __attribute__((noinline)) D( const A&a);
65
66 B b_;
67 };
68
69 D::D(const A&a) : b_(a) {}
70
71 int main()
72 {
73 A a;
74 D d(a);
75
76 return 0;
77 }
78