1 // RUN: %clang_cc1 -fcxx-exceptions -fdeclspec -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++17-extensions %s 2 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 initializer_list(const _E * __b,size_t __s)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 initializer_list()28 initializer_list() : __begin_(nullptr), __size_(0) {} 29 size() const30 size_t size() const {return __size_;} begin() const31 const _E* begin() const {return __begin_;} end() const32 const _E* end() const {return __begin_ + __size_;} 33 }; 34 } 35 36 37 // Declaration syntax checks 38 [[]] int before_attr; 39 int [[]] between_attr; 40 const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}} 41 int after_attr [[]]; 42 int * [[]] ptr_attr; 43 int & [[]] ref_attr = after_attr; 44 int & [[unknown]] ref_attr_2 = after_attr; // expected-warning {{unknown attribute 'unknown' ignored}} 45 int & [[noreturn]] ref_attr_3 = after_attr; // expected-error {{'noreturn' attribute cannot be applied to types}} 46 int && [[]] rref_attr = 0; 47 int array_attr [1] [[]]; 48 alignas(8) int aligned_attr; 49 [[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}} 58 [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} 59 void fn_attr () [[]]; 60 void noexcept_fn_attr () noexcept [[]]; 61 struct MemberFnOrder { 62 virtual void f() const volatile && noexcept [[]] final = 0; 63 }; 64 struct [[]] struct_attr; 65 class [[]] class_attr {}; 66 union [[]] union_attr; 67 enum [[]] E { }; 68 namespace test_misplacement { 69 [[]] struct struct_attr2; //expected-error{{misplaced attributes}} 70 [[]] class class_attr2; //expected-error{{misplaced attributes}} 71 [[]] union union_attr2; //expected-error{{misplaced attributes}} 72 [[]] enum E2 { }; //expected-error{{misplaced attributes}} 73 } 74 75 // Checks attributes placed at wrong syntactic locations of class specifiers. 76 class [[]] [[]] 77 attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}} 78 79 class [[]] [[]] 80 attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}} 81 82 class [[]] c {}; 83 class c [[]] [[]] x; 84 class c [[]] [[]] y [[]] [[]]; 85 class c final [(int){0}]; 86 87 class base {}; 88 class [[]] [[]] final_class 89 alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}} 90 alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}} 91 92 class [[]] [[]] final_class_another 93 [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} 94 [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} 95 96 // The diagnostics here don't matter much, this just shouldn't crash: 97 class C final [[deprecated(l]] {}); //expected-error {{expected string literal as argument of 'deprecated' attribute}} expected-error {{an attribute list cannot appear here}} expected-error {{expected unqualified-id}} 98 class D final alignas ([l) {}]{}); // expected-error {{expected ',' or ']' in lambda capture list}} expected-error {{an attribute list cannot appear here}} 99 100 [[]] struct with_init_declarators {} init_declarator; 101 [[]] struct no_init_declarators; // expected-error {{misplaced attributes}} 102 template<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}} 103 void fn_with_structs() { 104 [[]] struct with_init_declarators {} init_declarator; 105 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 106 } 107 [[]]; 108 struct ctordtor { 109 [[]] ctordtor [[]] () [[]]; 110 ctordtor (C) [[]]; 111 [[]] ~ctordtor [[]] () [[]]; 112 }; 113 [[]] ctordtor::ctordtor [[]] () [[]] {} 114 [[]] ctordtor::ctordtor (C) [[]] try {} catch (...) {} 115 [[]] ctordtor::~ctordtor [[]] () [[]] {} 116 extern "C++" [[]] int extern_attr; 117 template <typename T> [[]] void template_attr (); 118 [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 119 120 int comma_attr [[,]]; 121 int scope_attr [[foo::]]; // expected-error {{expected identifier}} 122 int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 123 unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} 124 unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} 125 class foo { 126 void const_after_attr () [[]] const; // expected-error {{expected ';'}} 127 }; 128 extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 129 [[]] extern "C++" { } // expected-error {{an attribute list cannot appear here}} 130 [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 131 [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 132 [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 133 [[]] asm(""); // expected-error {{an attribute list cannot appear here}} 134 135 [[]] using ns::i; 136 [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} 137 [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}} 138 namespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are a C++17 extension}} 139 140 using[[]] alignas(4)[[]] ns::i; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to variables, data members and tag types}} expected-warning {{ISO C++}} 141 using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} 142 143 void bad_attributes_in_do_while() { 144 do {} while ( 145 [[ns::i); // expected-error {{expected ']'}} \ 146 // expected-note {{to match this '['}} \ 147 // expected-error {{expected expression}} 148 do {} while ( 149 [[a]b ns::i); // expected-error {{expected ']'}} \ 150 // expected-note {{to match this '['}} \ 151 // expected-error {{expected expression}} 152 do {} while ( 153 [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} 154 do {} while ( // expected-note {{to match this '('}} 155 alignas(4 ns::i; // expected-note {{to match this '('}} 156 // expected-error@-1 {{expected ';' after do/while}} 157 } // expected-error 2{{expected ')'}} expected-error {{expected expression}} 158 159 [[]] using T = int; // expected-error {{an attribute list cannot appear here}} 160 using T [[]] = int; // ok 161 template<typename T> using U [[]] = T; 162 using ns::i [[]]; 163 using ns::i [[]], ns::i [[]]; // expected-warning {{use of multiple declarators in a single using declaration is a C++17 extension}} 164 struct using_in_struct_base { 165 typedef int i, j, k, l; 166 }; 167 struct using_in_struct : using_in_struct_base { 168 [[]] using using_in_struct_base::i; 169 using using_in_struct_base::j [[]]; 170 [[]] using using_in_struct_base::k [[]], using_in_struct_base::l [[]]; // expected-warning {{use of multiple declarators in a single using declaration is a C++17 extension}} 171 }; 172 using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 173 using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} 174 using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}} 175 using V = int; // expected-note {{previous}} 176 using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} 177 178 auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} 179 auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} 180 auto trailing() -> const int [[]]; 181 auto trailing_2() -> struct struct_attr [[]]; 182 183 namespace N { 184 struct S {}; 185 }; 186 template<typename> struct Template {}; 187 188 // FIXME: Improve this diagnostic 189 struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} 190 struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} 191 struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} 192 template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} 193 template struct __attribute__((pure)) Template<std::size_t>; // We still allow GNU-style attributes here 194 template <> struct [[]] Template<void>; 195 196 enum [[]] E1 {}; 197 enum [[]] E2; // expected-error {{forbids forward references}} 198 enum [[]] E1; 199 enum [[]] E3 : int; 200 enum [[]] { 201 k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are a C++17 extension}} 202 }; 203 enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} 204 enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} 205 enum struct [[]] E5; 206 207 struct S { 208 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} 209 friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} 210 friend int f2 [[]] [[noreturn]] () {} 211 [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} 212 [[]] friend int h() { 213 } 214 [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} 215 friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} 216 friend class [[]] C; // expected-error{{an attribute list cannot appear here}} 217 [[]] friend class D; // expected-error{{an attribute list cannot appear here}} 218 [[]] friend int; // expected-error{{an attribute list cannot appear here}} 219 }; 220 template<typename T> void tmpl(T) {} 221 template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} 222 template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} 223 template void [[]] tmpl(short); 224 225 // Argument tests 226 alignas int aligned_no_params; // expected-error {{expected '('}} 227 alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 228 229 // Statement tests 230 void foo () { 231 [[]] ; 232 [[]] { } 233 [[]] if (0) { } 234 [[]] for (;;); 235 [[]] do { 236 [[]] continue; 237 } while (0); 238 [[]] while (0); 239 240 [[]] switch (i) { 241 [[]] case 0: 242 [[]] default: 243 [[]] break; 244 } 245 246 [[]] goto there; 247 [[]] there: 248 249 [[]] try { 250 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 251 } 252 struct S { int arr[2]; } s; 253 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 254 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 255 256 void bar [[noreturn]] ([[]] int i, [[]] int j); 257 using FuncType = void ([[]] int); 258 void baz([[]]...); // expected-error {{expected parameter declarator}} 259 260 [[]] return; 261 } 262 263 template<typename...Ts> void variadic() { 264 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 265 } 266 267 template <int... Is> void variadic_nttp() { 268 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 269 void baz [[clang::no_sanitize(Is...)]] (); // expected-error {{expected string literal as argument of 'no_sanitize' attribute}} 270 void bor [[clang::annotate("A", "V" ...)]] (); // expected-error {{pack expansion does not contain any unexpanded parameter packs}} 271 void bir [[clang::annotate("B", {1, 2, 3, 4})]] (); // expected-error {{'annotate' attribute requires parameter 1 to be a constant expression}} expected-note {{subexpression not valid in a constant expression}} 272 void boo [[unknown::foo(Is...)]] (); // expected-warning {{unknown attribute 'foo' ignored}} 273 void faz [[clang::annotate("C", (Is + ...))]] (); // expected-warning {{pack fold expression is a C++17 extension}} 274 void far [[clang::annotate("D", Is...)]] (); 275 void foz [[clang::annotate("E", 1, 2, 3, Is...)]] (); 276 void fiz [[clang::annotate("F", Is..., 1, 2, 3)]] (); 277 void fir [[clang::annotate("G", 1, Is..., 2, 3)]] (); 278 } 279 280 // Expression tests 281 void bar () { 282 // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains 283 // to the operator()'s type, and GCC does not otherwise accept attributes 284 // applied to types. Use that to test this. 285 [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} 286 [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} 287 new int[42][[]][5][[]]{}; 288 } 289 290 // Condition tests 291 void baz () { 292 if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} 293 switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 294 default: 295 for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} 296 } 297 } 298 } 299 int x; 300 // An attribute can be applied to an expression-statement, such as the first 301 // statement in a for. But it can't be applied to a condition which is an 302 // expression. 303 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} 304 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} 305 while ([[]] bool k { false }) { 306 } 307 while ([[]] true) { // expected-error {{an attribute list cannot appear here}} 308 } 309 do { 310 } while ([[]] false); // expected-error {{an attribute list cannot appear here}} 311 312 for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 313 } 314 } 315 316 enum class __attribute__((visibility("hidden"))) SecretKeepers { 317 one, /* rest are deprecated */ two, three 318 }; 319 enum class [[]] EvenMoreSecrets {}; 320 321 namespace arguments { 322 void f[[gnu::format(printf, 1, 2)]](const char*, ...); 323 void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}} 324 [[deprecated("with argument")]] int i; 325 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 326 } 327 328 // Forbid attributes on decl specifiers. 329 unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ 330 expected-error {{an attribute list cannot appear here}} 331 typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ 332 expected-error {{an attribute list cannot appear here}} 333 int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} 334 335 // Forbid [[gnu::...]] attributes on declarator chunks. 336 int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} 337 int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 338 int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 339 340 [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} 341 [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}} 342 [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to parameters, Objective-C methods, and functions}} 343 344 class A { 345 A([[gnu::unused]] int a); 346 }; 347 A::A([[gnu::unused]] int a) {} 348 349 namespace GccConst { 350 // GCC's tokenizer treats const and __const as the same token. 351 [[gnu::const]] int *f1(); 352 [[gnu::__const]] int *f2(); 353 [[gnu::__const__]] int *f3(); 354 void f(const int *); 355 void g() { f(f1()); f(f2()); } 356 void h() { f(f3()); } 357 } 358 359 namespace GccASan { 360 __attribute__((no_address_safety_analysis)) void f1(); 361 __attribute__((no_sanitize_address)) void f2(); 362 [[gnu::no_address_safety_analysis]] void f3(); 363 [[gnu::no_sanitize_address]] void f4(); 364 } 365 366 namespace { 367 [[deprecated]] void bar(); 368 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 369 [[deprecated("hello")]] void baz(); 370 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 371 [[deprecated()]] void foo(); 372 // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}} 373 [[gnu::deprecated()]] void quux(); 374 } 375 376 namespace { 377 [[ // expected-error {{expected ']'}} 378 #pragma pack(pop) 379 deprecated 380 ]] void bad(); 381 } 382 383 int fallthru(int n) { 384 switch (n) { 385 case 0: 386 n += 5; 387 [[fallthrough]]; // expected-warning {{use of the 'fallthrough' attribute is a C++17 extension}} 388 case 1: 389 n *= 2; 390 break; 391 } 392 return n; 393 } 394 395 template<typename T> struct TemplateStruct {}; 396 class FriendClassesWithAttributes { 397 // We allow GNU-style attributes here 398 template <class _Tp, class _Alloc> friend class __attribute__((__type_visibility__("default"))) vector; 399 template <class _Tp, class _Alloc> friend class __declspec(code_seg("foo,whatever")) vector2; 400 // But not C++11 ones 401 template <class _Tp, class _Alloc> friend class[[]] vector3; // expected-error {{an attribute list cannot appear here}} 402 template <class _Tp, class _Alloc> friend class [[clang::__type_visibility__(("default"))]] vector4; // expected-error {{an attribute list cannot appear here}} 403 404 // Also allowed 405 friend struct __attribute__((__type_visibility__("default"))) TemplateStruct<FriendClassesWithAttributes>; 406 friend struct __declspec(code_seg("foo,whatever")) TemplateStruct<FriendClassesWithAttributes>; 407 friend struct[[]] TemplateStruct<FriendClassesWithAttributes>; // expected-error {{an attribute list cannot appear here}} 408 friend struct [[clang::__type_visibility__("default")]] TemplateStruct<FriendClassesWithAttributes>; // expected-error {{an attribute list cannot appear here}} 409 }; 410 411 #define attr_name bitand 412 #define attr_name_2(x) x 413 #define attr_name_3(x, y) x##y 414 [[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \ 415 expected-warning {{unknown attribute 'bitor' ignored}} \ 416 expected-warning {{unknown attribute 'bitand' ignored}} 417 418 // Check that we can parse an attribute in our vendor namespace. 419 [[clang::annotate("test")]] void annotate1(); 420 [[_Clang::annotate("test")]] void annotate2(); 421 // Note: __clang__ is a predefined macro, which is why _Clang is the 422 // prefered "protected" vendor namespace. We support __clang__ only for 423 // people expecting it to behave the same as __gnu__. 424 [[__clang__::annotate("test")]] void annotate3(); // expected-warning {{'__clang__' is a predefined macro name, not an attribute scope specifier; did you mean '_Clang' instead?}} 425 426 // Check ordering: C++11 attributes must appear before GNU attributes. 427 class Ordering { 428 void f1( 429 int ([[]] __attribute__(()) int n) 430 ) { 431 } 432 433 void f2( 434 int (*)([[]] __attribute__(()) int n) 435 ) { 436 } 437 438 void f3( 439 int (__attribute__(()) [[]] int n) // expected-error {{an attribute list cannot appear here}} 440 ) { 441 } 442 443 void f4( 444 int (*)(__attribute__(()) [[]] int n) // expected-error {{an attribute list cannot appear here}} 445 ) { 446 } 447 }; 448 449 namespace P2361 { 450 [[deprecated(L"abc")]] void a(); // expected-warning{{encoding prefix 'L' on an unevaluated string literal has no effect and is incompatible with c++2c}} \ 451 // expected-warning {{use of the 'deprecated' attribute is a C++14 extension}} 452 [[nodiscard("\123")]] int b(); // expected-error{{invalid escape sequence '\123' in an unevaluated string literal}} 453 } 454 455 alignas(int) struct AlignAsAttribute {}; // expected-error {{misplaced attributes; expected attributes here}} 456