Richard Smith | 9beaf20 | 2013-09-28 05:38:27 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -pedantic-errors -fblocks -std=c++1y -emit-pch %s -o %t-cxx11 |
| 2 | // RUN: %clang_cc1 -ast-print -pedantic-errors -fblocks -std=c++1y -include-pch %t-cxx11 %s | FileCheck -check-prefix=CHECK-PRINT %s |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 3 | |
| 4 | #ifndef HEADER_INCLUDED |
| 5 | |
| 6 | #define HEADER_INCLUDED |
| 7 | template<typename T> |
| 8 | T add_slowly(const T& x, const T &y) { |
| 9 | return [=, &y] { return x + y; }(); |
| 10 | }; |
| 11 | |
| 12 | inline int add_int_slowly_twice(int x, int y) { |
| 13 | int i = add_slowly(x, y); |
| 14 | auto lambda = [&](int z) { return x + z; }; |
| 15 | return i + lambda(y); |
| 16 | } |
| 17 | |
| 18 | inline int sum_array(int n) { |
| 19 | int array[5] = { 1, 2, 3, 4, 5}; |
| 20 | auto lambda = [=](int N) -> int { |
| 21 | int sum = 0; |
| 22 | for (unsigned I = 0; I < N; ++I) |
| 23 | sum += array[N]; |
| 24 | return sum; |
| 25 | }; |
| 26 | |
| 27 | return lambda(n); |
| 28 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 29 | |
| 30 | inline int to_block_pointer(int n) { |
| 31 | auto lambda = [=](int m) { return n + m; }; |
| 32 | int (^block)(int) = lambda; |
| 33 | return block(17); |
| 34 | } |
| 35 | |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 36 | template<typename T> |
| 37 | int init_capture(T t) { |
| 38 | return [&, x(t)] { return sizeof(x); }; |
| 39 | } |
| 40 | |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 41 | #else |
| 42 | |
Douglas Gregor | 9d66504 | 2012-02-14 18:47:12 +0000 | [diff] [blame] | 43 | // CHECK-PRINT: T add_slowly |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 44 | // CHECK-PRINT: return [=, &y] |
| 45 | template float add_slowly(const float&, const float&); |
| 46 | |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 47 | int add(int x, int y) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 48 | return add_int_slowly_twice(x, y) + sum_array(4) + to_block_pointer(5); |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // CHECK-PRINT: inline int add_int_slowly_twice |
| 52 | // CHECK-PRINT: lambda = [&] (int z) |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 53 | |
| 54 | // CHECK-PRINT: init_capture |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 55 | // CHECK-PRINT: [&, x(t)] |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 57 | #endif |