Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat %s |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 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 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 37 | // Declaration syntax checks |
| 38 | [[]] int before_attr; |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 39 | int [[]] between_attr; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 40 | const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 41 | int after_attr [[]]; |
| 42 | int * [[]] ptr_attr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 43 | int & [[]] ref_attr = after_attr; |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 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}} |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 46 | int && [[]] rref_attr = 0; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 47 | int array_attr [1] [[]]; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 48 | alignas(8) int aligned_attr; |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 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}} \ |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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}} |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 58 | [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 59 | void fn_attr () [[]]; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 60 | void noexcept_fn_attr () noexcept [[]]; |
| 61 | struct MemberFnOrder { |
| 62 | virtual void f() const volatile && noexcept [[]] final = 0; |
| 63 | }; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 64 | struct [[]] struct_attr; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 65 | class [[]] class_attr {}; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 66 | union [[]] union_attr; |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 67 | |
| 68 | // Checks attributes placed at wrong syntactic locations of class specifiers. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 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 {}; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 81 | class [[]] [[]] final_class |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 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 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 85 | class [[]] [[]] final_class_another |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 86 | [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} |
| 87 | [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} |
| 88 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 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 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 93 | [[]] struct with_init_declarators {} init_declarator; |
| 94 | [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 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 | } |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 100 | [[]]; |
| 101 | struct ctordtor { |
| 102 | [[]] ctordtor(); |
| 103 | [[]] ~ctordtor(); |
| 104 | }; |
| 105 | [[]] ctordtor::ctordtor() {} |
| 106 | [[]] ctordtor::~ctordtor() {} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 107 | extern "C++" [[]] int extern_attr; |
| 108 | template <typename T> [[]] void template_attr (); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 109 | [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 110 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 111 | int comma_attr [[,]]; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 112 | int scope_attr [[foo::]]; // expected-error {{expected identifier}} |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 113 | int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 114 | unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} |
| 115 | unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 116 | class foo { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 117 | void const_after_attr () [[]] const; // expected-error {{expected ';'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 118 | }; |
| 119 | extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} |
| 120 | [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 121 | [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 122 | [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} |
| 123 | [[]] asm(""); // expected-error {{an attribute list cannot appear here}} |
| 124 | |
| 125 | [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 126 | [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 127 | [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}} |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 128 | namespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are incompatible with C++ standards before C++1z}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 129 | |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 130 | using [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}} |
| 131 | using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} |
| 132 | |
| 133 | void bad_attributes_in_do_while() { |
| 134 | do {} while ( |
| 135 | [[ns::i); // expected-error {{expected ']'}} \ |
| 136 | // expected-note {{to match this '['}} \ |
| 137 | // expected-error {{expected expression}} |
| 138 | do {} while ( |
| 139 | [[a]b ns::i); // expected-error {{expected ']'}} \ |
| 140 | // expected-note {{to match this '['}} \ |
| 141 | // expected-error {{expected expression}} |
| 142 | do {} while ( |
| 143 | [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} |
| 144 | do {} while ( // expected-note {{to match this '('}} |
| 145 | alignas(4 ns::i; // expected-note {{to match this '('}} |
| 146 | } // expected-error 2{{expected ')'}} expected-error {{expected expression}} |
| 147 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 148 | [[]] using T = int; // expected-error {{an attribute list cannot appear here}} |
| 149 | using T [[]] = int; // ok |
| 150 | template<typename T> using U [[]] = T; |
| 151 | using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} |
| 152 | using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 153 | using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 154 | using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}} |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 155 | using V = int; // expected-note {{previous}} |
| 156 | using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 157 | |
| 158 | auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} |
| 159 | auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} |
| 160 | auto trailing() -> const int [[]]; |
| 161 | auto trailing_2() -> struct struct_attr [[]]; |
| 162 | |
| 163 | namespace N { |
| 164 | struct S {}; |
| 165 | }; |
| 166 | template<typename> struct Template {}; |
| 167 | |
| 168 | // FIXME: Improve this diagnostic |
| 169 | struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} |
| 170 | struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} |
| 171 | struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} |
| 172 | template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} |
| 173 | template <> struct [[]] Template<void>; |
| 174 | |
| 175 | enum [[]] E1 {}; |
| 176 | enum [[]] E2; // expected-error {{forbids forward references}} |
| 177 | enum [[]] E1; |
| 178 | enum [[]] E3 : int; |
| 179 | enum [[]] { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 180 | k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are incompatible with C++ standards before C++1z}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 181 | }; |
| 182 | enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} |
| 183 | enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} |
| 184 | enum struct [[]] E5; |
| 185 | |
| 186 | struct S { |
| 187 | friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 188 | friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} |
| 189 | friend int f2 [[]] [[noreturn]] () {} |
| 190 | [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 191 | [[]] friend int h() { |
| 192 | } |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 193 | [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} |
| 194 | friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 195 | friend class [[]] C; // expected-error{{an attribute list cannot appear here}} |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 196 | [[]] friend class D; // expected-error{{an attribute list cannot appear here}} |
| 197 | [[]] friend int; // expected-error{{an attribute list cannot appear here}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 198 | }; |
| 199 | template<typename T> void tmpl(T) {} |
| 200 | template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} |
| 201 | template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} |
| 202 | template void [[]] tmpl(short); |
| 203 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 204 | // Argument tests |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 205 | alignas int aligned_no_params; // expected-error {{expected '('}} |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 206 | alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 207 | |
| 208 | // Statement tests |
| 209 | void foo () { |
| 210 | [[]] ; |
| 211 | [[]] { } |
| 212 | [[]] if (0) { } |
| 213 | [[]] for (;;); |
| 214 | [[]] do { |
| 215 | [[]] continue; |
| 216 | } while (0); |
| 217 | [[]] while (0); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 218 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 219 | [[]] switch (i) { |
| 220 | [[]] case 0: |
| 221 | [[]] default: |
| 222 | [[]] break; |
| 223 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 224 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 225 | [[]] goto there; |
| 226 | [[]] there: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 227 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 228 | [[]] try { |
| 229 | } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} |
| 230 | } |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 231 | struct S { int arr[2]; } s; |
| 232 | (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 233 | int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 234 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 235 | void bar [[noreturn]] ([[]] int i, [[]] int j); |
| 236 | using FuncType = void ([[]] int); |
| 237 | void baz([[]]...); // expected-error {{expected parameter declarator}} |
| 238 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 239 | [[]] return; |
| 240 | } |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 241 | |
| 242 | template<typename...Ts> void variadic() { |
| 243 | void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} |
| 244 | } |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 245 | |
| 246 | // Expression tests |
| 247 | void bar () { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 248 | // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains |
| 249 | // to the operator()'s type, and GCC does not otherwise accept attributes |
| 250 | // applied to types. Use that to test this. |
| 251 | [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} |
| 252 | [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 253 | new int[42][[]][5][[]]{}; |
| 254 | } |
| 255 | |
| 256 | // Condition tests |
| 257 | void baz () { |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 258 | if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} |
| 259 | switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 260 | default: |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 261 | for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | int x; |
| 266 | // An attribute can be applied to an expression-statement, such as the first |
| 267 | // statement in a for. But it can't be applied to a condition which is an |
| 268 | // expression. |
| 269 | for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} |
| 270 | for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} |
| 271 | while ([[]] bool k { false }) { |
| 272 | } |
| 273 | while ([[]] true) { // expected-error {{an attribute list cannot appear here}} |
| 274 | } |
| 275 | do { |
| 276 | } while ([[]] false); // expected-error {{an attribute list cannot appear here}} |
| 277 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 278 | for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 281 | |
| 282 | enum class __attribute__((visibility("hidden"))) SecretKeepers { |
| 283 | one, /* rest are deprecated */ two, three |
| 284 | }; |
| 285 | enum class [[]] EvenMoreSecrets {}; |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 286 | |
| 287 | namespace arguments { |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 288 | void f[[gnu::format(printf, 1, 2)]](const char*, ...); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 289 | void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}} |
| 290 | [[deprecated("with argument")]] int i; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 291 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 292 | } |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 293 | |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 294 | // Forbid attributes on decl specifiers. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 295 | unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 296 | expected-error {{an attribute list cannot appear here}} |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 297 | typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 298 | expected-error {{an attribute list cannot appear here}} |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 299 | int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 300 | |
| 301 | // Forbid [[gnu::...]] attributes on declarator chunks. |
| 302 | int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} |
| 303 | int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} |
| 304 | int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 305 | |
| 306 | [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 307 | [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}} |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 308 | [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}} |
Richard Smith | 9ec2891 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 309 | |
| 310 | class A { |
| 311 | A([[gnu::unused]] int a); |
| 312 | }; |
| 313 | A::A([[gnu::unused]] int a) {} |
Nick Lewycky | 966a0fc | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 314 | |
| 315 | namespace GccConst { |
| 316 | // GCC's tokenizer treats const and __const as the same token. |
| 317 | [[gnu::const]] int *f1(); |
| 318 | [[gnu::__const]] int *f2(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 319 | [[gnu::__const__]] int *f3(); |
Nick Lewycky | 966a0fc | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 320 | void f(const int *); |
| 321 | void g() { f(f1()); f(f2()); } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 322 | void h() { f(f3()); } |
Nick Lewycky | 966a0fc | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 323 | } |
Nick Lewycky | 8fa2cbf | 2013-10-29 03:54:41 +0000 | [diff] [blame] | 324 | |
| 325 | namespace GccASan { |
| 326 | __attribute__((no_address_safety_analysis)) void f1(); |
| 327 | __attribute__((no_sanitize_address)) void f2(); |
| 328 | [[gnu::no_address_safety_analysis]] void f3(); |
| 329 | [[gnu::no_sanitize_address]] void f4(); |
| 330 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 331 | |
| 332 | namespace { |
| 333 | [[deprecated]] void bar(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 334 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 335 | [[deprecated("hello")]] void baz(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 336 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
| 337 | [[deprecated()]] void foo(); |
| 338 | // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}} |
| 339 | // expected-warning@-2 {{use of the 'deprecated' attribute is a C++14 extension}} |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 340 | [[gnu::deprecated()]] void quux(); |
| 341 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 342 | |
| 343 | namespace { |
| 344 | [[ // expected-error {{expected ']'}} |
| 345 | #pragma pack(pop) |
| 346 | deprecated |
| 347 | ]] void bad(); |
| 348 | } |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 349 | |
| 350 | #define attr_name bitand |
| 351 | #define attr_name_2(x) x |
| 352 | #define attr_name_3(x, y) x##y |
| 353 | [[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \ |
| 354 | expected-warning {{unknown attribute 'bitor' ignored}} \ |
| 355 | expected-warning {{unknown attribute 'bitand' ignored}} |