1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %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 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 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 && [[]] rref_attr = 0; 45 int array_attr [1] [[]]; 46 alignas(8) int aligned_attr; 47 [[test::valid(for 42 [very] **** '+' symbols went on a trip and had a "good"_time; the end.)]] 48 int garbage_attr; 49 [[,,,static, class, namespace,, inline, constexpr, mutable,, bi\ 50 tand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; 51 [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} 52 void fn_attr () [[]]; 53 void noexcept_fn_attr () noexcept [[]]; 54 struct MemberFnOrder { 55 virtual void f() const volatile && noexcept [[]] final = 0; 56 }; 57 struct [[]] struct_attr; 58 class [[]] class_attr {}; 59 union [[]] union_attr; 60 [[]] struct with_init_declarators {} init_declarator; 61 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 62 [[]]; 63 struct ctordtor { 64 [[]] ctordtor(); 65 [[]] ~ctordtor(); 66 }; 67 [[]] ctordtor::ctordtor() {} 68 [[]] ctordtor::~ctordtor() {} 69 extern "C++" [[]] int extern_attr; 70 template <typename T> [[]] void template_attr (); 71 [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 72 73 int comma_attr [[,]]; 74 int scope_attr [[foo::]]; // expected-error {{expected identifier}} 75 int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 76 unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} 77 unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} 78 class foo { 79 void const_after_attr () [[]] const; // expected-error {{expected ';'}} 80 }; 81 extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 82 [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 83 [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 84 [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 85 [[]] asm(""); // expected-error {{an attribute list cannot appear here}} 86 87 [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} 88 [[]] using namespace ns; 89 90 [[]] using T = int; // expected-error {{an attribute list cannot appear here}} 91 using T [[]] = int; // ok 92 template<typename T> using U [[]] = T; 93 using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} 94 using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 95 96 auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} 97 auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} 98 auto trailing() -> const int [[]]; 99 auto trailing_2() -> struct struct_attr [[]]; 100 101 namespace N { 102 struct S {}; 103 }; 104 template<typename> struct Template {}; 105 106 // FIXME: Improve this diagnostic 107 struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} 108 struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} 109 struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} 110 template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} 111 template <> struct [[]] Template<void>; 112 113 enum [[]] E1 {}; 114 enum [[]] E2; // expected-error {{forbids forward references}} 115 enum [[]] E1; 116 enum [[]] E3 : int; 117 enum [[]] { 118 k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}} 119 }; 120 enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} 121 enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} 122 enum struct [[]] E5; 123 124 struct S { 125 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} 126 [[]] friend int g(); // expected-FIXME{{an attribute list cannot appear here}} 127 [[]] friend int h() { 128 } 129 friend class [[]] C; // expected-error{{an attribute list cannot appear here}} 130 }; 131 template<typename T> void tmpl(T) {} 132 template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} 133 template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} 134 template void [[]] tmpl(short); 135 136 // Argument tests 137 alignas int aligned_no_params; // expected-error {{expected '('}} 138 alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 139 140 // Statement tests 141 void foo () { 142 [[]] ; 143 [[]] { } 144 [[]] if (0) { } 145 [[]] for (;;); 146 [[]] do { 147 [[]] continue; 148 } while (0); 149 [[]] while (0); 150 151 [[]] switch (i) { 152 [[]] case 0: 153 [[]] default: 154 [[]] break; 155 } 156 157 [[]] goto there; 158 [[]] there: 159 160 [[]] try { 161 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 162 } 163 struct S { int arr[2]; } s; 164 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 165 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 166 167 void bar [[noreturn]] ([[]] int i, [[]] int j); 168 using FuncType = void ([[]] int); 169 void baz([[]]...); // expected-error {{expected parameter declarator}} 170 171 [[]] return; 172 } 173 174 template<typename...Ts> void variadic() { 175 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 176 } 177 178 // Expression tests 179 void bar () { 180 [] () [[noreturn]] { return; } (); // expected-error {{should not return}} 181 [] () [[noreturn]] { throw; } (); 182 new int[42][[]][5][[]]{}; 183 } 184 185 // Condition tests 186 void baz () { 187 if ([[]] bool b = true) { 188 switch ([[]] int n { 42 }) { 189 default: 190 for ([[]] int n = 0; [[]] char b = n < 5; ++b) { 191 } 192 } 193 } 194 int x; 195 // An attribute can be applied to an expression-statement, such as the first 196 // statement in a for. But it can't be applied to a condition which is an 197 // expression. 198 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} 199 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} 200 while ([[]] bool k { false }) { 201 } 202 while ([[]] true) { // expected-error {{an attribute list cannot appear here}} 203 } 204 do { 205 } while ([[]] false); // expected-error {{an attribute list cannot appear here}} 206 207 for ([[]] int n : { 1, 2, 3 }) { 208 } 209 } 210 211 enum class __attribute__((visibility("hidden"))) SecretKeepers { 212 one, /* rest are deprecated */ two, three 213 }; 214 enum class [[]] EvenMoreSecrets {}; 215