Home | History | Annotate | Download | only in CodeGenCXX
      1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-macosx10.7.0 -emit-llvm -o - %s | FileCheck %s
      2 
      3 // CHECK: @_ZZNK7PR12917IJiiEE1nMUlvE_clEvE1n = linkonce_odr global i32 0
      4 // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd_NKUlvE_clEvE1n = linkonce_odr global i32 0
      5 // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd0_NKUlvE_clEvE1n = linkonce_odr global i32 0
      6 // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd1_NKUlvE_clEvE1n = linkonce_odr global i32 0
      7 
      8 // CHECK: define linkonce_odr void @_Z11inline_funci
      9 inline void inline_func(int n) {
     10   // CHECK: call i32 @_ZZ11inline_funciENKUlvE_clEv
     11   int i = []{ return 1; }();
     12 
     13   // CHECK: call i32 @_ZZ11inline_funciENKUlvE0_clEv
     14   int j = [=] { return n + i; }();
     15 
     16   // CHECK: call double @_ZZ11inline_funciENKUlvE1_clEv
     17   int k = [=] () -> double { return n + i; }();
     18 
     19   // CHECK: call i32 @_ZZ11inline_funciENKUliE_clEi
     20   int l = [=] (int x) -> int { return x + i; }(n);
     21 
     22   int inner(int i = []{ return 17; }());
     23   // CHECK: call i32 @_ZZ11inline_funciENKUlvE2_clEv
     24   // CHECK-NEXT: call i32 @_Z5inneri
     25   inner();
     26 
     27   // CHECK-NEXT: ret void
     28 }
     29 
     30 void call_inline_func() {
     31   inline_func(17);
     32 }
     33 
     34 struct S {
     35   void f(int = []{return 1;}()
     36              + []{return 2;}(),
     37          int = []{return 3;}());
     38   void g(int, int);
     39 };
     40 
     41 void S::g(int i = []{return 1;}(),
     42           int j = []{return 2; }()) {}
     43 
     44 // CHECK: define void @_Z6test_S1S
     45 void test_S(S s) {
     46   // CHECK: call i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
     47   // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
     48   // CHECK-NEXT: add nsw i32
     49   // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
     50   // CHECK-NEXT: call void @_ZN1S1fEii
     51   s.f();
     52 
     53   // NOTE: These manglings don't actually matter that much, because
     54   // the lambdas in the default arguments of g() won't be seen by
     55   // multiple translation units. We check them mainly to ensure that they don't
     56   // get the special mangling for lambdas in in-class default arguments.
     57   // CHECK: call i32 @"_ZNK1S3$_0clEv"
     58   // CHECK-NEXT: call i32 @"_ZNK1S3$_1clEv"
     59   // CHECK-NEXT: call void @_ZN1S1gEi
     60   s.g();
     61 
     62   // CHECK-NEXT: ret void
     63 }
     64 
     65 // Check the linkage of the lambda call operators used in test_S.
     66 // CHECK: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
     67 // CHECK: ret i32 1
     68 // CHECK: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
     69 // CHECK: ret i32 2
     70 // CHECK: define linkonce_odr i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
     71 // CHECK: ret i32 3
     72 // CHECK: define internal i32 @"_ZNK1S3$_0clEv"
     73 // CHECK: ret i32 1
     74 // CHECK: define internal i32 @"_ZNK1S3$_1clEv"
     75 // CHECK: ret i32 2
     76 
     77 template<typename T>
     78 struct ST {
     79   void f(T = []{return T() + 1;}()
     80            + []{return T() + 2;}(),
     81          T = []{return T(3);}());
     82 };
     83 
     84 // CHECK: define void @_Z7test_ST2STIdE
     85 void test_ST(ST<double> st) {
     86   // CHECK: call double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
     87   // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
     88   // CHECK-NEXT: fadd double
     89   // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
     90   // CHECK-NEXT: call void @_ZN2STIdE1fEdd
     91   st.f();
     92 
     93   // CHECK-NEXT: ret void
     94 }
     95 
     96 // Check the linkage of the lambda call operators used in test_ST.
     97 // CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
     98 // CHECK: ret double 1
     99 // CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
    100 // CHECK: ret double 2
    101 // CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
    102 // CHECK: ret double 3
    103 
    104 template<typename T>
    105 struct StaticMembers {
    106   static T x;
    107   static T y;
    108   static T z;
    109   static int (*f)();
    110 };
    111 
    112 template<typename T> int accept_lambda(T);
    113 
    114 template<typename T>
    115 T StaticMembers<T>::x = []{return 1;}() + []{return 2;}();
    116 
    117 template<typename T>
    118 T StaticMembers<T>::y = []{return 3;}();
    119 
    120 template<typename T>
    121 T StaticMembers<T>::z = accept_lambda([]{return 4;});
    122 
    123 template<typename T>
    124 int (*StaticMembers<T>::f)() = []{return 5;};
    125 
    126 // CHECK: define internal void @__cxx_global_var_init()
    127 // CHECK: call i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
    128 // CHECK-NEXT: call i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
    129 // CHECK-NEXT: add nsw
    130 // CHECK: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
    131 // CHECK: ret i32 1
    132 // CHECK: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
    133 // CHECK: ret i32 2
    134 template float StaticMembers<float>::x;
    135 
    136 // CHECK: define internal void @__cxx_global_var_init1()
    137 // CHECK: call i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
    138 // CHECK: define linkonce_odr i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
    139 // CHECK: ret i32 3
    140 template float StaticMembers<float>::y;
    141 
    142 // CHECK: define internal void @__cxx_global_var_init2()
    143 // CHECK: call i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_
    144 // CHECK: declare i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_()
    145 template float StaticMembers<float>::z;
    146 
    147 // CHECK: define internal void @__cxx_global_var_init3()
    148 // CHECK: call {{.*}} @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
    149 // CHECK: define linkonce_odr i32 ()* @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
    150 template int (*StaticMembers<float>::f)();
    151 
    152 // CHECK: define internal void @__cxx_global_var_init4
    153 // CHECK: call i32 @"_ZNK13StaticMembersIdE3$_2clEv"
    154 // CHECK: define internal i32 @"_ZNK13StaticMembersIdE3$_2clEv"
    155 // CHECK: ret i32 42
    156 template<> double StaticMembers<double>::z = []{return 42; }();
    157 
    158 template<typename T>
    159 void func_template(T = []{ return T(); }());
    160 
    161 // CHECK: define void @_Z17use_func_templatev()
    162 void use_func_template() {
    163   // CHECK: call i32 @"_ZZ13func_templateIiEvT_ENK3$_3clEv"
    164   func_template<int>();
    165 }
    166 
    167 
    168 template<typename...T> struct PR12917 {
    169   PR12917(T ...t = []{ static int n = 0; return ++n; }());
    170 
    171   static int n[3];
    172 };
    173 template<typename...T> int PR12917<T...>::n[3] = {
    174   []{ static int n = 0; return ++n; }()
    175 };
    176 
    177 // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd1_NKUlvE_clEv(
    178 // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd0_NKUlvE_clEv(
    179 // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd_NKUlvE_clEv(
    180 // CHECK: call void @_ZN7PR12917IJicdEEC1Eicd(
    181 PR12917<int, char, double> pr12917;
    182 int *pr12917_p = PR12917<int, int>::n;
    183 
    184 namespace std {
    185   struct type_info;
    186 }
    187 namespace PR12123 {
    188   struct A { virtual ~A(); } g;
    189   struct B {
    190     void f(const std::type_info& x = typeid([]()->A& { return g; }()));
    191     void h();
    192   };
    193   void B::h() { f(); }
    194 }
    195 // CHECK: define linkonce_odr %"struct.PR12123::A"* @_ZZN7PR121231B1fERKSt9type_infoEd_NKUlvE_clEv
    196 
    197 namespace PR12808 {
    198   template <typename> struct B {
    199     int a;
    200     template <typename L> constexpr B(L&& x) : a(x()) { }
    201   };
    202   template <typename> void b(int) {
    203     [&]{ (void)B<int>([&]{ return 1; }); }();
    204   }
    205   void f() {
    206     b<int>(1);
    207   }
    208   // CHECK: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
    209   // CHECK: define linkonce_odr i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
    210 }
    211 
    212 // CHECK: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
    213 
    214 struct Members {
    215   int x = [] { return 1; }() + [] { return 2; }();
    216   int y = [] { return 3; }();
    217 };
    218 
    219 void test_Members() {
    220   // CHECK: define linkonce_odr void @_ZN7MembersC2Ev
    221   // CHECK: call i32 @_ZNK7Members1xMUlvE_clEv
    222   // CHECK-NEXT: call i32 @_ZNK7Members1xMUlvE0_clE
    223   // CHECK-NEXT: add nsw i32
    224   // CHECK: call i32 @_ZNK7Members1yMUlvE_clEv
    225   Members members;
    226   // CHECK: ret void
    227 }
    228 
    229 template<typename P> void f(P) { }
    230 
    231 struct TestNestedInstantiation {
    232    void operator()() const {
    233      []() -> void {
    234        return f([]{});
    235      }();
    236    }
    237 };
    238 
    239 void test_NestedInstantiation() {
    240   TestNestedInstantiation()();
    241 }
    242 
    243 // Check the linkage of the lambdas used in test_Members.
    244 // CHECK: define linkonce_odr i32 @_ZNK7Members1xMUlvE_clEv
    245 // CHECK: ret i32 1
    246 // CHECK: define linkonce_odr i32 @_ZNK7Members1xMUlvE0_clEv
    247 // CHECK: ret i32 2
    248 // CHECK: define linkonce_odr i32 @_ZNK7Members1yMUlvE_clEv
    249 // CHECK: ret i32 3
    250 
    251 // Check linkage of the various lambdas.
    252 // CHECK: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE_clEv
    253 // CHECK: ret i32 1
    254 // CHECK: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE0_clEv
    255 // CHECK: ret i32
    256 // CHECK: define linkonce_odr double @_ZZ11inline_funciENKUlvE1_clEv
    257 // CHECK: ret double
    258 // CHECK: define linkonce_odr i32 @_ZZ11inline_funciENKUliE_clEi
    259 // CHECK: ret i32
    260 // CHECK: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE2_clEv
    261 // CHECK: ret i32 17
    262