xref: /minix3/external/bsd/llvm/dist/clang/test/PCH/cxx11-lambdas.mm (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -pedantic-errors -fblocks -std=c++1y -emit-pch %s -o %t-cxx11
2*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -ast-print -pedantic-errors -fblocks -std=c++1y -include-pch %t-cxx11  %s | FileCheck -check-prefix=CHECK-PRINT %s
3*f4a2713aSLionel Sambuc
4*f4a2713aSLionel Sambuc#ifndef HEADER_INCLUDED
5*f4a2713aSLionel Sambuc
6*f4a2713aSLionel Sambuc#define HEADER_INCLUDED
7*f4a2713aSLionel Sambuctemplate<typename T>
8*f4a2713aSLionel SambucT add_slowly(const T& x, const T &y) {
9*f4a2713aSLionel Sambuc  return [=, &y] { return x + y; }();
10*f4a2713aSLionel Sambuc};
11*f4a2713aSLionel Sambuc
12*f4a2713aSLionel Sambucinline int add_int_slowly_twice(int x, int y) {
13*f4a2713aSLionel Sambuc  int i = add_slowly(x, y);
14*f4a2713aSLionel Sambuc  auto lambda = [&](int z) { return x + z; };
15*f4a2713aSLionel Sambuc  return i + lambda(y);
16*f4a2713aSLionel Sambuc}
17*f4a2713aSLionel Sambuc
18*f4a2713aSLionel Sambucinline int sum_array(int n) {
19*f4a2713aSLionel Sambuc  int array[5] = { 1, 2, 3, 4, 5};
20*f4a2713aSLionel Sambuc  auto lambda = [=](int N) -> int {
21*f4a2713aSLionel Sambuc    int sum = 0;
22*f4a2713aSLionel Sambuc    for (unsigned I = 0; I < N; ++I)
23*f4a2713aSLionel Sambuc      sum += array[N];
24*f4a2713aSLionel Sambuc    return sum;
25*f4a2713aSLionel Sambuc  };
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc  return lambda(n);
28*f4a2713aSLionel Sambuc}
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel Sambucinline int to_block_pointer(int n) {
31*f4a2713aSLionel Sambuc  auto lambda = [=](int m) { return n + m; };
32*f4a2713aSLionel Sambuc  int (^block)(int) = lambda;
33*f4a2713aSLionel Sambuc  return block(17);
34*f4a2713aSLionel Sambuc}
35*f4a2713aSLionel Sambuc
36*f4a2713aSLionel Sambuctemplate<typename T>
37*f4a2713aSLionel Sambucint init_capture(T t) {
38*f4a2713aSLionel Sambuc  return [&, x(t)] { return sizeof(x); };
39*f4a2713aSLionel Sambuc}
40*f4a2713aSLionel Sambuc
41*f4a2713aSLionel Sambuc#else
42*f4a2713aSLionel Sambuc
43*f4a2713aSLionel Sambuc// CHECK-PRINT: T add_slowly
44*f4a2713aSLionel Sambuc// CHECK-PRINT: return [=, &y]
45*f4a2713aSLionel Sambuctemplate float add_slowly(const float&, const float&);
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel Sambucint add(int x, int y) {
48*f4a2713aSLionel Sambuc  return add_int_slowly_twice(x, y) + sum_array(4) + to_block_pointer(5);
49*f4a2713aSLionel Sambuc}
50*f4a2713aSLionel Sambuc
51*f4a2713aSLionel Sambuc// CHECK-PRINT: inline int add_int_slowly_twice
52*f4a2713aSLionel Sambuc// CHECK-PRINT: lambda = [&] (int z)
53*f4a2713aSLionel Sambuc
54*f4a2713aSLionel Sambuc// CHECK-PRINT: init_capture
55*f4a2713aSLionel Sambuc// CHECK-PRINT: [&, x( t )]
56*f4a2713aSLionel Sambuc
57*f4a2713aSLionel Sambuc#endif
58