1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++1z-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 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 // The diagnostics here don't matter much, this just shouldn't crash: 90 class 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}} 91 class D final alignas ([l) {}]{}); // expected-error {{expected ',' or ']' in lambda capture list}} expected-error {{an attribute list cannot appear here}} 92 93 [[]] struct with_init_declarators {} init_declarator; 94 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 95 template<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}} 96 void fn_with_structs() { 97 [[]] struct with_init_declarators {} init_declarator; 98 [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} 99 } 100 [[]]; 101 struct ctordtor { 102 [[]] ctordtor [[]] () [[]]; 103 ctordtor (C) [[]]; 104 [[]] ~ctordtor [[]] () [[]]; 105 }; 106 [[]] ctordtor::ctordtor [[]] () [[]] {} 107 [[]] ctordtor::ctordtor (C) [[]] try {} catch (...) {} 108 [[]] ctordtor::~ctordtor [[]] () [[]] {} 109 extern "C++" [[]] int extern_attr; 110 template <typename T> [[]] void template_attr (); 111 [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 112 113 int comma_attr [[,]]; 114 int scope_attr [[foo::]]; // expected-error {{expected identifier}} 115 int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 116 unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} 117 unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} 118 class foo { 119 void const_after_attr () [[]] const; // expected-error {{expected ';'}} 120 }; 121 extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 122 [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 123 [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 124 [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 125 [[]] asm(""); // expected-error {{an attribute list cannot appear here}} 126 127 [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} 128 [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} 129 [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}} 130 namespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are incompatible with C++ standards before C++1z}} 131 132 using [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 133 using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} 134 135 void bad_attributes_in_do_while() { 136 do {} while ( 137 [[ns::i); // expected-error {{expected ']'}} \ 138 // expected-note {{to match this '['}} \ 139 // expected-error {{expected expression}} 140 do {} while ( 141 [[a]b ns::i); // expected-error {{expected ']'}} \ 142 // expected-note {{to match this '['}} \ 143 // expected-error {{expected expression}} 144 do {} while ( 145 [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} 146 do {} while ( // expected-note {{to match this '('}} 147 alignas(4 ns::i; // expected-note {{to match this '('}} 148 } // expected-error 2{{expected ')'}} expected-error {{expected expression}} 149 150 [[]] using T = int; // expected-error {{an attribute list cannot appear here}} 151 using T [[]] = int; // ok 152 template<typename T> using U [[]] = T; 153 using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} 154 using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} 155 using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} 156 using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}} 157 using V = int; // expected-note {{previous}} 158 using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} 159 160 auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} 161 auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} 162 auto trailing() -> const int [[]]; 163 auto trailing_2() -> struct struct_attr [[]]; 164 165 namespace N { 166 struct S {}; 167 }; 168 template<typename> struct Template {}; 169 170 // FIXME: Improve this diagnostic 171 struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} 172 struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} 173 struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} 174 template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} 175 template <> struct [[]] Template<void>; 176 177 enum [[]] E1 {}; 178 enum [[]] E2; // expected-error {{forbids forward references}} 179 enum [[]] E1; 180 enum [[]] E3 : int; 181 enum [[]] { 182 k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are incompatible with C++ standards before C++1z}} 183 }; 184 enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} 185 enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} 186 enum struct [[]] E5; 187 188 struct S { 189 friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} 190 friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} 191 friend int f2 [[]] [[noreturn]] () {} 192 [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} 193 [[]] friend int h() { 194 } 195 [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} 196 friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} 197 friend class [[]] C; // expected-error{{an attribute list cannot appear here}} 198 [[]] friend class D; // expected-error{{an attribute list cannot appear here}} 199 [[]] friend int; // expected-error{{an attribute list cannot appear here}} 200 }; 201 template<typename T> void tmpl(T) {} 202 template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} 203 template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} 204 template void [[]] tmpl(short); 205 206 // Argument tests 207 alignas int aligned_no_params; // expected-error {{expected '('}} 208 alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 209 210 // Statement tests 211 void foo () { 212 [[]] ; 213 [[]] { } 214 [[]] if (0) { } 215 [[]] for (;;); 216 [[]] do { 217 [[]] continue; 218 } while (0); 219 [[]] while (0); 220 221 [[]] switch (i) { 222 [[]] case 0: 223 [[]] default: 224 [[]] break; 225 } 226 227 [[]] goto there; 228 [[]] there: 229 230 [[]] try { 231 } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 232 } 233 struct S { int arr[2]; } s; 234 (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 235 int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 236 237 void bar [[noreturn]] ([[]] int i, [[]] int j); 238 using FuncType = void ([[]] int); 239 void baz([[]]...); // expected-error {{expected parameter declarator}} 240 241 [[]] return; 242 } 243 244 template<typename...Ts> void variadic() { 245 void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 246 } 247 248 // Expression tests 249 void bar () { 250 // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains 251 // to the operator()'s type, and GCC does not otherwise accept attributes 252 // applied to types. Use that to test this. 253 [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} 254 [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} 255 new int[42][[]][5][[]]{}; 256 } 257 258 // Condition tests 259 void baz () { 260 if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} 261 switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 262 default: 263 for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} 264 } 265 } 266 } 267 int x; 268 // An attribute can be applied to an expression-statement, such as the first 269 // statement in a for. But it can't be applied to a condition which is an 270 // expression. 271 for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} 272 for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} 273 while ([[]] bool k { false }) { 274 } 275 while ([[]] true) { // expected-error {{an attribute list cannot appear here}} 276 } 277 do { 278 } while ([[]] false); // expected-error {{an attribute list cannot appear here}} 279 280 for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} 281 } 282 } 283 284 enum class __attribute__((visibility("hidden"))) SecretKeepers { 285 one, /* rest are deprecated */ two, three 286 }; 287 enum class [[]] EvenMoreSecrets {}; 288 289 namespace arguments { 290 void f[[gnu::format(printf, 1, 2)]](const char*, ...); 291 void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}} 292 [[deprecated("with argument")]] int i; 293 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 294 } 295 296 // Forbid attributes on decl specifiers. 297 unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ 298 expected-error {{an attribute list cannot appear here}} 299 typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ 300 expected-error {{an attribute list cannot appear here}} 301 int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} 302 303 // Forbid [[gnu::...]] attributes on declarator chunks. 304 int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} 305 int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 306 int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} 307 308 [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} 309 [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}} 310 [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}} 311 312 class A { 313 A([[gnu::unused]] int a); 314 }; 315 A::A([[gnu::unused]] int a) {} 316 317 namespace GccConst { 318 // GCC's tokenizer treats const and __const as the same token. 319 [[gnu::const]] int *f1(); 320 [[gnu::__const]] int *f2(); 321 [[gnu::__const__]] int *f3(); 322 void f(const int *); 323 void g() { f(f1()); f(f2()); } 324 void h() { f(f3()); } 325 } 326 327 namespace GccASan { 328 __attribute__((no_address_safety_analysis)) void f1(); 329 __attribute__((no_sanitize_address)) void f2(); 330 [[gnu::no_address_safety_analysis]] void f3(); 331 [[gnu::no_sanitize_address]] void f4(); 332 } 333 334 namespace { 335 [[deprecated]] void bar(); 336 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 337 [[deprecated("hello")]] void baz(); 338 // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} 339 [[deprecated()]] void foo(); 340 // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}} 341 [[gnu::deprecated()]] void quux(); 342 } 343 344 namespace { 345 [[ // expected-error {{expected ']'}} 346 #pragma pack(pop) 347 deprecated 348 ]] void bad(); 349 } 350 351 int fallthru(int n) { 352 switch (n) { 353 case 0: 354 n += 5; 355 [[fallthrough]]; // expected-warning {{use of the 'fallthrough' attribute is a C++1z extension}} 356 case 1: 357 n *= 2; 358 break; 359 } 360 return n; 361 } 362 363 #define attr_name bitand 364 #define attr_name_2(x) x 365 #define attr_name_3(x, y) x##y 366 [[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \ 367 expected-warning {{unknown attribute 'bitor' ignored}} \ 368 expected-warning {{unknown attribute 'bitand' ignored}} 369