[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGen / constructor-attribute.c
blob11e5990be717d843f774f8c157f3d9298b5ef87d
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=WITHOUTATEXIT %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fregister-global-dtors-with-atexit -debug-info-kind=line-tables-only -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=CXAATEXIT --check-prefix=WITHATEXIT %s
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fno-use-cxa-atexit -fregister-global-dtors-with-atexit -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=ATEXIT --check-prefix=WITHATEXIT %s
5 // WITHOUTATEXIT: global_ctors{{.*}}@A{{.*}}@C
6 // WITHOUTATEXIT: @llvm.global_dtors = appending global [5 x { i32, ptr, ptr }]{{.*}}@B{{.*}}@E{{.*}}@F{{.*}}@G{{.*}}@D
7 // WITHATEXIT: @llvm.global_ctors = appending global [5 x { i32, ptr, ptr }]{{.*}}i32 65535, ptr @A,{{.*}}i32 65535, ptr @C,{{.*}}i32 123, ptr @__GLOBAL_init_123,{{.*}}i32 789, ptr @[[GLOBAL_INIT_789:__GLOBAL_init_789.[0-9]+]],{{.*}}i32 65535, ptr @__GLOBAL_init_65535,
8 // WITHATEXIT-NOT: global_dtors
10 // CHECK: define{{.*}} void @A()
11 // CHECK: define{{.*}} void @B()
12 // CHECK: define internal void @E()
13 // CHECK: define internal void @F()
14 // CHECK: define internal void @G()
15 // CHECK: define{{.*}} i32 @__GLOBAL_init_789(i32 noundef %{{.*}})
16 // CHECK: define internal void @C()
17 // CHECK: define internal i32 @foo()
18 // CHECK: define internal void @D()
19 // CHECK: define{{.*}} i32 @main()
20 // WITHOUTATEXIT-NOT: define
22 // CXAATEXIT: define internal void @__GLOBAL_init_123(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions" !dbg ![[GLOBAL_INIT_SP:.*]] {
23 // ATEXIT: define internal void @__GLOBAL_init_123(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions"
24 // CXAATEXIT: call i32 @__cxa_atexit(ptr @E, ptr null, ptr @__dso_handle) {{.*}}, !dbg ![[GLOBAL_INIT_LOC:.*]]
25 // CXAATEXIT: call i32 @__cxa_atexit(ptr @G, ptr null, ptr @__dso_handle)
26 // ATEXIT: call i32 @atexit(ptr @E)
27 // ATEXIT: call i32 @atexit(ptr @G)
29 // WITHATEXIT: define internal void @[[GLOBAL_INIT_789]](){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions"
30 // CXAATEXIT: call i32 @__cxa_atexit(ptr @F, ptr null, ptr @__dso_handle)
31 // ATEXIT: call i32 @atexit(ptr @F)
33 // WITHATEXIT: define internal void @__GLOBAL_init_65535(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions"
34 // CXAATEXIT: call i32 @__cxa_atexit(ptr @B, ptr null, ptr @__dso_handle)
35 // CXAATEXIT: call i32 @__cxa_atexit(ptr @D, ptr null, ptr @__dso_handle)
36 // ATEXIT: call i32 @atexit(ptr @B)
37 // ATEXIT: call i32 @atexit(ptr @D)
39 int printf(const char *, ...);
41 void A(void) __attribute__((constructor));
42 void B(void) __attribute__((destructor));
44 void A(void) {
45 printf("A\n");
48 void B(void) {
49 printf("B\n");
52 static void C(void) __attribute__((constructor));
54 static void D(void) __attribute__((destructor));
56 static __attribute__((destructor(123))) void E(void) {
59 static __attribute__((destructor(789))) void F(void) {
62 static __attribute__((destructor(123))) void G(void) {
65 // Test that this function doesn't collide with the synthesized constructor
66 // function for destructors with priority 789.
67 int __GLOBAL_init_789(int a) {
68 return a * a;
71 static int foo(void) {
72 return 10;
75 static void C(void) {
76 printf("A: %d\n", foo());
79 static void D(void) {
80 printf("B\n");
83 int main(void) {
84 return 0;
87 // CXAATEXIT: ![[GLOBAL_INIT_SP]] = distinct !DISubprogram(linkageName: "__GLOBAL_init_123",
88 // CXAATEXIT: ![[GLOBAL_INIT_LOC]] = !DILocation(line: 0, scope: ![[GLOBAL_INIT_SP]])