cxx0x-attributes.cpp revision c56298d87a9df507805a548d7d515e8b511df2c0
1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s 2bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 3bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Declaration syntax checks 4bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] int before_attr; 5f19076848566cb52a40ddbdb2555c53d12e9a003Peter Collingbourneint [[]] between_attr; 6bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint after_attr [[]]; 7bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint * [[]] ptr_attr; 86ee326af4e77e6f05973486097884d7431f2108dRichard Smithint & [[]] ref_attr = after_attr; 96ee326af4e77e6f05973486097884d7431f2108dRichard Smithint && [[]] rref_attr = 0; 10bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint array_attr [1] [[]]; 1182d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbournealignas(8) int aligned_attr; 12bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[test::valid(for 42 [very] **** '+' symbols went on a trip; the end.)]] 13bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt int garbage_attr; 14c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith[[,,,static, class, namespace,, inline, constexpr, mutable,, bi\ 15c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithtand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; 16c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith[[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} 17bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntvoid fn_attr () [[]]; 186ee326af4e77e6f05973486097884d7431f2108dRichard Smithvoid noexcept_fn_attr () noexcept [[]]; 196ee326af4e77e6f05973486097884d7431f2108dRichard Smithstruct MemberFnOrder { 206ee326af4e77e6f05973486097884d7431f2108dRichard Smith virtual void f() const volatile && noexcept [[]] final = 0; 216ee326af4e77e6f05973486097884d7431f2108dRichard Smith}; 22bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntclass [[]] class_attr {}; 23bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] int extern_attr; 24bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunttemplate <typename T> [[]] void template_attr (); 253497fdfdb742f55d7b7ec8e22779fb08962b8441Peter Collingbourne[[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; 26bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 27c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithint comma_attr [[,]]; 28bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntint scope_attr [[foo::]]; // expected-error {{expected identifier}} 296ee326af4e77e6f05973486097884d7431f2108dRichard Smithint (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} 30f19076848566cb52a40ddbdb2555c53d12e9a003Peter Collingbourneunsigned [[]] int attr_in_decl_spec; // expected-error {{expected unqualified-id}} 31bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntclass foo { 326ee326af4e77e6f05973486097884d7431f2108dRichard Smith void const_after_attr () [[]] const; // expected-error {{expected ';'}} 33bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt}; 34bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntextern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} 35bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} 36282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith[[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} 37bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} 38bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] asm(""); // expected-error {{an attribute list cannot appear here}} 39bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 40bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] using ns::i; // expected-error {{an attribute list cannot appear here}} 41bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt[[]] using namespace ns; 42bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 43bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Argument tests 4482d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbournealignas int aligned_no_params; // expected-error {{expected '('}} 45282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithalignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} 46bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 47bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt// Statement tests 48bbd37c62e34db3f5a95c899723484a76c71d7757Sean Huntvoid foo () { 49bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] ; 50bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] { } 51bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] if (0) { } 52bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] for (;;); 53bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] do { 54bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] continue; 55bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } while (0); 56bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] while (0); 57bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 58bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] switch (i) { 59bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] case 0: 60bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] default: 61bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] break; 62bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 63bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 64bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] goto there; 65bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] there: 66bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt 67bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] try { 68bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} 69bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 706ee326af4e77e6f05973486097884d7431f2108dRichard Smith struct S { int arr[2]; } s; 716ee326af4e77e6f05973486097884d7431f2108dRichard Smith (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 726ee326af4e77e6f05973486097884d7431f2108dRichard Smith int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} 736ee326af4e77e6f05973486097884d7431f2108dRichard Smith 74bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt [[]] return; 75bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt} 76c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith 77c56298d87a9df507805a548d7d515e8b511df2c0Richard Smithtemplate<typename...Ts> void variadic() { 78c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} 79c56298d87a9df507805a548d7d515e8b511df2c0Richard Smith} 80