blob: 2ddcf18409e98ee7d636ee731d44f1bf986377ac [file] [log] [blame]
Douglas Gregore31e6062012-02-07 10:09:13 +00001// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
2
Douglas Gregor199cec72012-02-09 02:45:47 +00003template<typename T> void capture(const T&);
4
Douglas Gregore31e6062012-02-07 10:09:13 +00005class NonCopyable {
6 NonCopyable(const NonCopyable&); // expected-note 2 {{implicitly declared private here}}
Douglas Gregor19666fb2012-02-15 16:57:26 +00007public:
8 void foo() const;
Douglas Gregore31e6062012-02-07 10:09:13 +00009};
10
Douglas Gregor02267a82012-02-15 17:05:32 +000011class NonConstCopy {
12public:
13 NonConstCopy(NonConstCopy&); // expected-note{{would lose const}}
14};
15
16void capture_by_copy(NonCopyable nc, NonCopyable &ncr, const NonConstCopy nco) {
Douglas Gregor19666fb2012-02-15 16:57:26 +000017 (void)[nc] { }; // expected-error{{capture of variable 'nc' as type 'NonCopyable' calls private copy constructor}}
18 (void)[=] {
19 ncr.foo(); // expected-error{{capture of variable 'ncr' as type 'NonCopyable' calls private copy constructor}}
20 }();
Douglas Gregor02267a82012-02-15 17:05:32 +000021
22 [nco] {}(); // expected-error{{no matching constructor for initialization of 'const NonConstCopy'}}
Douglas Gregore31e6062012-02-07 10:09:13 +000023}
24
Douglas Gregor8c50e7c2012-02-09 00:47:04 +000025struct NonTrivial {
26 NonTrivial();
27 NonTrivial(const NonTrivial &);
28 ~NonTrivial();
29};
30
31struct CopyCtorDefault {
Douglas Gregor199cec72012-02-09 02:45:47 +000032 CopyCtorDefault();
Douglas Gregor8c50e7c2012-02-09 00:47:04 +000033 CopyCtorDefault(const CopyCtorDefault&, NonTrivial nt = NonTrivial());
34
35 void foo() const;
36};
37
38void capture_with_default_args(CopyCtorDefault cct) {
Douglas Gregor656bc622012-02-09 08:26:42 +000039 (void)[=] () -> void { cct.foo(); };
Douglas Gregor8c50e7c2012-02-09 00:47:04 +000040}
41
Douglas Gregor199cec72012-02-09 02:45:47 +000042struct ExpectedArrayLayout {
43 CopyCtorDefault array[3];
44};
45
46void capture_array() {
47 CopyCtorDefault array[3];
Douglas Gregor656bc622012-02-09 08:26:42 +000048 auto x = [=]() -> void {
Douglas Gregor199cec72012-02-09 02:45:47 +000049 capture(array[0]);
50 };
51 static_assert(sizeof(x) == sizeof(ExpectedArrayLayout), "layout mismatch");
52}
Douglas Gregor3d23f7882012-02-09 02:12:34 +000053
54// Check for the expected non-static data members.
55
56struct ExpectedLayout {
57 char a;
58 short b;
59};
60
Douglas Gregor3d23f7882012-02-09 02:12:34 +000061void test_layout(char a, short b) {
Douglas Gregor656bc622012-02-09 08:26:42 +000062 auto x = [=] () -> void {
Douglas Gregor3d23f7882012-02-09 02:12:34 +000063 capture(a);
64 capture(b);
65 };
66 static_assert(sizeof(x) == sizeof(ExpectedLayout), "Layout mismatch!");
67}
Eli Friedmanc9751062012-02-11 02:51:16 +000068
69struct ExpectedThisLayout {
70 ExpectedThisLayout* a;
71 void f() {
72 auto x = [this]() -> void {};
73 static_assert(sizeof(x) == sizeof(ExpectedThisLayout), "Layout mismatch!");
74 }
75};
Douglas Gregord814a052012-10-25 18:39:16 +000076
77struct CaptureArrayAndThis {
78 int value;
79
80 void f() {
81 int array[3];
82 [=]() -> int {
83 int result = value;
84 for (unsigned i = 0; i < 3; ++i)
85 result += array[i];
86 return result;
87 }();
88 }
89};
90
Douglas Gregor71fe0e82013-10-11 04:25:21 +000091namespace rdar14468891 {
92 class X {
93 public:
94 virtual ~X() = 0; // expected-note{{unimplemented pure virtual method '~X' in 'X'}}
95 };
96
97 class Y : public X { };
98
99 void capture(X &x) {
100 [x]() {}(); // expected-error{{by-copy capture of value of abstract type 'rdar14468891::X'}}
101 }
102}