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