blob: 58e42bffcff3140674372c978b5a6dd6225a5664 [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;
Michael Han6880f492012-10-03 01:56:22 +000047[[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}}
48[[,,,static, class, namespace,, inline, constexpr, mutable,, bitand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; // expected-warning {{unknown attribute 'static' ignored}} \
49 // expected-warning {{unknown attribute 'class' ignored}} \
50 // expected-warning {{unknown attribute 'namespace' ignored}} \
51 // expected-warning {{unknown attribute 'inline' ignored}} \
52 // expected-warning {{unknown attribute 'constexpr' ignored}} \
53 // expected-warning {{unknown attribute 'mutable' ignored}} \
54 // expected-warning {{unknown attribute 'bitand' ignored}} \
55 // expected-warning {{unknown attribute 'compl' ignored}}
Richard Smithc56298d2012-04-10 03:25:07 +000056[[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}}
Sean Huntbbd37c62009-11-21 08:43:09 +000057void fn_attr () [[]];
Richard Smith6ee326a2012-04-10 01:32:12 +000058void noexcept_fn_attr () noexcept [[]];
59struct MemberFnOrder {
60 virtual void f() const volatile && noexcept [[]] final = 0;
61};
Sean Hunt2edf0a22012-06-23 05:07:58 +000062struct [[]] struct_attr;
Sean Huntbbd37c62009-11-21 08:43:09 +000063class [[]] class_attr {};
Sean Hunt2edf0a22012-06-23 05:07:58 +000064union [[]] union_attr;
65[[]] struct with_init_declarators {} init_declarator;
66[[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
67[[]];
68struct ctordtor {
69 [[]] ctordtor();
70 [[]] ~ctordtor();
71};
72[[]] ctordtor::ctordtor() {}
73[[]] ctordtor::~ctordtor() {}
Sean Huntbbd37c62009-11-21 08:43:09 +000074extern "C++" [[]] int extern_attr;
75template <typename T> [[]] void template_attr ();
Peter Collingbourne3497fdf2011-09-29 18:04:05 +000076[[]] [[]] int [[]] [[]] multi_attr [[]] [[]];
Sean Huntbbd37c62009-11-21 08:43:09 +000077
Richard Smithc56298d2012-04-10 03:25:07 +000078int comma_attr [[,]];
Sean Huntbbd37c62009-11-21 08:43:09 +000079int scope_attr [[foo::]]; // expected-error {{expected identifier}}
Richard Smith6ee326a2012-04-10 01:32:12 +000080int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}}
Sean Hunt2edf0a22012-06-23 05:07:58 +000081unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}}
82unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}}
Sean Huntbbd37c62009-11-21 08:43:09 +000083class foo {
Richard Smith6ee326a2012-04-10 01:32:12 +000084 void const_after_attr () [[]] const; // expected-error {{expected ';'}}
Sean Huntbbd37c62009-11-21 08:43:09 +000085};
86extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}}
87[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}}
Richard Smith282e7e62012-02-04 09:53:13 +000088[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}}
Sean Huntbbd37c62009-11-21 08:43:09 +000089[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}}
90[[]] asm(""); // expected-error {{an attribute list cannot appear here}}
91
92[[]] using ns::i; // expected-error {{an attribute list cannot appear here}}
93[[]] using namespace ns;
94
Sean Hunt2edf0a22012-06-23 05:07:58 +000095[[]] using T = int; // expected-error {{an attribute list cannot appear here}}
96using T [[]] = int; // ok
97template<typename T> using U [[]] = T;
98using ns::i [[]]; // expected-error {{an attribute list cannot appear here}}
99using [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
100
101auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
102auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
103auto trailing() -> const int [[]];
104auto trailing_2() -> struct struct_attr [[]];
105
106namespace N {
107 struct S {};
108};
109template<typename> struct Template {};
110
111// FIXME: Improve this diagnostic
112struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
113struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
114struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
115template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
116template <> struct [[]] Template<void>;
117
118enum [[]] E1 {};
119enum [[]] E2; // expected-error {{forbids forward references}}
120enum [[]] E1;
121enum [[]] E3 : int;
122enum [[]] {
123 k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}}
124};
125enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}}
126enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}}
127enum struct [[]] E5;
128
129struct S {
130 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}}
131 [[]] friend int g(); // expected-FIXME{{an attribute list cannot appear here}}
132 [[]] friend int h() {
133 }
134 friend class [[]] C; // expected-error{{an attribute list cannot appear here}}
135};
136template<typename T> void tmpl(T) {}
137template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}}
138template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}}
139template void [[]] tmpl(short);
140
Sean Huntbbd37c62009-11-21 08:43:09 +0000141// Argument tests
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +0000142alignas int aligned_no_params; // expected-error {{expected '('}}
Richard Smith282e7e62012-02-04 09:53:13 +0000143alignas(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 +0000144
145// Statement tests
146void foo () {
147 [[]] ;
148 [[]] { }
149 [[]] if (0) { }
150 [[]] for (;;);
151 [[]] do {
152 [[]] continue;
153 } while (0);
154 [[]] while (0);
155
156 [[]] switch (i) {
157 [[]] case 0:
158 [[]] default:
159 [[]] break;
160 }
161
162 [[]] goto there;
163 [[]] there:
164
165 [[]] try {
166 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}}
167 }
Richard Smith6ee326a2012-04-10 01:32:12 +0000168 struct S { int arr[2]; } s;
169 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
170 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
171
Richard Smith6ce48a72012-04-11 04:01:28 +0000172 void bar [[noreturn]] ([[]] int i, [[]] int j);
173 using FuncType = void ([[]] int);
174 void baz([[]]...); // expected-error {{expected parameter declarator}}
175
Sean Huntbbd37c62009-11-21 08:43:09 +0000176 [[]] return;
177}
Richard Smithc56298d2012-04-10 03:25:07 +0000178
179template<typename...Ts> void variadic() {
180 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}}
181}
Sean Hunt2edf0a22012-06-23 05:07:58 +0000182
183// Expression tests
184void bar () {
185 [] () [[noreturn]] { return; } (); // expected-error {{should not return}}
186 [] () [[noreturn]] { throw; } ();
187 new int[42][[]][5][[]]{};
188}
189
190// Condition tests
191void baz () {
192 if ([[]] bool b = true) {
193 switch ([[]] int n { 42 }) {
194 default:
195 for ([[]] int n = 0; [[]] char b = n < 5; ++b) {
196 }
197 }
198 }
199 int x;
200 // An attribute can be applied to an expression-statement, such as the first
201 // statement in a for. But it can't be applied to a condition which is an
202 // expression.
203 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}}
204 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}}
205 while ([[]] bool k { false }) {
206 }
207 while ([[]] true) { // expected-error {{an attribute list cannot appear here}}
208 }
209 do {
210 } while ([[]] false); // expected-error {{an attribute list cannot appear here}}
211
212 for ([[]] int n : { 1, 2, 3 }) {
213 }
214}
John McCall1e12b3d2012-06-23 22:30:04 +0000215
216enum class __attribute__((visibility("hidden"))) SecretKeepers {
217 one, /* rest are deprecated */ two, three
218};
219enum class [[]] EvenMoreSecrets {};
Michael Han6880f492012-10-03 01:56:22 +0000220
221namespace arguments {
222 // FIXME: remove the sema warnings after migrating existing gnu attributes to c++11 syntax.
223 void f(const char*, ...) [[gnu::format(printf, 1, 2)]]; // expected-warning {{unknown attribute 'format' ignored}}
224 void g() [[unknown::foo(currently arguments of attributes from unknown namespace other than 'gnu' namespace are ignored... blah...)]]; // expected-warning {{unknown attribute 'foo' ignored}}
225}
Michael Hanf64231e2012-11-06 19:34:54 +0000226
227// forbid attributes on decl specifiers
228unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-warning {{attribute 'unused' ignored, because it is not attached to a declaration}} \
229 expected-error {{an attribute list cannot appear here}}
230typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-warning {{attribute 'unused' ignored, because it is not attached to a declaration}} \
231 expected-error {{an attribute list cannot appear here}}
232int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-warning 2{{attribute 'carries_dependency' ignored, because it is not attached to a declaration}}