Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3 | // Need std::initializer_list |
| 4 | namespace 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 Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 37 | // Declaration syntax checks |
| 38 | [[]] int before_attr; |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 39 | int [[]] between_attr; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 40 | const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 41 | int after_attr [[]]; |
| 42 | int * [[]] ptr_attr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 43 | int & [[]] ref_attr = after_attr; |
| 44 | int && [[]] rref_attr = 0; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 45 | int array_attr [1] [[]]; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 46 | alignas(8) int aligned_attr; |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 47 | [[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 Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 56 | [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 57 | void fn_attr () [[]]; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 58 | void noexcept_fn_attr () noexcept [[]]; |
| 59 | struct MemberFnOrder { |
| 60 | virtual void f() const volatile && noexcept [[]] final = 0; |
| 61 | }; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 62 | struct [[]] struct_attr; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 63 | class [[]] class_attr {}; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 64 | union [[]] union_attr; |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 65 | |
| 66 | // Checks attributes placed at wrong syntactic locations of class specifiers. |
| 67 | // FIXME: provide fix-it hint. |
| 68 | class [[]] [[]] |
| 69 | attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}} |
| 70 | |
| 71 | class [[]] [[]] |
| 72 | attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}} |
| 73 | |
| 74 | class [[]] c {}; |
| 75 | class c [[]] [[]] x; |
| 76 | class c [[]] [[]] y [[]] [[]]; |
| 77 | class c final [(int){0}]; |
| 78 | |
| 79 | class base {}; |
| 80 | class [[]] [[]] final_class |
| 81 | alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}} |
| 82 | alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}} |
| 83 | |
| 84 | class [[]] [[]] final_class_another |
| 85 | [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} |
| 86 | [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} |
| 87 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 88 | [[]] struct with_init_declarators {} init_declarator; |
| 89 | [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} |
| 90 | [[]]; |
| 91 | struct ctordtor { |
| 92 | [[]] ctordtor(); |
| 93 | [[]] ~ctordtor(); |
| 94 | }; |
| 95 | [[]] ctordtor::ctordtor() {} |
| 96 | [[]] ctordtor::~ctordtor() {} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 97 | extern "C++" [[]] int extern_attr; |
| 98 | template <typename T> [[]] void template_attr (); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 99 | [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 100 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 101 | int comma_attr [[,]]; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 102 | int scope_attr [[foo::]]; // expected-error {{expected identifier}} |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 103 | int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 104 | unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} |
| 105 | unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 106 | class foo { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 107 | void const_after_attr () [[]] const; // expected-error {{expected ';'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 108 | }; |
| 109 | extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} |
| 110 | [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 111 | [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 112 | [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} |
| 113 | [[]] asm(""); // expected-error {{an attribute list cannot appear here}} |
| 114 | |
| 115 | [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} |
| 116 | [[]] using namespace ns; |
| 117 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 118 | [[]] using T = int; // expected-error {{an attribute list cannot appear here}} |
| 119 | using T [[]] = int; // ok |
| 120 | template<typename T> using U [[]] = T; |
| 121 | using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} |
| 122 | using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} |
| 123 | |
| 124 | auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} |
| 125 | auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} |
| 126 | auto trailing() -> const int [[]]; |
| 127 | auto trailing_2() -> struct struct_attr [[]]; |
| 128 | |
| 129 | namespace N { |
| 130 | struct S {}; |
| 131 | }; |
| 132 | template<typename> struct Template {}; |
| 133 | |
| 134 | // FIXME: Improve this diagnostic |
| 135 | struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} |
| 136 | struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} |
| 137 | struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} |
| 138 | template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} |
| 139 | template <> struct [[]] Template<void>; |
| 140 | |
| 141 | enum [[]] E1 {}; |
| 142 | enum [[]] E2; // expected-error {{forbids forward references}} |
| 143 | enum [[]] E1; |
| 144 | enum [[]] E3 : int; |
| 145 | enum [[]] { |
| 146 | k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}} |
| 147 | }; |
| 148 | enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} |
| 149 | enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} |
| 150 | enum struct [[]] E5; |
| 151 | |
| 152 | struct S { |
| 153 | friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame^] | 154 | friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} |
| 155 | friend int f2 [[]] [[noreturn]] () {} |
| 156 | [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 157 | [[]] friend int h() { |
| 158 | } |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame^] | 159 | [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} |
| 160 | friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 161 | friend class [[]] C; // expected-error{{an attribute list cannot appear here}} |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame^] | 162 | [[]] friend class D; // expected-error{{an attribute list cannot appear here}} |
| 163 | [[]] friend int; // expected-error{{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 164 | }; |
| 165 | template<typename T> void tmpl(T) {} |
| 166 | template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} |
| 167 | template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} |
| 168 | template void [[]] tmpl(short); |
| 169 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 170 | // Argument tests |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 171 | alignas int aligned_no_params; // expected-error {{expected '('}} |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 172 | alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 173 | |
| 174 | // Statement tests |
| 175 | void foo () { |
| 176 | [[]] ; |
| 177 | [[]] { } |
| 178 | [[]] if (0) { } |
| 179 | [[]] for (;;); |
| 180 | [[]] do { |
| 181 | [[]] continue; |
| 182 | } while (0); |
| 183 | [[]] while (0); |
| 184 | |
| 185 | [[]] switch (i) { |
| 186 | [[]] case 0: |
| 187 | [[]] default: |
| 188 | [[]] break; |
| 189 | } |
| 190 | |
| 191 | [[]] goto there; |
| 192 | [[]] there: |
| 193 | |
| 194 | [[]] try { |
| 195 | } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} |
| 196 | } |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 197 | struct S { int arr[2]; } s; |
| 198 | (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 199 | int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 200 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 201 | void bar [[noreturn]] ([[]] int i, [[]] int j); |
| 202 | using FuncType = void ([[]] int); |
| 203 | void baz([[]]...); // expected-error {{expected parameter declarator}} |
| 204 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 205 | [[]] return; |
| 206 | } |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 207 | |
| 208 | template<typename...Ts> void variadic() { |
| 209 | void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} |
| 210 | } |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 211 | |
| 212 | // Expression tests |
| 213 | void bar () { |
| 214 | [] () [[noreturn]] { return; } (); // expected-error {{should not return}} |
| 215 | [] () [[noreturn]] { throw; } (); |
| 216 | new int[42][[]][5][[]]{}; |
| 217 | } |
| 218 | |
| 219 | // Condition tests |
| 220 | void baz () { |
| 221 | if ([[]] bool b = true) { |
| 222 | switch ([[]] int n { 42 }) { |
| 223 | default: |
| 224 | for ([[]] int n = 0; [[]] char b = n < 5; ++b) { |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | int x; |
| 229 | // An attribute can be applied to an expression-statement, such as the first |
| 230 | // statement in a for. But it can't be applied to a condition which is an |
| 231 | // expression. |
| 232 | for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} |
| 233 | for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} |
| 234 | while ([[]] bool k { false }) { |
| 235 | } |
| 236 | while ([[]] true) { // expected-error {{an attribute list cannot appear here}} |
| 237 | } |
| 238 | do { |
| 239 | } while ([[]] false); // expected-error {{an attribute list cannot appear here}} |
| 240 | |
| 241 | for ([[]] int n : { 1, 2, 3 }) { |
| 242 | } |
| 243 | } |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 244 | |
| 245 | enum class __attribute__((visibility("hidden"))) SecretKeepers { |
| 246 | one, /* rest are deprecated */ two, three |
| 247 | }; |
| 248 | enum class [[]] EvenMoreSecrets {}; |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 249 | |
| 250 | namespace arguments { |
| 251 | // FIXME: remove the sema warnings after migrating existing gnu attributes to c++11 syntax. |
| 252 | void f(const char*, ...) [[gnu::format(printf, 1, 2)]]; // expected-warning {{unknown attribute 'format' ignored}} |
| 253 | void g() [[unknown::foo(currently arguments of attributes from unknown namespace other than 'gnu' namespace are ignored... blah...)]]; // expected-warning {{unknown attribute 'foo' ignored}} |
| 254 | } |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 255 | |
| 256 | // forbid attributes on decl specifiers |
| 257 | unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-warning {{attribute 'unused' ignored, because it is not attached to a declaration}} \ |
| 258 | expected-error {{an attribute list cannot appear here}} |
| 259 | typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-warning {{attribute 'unused' ignored, because it is not attached to a declaration}} \ |
| 260 | expected-error {{an attribute list cannot appear here}} |
| 261 | int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-warning 2{{attribute 'carries_dependency' ignored, because it is not attached to a declaration}} |