blob: db5eebfaacff443659c0b955fbb0acacfcbb03b2 [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
Sean Huntbbd37c62009-11-21 08:43:09 +00002
Sean Hunt2edf0a22012-06-23 05:07:58 +00003// Need std::initializer_list
4namespace std {
5 typedef decltype(sizeof(int)) size_t;
6
7 // libc++'s implementation
8 template <class _E>
9 class initializer_list
10 {
11 const _E* __begin_;
12 size_t __size_;
13
14 initializer_list(const _E* __b, size_t __s)
15 : __begin_(__b),
16 __size_(__s)
17 {}
18
19 public:
20 typedef _E value_type;
21 typedef const _E& reference;
22 typedef const _E& const_reference;
23 typedef size_t size_type;
24
25 typedef const _E* iterator;
26 typedef const _E* const_iterator;
27
28 initializer_list() : __begin_(nullptr), __size_(0) {}
29
30 size_t size() const {return __size_;}
31 const _E* begin() const {return __begin_;}
32 const _E* end() const {return __begin_ + __size_;}
33 };
34}
35
36
Sean Huntbbd37c62009-11-21 08:43:09 +000037// Declaration syntax checks
38[[]] int before_attr;
Peter Collingbournef1907682011-09-29 18:03:57 +000039int [[]] between_attr;
Sean Hunt2edf0a22012-06-23 05:07:58 +000040const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}}
Sean Huntbbd37c62009-11-21 08:43:09 +000041int after_attr [[]];
42int * [[]] ptr_attr;
Richard Smith6ee326a2012-04-10 01:32:12 +000043int & [[]] ref_attr = after_attr;
44int && [[]] rref_attr = 0;
Sean Huntbbd37c62009-11-21 08:43:09 +000045int array_attr [1] [[]];
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +000046alignas(8) int aligned_attr;
Sean Hunt2edf0a22012-06-23 05:07:58 +000047[[test::valid(for 42 [very] **** '+' symbols went on a trip and had a "good"_time; the end.)]]
Sean Huntbbd37c62009-11-21 08:43:09 +000048 int garbage_attr;
Richard Smithc56298d2012-04-10 03:25:07 +000049[[,,,static, class, namespace,, inline, constexpr, mutable,, bi\
50tand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr;
51[[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}}
Sean Huntbbd37c62009-11-21 08:43:09 +000052void fn_attr () [[]];
Richard Smith6ee326a2012-04-10 01:32:12 +000053void noexcept_fn_attr () noexcept [[]];
54struct MemberFnOrder {
55 virtual void f() const volatile && noexcept [[]] final = 0;
56};
Sean Hunt2edf0a22012-06-23 05:07:58 +000057struct [[]] struct_attr;
Sean Huntbbd37c62009-11-21 08:43:09 +000058class [[]] class_attr {};
Sean Hunt2edf0a22012-06-23 05:07:58 +000059union [[]] union_attr;
60[[]] struct with_init_declarators {} init_declarator;
61[[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
62[[]];
63struct ctordtor {
64 [[]] ctordtor();
65 [[]] ~ctordtor();
66};
67[[]] ctordtor::ctordtor() {}
68[[]] ctordtor::~ctordtor() {}
Sean Huntbbd37c62009-11-21 08:43:09 +000069extern "C++" [[]] int extern_attr;
70template <typename T> [[]] void template_attr ();
Peter Collingbourne3497fdf2011-09-29 18:04:05 +000071[[]] [[]] int [[]] [[]] multi_attr [[]] [[]];
Sean Huntbbd37c62009-11-21 08:43:09 +000072
Richard Smithc56298d2012-04-10 03:25:07 +000073int comma_attr [[,]];
Sean Huntbbd37c62009-11-21 08:43:09 +000074int scope_attr [[foo::]]; // expected-error {{expected identifier}}
Richard Smith6ee326a2012-04-10 01:32:12 +000075int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +000076unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}}
77unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}}
Sean Huntbbd37c62009-11-21 08:43:09 +000078class foo {
Richard Smith6ee326a2012-04-10 01:32:12 +000079 void const_after_attr () [[]] const; // expected-error {{expected ';'}}
Sean Huntbbd37c62009-11-21 08:43:09 +000080};
81extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}}
82[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}}
Richard Smith282e7e62012-02-04 09:53:13 +000083[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}}
Sean Huntbbd37c62009-11-21 08:43:09 +000084[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}}
85[[]] asm(""); // expected-error {{an attribute list cannot appear here}}
86
87[[]] using ns::i; // expected-error {{an attribute list cannot appear here}}
88[[]] using namespace ns;
89
Sean Hunt2edf0a22012-06-23 05:07:58 +000090[[]] using T = int; // expected-error {{an attribute list cannot appear here}}
91using T [[]] = int; // ok
92template<typename T> using U [[]] = T;
93using ns::i [[]]; // expected-error {{an attribute list cannot appear here}}
94using [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
95
96auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
97auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
98auto trailing() -> const int [[]];
99auto trailing_2() -> struct struct_attr [[]];
100
101namespace N {
102 struct S {};
103};
104template<typename> struct Template {};
105
106// FIXME: Improve this diagnostic
107struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
108struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
109struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
110template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
111template <> struct [[]] Template<void>;
112
113enum [[]] E1 {};
114enum [[]] E2; // expected-error {{forbids forward references}}
115enum [[]] E1;
116enum [[]] E3 : int;
117enum [[]] {
118 k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}}
119};
120enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}}
121enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}}
122enum struct [[]] E5;
123
124struct S {
125 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}}
126 [[]] friend int g(); // expected-FIXME{{an attribute list cannot appear here}}
127 [[]] friend int h() {
128 }
129 friend class [[]] C; // expected-error{{an attribute list cannot appear here}}
130};
131template<typename T> void tmpl(T) {}
132template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}}
133template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}}
134template void [[]] tmpl(short);
135
Sean Huntbbd37c62009-11-21 08:43:09 +0000136// Argument tests
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000137alignas int aligned_no_params; // expected-error {{expected '('}}
Richard Smith282e7e62012-02-04 09:53:13 +0000138alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}}
Sean Huntbbd37c62009-11-21 08:43:09 +0000139
140// Statement tests
141void foo () {
142 [[]] ;
143 [[]] { }
144 [[]] if (0) { }
145 [[]] for (;;);
146 [[]] do {
147 [[]] continue;
148 } while (0);
149 [[]] while (0);
150
151 [[]] switch (i) {
152 [[]] case 0:
153 [[]] default:
154 [[]] break;
155 }
156
157 [[]] goto there;
158 [[]] there:
159
160 [[]] try {
161 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}}
162 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000163 struct S { int arr[2]; } s;
164 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
165 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
166
Richard Smith6ce48a72012-04-11 04:01:28 +0000167 void bar [[noreturn]] ([[]] int i, [[]] int j);
168 using FuncType = void ([[]] int);
169 void baz([[]]...); // expected-error {{expected parameter declarator}}
170
Sean Huntbbd37c62009-11-21 08:43:09 +0000171 [[]] return;
172}
Richard Smithc56298d2012-04-10 03:25:07 +0000173
174template<typename...Ts> void variadic() {
175 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}}
176}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000177
178// Expression tests
179void bar () {
180 [] () [[noreturn]] { return; } (); // expected-error {{should not return}}
181 [] () [[noreturn]] { throw; } ();
182 new int[42][[]][5][[]]{};
183}
184
185// Condition tests
186void baz () {
187 if ([[]] bool b = true) {
188 switch ([[]] int n { 42 }) {
189 default:
190 for ([[]] int n = 0; [[]] char b = n < 5; ++b) {
191 }
192 }
193 }
194 int x;
195 // An attribute can be applied to an expression-statement, such as the first
196 // statement in a for. But it can't be applied to a condition which is an
197 // expression.
198 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}}
199 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}}
200 while ([[]] bool k { false }) {
201 }
202 while ([[]] true) { // expected-error {{an attribute list cannot appear here}}
203 }
204 do {
205 } while ([[]] false); // expected-error {{an attribute list cannot appear here}}
206
207 for ([[]] int n : { 1, 2, 3 }) {
208 }
209}