blob: ac847a4893edc7b8f7a7a2f124a71404cfde23a3 [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;
Richard Smith6b3d3e52013-02-20 19:22:51 +000044int & [[unknown]] ref_attr_2 = after_attr; // expected-warning {{unknown attribute 'unknown' ignored}}
45int & [[noreturn]] ref_attr_3 = after_attr; // expected-error {{'noreturn' attribute cannot be applied to types}}
Richard Smith6ee326a2012-04-10 01:32:12 +000046int && [[]] rref_attr = 0;
Sean Huntbbd37c62009-11-21 08:43:09 +000047int array_attr [1] [[]];
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +000048alignas(8) int aligned_attr;
Michael Han6880f492012-10-03 01:56:22 +000049[[test::valid(for 42 [very] **** '+' symbols went on a trip and had a "good"_time; the end.)]] int garbage_attr; // expected-warning {{unknown attribute 'valid' ignored}}
50[[,,,static, class, namespace,, inline, constexpr, mutable,, bitand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; // expected-warning {{unknown attribute 'static' ignored}} \
51 // expected-warning {{unknown attribute 'class' ignored}} \
52 // expected-warning {{unknown attribute 'namespace' ignored}} \
53 // expected-warning {{unknown attribute 'inline' ignored}} \
54 // expected-warning {{unknown attribute 'constexpr' ignored}} \
55 // expected-warning {{unknown attribute 'mutable' ignored}} \
56 // expected-warning {{unknown attribute 'bitand' ignored}} \
57 // expected-warning {{unknown attribute 'compl' ignored}}
Richard Smithc56298d2012-04-10 03:25:07 +000058[[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}}
Sean Huntbbd37c62009-11-21 08:43:09 +000059void fn_attr () [[]];
Richard Smith6ee326a2012-04-10 01:32:12 +000060void noexcept_fn_attr () noexcept [[]];
61struct MemberFnOrder {
62 virtual void f() const volatile && noexcept [[]] final = 0;
63};
Sean Hunt2edf0a22012-06-23 05:07:58 +000064struct [[]] struct_attr;
Sean Huntbbd37c62009-11-21 08:43:09 +000065class [[]] class_attr {};
Sean Hunt2edf0a22012-06-23 05:07:58 +000066union [[]] union_attr;
Michael Han2e397132012-11-26 22:54:45 +000067
68// Checks attributes placed at wrong syntactic locations of class specifiers.
Michael Han2e397132012-11-26 22:54:45 +000069class [[]] [[]]
70 attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}}
71
72class [[]] [[]]
73 attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}}
74
75class [[]] c {};
76class c [[]] [[]] x;
77class c [[]] [[]] y [[]] [[]];
78class c final [(int){0}];
79
80class base {};
81class [[]] [[]] final_class
82 alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}}
83 alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}}
84
85class [[]] [[]] final_class_another
86 [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}}
87 [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}}
88
Sean Hunt2edf0a22012-06-23 05:07:58 +000089[[]] struct with_init_declarators {} init_declarator;
90[[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
91[[]];
92struct ctordtor {
93 [[]] ctordtor();
94 [[]] ~ctordtor();
95};
96[[]] ctordtor::ctordtor() {}
97[[]] ctordtor::~ctordtor() {}
Sean Huntbbd37c62009-11-21 08:43:09 +000098extern "C++" [[]] int extern_attr;
99template <typename T> [[]] void template_attr ();
Peter Collingbourne3497fdf2011-09-29 18:04:05 +0000100[[]] [[]] int [[]] [[]] multi_attr [[]] [[]];
Sean Huntbbd37c62009-11-21 08:43:09 +0000101
Richard Smithc56298d2012-04-10 03:25:07 +0000102int comma_attr [[,]];
Sean Huntbbd37c62009-11-21 08:43:09 +0000103int scope_attr [[foo::]]; // expected-error {{expected identifier}}
Richard Smith6ee326a2012-04-10 01:32:12 +0000104int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000105unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}}
106unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}}
Sean Huntbbd37c62009-11-21 08:43:09 +0000107class foo {
Richard Smith6ee326a2012-04-10 01:32:12 +0000108 void const_after_attr () [[]] const; // expected-error {{expected ';'}}
Sean Huntbbd37c62009-11-21 08:43:09 +0000109};
110extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}}
111[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}}
Richard Smith282e7e62012-02-04 09:53:13 +0000112[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}}
Sean Huntbbd37c62009-11-21 08:43:09 +0000113[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}}
114[[]] asm(""); // expected-error {{an attribute list cannot appear here}}
115
116[[]] using ns::i; // expected-error {{an attribute list cannot appear here}}
Richard Smith6b3d3e52013-02-20 19:22:51 +0000117[[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}}
118[[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions and methods}}
Sean Huntbbd37c62009-11-21 08:43:09 +0000119
Sean Hunt2edf0a22012-06-23 05:07:58 +0000120[[]] using T = int; // expected-error {{an attribute list cannot appear here}}
121using T [[]] = int; // ok
122template<typename T> using U [[]] = T;
123using ns::i [[]]; // expected-error {{an attribute list cannot appear here}}
124using [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
Richard Smith6b3d3e52013-02-20 19:22:51 +0000125using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}}
126using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions and methods}}
127using V = int; // expected-note {{previous}}
128using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000129
130auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
131auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
132auto trailing() -> const int [[]];
133auto trailing_2() -> struct struct_attr [[]];
134
135namespace N {
136 struct S {};
137};
138template<typename> struct Template {};
139
140// FIXME: Improve this diagnostic
141struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
142struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
143struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
144template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
145template <> struct [[]] Template<void>;
146
147enum [[]] E1 {};
148enum [[]] E2; // expected-error {{forbids forward references}}
149enum [[]] E1;
150enum [[]] E3 : int;
151enum [[]] {
152 k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}}
153};
154enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}}
155enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}}
156enum struct [[]] E5;
157
158struct S {
159 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}}
Michael Han52b501c2012-11-28 23:17:40 +0000160 friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}}
161 friend int f2 [[]] [[noreturn]] () {}
162 [[]] friend int g(); // expected-error{{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000163 [[]] friend int h() {
164 }
Michael Han52b501c2012-11-28 23:17:40 +0000165 [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}}
166 friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000167 friend class [[]] C; // expected-error{{an attribute list cannot appear here}}
Michael Han52b501c2012-11-28 23:17:40 +0000168 [[]] friend class D; // expected-error{{an attribute list cannot appear here}}
169 [[]] friend int; // expected-error{{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000170};
171template<typename T> void tmpl(T) {}
172template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}}
173template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}}
174template void [[]] tmpl(short);
175
Sean Huntbbd37c62009-11-21 08:43:09 +0000176// Argument tests
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000177alignas int aligned_no_params; // expected-error {{expected '('}}
Richard Smith282e7e62012-02-04 09:53:13 +0000178alignas(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 +0000179
180// Statement tests
181void foo () {
182 [[]] ;
183 [[]] { }
184 [[]] if (0) { }
185 [[]] for (;;);
186 [[]] do {
187 [[]] continue;
188 } while (0);
189 [[]] while (0);
190
191 [[]] switch (i) {
192 [[]] case 0:
193 [[]] default:
194 [[]] break;
195 }
196
197 [[]] goto there;
198 [[]] there:
199
200 [[]] try {
201 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}}
202 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000203 struct S { int arr[2]; } s;
204 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
205 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
206
Richard Smith6ce48a72012-04-11 04:01:28 +0000207 void bar [[noreturn]] ([[]] int i, [[]] int j);
208 using FuncType = void ([[]] int);
209 void baz([[]]...); // expected-error {{expected parameter declarator}}
210
Sean Huntbbd37c62009-11-21 08:43:09 +0000211 [[]] return;
212}
Richard Smithc56298d2012-04-10 03:25:07 +0000213
214template<typename...Ts> void variadic() {
215 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}}
216}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000217
218// Expression tests
219void bar () {
Richard Smithcd8ab512013-01-17 01:30:42 +0000220 // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains
221 // to the operator()'s type, and GCC does not otherwise accept attributes
222 // applied to types. Use that to test this.
223 [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}}
224 [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000225 new int[42][[]][5][[]]{};
226}
227
228// Condition tests
229void baz () {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000230 if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}}
231 switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000232 default:
Richard Smith6b3d3e52013-02-20 19:22:51 +0000233 for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000234 }
235 }
236 }
237 int x;
238 // An attribute can be applied to an expression-statement, such as the first
239 // statement in a for. But it can't be applied to a condition which is an
240 // expression.
241 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}}
242 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}}
243 while ([[]] bool k { false }) {
244 }
245 while ([[]] true) { // expected-error {{an attribute list cannot appear here}}
246 }
247 do {
248 } while ([[]] false); // expected-error {{an attribute list cannot appear here}}
249
Richard Smith6b3d3e52013-02-20 19:22:51 +0000250 for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000251 }
252}
John McCall1e12b3d2012-06-23 22:30:04 +0000253
254enum class __attribute__((visibility("hidden"))) SecretKeepers {
255 one, /* rest are deprecated */ two, three
256};
257enum class [[]] EvenMoreSecrets {};
Michael Han6880f492012-10-03 01:56:22 +0000258
259namespace arguments {
Richard Smithf7a05272013-01-14 07:53:01 +0000260 void f[[gnu::format(printf, 1, 2)]](const char*, ...);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000261 void g() [[unknown::foo(arguments of attributes from unknown namespace other than 'gnu' namespace are ignored... blah...)]]; // expected-warning {{unknown attribute 'foo' ignored}}
Michael Han6880f492012-10-03 01:56:22 +0000262}
Michael Hanf64231e2012-11-06 19:34:54 +0000263
Richard Smithf7a05272013-01-14 07:53:01 +0000264// Forbid attributes on decl specifiers.
Richard Smithd03de6a2013-01-29 10:02:16 +0000265unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \
Michael Hanf64231e2012-11-06 19:34:54 +0000266 expected-error {{an attribute list cannot appear here}}
Richard Smithd03de6a2013-01-29 10:02:16 +0000267typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \
Michael Hanf64231e2012-11-06 19:34:54 +0000268 expected-error {{an attribute list cannot appear here}}
Richard Smithd03de6a2013-01-29 10:02:16 +0000269int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}}
Richard Smithf7a05272013-01-14 07:53:01 +0000270
271// Forbid [[gnu::...]] attributes on declarator chunks.
272int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}}
273int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
274int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
Richard Smith6b3d3e52013-02-20 19:22:51 +0000275
276[[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}}
277[[noreturn]]; // expected-error {{'noreturn' attribute cannot be used in an attribute declaration}}