193 lines · cpp
1// RUN: %clang_cc1 %s -triple=x86_64-pc-linuxs -emit-llvm -std=c++98 -o - | FileCheck -check-prefix=CHECK -check-prefix=CHECK98 %s2// RUN: %clang_cc1 %s -triple=x86_64-pc-linuxs -emit-llvm -std=c++11 -o - | FileCheck -check-prefix=CHECK -check-prefix=CHECK11 %s3// RUN: %clang_cc1 %s -triple=x86_64-pc-linuxs -emit-llvm -std=c++20 -o - | FileCheck -check-prefix=CHECK -check-prefix=CHECK20 %s4 5// CHECK: @_ZZ1hvE1i = internal global i32 0, align 46// CHECK: @base_req ={{.*}} global [4 x i8] c"foo\00", align 17// CHECK: @base_req_uchar ={{.*}} global [4 x i8] c"bar\00", align 18 9// CHECK: @_ZZN5test31BC1EvE1u = internal global { i8, [3 x i8] } { i8 97, [3 x i8] undef }, align 410 11// CHECK20: @_ZZN5test51fEvE1a = internal constant %"struct.test5::A" { i32 42 }12 13// CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0, comdat, align 414// CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0, comdat, align 8{{$}}15// CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 1616// CHECK98: @_ZZN5test414useStaticLocalEvE3obj = linkonce_odr global %"struct.test4::HasVTable" zeroinitializer, comdat, align 817// CHECK11: @_ZZN5test414useStaticLocalEvE3obj = linkonce_odr global %"struct.test4::HasVTable" { ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTVN5test49HasVTableE, i32 0, i32 0, i32 2) }, comdat, align 818 19struct A {20 A();21 ~A();22};23 24void f() {25 // CHECK: load atomic i8, ptr @_ZGVZ1fvE1a acquire, align 826 // CHECK: call i32 @__cxa_guard_acquire27 // CHECK: call void @_ZN1AC1Ev28 // CHECK: call i32 @__cxa_atexit(ptr @_ZN1AD1Ev, ptr @_ZZ1fvE1a, ptr @__dso_handle)29 // CHECK: call void @__cxa_guard_release30 static A a;31}32 33void g() {34 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 1)35 // CHECK: call void @_ZN1AC1Ev(36 static A& a = *new A;37}38 39int a();40void h() {41 static const int i = a();42}43 44// CHECK: define linkonce_odr void @_Z2h2v() {{.*}} comdat {45inline void h2() {46 static int i = a();47}48 49void h3() {50 h2();51}52 53// PR6980: this shouldn't crash54namespace test0 {55 struct A { A(); };56 __attribute__((noreturn)) int throw_exception();57 58 void test() {59 throw_exception();60 static A r;61 }62}63 64namespace test1 {65 // CHECK-LABEL: define internal noundef i32 @_ZN5test1L6getvarEi(66 static inline int getvar(int index) {67 static const int var[] = { 1, 0, 2, 4 };68 return var[index];69 }70 71 void test() { (void) getvar(2); }72}73 74// Make sure we emit the initializer correctly for the following:75char base_req[] = { "foo" };76unsigned char base_req_uchar[] = { "bar" };77 78namespace union_static_local {79 // CHECK-LABEL: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv80 // CHECK: call void @_ZN18union_static_local1fEPNS_1xE(ptr noundef @_ZZN18union_static_local4testEvE3foo)81 union x { long double y; const char *x[2]; };82 void f(union x*);83 void test() {84 static union x foo = { .x = { "a", "b" } };85 struct c {86 static void main() {87 f(&foo);88 }89 };90 c::main();91 }92}93 94// Static variables should be consistent across constructor95// or destructor variants.96namespace test2 {97 struct A {98 A();99 ~A();100 };101 102 struct B : virtual A {103 B();104 ~B();105 };106 107 // If we ever implement this as a delegate ctor call, just change108 // this to take variadic arguments or something.109 extern int foo();110 B::B() {111 static int x = foo();112 }113 // CHECK-LABEL: define{{.*}} void @_ZN5test21BC2Ev114 // CHECK: load atomic i8, ptr @_ZGVZN5test21BC1EvE1x acquire, align 8115 // CHECK: call i32 @__cxa_guard_acquire(ptr @_ZGVZN5test21BC1EvE1x)116 // CHECK: [[T0:%.*]] = call noundef i32 @_ZN5test23fooEv()117 // CHECK: store i32 [[T0]], ptr @_ZZN5test21BC1EvE1x,118 // CHECK: call void @__cxa_guard_release(ptr @_ZGVZN5test21BC1EvE1x)119 120 // CHECK-LABEL: define{{.*}} void @_ZN5test21BC1Ev121 // CHECK: load atomic i8, ptr @_ZGVZN5test21BC1EvE1x acquire, align 8122 // CHECK: call i32 @__cxa_guard_acquire(ptr @_ZGVZN5test21BC1EvE1x)123 // CHECK: [[T0:%.*]] = call noundef i32 @_ZN5test23fooEv()124 // CHECK: store i32 [[T0]], ptr @_ZZN5test21BC1EvE1x,125 // CHECK: call void @__cxa_guard_release(ptr @_ZGVZN5test21BC1EvE1x)126 127 // This is just for completeness, because we actually emit this128 // using a delegate dtor call.129 B::~B() {130 static int y = foo();131 }132 // CHECK-LABEL: define{{.*}} void @_ZN5test21BD2Ev(133 // CHECK: load atomic i8, ptr @_ZGVZN5test21BD1EvE1y acquire, align 8134 // CHECK: call i32 @__cxa_guard_acquire(ptr @_ZGVZN5test21BD1EvE1y)135 // CHECK: [[T0:%.*]] = call noundef i32 @_ZN5test23fooEv()136 // CHECK: store i32 [[T0]], ptr @_ZZN5test21BD1EvE1y,137 // CHECK: call void @__cxa_guard_release(ptr @_ZGVZN5test21BD1EvE1y)138 139 // CHECK-LABEL: define{{.*}} void @_ZN5test21BD1Ev(140 // CHECK: call void @_ZN5test21BD2Ev(141}142 143// This shouldn't error out.144namespace test3 {145 struct A {146 A();147 ~A();148 };149 150 struct B : virtual A {151 B();152 ~B();153 };154 155 B::B() {156 union U { char x; int i; };157 static U u = { 'a' };158 }159 // CHECK-LABEL: define{{.*}} void @_ZN5test31BC2Ev(160 // CHECK-LABEL: define{{.*}} void @_ZN5test31BC1Ev(161}162 163// We forgot to set the comdat when replacing the global with a different type.164namespace test4 {165struct HasVTable {166 virtual void f();167};168inline HasVTable &useStaticLocal() {169 static HasVTable obj;170 return obj;171}172void useit() {173 useStaticLocal();174}175// CHECK: define linkonce_odr noundef nonnull align 8 dereferenceable(8) ptr @_ZN5test414useStaticLocalEv()176// CHECK: ret ptr{{.*}} @_ZZN5test414useStaticLocalEvE3obj177}178 179#if __cplusplus >= 202002L180// A const object with constexpr destructor can be emitted as a constant.181namespace test5 {182 struct A {183 constexpr A(int x) : x_(x) {}184 constexpr ~A() {}185 int x_;186 };187 const int *f() {188 static const A a{42};189 return &a.x_;190 }191}192#endif193