1 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s -std=c++11 | FileCheck %s 2 3 // FIXME: The padding in all these objects should be zero-initialized. 4 namespace StructUnion { 5 struct A { 6 int n; 7 double d; 8 union U { 9 constexpr U(int x) : x(x) {} 10 constexpr U(const char *y) : y(y) {} 11 int x; 12 const char *y; 13 } u; 14 15 constexpr A(int n, double d, int x) : n(n), d(d), u(x) {} 16 constexpr A(int n, double d, const char *y) : n(n), d(d), u(y) {} 17 }; 18 19 // CHECK: @_ZN11StructUnion1aE = constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } } 20 extern constexpr A a(1, 2.0, 3); 21 22 // CHECK: @_ZN11StructUnion1bE = constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { i8* getelementptr inbounds ([6 x i8]* @{{.*}}, i32 0, i32 0) } } 23 extern constexpr A b(4, 5, "hello"); 24 25 struct B { 26 int n; 27 }; 28 29 // CHECK: @_ZN11StructUnion1cE = global {{.*}} zeroinitializer 30 // CHECK: @_ZN11StructUnion2c2E = global {{.*}} zeroinitializer 31 B c; 32 B c2 = B(); 33 34 // CHECK: @_ZN11StructUnion1dE = global {{.*}} zeroinitializer 35 B d[10]; 36 37 struct C { 38 constexpr C() : c(0) {} 39 int c; 40 }; 41 42 // CHECK: @_ZN11StructUnion1eE = global {{.*}} zeroinitializer 43 C e[10]; 44 45 struct D { 46 constexpr D() : d(5) {} 47 int d; 48 }; 49 50 // CHECK: @_ZN11StructUnion1fE = global {{.*}} { i32 5 } 51 D f; 52 } 53 54 namespace BaseClass { 55 template<typename T, unsigned> struct X : T {}; 56 struct C { char c = 1; }; 57 template<unsigned... Ns> struct Cs : X<C,Ns>... {}; 58 struct N { int n = 3; }; 59 struct D { double d = 4.0; }; 60 61 template<typename ...Ts> 62 struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; }; 63 64 using Test1 = Test<N, C, Cs<1,2>, D, X<C,1>>; 65 // CHECK: @_ZN9BaseClass2t1E = constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8 66 extern constexpr Test1 t1 = Test1(); 67 68 struct DN : D, N {}; 69 struct DND : DN, X<D,0> {}; 70 struct DNN : DN, X<N,0> {}; 71 // CHECK: @_ZN9BaseClass3dndE = constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 } 72 extern constexpr DND dnd = DND(); 73 // Note, N subobject is laid out in DN subobject's tail padding. 74 // CHECK: @_ZN9BaseClass3dnnE = constant {{.*}} { double 4.000000e+00, i32 3, i32 3 } 75 extern constexpr DNN dnn = DNN(); 76 77 struct E {}; 78 struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {}; 79 // CHECK: @_ZN9BaseClass2t2E = constant {{.*}} undef 80 extern constexpr Test2 t2 = Test2(); 81 } 82 83 namespace Array { 84 // CHECK: @_ZN5Array3arrE = constant [2 x i32] [i32 4, i32 0] 85 extern constexpr int arr[2] = { 4 }; 86 87 // CHECK: @_ZN5Array1cE = constant [6 x [4 x i8]] [{{.*}} c"foo\00", [4 x i8] c"a\00\00\00", [4 x i8] c"bar\00", [4 x i8] c"xyz\00", [4 x i8] c"b\00\00\00", [4 x i8] c"123\00"] 88 extern constexpr char c[6][4] = { "foo", "a", { "bar" }, { 'x', 'y', 'z' }, { "b" }, '1', '2', '3' }; 89 90 struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; }; 91 // CHECK: @_ZN5Array5ctorsE = constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }] 92 extern const C ctors[3]; 93 constexpr C ctors[3]; 94 95 // CHECK: @_ZN5Array1dE = constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] } 96 struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 }; 97 98 struct E { 99 char c[4]; 100 char d[4]; 101 constexpr E() : c("foo"), d("x") {} 102 }; 103 // CHECK: @_ZN5Array1eE = constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" } 104 extern constexpr E e = E(); 105 } 106 107 namespace MemberPtr { 108 struct B1 { 109 int a, b; 110 virtual void f(); 111 void g(); 112 }; 113 struct B2 { 114 int c, d; 115 virtual void h(); 116 void i(); 117 }; 118 struct C : B1 { 119 int e; 120 virtual void j(); 121 void k(); 122 }; 123 struct D : C, B2 { 124 int z; 125 virtual void l(); 126 void m(); 127 }; 128 129 // CHECK: @_ZN9MemberPtr2daE = constant i64 8 130 // CHECK: @_ZN9MemberPtr2dbE = constant i64 12 131 // CHECK: @_ZN9MemberPtr2dcE = constant i64 32 132 // CHECK: @_ZN9MemberPtr2ddE = constant i64 36 133 // CHECK: @_ZN9MemberPtr2deE = constant i64 16 134 // CHECK: @_ZN9MemberPtr2dzE = constant i64 40 135 extern constexpr int (D::*da) = &B1::a; 136 extern constexpr int (D::*db) = &C::b; 137 extern constexpr int (D::*dc) = &B2::c; 138 extern constexpr int (D::*dd) = &D::d; 139 extern constexpr int (D::*de) = &C::e; 140 extern constexpr int (D::*dz) = &D::z; 141 142 // CHECK: @_ZN9MemberPtr2baE = constant i64 8 143 // CHECK: @_ZN9MemberPtr2bbE = constant i64 12 144 // CHECK: @_ZN9MemberPtr2bcE = constant i64 8 145 // CHECK: @_ZN9MemberPtr2bdE = constant i64 12 146 // CHECK: @_ZN9MemberPtr2beE = constant i64 16 147 // CHECK: @_ZN9MemberPtr3b1zE = constant i64 40 148 // CHECK: @_ZN9MemberPtr3b2zE = constant i64 16 149 extern constexpr int (B1::*ba) = (int(B1::*))&B1::a; 150 extern constexpr int (B1::*bb) = (int(B1::*))&C::b; 151 extern constexpr int (B2::*bc) = (int(B2::*))&B2::c; 152 extern constexpr int (B2::*bd) = (int(B2::*))&D::d; 153 extern constexpr int (B1::*be) = (int(B1::*))&C::e; 154 extern constexpr int (B1::*b1z) = (int(B1::*))&D::z; 155 extern constexpr int (B2::*b2z) = (int(B2::*))&D::z; 156 157 // CHECK: @_ZN9MemberPtr2dfE = constant {{.*}} { i64 1, i64 0 } 158 // CHECK: @_ZN9MemberPtr2dgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 } 159 // CHECK: @_ZN9MemberPtr2dhE = constant {{.*}} { i64 1, i64 24 } 160 // CHECK: @_ZN9MemberPtr2diE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 } 161 // CHECK: @_ZN9MemberPtr2djE = constant {{.*}} { i64 9, i64 0 } 162 // CHECK: @_ZN9MemberPtr2dkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 } 163 // CHECK: @_ZN9MemberPtr2dlE = constant {{.*}} { i64 17, i64 0 } 164 // CHECK: @_ZN9MemberPtr2dmE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 } 165 extern constexpr void (D::*df)() = &C::f; 166 extern constexpr void (D::*dg)() = &B1::g; 167 extern constexpr void (D::*dh)() = &B2::h; 168 extern constexpr void (D::*di)() = &D::i; 169 extern constexpr void (D::*dj)() = &C::j; 170 extern constexpr void (D::*dk)() = &C::k; 171 extern constexpr void (D::*dl)() = &D::l; 172 extern constexpr void (D::*dm)() = &D::m; 173 174 // CHECK: @_ZN9MemberPtr2bfE = constant {{.*}} { i64 1, i64 0 } 175 // CHECK: @_ZN9MemberPtr2bgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 } 176 // CHECK: @_ZN9MemberPtr2bhE = constant {{.*}} { i64 1, i64 0 } 177 // CHECK: @_ZN9MemberPtr2biE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 } 178 // CHECK: @_ZN9MemberPtr2bjE = constant {{.*}} { i64 9, i64 0 } 179 // CHECK: @_ZN9MemberPtr2bkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 } 180 // CHECK: @_ZN9MemberPtr3b1lE = constant {{.*}} { i64 17, i64 0 } 181 // CHECK: @_ZN9MemberPtr3b1mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 } 182 // CHECK: @_ZN9MemberPtr3b2lE = constant {{.*}} { i64 17, i64 -24 } 183 // CHECK: @_ZN9MemberPtr3b2mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 } 184 extern constexpr void (B1::*bf)() = (void(B1::*)())&C::f; 185 extern constexpr void (B1::*bg)() = (void(B1::*)())&B1::g; 186 extern constexpr void (B2::*bh)() = (void(B2::*)())&B2::h; 187 extern constexpr void (B2::*bi)() = (void(B2::*)())&D::i; 188 extern constexpr void (B1::*bj)() = (void(B1::*)())&C::j; 189 extern constexpr void (B1::*bk)() = (void(B1::*)())&C::k; 190 extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l; 191 extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m; 192 extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l; 193 extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m; 194 } 195 196 namespace LiteralReference { 197 struct Lit { 198 constexpr Lit() : n(5) {} 199 int n; 200 }; 201 // FIXME: This should have static initialization, but we do not implement 202 // that yet. For now, just check that we don't set the (pointer) value of 203 // the reference to 5! 204 // 205 // CHECK: @_ZN16LiteralReference3litE = global {{.*}} null 206 const Lit &lit = Lit(); 207 } 208 209 namespace NonLiteralConstexpr { 210 constexpr int factorial(int n) { 211 return n ? factorial(n-1) * n : 1; 212 } 213 extern void f(int *p); 214 215 struct NonTrivialDtor { 216 constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {} 217 ~NonTrivialDtor() { 218 f(p); 219 } 220 221 int n; 222 int *p; 223 }; 224 static_assert(!__is_literal(NonTrivialDtor), ""); 225 // CHECK: @_ZN19NonLiteralConstexpr3ntdE = global {{.*}} { i32 120, i32* getelementptr 226 NonTrivialDtor ntd; 227 228 struct VolatileMember { 229 constexpr VolatileMember() : n(5) {} 230 volatile int n; 231 }; 232 static_assert(!__is_literal(VolatileMember), ""); 233 // CHECK: @_ZN19NonLiteralConstexpr2vmE = global {{.*}} { i32 5 } 234 VolatileMember vm; 235 236 struct Both { 237 constexpr Both() : n(10) {} 238 ~Both(); 239 volatile int n; 240 }; 241 // CHECK: @_ZN19NonLiteralConstexpr1bE = global {{.*}} { i32 10 } 242 Both b; 243 244 void StaticVars() { 245 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, i32* getelementptr {{.*}} 246 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd = 247 static NonTrivialDtor ntd; 248 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 } 249 // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm = 250 static VolatileMember vm; 251 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 } 252 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b = 253 static Both b; 254 } 255 } 256 257 // Constant initialization tests go before this point, 258 // dynamic initialization tests go after. 259 260 // We must emit a constant initializer for NonLiteralConstexpr::ntd, but also 261 // emit an initializer to register its destructor. 262 // CHECK: define {{.*}}cxx_global_var_init{{.*}} 263 // CHECK-NOT: NonLiteralConstexpr 264 // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev {{.*}} @_ZN19NonLiteralConstexpr3ntdE 265 // CHECK-NEXT: ret void 266 267 // We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm. 268 // CHECK-NOT: NonLiteralConstexpr2vm 269 270 // We must emit a constant initializer for NonLiteralConstexpr::b, but also 271 // emit an initializer to register its destructor. 272 // CHECK: define {{.*}}cxx_global_var_init{{.*}} 273 // CHECK-NOT: NonLiteralConstexpr 274 // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr4BothD1Ev {{.*}} @_ZN19NonLiteralConstexpr1bE 275 // CHECK-NEXT: ret void 276 277 // CHECK: define {{.*}}NonLiteralConstexpr10StaticVars 278 // CHECK-NOT: } 279 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev 280 // CHECK-NOT: } 281 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev 282 283 namespace CrossFuncLabelDiff { 284 // Make sure we refuse to constant-fold the variable b. 285 constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); } 286 void test() { static long b = (long)&&lbl - a(false); lbl: return; } 287 // CHECK: sub nsw i64 ptrtoint (i8* blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64), 288 // CHECK: store i64 {{.*}}, i64* @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8 289 } 290 291 // PR12012 292 namespace VirtualBase { 293 struct B {}; 294 struct D : virtual B {}; 295 D d; 296 // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev 297 298 template<typename T> struct X : T { 299 constexpr X() : T() {} 300 }; 301 X<D> x; 302 // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev 303 } 304