14967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++1z-extensions %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
890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// The diagnostics here don't matter much, this just shouldn't crash:
900e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesclass C final [[deprecated(l]] {}); // expected-error {{use of undeclared identifier}} expected-error {{expected ']'}} expected-error {{an attribute list cannot appear here}} expected-error {{expected unqualified-id}}
910e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesclass D final alignas ([l) {}]{}); // expected-error {{expected ',' or ']' in lambda capture list}} expected-error {{an attribute list cannot appear here}}
920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
932edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] struct with_init_declarators {} init_declarator;
942edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
9568ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smithtemplate<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}}
9668ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smithvoid fn_with_structs() {
9768ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith  [[]] struct with_init_declarators {} init_declarator;
9868ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith  [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
9968ea3aeff1be9412658f4c96fdab0d8ad875be6cRichard Smith}
1002edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]];
1012edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct ctordtor {
1022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  [[]] ctordtor();
1032edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  [[]] ~ctordtor();
1042edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt};
1052edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] ctordtor::ctordtor() {}
1062edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] ctordtor::~ctordtor() {}
107bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] int extern_attr;
108bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunttemplate <typename T> [[]] void template_attr ();
1093497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne[[]] [[]] int [[]] [[]] multi_attr [[]] [[]];
110bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
111c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithint comma_attr [[,]];
112bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint scope_attr [[foo::]]; // expected-error {{expected identifier}}
1136ee326af4e77e6f05973486097884d7431f2108dRichard Smithint (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}}
1142edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntunsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}}
1152edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntunsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}}
116bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntclass foo {
1176ee326af4e77e6f05973486097884d7431f2108dRichard Smith  void const_after_attr () [[]] const; // expected-error {{expected ';'}}
118bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt};
119bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}}
120bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}}
121282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}}
122bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}}
123bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] asm(""); // expected-error {{an attribute list cannot appear here}}
124bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
125bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] using ns::i; // expected-error {{an attribute list cannot appear here}}
1266b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith[[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}}
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines[[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}}
128176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesnamespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are incompatible with C++ standards before C++1z}}
129bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
1305eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithusing [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
1315eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithusing [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}}
1325eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
1335eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smithvoid bad_attributes_in_do_while() {
1345eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  do {} while (
1355eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      [[ns::i); // expected-error {{expected ']'}} \
1365eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith                // expected-note {{to match this '['}} \
1375eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith                // expected-error {{expected expression}}
1385eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  do {} while (
1395eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      [[a]b ns::i); // expected-error {{expected ']'}} \
1405eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith                    // expected-note {{to match this '['}} \
1415eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith                    // expected-error {{expected expression}}
1425eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  do {} while (
1435eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}}
1445eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith  do {} while ( // expected-note {{to match this '('}}
1455eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith      alignas(4 ns::i; // expected-note {{to match this '('}}
1465eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith} // expected-error 2{{expected ')'}} expected-error {{expected expression}}
1475eed7e00b4ac8d589ca83e126dafa8767e8a0358Richard Smith
1482edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt[[]] using T = int; // expected-error {{an attribute list cannot appear here}}
1492edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing T [[]] = int; // ok
1502edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename T> using U [[]] = T;
1512edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing ns::i [[]]; // expected-error {{an attribute list cannot appear here}}
1522edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntusing [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
1536b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}}
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesusing T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}}
1556b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing V = int; // expected-note {{previous}}
1566b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smithusing V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}}
1572edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1582edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
1592edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
1602edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing() -> const int [[]];
1612edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntauto trailing_2() -> struct struct_attr [[]];
1622edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1632edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntnamespace N {
1642edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  struct S {};
1652edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt};
1662edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename> struct Template {};
1672edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1682edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// FIXME: Improve this diagnostic
1692edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
1702edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
1712edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
1722edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
1732edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate <> struct [[]] Template<void>;
1742edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1752edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1 {};
1762edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E2; // expected-error {{forbids forward references}}
1772edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1;
1782edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E3 : int;
1792edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] {
180176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are incompatible with C++ standards before C++1z}}
1812edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt};
1822edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] E1 e; // expected-error {{an attribute list cannot appear here}}
1832edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}}
1842edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntenum struct [[]] E5;
1852edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
1862edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntstruct S {
1872edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}}
18852b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}}
18952b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  friend int f2 [[]] [[noreturn]] () {}
19052b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  [[]] friend int g(); // expected-error{{an attribute list cannot appear here}}
1912edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  [[]] friend int h() {
1922edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
19352b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}}
19452b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}}
1952edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  friend class [[]] C; // expected-error{{an attribute list cannot appear here}}
19652b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  [[]] friend class D; // expected-error{{an attribute list cannot appear here}}
19752b501cd723d56efe3ad2ab708c2b75530fe6caaMichael Han  [[]] friend int; // expected-error{{an attribute list cannot appear here}}
1982edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt};
1992edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate<typename T> void tmpl(T) {}
2002edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}}
2012edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}}
2022edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunttemplate void [[]] tmpl(short);
2032edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
204bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Argument tests
20582d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbournealignas int aligned_no_params; // expected-error {{expected '('}}
206282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithalignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}}
207bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
208bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Statement tests
209bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntvoid foo () {
210bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] ;
211bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] { }
212bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] if (0) { }
213bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] for (;;);
214bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] do {
215bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    [[]] continue;
216bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  } while (0);
217bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] while (0);
218651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
219bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] switch (i) {
220bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    [[]] case 0:
221bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    [[]] default:
222bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      [[]] break;
223bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  }
224651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
225bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] goto there;
226bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] there:
227651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
228bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] try {
229bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}}
230bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  }
2316ee326af4e77e6f05973486097884d7431f2108dRichard Smith  struct S { int arr[2]; } s;
2326ee326af4e77e6f05973486097884d7431f2108dRichard Smith  (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
2336ee326af4e77e6f05973486097884d7431f2108dRichard Smith  int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
2346ee326af4e77e6f05973486097884d7431f2108dRichard Smith
2356ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith  void bar [[noreturn]] ([[]] int i, [[]] int j);
2366ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith  using FuncType = void ([[]] int);
2376ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith  void baz([[]]...); // expected-error {{expected parameter declarator}}
2386ce48a70ace62eb0eaf7b2769d05c5f13b7c7b6cRichard Smith
239bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  [[]] return;
240bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt}
241c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith
242c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithtemplate<typename...Ts> void variadic() {
243c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith  void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}}
244c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith}
2452edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
2462edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// Expression tests
2472edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntvoid bar () {
248cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains
249cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  // to the operator()'s type, and GCC does not otherwise accept attributes
250cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  // applied to types. Use that to test this.
251cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}}
252cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith  [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}}
2532edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  new int[42][[]][5][[]]{};
2542edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}
2552edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
2562edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt// Condition tests
2572edf0a2520313cde900799b1eb9bd11c9c776afeSean Huntvoid baz () {
2586b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}}
2596b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith    switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
2602edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    default:
2616b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}}
2622edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      }
2632edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt    }
2642edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
2652edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  int x;
2662edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  // An attribute can be applied to an expression-statement, such as the first
2672edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  // statement in a for. But it can't be applied to a condition which is an
2682edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  // expression.
2692edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}}
2702edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}}
2712edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  while ([[]] bool k { false }) {
2722edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
2732edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  while ([[]] true) { // expected-error {{an attribute list cannot appear here}}
2742edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
2752edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  do {
2762edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  } while ([[]] false); // expected-error {{an attribute list cannot appear here}}
2772edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt
2786b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
2792edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt  }
2802edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt}
2811e12b3d777b36b80527da566192f8aeefad8f9d4John McCall
2821e12b3d777b36b80527da566192f8aeefad8f9d4John McCallenum class __attribute__((visibility("hidden"))) SecretKeepers {
2831e12b3d777b36b80527da566192f8aeefad8f9d4John McCall  one, /* rest are deprecated */ two, three
2841e12b3d777b36b80527da566192f8aeefad8f9d4John McCall};
2851e12b3d777b36b80527da566192f8aeefad8f9d4John McCallenum class [[]] EvenMoreSecrets {};
2866880f492365cc4fa4c941aa83688635003ee7498Michael Han
2876880f492365cc4fa4c941aa83688635003ee7498Michael Hannamespace arguments {
288f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  void f[[gnu::format(printf, 1, 2)]](const char*, ...);
289651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}}
290651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  [[deprecated("with argument")]] int i;
2910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
2926880f492365cc4fa4c941aa83688635003ee7498Michael Han}
293f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han
294f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith// Forbid attributes on decl specifiers.
295d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithunsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \
296f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han           expected-error {{an attribute list cannot appear here}}
297d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithtypedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \
298f64231e9f47234826fbcdc3b4fe0370ef6c9961dMichael Han          expected-error {{an attribute list cannot appear here}}
299d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smithint [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}}
300f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
301f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith// Forbid [[gnu::...]] attributes on declarator chunks.
302f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}}
303f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
304f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithint v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
3056b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith
3066b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith[[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}}
307651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines[[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}}
308684aa73192d92850a926870be62a1787eb5b7ed9Michael Han[[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}}
3099ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith
3109ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smithclass A {
3119ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith  A([[gnu::unused]] int a);
3129ec28913dcec67bf3f6b841f34d1401229ba4604Richard Smith};
3139ec28913dcec67bf3f6b841f34d1401229ba4604Richard SmithA::A([[gnu::unused]] int a) {}
314966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky
315966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewyckynamespace GccConst {
316966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky  // GCC's tokenizer treats const and __const as the same token.
317966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky  [[gnu::const]] int *f1();
318966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky  [[gnu::__const]] int *f2();
319651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  [[gnu::__const__]] int *f3();
320966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky  void f(const int *);
321966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky  void g() { f(f1()); f(f2()); }
322651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void h() { f(f3()); }
323966a0fc4681a6c3b55ed6c1404c10d9816eeefdaNick Lewycky}
3248fa2cbf31edbd5124692864007de566d59125559Nick Lewycky
3258fa2cbf31edbd5124692864007de566d59125559Nick Lewyckynamespace GccASan {
3268fa2cbf31edbd5124692864007de566d59125559Nick Lewycky  __attribute__((no_address_safety_analysis)) void f1();
3278fa2cbf31edbd5124692864007de566d59125559Nick Lewycky  __attribute__((no_sanitize_address)) void f2();
3288fa2cbf31edbd5124692864007de566d59125559Nick Lewycky  [[gnu::no_address_safety_analysis]] void f3();
3298fa2cbf31edbd5124692864007de566d59125559Nick Lewycky  [[gnu::no_sanitize_address]] void f4();
3308fa2cbf31edbd5124692864007de566d59125559Nick Lewycky}
3316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesnamespace {
3336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  [[deprecated]] void bar();
3340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
3356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  [[deprecated("hello")]] void baz();
3360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
3370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  [[deprecated()]] void foo();
3380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}}
3396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  [[gnu::deprecated()]] void quux();
3406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
3410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3420e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesnamespace {
3430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines[[ // expected-error {{expected ']'}}
3440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#pragma pack(pop)
3450e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesdeprecated
3460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines]] void bad();
3470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines}
34858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar
3494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarint fallthru(int n) {
3504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  switch (n) {
3514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case 0:
3524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    n += 5;
3534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    [[fallthrough]]; // expected-warning {{use of the 'fallthrough' attribute is a C++1z extension}}
3544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case 1:
3554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    n *= 2;
3564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
3574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
3584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  return n;
3594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
3604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
36158878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar#define attr_name bitand
36258878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar#define attr_name_2(x) x
36358878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar#define attr_name_3(x, y) x##y
36458878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar[[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \
36558878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar   expected-warning {{unknown attribute 'bitor' ignored}} \
36658878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar   expected-warning {{unknown attribute 'bitand' ignored}}
367