cxx0x-attributes.cpp revision 176edba5311f6eff0cad2631449885ddf4fbc9ea
1176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat %s 2bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 32edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// Need std::initializer_list 42edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntnamespace std { 52edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef decltype(sizeof(int)) size_t; 62edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 72edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // libc++'s implementation 82edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt template <class _E> 92edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt class initializer_list 102edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt { 112edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt const _E* __begin_; 122edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt size_t __size_; 132edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 142edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt initializer_list(const _E* __b, size_t __s) 152edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt : __begin_(__b), 162edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt __size_(__s) 172edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt {} 182edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 192edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt public: 202edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef _E value_type; 212edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef const _E& reference; 222edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef const _E& const_reference; 232edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef size_t size_type; 242edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 252edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef const _E* iterator; 262edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt typedef const _E* const_iterator; 272edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 282edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt initializer_list() : __begin_(nullptr), __size_(0) {} 292edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 302edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt size_t size() const {return __size_;} 312edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt const _E* begin() const {return __begin_;} 322edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt const _E* end() const {return __begin_ + __size_;} 332edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt }; 342edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt} 352edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 362edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 37bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Declaration syntax checks 38bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] int before_attr; 39f19076848566cb52a40ddbdb2555c53d12e9a003Peter Collingbourneint [[]] between_attr; 402edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntconst [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}} 41bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint after_attr [[]]; 42bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint * [[]] ptr_attr; 436ee326af4e77e6f05973486097884d7431f2108dRichard Smithint & [[]] ref_attr = after_attr; 446b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithint & [[unknown]] ref_attr_2 = after_attr; // expected-warning {{unknown attribute 'unknown' ignored}} 456b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithint & [[noreturn]] ref_attr_3 = after_attr; // expected-error {{'noreturn' attribute cannot be applied to types}} 466ee326af4e77e6f05973486097884d7431f2108dRichard Smithint && [[]] rref_attr = 0; 47bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint array_attr [1] [[]]; 4882d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbournealignas(8) int aligned_attr; 496880f492365cc4fa4c941aa83688635003ee7498Michael Han[[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}} 506880f492365cc4fa4c941aa83688635003ee7498Michael Han[[,,,static, class, namespace,, inline, constexpr, mutable,, bitand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; // expected-warning {{unknown attribute 'static' ignored}} \ 51651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'class' ignored}} \ 52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'namespace' ignored}} \ 53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'inline' ignored}} \ 54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'constexpr' ignored}} \ 55651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'mutable' ignored}} \ 56651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'bitand' ignored}} \ 57651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // expected-warning {{unknown attribute 'compl' ignored}} 58c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith[[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} 59bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntvoid fn_attr () [[]]; 606ee326af4e77e6f05973486097884d7431f2108dRichard Smithvoid noexcept_fn_attr () noexcept [[]]; 616ee326af4e77e6f05973486097884d7431f2108dRichard Smithstruct MemberFnOrder { 626ee326af4e77e6f05973486097884d7431f2108dRichard Smith virtual void f() const volatile && noexcept [[]] final = 0; 636ee326af4e77e6f05973486097884d7431f2108dRichard Smith}; 642edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] struct_attr; 65bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntclass [[]] class_attr {}; 662edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntunion [[]] union_attr; 672e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 682e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han// Checks attributes placed at wrong syntactic locations of class specifiers. 692e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass [[]] [[]] 702e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}} 712e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 722e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass [[]] [[]] 732e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}} 742e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 752e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass [[]] c {}; 762e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass c [[]] [[]] x; 772e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass c [[]] [[]] y [[]] [[]]; 782e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass c final [(int){0}]; 792e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 802e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Hanclass base {}; 81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesclass [[]] [[]] final_class 822e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}} 832e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}} 842e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesclass [[]] [[]] final_class_another 862e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} 872e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} 882e39713a3d72c243a2bcd13cc8f5036ba6b487d9Michael Han 892edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] struct with_init_declarators {} init_declarator; 902edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 9168ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smithtemplate<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}} 9268ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smithvoid fn_with_structs() { 9368ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith [[]] struct with_init_declarators {} init_declarator; 9468ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 9568ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith} 962edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]]; 972edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct ctordtor { 982edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt [[]] ctordtor(); 992edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt [[]] ~ctordtor(); 1002edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}; 1012edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] ctordtor::ctordtor() {} 1022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] ctordtor::~ctordtor() {} 103bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] int extern_attr; 104bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunttemplate <typename T> [[]] void template_attr (); 1053497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne[[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 106bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 107c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithint comma_attr [[,]]; 108bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint scope_attr [[foo::]]; // expected-error {{expected identifier}} 1096ee326af4e77e6f05973486097884d7431f2108dRichard Smithint (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 1102edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntunsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} 1112edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntunsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} 112bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntclass foo { 1136ee326af4e77e6f05973486097884d7431f2108dRichard Smith void const_after_attr () [[]] const; // expected-error {{expected ';'}} 114bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt}; 115bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 116bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 117282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 118bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 119bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] asm(""); // expected-error {{an attribute list cannot appear here}} 120bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 121bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] using ns::i; // expected-error {{an attribute list cannot appear here}} 1226b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith[[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} 123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines[[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}} 124176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesnamespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are incompatible with C++ standards before C++1z}} 125bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 1265eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithusing [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 1275eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithusing [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} 1285eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith 1295eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithvoid bad_attributes_in_do_while() { 1305eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith do {} while ( 1315eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith [[ns::i); // expected-error {{expected ']'}} \ 1325eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // expected-note {{to match this '['}} \ 1335eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // expected-error {{expected expression}} 1345eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith do {} while ( 1355eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith [[a]b ns::i); // expected-error {{expected ']'}} \ 1365eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // expected-note {{to match this '['}} \ 1375eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith // expected-error {{expected expression}} 1385eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith do {} while ( 1395eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} 1405eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith do {} while ( // expected-note {{to match this '('}} 1415eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith alignas(4 ns::i; // expected-note {{to match this '('}} 1425eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith} // expected-error 2{{expected ')'}} expected-error {{expected expression}} 1435eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith 1442edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] using T = int; // expected-error {{an attribute list cannot appear here}} 1452edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing T [[]] = int; // ok 1462edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename T> using U [[]] = T; 1472edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing ns::i [[]]; // expected-error {{an attribute list cannot appear here}} 1482edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 1496b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} 150651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesusing T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}} 1516b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing V = int; // expected-note {{previous}} 1526b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} 1532edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 1542edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} 1552edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} 1562edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> const int [[]]; 1572edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing_2() -> struct struct_attr [[]]; 1582edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 1592edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntnamespace N { 1602edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt struct S {}; 1612edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}; 1622edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename> struct Template {}; 1632edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 1642edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// FIXME: Improve this diagnostic 1652edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} 1662edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} 1672edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} 1682edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} 1692edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate <> struct [[]] Template<void>; 1702edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 1712edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1 {}; 1722edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E2; // expected-error {{forbids forward references}} 1732edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1; 1742edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E3 : int; 1752edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] { 176176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are incompatible with C++ standards before C++1z}} 1772edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}; 1782edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} 1792edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} 1802edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum struct [[]] E5; 1812edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 1822edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct S { 1832edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} 18452b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} 18552b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han friend int f2 [[]] [[noreturn]] () {} 18652b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} 1872edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt [[]] friend int h() { 1882edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 18952b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} 19052b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} 1912edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt friend class [[]] C; // expected-error{{an attribute list cannot appear here}} 19252b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han [[]] friend class D; // expected-error{{an attribute list cannot appear here}} 19352b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han [[]] friend int; // expected-error{{an attribute list cannot appear here}} 1942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}; 1952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename T> void tmpl(T) {} 1962edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} 1972edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} 1982edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate void [[]] tmpl(short); 1992edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 200bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Argument tests 20182d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbournealignas int aligned_no_params; // expected-error {{expected '('}} 202282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithalignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 203bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 204bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Statement tests 205bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntvoid foo () { 206bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] ; 207bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] { } 208bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] if (0) { } 209bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] for (;;); 210bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] do { 211bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] continue; 212bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } while (0); 213bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] while (0); 214651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 215bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] switch (i) { 216bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] case 0: 217bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] default: 218bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] break; 219bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 220651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 221bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] goto there; 222bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] there: 223651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 224bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] try { 225bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 226bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 2276ee326af4e77e6f05973486097884d7431f2108dRichard Smith struct S { int arr[2]; } s; 2286ee326af4e77e6f05973486097884d7431f2108dRichard Smith (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 2296ee326af4e77e6f05973486097884d7431f2108dRichard Smith int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 2306ee326af4e77e6f05973486097884d7431f2108dRichard Smith 2316ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith void bar [[noreturn]] ([[]] int i, [[]] int j); 2326ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith using FuncType = void ([[]] int); 2336ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith void baz([[]]...); // expected-error {{expected parameter declarator}} 2346ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith 235bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] return; 236bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt} 237c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith 238c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithtemplate<typename...Ts> void variadic() { 239c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 240c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith} 2412edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 2422edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// Expression tests 2432edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntvoid bar () { 244cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains 245cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith // to the operator()'s type, and GCC does not otherwise accept attributes 246cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith // applied to types. Use that to test this. 247cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} 248cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} 2492edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt new int[42][[]][5][[]]{}; 2502edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt} 2512edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 2522edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// Condition tests 2532edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntvoid baz () { 2546b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} 2556b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 2562edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt default: 2576b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} 2582edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2592edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2602edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2612edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt int x; 2622edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // An attribute can be applied to an expression-statement, such as the first 2632edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // statement in a for. But it can't be applied to a condition which is an 2642edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt // expression. 2652edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} 2662edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} 2672edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt while ([[]] bool k { false }) { 2682edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2692edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt while ([[]] true) { // expected-error {{an attribute list cannot appear here}} 2702edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2712edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt do { 2722edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } while ([[]] false); // expected-error {{an attribute list cannot appear here}} 2732edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt 2746b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 2752edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt } 2762edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt} 2771e12b3d777b36b80527da566192f8aeefad8f9d4John McCall 2781e12b3d777b36b80527da566192f8aeefad8f9d4John McCallenum class __attribute__((visibility("hidden"))) SecretKeepers { 2791e12b3d777b36b80527da566192f8aeefad8f9d4John McCall one, /* rest are deprecated */ two, three 2801e12b3d777b36b80527da566192f8aeefad8f9d4John McCall}; 2811e12b3d777b36b80527da566192f8aeefad8f9d4John McCallenum class [[]] EvenMoreSecrets {}; 2826880f492365cc4fa4c941aa83688635003ee7498Michael Han 2836880f492365cc4fa4c941aa83688635003ee7498Michael Hannamespace arguments { 284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith void f[[gnu::format(printf, 1, 2)]](const char*, ...); 285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}} 286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines [[deprecated("with argument")]] int i; 2876880f492365cc4fa4c941aa83688635003ee7498Michael Han} 288f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han 289f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith// Forbid attributes on decl specifiers. 290d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithunsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ 291f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han expected-error {{an attribute list cannot appear here}} 292d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithtypedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ 293f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han expected-error {{an attribute list cannot appear here}} 294d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithint [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} 295f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith 296f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith// Forbid [[gnu::...]] attributes on declarator chunks. 297f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} 298f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 299f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 3006b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith 3016b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith[[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} 302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines[[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}} 303684aa73192d92850a926870be62a1787eb5b7ed9Michael Han[[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}} 3049ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith 3059ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smithclass A { 3069ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith A([[gnu::unused]] int a); 3079ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith}; 3089ec28913dcec67bf3f6b841f34d1401229ba4604Richard SmithA::A([[gnu::unused]] int a) {} 309966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky 310966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewyckynamespace GccConst { 311966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky // GCC's tokenizer treats const and __const as the same token. 312966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky [[gnu::const]] int *f1(); 313966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky [[gnu::__const]] int *f2(); 314651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines [[gnu::__const__]] int *f3(); 315966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky void f(const int *); 316966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky void g() { f(f1()); f(f2()); } 317651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void h() { f(f3()); } 318966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky} 3198fa2cbf31edbd5124692864007de566d59125559Nick Lewycky 3208fa2cbf31edbd5124692864007de566d59125559Nick Lewyckynamespace GccASan { 3218fa2cbf31edbd5124692864007de566d59125559Nick Lewycky __attribute__((no_address_safety_analysis)) void f1(); 3228fa2cbf31edbd5124692864007de566d59125559Nick Lewycky __attribute__((no_sanitize_address)) void f2(); 3238fa2cbf31edbd5124692864007de566d59125559Nick Lewycky [[gnu::no_address_safety_analysis]] void f3(); 3248fa2cbf31edbd5124692864007de566d59125559Nick Lewycky [[gnu::no_sanitize_address]] void f4(); 3258fa2cbf31edbd5124692864007de566d59125559Nick Lewycky} 3266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 3276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesnamespace { 3286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines [[deprecated]] void bar(); 3296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines [[deprecated("hello")]] void baz(); 3306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines [[deprecated()]] void foo(); // expected-error {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}} 3316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines [[gnu::deprecated()]] void quux(); 3326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines} 333