xref: /llvm-project/clang/test/CodeGen/constructor-attribute.c (revision 39db5e1ed87363a9ffea81e53520b542201b3262)
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
4 
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
9 
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
21 
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)
28 
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)
32 
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)
38 
39 int printf(const char *, ...);
40 
41 void A(void) __attribute__((constructor));
42 void B(void) __attribute__((destructor));
43 
A(void)44 void A(void) {
45   printf("A\n");
46 }
47 
B(void)48 void B(void) {
49   printf("B\n");
50 }
51 
52 static void C(void) __attribute__((constructor));
53 
54 static void D(void) __attribute__((destructor));
55 
E(void)56 static __attribute__((destructor(123))) void E(void) {
57 }
58 
F(void)59 static __attribute__((destructor(789))) void F(void) {
60 }
61 
G(void)62 static __attribute__((destructor(123))) void G(void) {
63 }
64 
65 // Test that this function doesn't collide with the synthesized constructor
66 // function for destructors with priority 789.
__GLOBAL_init_789(int a)67 int __GLOBAL_init_789(int a) {
68   return a * a;
69 }
70 
foo(void)71 static int foo(void) {
72   return 10;
73 }
74 
C(void)75 static void C(void) {
76   printf("A: %d\n", foo());
77 }
78 
D(void)79 static void D(void) {
80   printf("B\n");
81 }
82 
main(void)83 int main(void) {
84   return 0;
85 }
86 
87 // CXAATEXIT: ![[GLOBAL_INIT_SP]] = distinct !DISubprogram(linkageName: "__GLOBAL_init_123",
88 // CXAATEXIT: ![[GLOBAL_INIT_LOC]] = !DILocation(line: 0, scope: ![[GLOBAL_INIT_SP]])
89