Richard Smith | cbaaa29 | 2017-08-13 22:26:53 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++17-extensions %s |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2 | |
Alexis Hunt | 6aa9bee | 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 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 37 | // Declaration syntax checks |
| 38 | [[]] int before_attr; |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 39 | int [[]] between_attr; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 40 | const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 41 | int after_attr [[]]; |
| 42 | int * [[]] ptr_attr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 43 | int & [[]] ref_attr = after_attr; |
Richard Smith | 54ecd98 | 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 | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 46 | int && [[]] rref_attr = 0; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 47 | int array_attr [1] [[]]; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 48 | alignas(8) int aligned_attr; |
Michael Han | 23214e5 | 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}} \ |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [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 | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 58 | [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 59 | void fn_attr () [[]]; |
Richard Smith | 7bdcc4a | 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 | }; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 64 | struct [[]] struct_attr; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 65 | class [[]] class_attr {}; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 66 | union [[]] union_attr; |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 67 | enum [[]] E { }; |
| 68 | namespace test_misplacement { |
| 69 | [[]] struct struct_attr2; //expected-error{{misplaced attributes}} |
| 70 | [[]] class class_attr2; //expected-error{{misplaced attributes}} |
| 71 | [[]] union union_attr2; //expected-error{{misplaced attributes}} |
| 72 | [[]] enum E2 { }; //expected-error{{misplaced attributes}} |
| 73 | } |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 74 | |
| 75 | // Checks attributes placed at wrong syntactic locations of class specifiers. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 76 | class [[]] [[]] |
| 77 | attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}} |
| 78 | |
| 79 | class [[]] [[]] |
| 80 | attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}} |
| 81 | |
| 82 | class [[]] c {}; |
| 83 | class c [[]] [[]] x; |
| 84 | class c [[]] [[]] y [[]] [[]]; |
| 85 | class c final [(int){0}]; |
| 86 | |
| 87 | class base {}; |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 88 | class [[]] [[]] final_class |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 89 | alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}} |
| 90 | alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}} |
| 91 | |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 92 | class [[]] [[]] final_class_another |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 93 | [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}} |
| 94 | [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}} |
| 95 | |
Nico Weber | 36de3a2 | 2014-12-29 21:56:22 +0000 | [diff] [blame] | 96 | // The diagnostics here don't matter much, this just shouldn't crash: |
| 97 | 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}} |
| 98 | class D final alignas ([l) {}]{}); // expected-error {{expected ',' or ']' in lambda capture list}} expected-error {{an attribute list cannot appear here}} |
| 99 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 100 | [[]] struct with_init_declarators {} init_declarator; |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 101 | [[]] struct no_init_declarators; // expected-error {{misplaced attributes}} |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 102 | template<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}} |
| 103 | void fn_with_structs() { |
| 104 | [[]] struct with_init_declarators {} init_declarator; |
| 105 | [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}} |
| 106 | } |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 107 | [[]]; |
| 108 | struct ctordtor { |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 109 | [[]] ctordtor [[]] () [[]]; |
| 110 | ctordtor (C) [[]]; |
| 111 | [[]] ~ctordtor [[]] () [[]]; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 112 | }; |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 113 | [[]] ctordtor::ctordtor [[]] () [[]] {} |
| 114 | [[]] ctordtor::ctordtor (C) [[]] try {} catch (...) {} |
| 115 | [[]] ctordtor::~ctordtor [[]] () [[]] {} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 116 | extern "C++" [[]] int extern_attr; |
| 117 | template <typename T> [[]] void template_attr (); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 118 | [[]] [[]] int [[]] [[]] multi_attr [[]] [[]]; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 119 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 120 | int comma_attr [[,]]; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 121 | int scope_attr [[foo::]]; // expected-error {{expected identifier}} |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 122 | int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 123 | unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}} |
| 124 | unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 125 | class foo { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 126 | void const_after_attr () [[]] const; // expected-error {{expected ';'}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 127 | }; |
| 128 | extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}} |
| 129 | [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}} |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 130 | [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 131 | [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}} |
| 132 | [[]] asm(""); // expected-error {{an attribute list cannot appear here}} |
| 133 | |
| 134 | [[]] using ns::i; // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 135 | [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}} |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 136 | [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}} |
Richard Smith | 40e202f | 2017-10-14 00:56:24 +0000 | [diff] [blame] | 137 | namespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are a C++17 extension}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 138 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 139 | using [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}} |
| 140 | using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}} |
| 141 | |
| 142 | void bad_attributes_in_do_while() { |
| 143 | do {} while ( |
| 144 | [[ns::i); // expected-error {{expected ']'}} \ |
| 145 | // expected-note {{to match this '['}} \ |
| 146 | // expected-error {{expected expression}} |
| 147 | do {} while ( |
| 148 | [[a]b ns::i); // expected-error {{expected ']'}} \ |
| 149 | // expected-note {{to match this '['}} \ |
| 150 | // expected-error {{expected expression}} |
| 151 | do {} while ( |
| 152 | [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}} |
| 153 | do {} while ( // expected-note {{to match this '('}} |
| 154 | alignas(4 ns::i; // expected-note {{to match this '('}} |
| 155 | } // expected-error 2{{expected ')'}} expected-error {{expected expression}} |
| 156 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 157 | [[]] using T = int; // expected-error {{an attribute list cannot appear here}} |
| 158 | using T [[]] = int; // ok |
| 159 | template<typename T> using U [[]] = T; |
| 160 | using ns::i [[]]; // expected-error {{an attribute list cannot appear here}} |
| 161 | using [[]] ns::i; // expected-error {{an attribute list cannot appear here}} |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 162 | using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}} |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 163 | using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}} |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 164 | using V = int; // expected-note {{previous}} |
| 165 | using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 166 | |
| 167 | auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}} |
| 168 | auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}} |
| 169 | auto trailing() -> const int [[]]; |
| 170 | auto trailing_2() -> struct struct_attr [[]]; |
| 171 | |
| 172 | namespace N { |
| 173 | struct S {}; |
| 174 | }; |
| 175 | template<typename> struct Template {}; |
| 176 | |
| 177 | // FIXME: Improve this diagnostic |
| 178 | struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}} |
| 179 | struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}} |
| 180 | struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}} |
| 181 | template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}} |
| 182 | template <> struct [[]] Template<void>; |
| 183 | |
| 184 | enum [[]] E1 {}; |
| 185 | enum [[]] E2; // expected-error {{forbids forward references}} |
| 186 | enum [[]] E1; |
| 187 | enum [[]] E3 : int; |
| 188 | enum [[]] { |
Richard Smith | 40e202f | 2017-10-14 00:56:24 +0000 | [diff] [blame] | 189 | k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are a C++17 extension}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 190 | }; |
| 191 | enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}} |
| 192 | enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}} |
| 193 | enum struct [[]] E5; |
| 194 | |
| 195 | struct S { |
| 196 | friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}} |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 197 | friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}} |
| 198 | friend int f2 [[]] [[noreturn]] () {} |
| 199 | [[]] friend int g(); // expected-error{{an attribute list cannot appear here}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 200 | [[]] friend int h() { |
| 201 | } |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 202 | [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}} |
| 203 | friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 204 | friend class [[]] C; // expected-error{{an attribute list cannot appear here}} |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 205 | [[]] friend class D; // expected-error{{an attribute list cannot appear here}} |
| 206 | [[]] friend int; // expected-error{{an attribute list cannot appear here}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 207 | }; |
| 208 | template<typename T> void tmpl(T) {} |
| 209 | template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}} |
| 210 | template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}} |
| 211 | template void [[]] tmpl(short); |
| 212 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 213 | // Argument tests |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 214 | alignas int aligned_no_params; // expected-error {{expected '('}} |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 215 | alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}} |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 216 | |
| 217 | // Statement tests |
| 218 | void foo () { |
| 219 | [[]] ; |
| 220 | [[]] { } |
| 221 | [[]] if (0) { } |
| 222 | [[]] for (;;); |
| 223 | [[]] do { |
| 224 | [[]] continue; |
| 225 | } while (0); |
| 226 | [[]] while (0); |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 227 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 228 | [[]] switch (i) { |
| 229 | [[]] case 0: |
| 230 | [[]] default: |
| 231 | [[]] break; |
| 232 | } |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 233 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 234 | [[]] goto there; |
| 235 | [[]] there: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 236 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 237 | [[]] try { |
| 238 | } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}} |
| 239 | } |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 240 | struct S { int arr[2]; } s; |
| 241 | (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 242 | int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} |
| 243 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 244 | void bar [[noreturn]] ([[]] int i, [[]] int j); |
| 245 | using FuncType = void ([[]] int); |
| 246 | void baz([[]]...); // expected-error {{expected parameter declarator}} |
| 247 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 248 | [[]] return; |
| 249 | } |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 250 | |
| 251 | template<typename...Ts> void variadic() { |
| 252 | void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}} |
| 253 | } |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 254 | |
| 255 | // Expression tests |
| 256 | void bar () { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 257 | // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains |
| 258 | // to the operator()'s type, and GCC does not otherwise accept attributes |
| 259 | // applied to types. Use that to test this. |
| 260 | [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}} |
| 261 | [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 262 | new int[42][[]][5][[]]{}; |
| 263 | } |
| 264 | |
| 265 | // Condition tests |
| 266 | void baz () { |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 267 | if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}} |
| 268 | switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 269 | default: |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 270 | for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | int x; |
| 275 | // An attribute can be applied to an expression-statement, such as the first |
| 276 | // statement in a for. But it can't be applied to a condition which is an |
| 277 | // expression. |
| 278 | for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}} |
| 279 | for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}} |
| 280 | while ([[]] bool k { false }) { |
| 281 | } |
| 282 | while ([[]] true) { // expected-error {{an attribute list cannot appear here}} |
| 283 | } |
| 284 | do { |
| 285 | } while ([[]] false); // expected-error {{an attribute list cannot appear here}} |
| 286 | |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 287 | for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}} |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 290 | |
| 291 | enum class __attribute__((visibility("hidden"))) SecretKeepers { |
| 292 | one, /* rest are deprecated */ two, three |
| 293 | }; |
| 294 | enum class [[]] EvenMoreSecrets {}; |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 295 | |
| 296 | namespace arguments { |
Richard Smith | 368ca52 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 297 | void f[[gnu::format(printf, 1, 2)]](const char*, ...); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 298 | void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}} |
| 299 | [[deprecated("with argument")]] int i; |
Aaron Ballman | f25731a | 2015-02-16 23:12:37 +0000 | [diff] [blame] | 300 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 301 | } |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 302 | |
Richard Smith | 368ca52 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 303 | // Forbid attributes on decl specifiers. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 304 | unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \ |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 305 | expected-error {{an attribute list cannot appear here}} |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 306 | typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \ |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 307 | expected-error {{an attribute list cannot appear here}} |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 308 | int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}} |
Richard Smith | 368ca52 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 309 | |
| 310 | // Forbid [[gnu::...]] attributes on declarator chunks. |
| 311 | int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}} |
| 312 | int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} |
| 313 | int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}} |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 314 | |
| 315 | [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}} |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 316 | [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}} |
Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 317 | [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to parameters, Objective-C methods, and functions}} |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 318 | |
| 319 | class A { |
| 320 | A([[gnu::unused]] int a); |
| 321 | }; |
| 322 | A::A([[gnu::unused]] int a) {} |
Nick Lewycky | d060467 | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 323 | |
| 324 | namespace GccConst { |
| 325 | // GCC's tokenizer treats const and __const as the same token. |
| 326 | [[gnu::const]] int *f1(); |
| 327 | [[gnu::__const]] int *f2(); |
Saleem Abdulrasool | 73196ba | 2013-11-30 21:17:12 +0000 | [diff] [blame] | 328 | [[gnu::__const__]] int *f3(); |
Nick Lewycky | d060467 | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 329 | void f(const int *); |
| 330 | void g() { f(f1()); f(f2()); } |
Saleem Abdulrasool | 73196ba | 2013-11-30 21:17:12 +0000 | [diff] [blame] | 331 | void h() { f(f3()); } |
Nick Lewycky | d060467 | 2013-10-29 03:33:57 +0000 | [diff] [blame] | 332 | } |
Nick Lewycky | 2c53116 | 2013-10-29 03:54:41 +0000 | [diff] [blame] | 333 | |
| 334 | namespace GccASan { |
| 335 | __attribute__((no_address_safety_analysis)) void f1(); |
| 336 | __attribute__((no_sanitize_address)) void f2(); |
| 337 | [[gnu::no_address_safety_analysis]] void f3(); |
| 338 | [[gnu::no_sanitize_address]] void f4(); |
| 339 | } |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 340 | |
| 341 | namespace { |
| 342 | [[deprecated]] void bar(); |
Aaron Ballman | f25731a | 2015-02-16 23:12:37 +0000 | [diff] [blame] | 343 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 344 | [[deprecated("hello")]] void baz(); |
Aaron Ballman | f25731a | 2015-02-16 23:12:37 +0000 | [diff] [blame] | 345 | // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}} |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 346 | [[deprecated()]] void foo(); |
| 347 | // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}} |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 348 | [[gnu::deprecated()]] void quux(); |
| 349 | } |
David Majnemer | d527199 | 2015-01-09 18:09:39 +0000 | [diff] [blame] | 350 | |
| 351 | namespace { |
| 352 | [[ // expected-error {{expected ']'}} |
| 353 | #pragma pack(pop) |
| 354 | deprecated |
| 355 | ]] void bad(); |
| 356 | } |
Benjamin Kramer | 60be563 | 2015-03-29 19:25:07 +0000 | [diff] [blame] | 357 | |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 358 | int fallthru(int n) { |
| 359 | switch (n) { |
| 360 | case 0: |
| 361 | n += 5; |
Richard Smith | cbaaa29 | 2017-08-13 22:26:53 +0000 | [diff] [blame] | 362 | [[fallthrough]]; // expected-warning {{use of the 'fallthrough' attribute is a C++17 extension}} |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 363 | case 1: |
| 364 | n *= 2; |
| 365 | break; |
| 366 | } |
| 367 | return n; |
| 368 | } |
| 369 | |
Benjamin Kramer | 60be563 | 2015-03-29 19:25:07 +0000 | [diff] [blame] | 370 | #define attr_name bitand |
| 371 | #define attr_name_2(x) x |
| 372 | #define attr_name_3(x, y) x##y |
| 373 | [[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \ |
| 374 | expected-warning {{unknown attribute 'bitor' ignored}} \ |
| 375 | expected-warning {{unknown attribute 'bitand' ignored}} |