File: constructors.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (170 lines) | stat: -rw-r--r-- 6,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s --implicit-check-not=should_not_appear_in_output --check-prefixes=CHECK,NULL-INVALID
// RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-apple-darwin10 %s -emit-llvm -fno-delete-null-pointer-checks -o - | FileCheck %s --implicit-check-not=should_not_appear_in_output --check-prefixes=CHECK,NULL-VALID

struct Member { int x; Member(); Member(int); Member(const Member &); };
struct VBase { int x; VBase(); VBase(int); VBase(const VBase &); };

struct ValueClass {
  ValueClass(int x, int y) : x(x), y(y) {}
  int x;
  int y;
}; // subject to ABI trickery



/* Test basic functionality. */
struct A {
  A(struct Undeclared &);
  A(ValueClass);
  Member mem;
};

A::A(struct Undeclared &ref) : mem(0) {}

// Check that delegation works.
// NULL-INVALID-LABEL: define{{.*}} void @_ZN1AC2ER10Undeclared(%struct.A* {{[^,]*}} %this, %struct.Undeclared* noundef nonnull align 1 %ref) unnamed_addr
// NULL-VALID-LABEL: define{{.*}} void @_ZN1AC2ER10Undeclared(%struct.A* {{[^,]*}} %this, %struct.Undeclared* noundef align 1 %ref) unnamed_addr
// CHECK: call void @_ZN6MemberC1Ei(

// NULL-INVALID-LABEL: define{{.*}} void @_ZN1AC1ER10Undeclared(%struct.A* {{[^,]*}} %this, %struct.Undeclared* noundef nonnull align 1 %ref) unnamed_addr
// NULL-VALID-LABEL: define{{.*}} void @_ZN1AC1ER10Undeclared(%struct.A* {{[^,]*}} %this, %struct.Undeclared* noundef align 1 %ref) unnamed_addr
// CHECK: call void @_ZN1AC2ER10Undeclared(

A::A(ValueClass v) : mem(v.y - v.x) {}

// CHECK-LABEL: define{{.*}} void @_ZN1AC2E10ValueClass(%struct.A* {{[^,]*}} %this, i64 %v.coerce) unnamed_addr
// CHECK: call void @_ZN6MemberC1Ei(

// CHECK-LABEL: define{{.*}} void @_ZN1AC1E10ValueClass(%struct.A* {{[^,]*}} %this, i64 %v.coerce) unnamed_addr
// CHECK: call void @_ZN1AC2E10ValueClass(

/* Test that things work for inheritance. */
struct B : A {
  B(struct Undeclared &);
  Member mem;
};

B::B(struct Undeclared &ref) : A(ref), mem(1) {}

// NULL-INVALID-LABEL: define{{.*}} void @_ZN1BC2ER10Undeclared(%struct.B* {{[^,]*}} %this, %struct.Undeclared* noundef nonnull align 1 %ref) unnamed_addr
// NULL-VALID-LABEL: define{{.*}} void @_ZN1BC2ER10Undeclared(%struct.B* {{[^,]*}} %this, %struct.Undeclared* noundef align 1 %ref) unnamed_addr
// CHECK: call void @_ZN1AC2ER10Undeclared(
// CHECK: call void @_ZN6MemberC1Ei(

// NULL-INVALID-LABEL: define{{.*}} void @_ZN1BC1ER10Undeclared(%struct.B* {{[^,]*}} %this, %struct.Undeclared* noundef nonnull align 1 %ref) unnamed_addr
// NULL-VALID-LABEL: define{{.*}} void @_ZN1BC1ER10Undeclared(%struct.B* {{[^,]*}} %this, %struct.Undeclared* noundef align 1 %ref) unnamed_addr
// CHECK: call void @_ZN1BC2ER10Undeclared(


/* Test that the delegation optimization is disabled for classes with
   virtual bases (for now).  This is necessary because a vbase
   initializer could access one of the parameter variables by
   reference.  That's a solvable problem, but let's not solve it right
   now. */
struct C : virtual A {
  C(int);
  Member mem;
};
C::C(int x) : A(ValueClass(x, x+1)), mem(x * x) {}

// CHECK-LABEL: define{{.*}} void @_ZN1CC2Ei(%struct.C* {{[^,]*}} %this, i8** noundef %vtt, i32 noundef %x) unnamed_addr
// CHECK: call void @_ZN6MemberC1Ei(

// CHECK-LABEL: define{{.*}} void @_ZN1CC1Ei(%struct.C* {{[^,]*}} %this, i32 noundef %x) unnamed_addr
// CHECK: call void @_ZN10ValueClassC1Eii(
// CHECK: call void @_ZN1AC2E10ValueClass(
// CHECK: call void @_ZN6MemberC1Ei(


/* Test that the delegation optimization is disabled for varargs
   constructors. */
struct D : A {
  D(int, ...);
  Member mem;
};

D::D(int x, ...) : A(ValueClass(x, x+1)), mem(x*x) {}

// CHECK-LABEL: define{{.*}} void @_ZN1DC2Eiz(%struct.D* {{[^,]*}} %this, i32 noundef %x, ...) unnamed_addr
// CHECK: call void @_ZN10ValueClassC1Eii(
// CHECK: call void @_ZN1AC2E10ValueClass(
// CHECK: call void @_ZN6MemberC1Ei(

// CHECK-LABEL: define{{.*}} void @_ZN1DC1Eiz(%struct.D* {{[^,]*}} %this, i32 noundef %x, ...) unnamed_addr
// CHECK: call void @_ZN10ValueClassC1Eii(
// CHECK: call void @_ZN1AC2E10ValueClass(
// CHECK: call void @_ZN6MemberC1Ei(

// PR6622:  this shouldn't crash
namespace test0 {
  struct A {};
  struct B : virtual A { int x; };
  struct C : B {};
  
  void test(C &in) {
    C tmp = in;
  }
}

namespace test1 {
  struct A { A(); void *ptr; };
  struct B { B(); int x; A a[0]; };
  B::B() {}
  // CHECK-LABEL:    define{{.*}} void @_ZN5test11BC2Ev(
  // CHECK:      [[THIS:%.*]] = load [[B:%.*]]*, [[B:%.*]]**
  // CHECK-NEXT: ret void
}

// Ensure that we
// a) emit the ABI-required but useless complete object and deleting destructor
//    symbols for an abstract class, and 
// b) do *not* emit references to virtual base destructors for an abstract class
//
// Our approach to this is to give these functions a body that simply traps.
//
// FIXME: We should ideally not create these symbols at all, but Clang can
// actually generate references to them in other TUs in some cases, so we can't
// stop emitting them without breaking ABI. See:
//
//   https://github.com/itanium-cxx-abi/cxx-abi/issues/10
namespace abstract {
  // Note, the destructor of this class is not instantiated here.
  template<typename T> struct should_not_appear_in_output {
    ~should_not_appear_in_output() { int arr[-(int)sizeof(T)]; }
  };

  struct X { ~X(); };

  struct A : virtual should_not_appear_in_output<int>, X {
    virtual ~A() = 0;
  };

  // CHECK-LABEL: define{{.*}} void @_ZN8abstract1AD2Ev(
  // CHECK: call {{.*}}@_ZN8abstract1XD2Ev(
  // CHECK: ret

  // CHECK-LABEL: define{{.*}} void @_ZN8abstract1AD1Ev(
  // CHECK: call {{.*}}@llvm.trap(
  // CHECK: unreachable

  // CHECK-LABEL: define{{.*}} void @_ZN8abstract1AD0Ev(
  // CHECK: call {{.*}}@llvm.trap(
  // CHECK: unreachable
  A::~A() {}

  struct B : virtual should_not_appear_in_output<int>, X {
    virtual void f() = 0;
    ~B();
  };

  // CHECK-LABEL: define{{.*}} void @_ZN8abstract1BD2Ev(
  // CHECK: call {{.*}}@_ZN8abstract1XD2Ev(
  // CHECK: ret

  // CHECK-LABEL: define{{.*}} void @_ZN8abstract1BD1Ev(
  // CHECK: call {{.*}}@llvm.trap(
  // CHECK: unreachable

  // CHECK-NOT: @_ZN8abstract1BD0Ev(
  B::~B() {}
}