• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - |FileCheck %s
2 // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck -check-prefix NOEXC %s
3 
4 struct A {
5   A();
6   ~A();
7 };
8 
9 struct B { B(); ~B(); };
10 
11 struct C { void *field; };
12 
13 struct D { ~D(); };
14 
15 // CHECK: @__dso_handle = external unnamed_addr global i8*
16 // CHECK: @c = global %struct.C zeroinitializer, align 8
17 
18 // It's okay if we ever implement the IR-generation optimization to remove this.
19 // CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8]*
20 
21 // PR6205: The casts should not require global initializers
22 // CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C"
23 // CHECK: @_ZN6PR59741aE = global %"struct.PR5974::A"* getelementptr inbounds (%"struct.PR5974::C"* @_ZN6PR59741cE, i32 0, i32 0)
24 // CHECK: @_ZN6PR59741bE = global %"struct.PR5974::B"* bitcast (i8* getelementptr (i8* bitcast (%"struct.PR5974::C"* @_ZN6PR59741cE to i8*), i64 4) to %"struct.PR5974::B"*), align 8
25 
26 // CHECK: call void @_ZN1AC1Ev(%struct.A* @a)
27 // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @a, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
28 A a;
29 
30 // CHECK: call void @_ZN1BC1Ev(%struct.B* @b)
31 // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.B*)* @_ZN1BD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.B* @b, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
32 B b;
33 
34 // PR6205: this should not require a global initializer
35 // CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c)
36 C c;
37 
38 // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.D*)* @_ZN1DD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.D* @d, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
39 D d;
40 
41 // <rdar://problem/7458115>
42 namespace test1 {
43   int f();
44   const int x = f();   // This has side-effects and gets emitted immediately.
45   const int y = x - 1; // This gets deferred.
46   const int z = ~y;    // This also gets deferred, but gets "undeferred" before y.
test()47   int test() { return z; }
48 // CHECK:      define i32 @_ZN5test14testEv()
49 
50   // All of these initializers end up delayed, so we check them later.
51 }
52 
53 // <rdar://problem/8246444>
54 namespace 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 {{.*}} @_ZN5test21aE
63 }
64 
65 namespace test3 {
66   // Tested at the beginning of the file.
67   const char * const var = "string";
68   extern const char * const var;
69 
test()70   const char *test() { return var; }
71 }
72 
73 namespace test6 {
74   struct A {
75     A();
76   };
77   extern int foo();
78 
79   // This needs an initialization function and guard variables.
80   // CHECK: load i8* bitcast (i64* @_ZGVN5test61xE
81   // CHECK: [[CALL:%.*]] = call i32 @_ZN5test63fooEv
82   // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test61xE
83   // CHECK-NEXT: store i64 1, i64* @_ZGVN5test61xE
84   __attribute__((weak)) int x = foo();
85 }
86 
87 namespace 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 // CHECK:      define internal void [[TEST1_Z_INIT:@.*]]()
99 // CHECK:        load i32* @_ZN5test1L1yE
100 // CHECK-NEXT:   xor
101 // CHECK-NEXT:   store i32 {{.*}}, i32* @_ZN5test1L1zE
102 // CHECK:      define internal void [[TEST1_Y_INIT:@.*]]()
103 // CHECK:        load i32* @_ZN5test1L1xE
104 // CHECK-NEXT:   sub
105 // CHECK-NEXT:   store i32 {{.*}}, i32* @_ZN5test1L1yE
106 
107 // PR9570: the indirect field shouldn't crash IR gen.
108 namespace test5 {
109   static union {
110     unsigned bar[4096] __attribute__((aligned(128)));
111   };
112 }
113 
114 
115 // At the end of the file, we check that y is initialized before z.
116 
117 // CHECK: define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" {
118 // CHECK:   call void [[TEST1_Y_INIT]]
119 // CHECK:   call void [[TEST1_Z_INIT]]
120 
121 // rdar://problem/8090834: this should be nounwind
122 // CHECK-NOEXC: define internal void @_GLOBAL__I_a() nounwind section "__TEXT,__StaticInit,regular,pure_instructions" {
123