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 & [[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 68 // Checks attributes placed at wrong syntactic locations of class specifiers. 69 class [[]] [[]] 70 attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}} 71 72 class [[]] [[]] 73 attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}} 74 75 class [[]] c {}; 76 class c [[]] [[]] x; 77 class c [[]] [[]] y [[]] [[]]; 78 class c final [(int){0}]; 79 80 class base {}; 81 class [[]] [[]] final_class 82 alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}} 83 alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}} 84 85 class [[]] [[]] final_class_another 86 [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} 87 [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} 88 89 [[]] struct with_init_declarators {} init_declarator; 90 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 91 template<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}} 92 void fn_with_structs() { 93 [[]] struct with_init_declarators {} init_declarator; 94 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 95 } 96 [[]]; 97 struct ctordtor { 98 [[]] ctordtor(); 99 [[]] ~ctordtor(); 100 }; 101 [[]] ctordtor::ctordtor() {} 102 [[]] ctordtor::~ctordtor() {} 103 extern "C++" [[]] int extern_attr; 104 template <typename T> [[]] void template_attr (); 105 [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 106 107 int comma_attr [[,]]; 108 int scope_attr [[foo::]]; // expected-error {{expected identifier}} 109 int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 110 unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} 111 unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} 112 class foo { 113 void const_after_attr () [[]] const; // expected-error {{expected ';'}} 114 }; 115 extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 116 [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 117 [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 118 [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 119 [[]] asm(""); // expected-error {{an attribute list cannot appear here}} 120 121 [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} 122 [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} 123 [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions and methods}} 124 125 using [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 126 using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} 127 128 void bad_attributes_in_do_while() { 129 do {} while ( 130 [[ns::i); // expected-error {{expected ']'}} \ 131 // expected-note {{to match this '['}} \ 132 // expected-error {{expected expression}} 133 do {} while ( 134 [[a]b ns::i); // expected-error {{expected ']'}} \ 135 // expected-note {{to match this '['}} \ 136 // expected-error {{expected expression}} 137 do {} while ( 138 [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} 139 do {} while ( // expected-note {{to match this '('}} 140 alignas(4 ns::i; // expected-note {{to match this '('}} 141 } // expected-error 2{{expected ')'}} expected-error {{expected expression}} 142 143 [[]] using T = int; // expected-error {{an attribute list cannot appear here}} 144 using T [[]] = int; // ok 145 template<typename T> using U [[]] = T; 146 using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} 147 using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 148 using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} 149 using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions and methods}} 150 using V = int; // expected-note {{previous}} 151 using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} 152 153 auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} 154 auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} 155 auto trailing() -> const int [[]]; 156 auto trailing_2() -> struct struct_attr [[]]; 157 158 namespace N { 159 struct S {}; 160 }; 161 template<typename> struct Template {}; 162 163 // FIXME: Improve this diagnostic 164 struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} 165 struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} 166 struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} 167 template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} 168 template <> struct [[]] Template<void>; 169 170 enum [[]] E1 {}; 171 enum [[]] E2; // expected-error {{forbids forward references}} 172 enum [[]] E1; 173 enum [[]] E3 : int; 174 enum [[]] { 175 k_123 [[]] = 123 // expected-error {{an attribute list cannot appear here}} 176 }; 177 enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} 178 enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} 179 enum struct [[]] E5; 180 181 struct S { 182 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} 183 friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} 184 friend int f2 [[]] [[noreturn]] () {} 185 [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} 186 [[]] friend int h() { 187 } 188 [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} 189 friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} 190 friend class [[]] C; // expected-error{{an attribute list cannot appear here}} 191 [[]] friend class D; // expected-error{{an attribute list cannot appear here}} 192 [[]] friend int; // expected-error{{an attribute list cannot appear here}} 193 }; 194 template<typename T> void tmpl(T) {} 195 template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} 196 template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} 197 template void [[]] tmpl(short); 198 199 // Argument tests 200 alignas int aligned_no_params; // expected-error {{expected '('}} 201 alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 202 203 // Statement tests 204 void foo () { 205 [[]] ; 206 [[]] { } 207 [[]] if (0) { } 208 [[]] for (;;); 209 [[]] do { 210 [[]] continue; 211 } while (0); 212 [[]] while (0); 213 214 [[]] switch (i) { 215 [[]] case 0: 216 [[]] default: 217 [[]] break; 218 } 219 220 [[]] goto there; 221 [[]] there: 222 223 [[]] try { 224 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 225 } 226 struct S { int arr[2]; } s; 227 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 228 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 229 230 void bar [[noreturn]] ([[]] int i, [[]] int j); 231 using FuncType = void ([[]] int); 232 void baz([[]]...); // expected-error {{expected parameter declarator}} 233 234 [[]] return; 235 } 236 237 template<typename...Ts> void variadic() { 238 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 239 } 240 241 // Expression tests 242 void bar () { 243 // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains 244 // to the operator()'s type, and GCC does not otherwise accept attributes 245 // applied to types. Use that to test this. 246 [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} 247 [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} 248 new int[42][[]][5][[]]{}; 249 } 250 251 // Condition tests 252 void baz () { 253 if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} 254 switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 255 default: 256 for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} 257 } 258 } 259 } 260 int x; 261 // An attribute can be applied to an expression-statement, such as the first 262 // statement in a for. But it can't be applied to a condition which is an 263 // expression. 264 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} 265 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} 266 while ([[]] bool k { false }) { 267 } 268 while ([[]] true) { // expected-error {{an attribute list cannot appear here}} 269 } 270 do { 271 } while ([[]] false); // expected-error {{an attribute list cannot appear here}} 272 273 for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 274 } 275 } 276 277 enum class __attribute__((visibility("hidden"))) SecretKeepers { 278 one, /* rest are deprecated */ two, three 279 }; 280 enum class [[]] EvenMoreSecrets {}; 281 282 namespace arguments { 283 void f[[gnu::format(printf, 1, 2)]](const char*, ...); 284 void g() [[unknown::foo(arguments of attributes from unknown namespace other than 'gnu' namespace are ignored... blah...)]]; // expected-warning {{unknown attribute 'foo' ignored}} 285 } 286 287 // Forbid attributes on decl specifiers. 288 unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ 289 expected-error {{an attribute list cannot appear here}} 290 typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ 291 expected-error {{an attribute list cannot appear here}} 292 int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} 293 294 // Forbid [[gnu::...]] attributes on declarator chunks. 295 int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} 296 int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 297 int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 298 299 [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} 300 [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions and methods}} 301 [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}} 302 303 class A { 304 A([[gnu::unused]] int a); 305 }; 306 A::A([[gnu::unused]] int a) {} 307 308 namespace GccConst { 309 // GCC's tokenizer treats const and __const as the same token. 310 [[gnu::const]] int *f1(); 311 [[gnu::__const]] int *f2(); 312 void f(const int *); 313 void g() { f(f1()); f(f2()); } 314 } 315 316 namespace GccASan { 317 __attribute__((no_address_safety_analysis)) void f1(); 318 __attribute__((no_sanitize_address)) void f2(); 319 [[gnu::no_address_safety_analysis]] void f3(); 320 [[gnu::no_sanitize_address]] void f4(); 321 } 322