215 lines · cpp
1// RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - |FileCheck %s --check-prefixes=CHECK,PRE172// RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck %s --check-prefixes=CHECK-NOEXC,PRE173// RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm -mframe-pointer=non-leaf %s -o - \4// RUN: | FileCheck -check-prefix CHECK-FP %s5// RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - -fno-builtin \6// RUN: | FileCheck -check-prefix CHECK-NOBUILTIN %s7// RUN: %clang_cc1 %std_cxx17- -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - | FileCheck %s8 9struct A {10 A();11 ~A();12};13 14struct B { B(); ~B(); };15 16struct C { void *field; };17 18struct D { ~D(); };19 20// CHECK: @__dso_handle = external hidden global i821// CHECK: @c ={{.*}} global %struct.C zeroinitializer, align 822 23// PR6205: The casts should not require global initializers24// CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C"25// CHECK: @_ZN6PR59741aE ={{.*}} global ptr @_ZN6PR59741cE26// CHECK: @_ZN6PR59741bE ={{.*}} global ptr getelementptr (i8, ptr @_ZN6PR59741cE, i64 4), align 827 28// CHECK: call void @_ZN1AC1Ev(ptr {{[^,]*}} @a)29// CHECK: call i32 @__cxa_atexit(ptr @_ZN1AD1Ev, ptr @a, ptr @__dso_handle)30A a;31 32// CHECK: call void @_ZN1BC1Ev(ptr {{[^,]*}} @b)33// CHECK: call i32 @__cxa_atexit(ptr @_ZN1BD1Ev, ptr @b, ptr @__dso_handle)34B b;35 36// PR6205: this should not require a global initializer37// CHECK-NOT: call void @_ZN1CC1Ev(ptr @c)38C c;39 40// CHECK: call i32 @__cxa_atexit(ptr @_ZN1DD1Ev, ptr @d, ptr @__dso_handle)41D d;42 43namespace test1 {44 int f();45 const int x = f(); // This has side-effects and gets emitted immediately.46 const int y = x - 1; // This gets deferred.47 const int z = ~y; // This also gets deferred, but gets "undeferred" before y.48 int test() { return z; }49// CHECK-LABEL: define{{.*}} i32 @_ZN5test14testEv()50 51 // All of these initializers end up delayed, so we check them later.52}53 54namespace test2 {55 struct allocator { allocator(); ~allocator(); };56 struct A { A(const allocator &a = allocator()); ~A(); };57 58 A a;59// CHECK: call void @_ZN5test29allocatorC1Ev(60// CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE(61// CHECK: call void @_ZN5test29allocatorD1Ev(62// CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev, {{.*}} @_ZN5test21aE63}64 65namespace test3 {66 // Tested at the beginning of the file.67 const char * const var = "string";68 extern const char * const var;69 70 const char *test() { return var; }71}72 73namespace test4 {74 struct A {75 A();76 };77 extern int foo();78 79 // This needs an initialization function and guard variables.80 // CHECK: load i8, ptr @_ZGVN5test41xE81 // CHECK: store i8 1, ptr @_ZGVN5test41xE82 // CHECK-NEXT: [[CALL:%.*]] = call noundef i32 @_ZN5test43fooEv83 // CHECK-NEXT: store i32 [[CALL]], ptr @_ZN5test41xE84 __attribute__((weak)) int x = foo();85}86 87namespace PR5974 {88 struct A { int a; };89 struct B { int b; };90 struct C : A, B { int c; };91 92 extern C c;93 94 // These should not require global initializers.95 A* a = &c;96 B* b = &c;97}98 99// PR9570: the indirect field shouldn't crash IR gen.100namespace test5 {101 static union {102 unsigned bar[4096] __attribute__((aligned(128)));103 };104}105 106namespace std { struct type_info; }107 108namespace test6 {109 struct A { virtual ~A(); };110 struct B : A {};111 extern A *p;112 113 // We must emit a dynamic initializer for 'q', because it could throw.114 B *const q = &dynamic_cast<B&>(*p);115 // CHECK: call void @__cxa_bad_cast()116 // CHECK: store {{.*}} @_ZN5test6L1qE117 118 // We don't need to emit 'r' at all, because it has internal linkage, is119 // unused, and its initialization has no side-effects.120 B *const r = dynamic_cast<B*>(p);121 // CHECK-NOT: call void @__cxa_bad_cast()122 // CHECK-NOT: store {{.*}} @_ZN5test6L1rE123 124 // This can throw, so we need to emit it.125 const std::type_info *const s = &typeid(*p);126 // CHECK: store {{.*}} @_ZN5test6L1sE127 128 // This can't throw, so we don't.129 const std::type_info *const t = &typeid(p);130 // CHECK-NOT: @_ZN5test6L1tE131 132 extern B *volatile v;133 // CHECK: store {{.*}} @_ZN5test6L1wE134 B *const w = dynamic_cast<B*>(v);135 136 // CHECK: load volatile137 // CHECK: store {{.*}} @_ZN5test6L1xE138 const int x = *(volatile int*)0x1234;139 140 namespace {141 int a = int();142 volatile int b = int();143 int c = a;144 int d = b;145 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1aE146 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE147 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1cE148 // CHECK: load volatile {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE149 // CHECK: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1dE150 }151}152 153namespace test7 {154 struct A { A(); };155 struct B { ~B(); int n; };156 struct C { C() = default; C(const C&); int n; };157 struct D {};158 159 // CHECK: call void @_ZN5test71AC1Ev({{.*}}@_ZN5test7L1aE)160 const A a = A();161 162 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZN5test7L2b1E163 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZGRN5test72b2E164 // CHECK: call void @_ZN5test71BD1Ev(165 // CHECK: store {{.*}} @_ZN5test7L2b3E166 const B b1 = B();167 const B &b2 = B();168 const int b3 = B().n;169 170 // CHECK-NOT: @_ZN5test7L2c1E171 // PRE17: call void @llvm.memset{{.*}} @_ZN5test7L2c1E172 // CHECK-NOT: @_ZN5test7L2c1E173 // CHECK: @_ZN5test7L2c2E174 // CHECK-NOT: @_ZN5test7L2c3E175 // PRE17: @_ZN5test7L2c4E176 const C c1 = C();177 const C c2 = static_cast<const C&>(C());178 const int c3 = C().n;179 const int c4 = C(C()).n;180 181 // CHECK-NOT: @_ZN5test7L1dE182 const D d = D();183 184 // CHECK: store {{.*}} @_ZN5test71eE185 int f(), e = f();186}187 188 189// At the end of the file, we check that y is initialized before z.190 191// CHECK: define internal void [[TEST1_Z_INIT:@.*]]()192// CHECK: load i32, ptr @_ZN5test1L1yE193// CHECK-NEXT: xor194// CHECK-NEXT: store i32 {{.*}}, ptr @_ZN5test1L1zE195// CHECK: define internal void [[TEST1_Y_INIT:@.*]]()196// CHECK: load i32, ptr @_ZN5test1L1xE197// CHECK-NEXT: sub198// CHECK-NEXT: store i32 {{.*}}, ptr @_ZN5test1L1yE199 200// CHECK: define internal void @_GLOBAL__sub_I_global_init.cpp() #{{[0-9]+}} section "__TEXT,__StaticInit,regular,pure_instructions" {201// CHECK: call void [[TEST1_Y_INIT]]202// CHECK: call void [[TEST1_Z_INIT]]203 204// this should be nounwind205// CHECK-NOEXC: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {206// CHECK-NOEXC: attributes [[NUW]] = { noinline nounwind{{.*}} }207 208// Make sure we mark global initializers with the no-builtins attribute.209// CHECK-NOBUILTIN: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {210// CHECK-NOBUILTIN: attributes [[NUW]] = { noinline nounwind{{.*}}"no-builtins"{{.*}} }211 212// PR21811: attach the appropriate attribute to the global init function213// CHECK-FP: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUX:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {214// CHECK-FP: attributes [[NUX]] = { noinline nounwind {{.*}}"frame-pointer"="non-leaf"{{.*}} }215