cxx0x-attributes.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat %s
  2. // Need std::initializer_list
  3. namespace std {
  4. typedef decltype(sizeof(int)) size_t;
  5. // libc++'s implementation
  6. template <class _E>
  7. class initializer_list
  8. {
  9. const _E* __begin_;
  10. size_t __size_;
  11. initializer_list(const _E* __b, size_t __s)
  12. : __begin_(__b),
  13. __size_(__s)
  14. {}
  15. public:
  16. typedef _E value_type;
  17. typedef const _E& reference;
  18. typedef const _E& const_reference;
  19. typedef size_t size_type;
  20. typedef const _E* iterator;
  21. typedef const _E* const_iterator;
  22. initializer_list() : __begin_(nullptr), __size_(0) {}
  23. size_t size() const {return __size_;}
  24. const _E* begin() const {return __begin_;}
  25. const _E* end() const {return __begin_ + __size_;}
  26. };
  27. }
  28. // Declaration syntax checks
  29. [[]] int before_attr;
  30. int [[]] between_attr;
  31. const [[]] int between_attr_2 = 0; // expected-error {{an attribute list cannot appear here}}
  32. int after_attr [[]];
  33. int * [[]] ptr_attr;
  34. int & [[]] ref_attr = after_attr;
  35. int & [[unknown]] ref_attr_2 = after_attr; // expected-warning {{unknown attribute 'unknown' ignored}}
  36. int & [[noreturn]] ref_attr_3 = after_attr; // expected-error {{'noreturn' attribute cannot be applied to types}}
  37. int && [[]] rref_attr = 0;
  38. int array_attr [1] [[]];
  39. alignas(8) int aligned_attr;
  40. [[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}}
  41. [[,,,static, class, namespace,, inline, constexpr, mutable,, bitand, bitor::compl(!.*_ Cx.!U^*R),,,]] int more_garbage_attr; // expected-warning {{unknown attribute 'static' ignored}} \
  42. // expected-warning {{unknown attribute 'class' ignored}} \
  43. // expected-warning {{unknown attribute 'namespace' ignored}} \
  44. // expected-warning {{unknown attribute 'inline' ignored}} \
  45. // expected-warning {{unknown attribute 'constexpr' ignored}} \
  46. // expected-warning {{unknown attribute 'mutable' ignored}} \
  47. // expected-warning {{unknown attribute 'bitand' ignored}} \
  48. // expected-warning {{unknown attribute 'compl' ignored}}
  49. [[u8"invalid!"]] int invalid_string_attr; // expected-error {{expected ']'}}
  50. void fn_attr () [[]];
  51. void noexcept_fn_attr () noexcept [[]];
  52. struct MemberFnOrder {
  53. virtual void f() const volatile && noexcept [[]] final = 0;
  54. };
  55. struct [[]] struct_attr;
  56. class [[]] class_attr {};
  57. union [[]] union_attr;
  58. // Checks attributes placed at wrong syntactic locations of class specifiers.
  59. class [[]] [[]]
  60. attr_after_class_name_decl [[]] [[]]; // expected-error {{an attribute list cannot appear here}}
  61. class [[]] [[]]
  62. attr_after_class_name_definition [[]] [[]] [[]]{}; // expected-error {{an attribute list cannot appear here}}
  63. class [[]] c {};
  64. class c [[]] [[]] x;
  65. class c [[]] [[]] y [[]] [[]];
  66. class c final [(int){0}];
  67. class base {};
  68. class [[]] [[]] final_class
  69. alignas(float) [[]] final // expected-error {{an attribute list cannot appear here}}
  70. alignas(float) [[]] [[]] alignas(float): base{}; // expected-error {{an attribute list cannot appear here}}
  71. class [[]] [[]] final_class_another
  72. [[]] [[]] alignas(16) final // expected-error {{an attribute list cannot appear here}}
  73. [[]] [[]] alignas(16) [[]]{}; // expected-error {{an attribute list cannot appear here}}
  74. // The diagnostics here don't matter much, this just shouldn't crash:
  75. 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}}
  76. class D final alignas ([l) {}]{}); // expected-error {{expected ',' or ']' in lambda capture list}} expected-error {{an attribute list cannot appear here}}
  77. [[]] struct with_init_declarators {} init_declarator;
  78. [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
  79. template<typename> [[]] struct no_init_declarators_template; // expected-error {{an attribute list cannot appear here}}
  80. void fn_with_structs() {
  81. [[]] struct with_init_declarators {} init_declarator;
  82. [[]] struct no_init_declarators; // expected-error {{an attribute list cannot appear here}}
  83. }
  84. [[]];
  85. struct ctordtor {
  86. [[]] ctordtor();
  87. [[]] ~ctordtor();
  88. };
  89. [[]] ctordtor::ctordtor() {}
  90. [[]] ctordtor::~ctordtor() {}
  91. extern "C++" [[]] int extern_attr;
  92. template <typename T> [[]] void template_attr ();
  93. [[]] [[]] int [[]] [[]] multi_attr [[]] [[]];
  94. int comma_attr [[,]];
  95. int scope_attr [[foo::]]; // expected-error {{expected identifier}}
  96. int (paren_attr) [[]]; // expected-error {{an attribute list cannot appear here}}
  97. unsigned [[]] int attr_in_decl_spec; // expected-error {{an attribute list cannot appear here}}
  98. unsigned [[]] int [[]] const double_decl_spec = 0; // expected-error 2{{an attribute list cannot appear here}}
  99. class foo {
  100. void const_after_attr () [[]] const; // expected-error {{expected ';'}}
  101. };
  102. extern "C++" [[]] { } // expected-error {{an attribute list cannot appear here}}
  103. [[]] template <typename T> void before_template_attr (); // expected-error {{an attribute list cannot appear here}}
  104. [[]] namespace ns { int i; } // expected-error {{an attribute list cannot appear here}} expected-note {{declared here}}
  105. [[]] static_assert(true, ""); //expected-error {{an attribute list cannot appear here}}
  106. [[]] asm(""); // expected-error {{an attribute list cannot appear here}}
  107. [[]] using ns::i; // expected-error {{an attribute list cannot appear here}}
  108. [[unknown]] using namespace ns; // expected-warning {{unknown attribute 'unknown' ignored}}
  109. [[noreturn]] using namespace ns; // expected-error {{'noreturn' attribute only applies to functions}}
  110. namespace [[]] ns2 {} // expected-warning {{attributes on a namespace declaration are incompatible with C++ standards before C++1z}}
  111. using [[]] alignas(4) [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
  112. using [[]] alignas(4) [[]] foobar = int; // expected-error {{an attribute list cannot appear here}} expected-error {{'alignas' attribute only applies to}}
  113. void bad_attributes_in_do_while() {
  114. do {} while (
  115. [[ns::i); // expected-error {{expected ']'}} \
  116. // expected-note {{to match this '['}} \
  117. // expected-error {{expected expression}}
  118. do {} while (
  119. [[a]b ns::i); // expected-error {{expected ']'}} \
  120. // expected-note {{to match this '['}} \
  121. // expected-error {{expected expression}}
  122. do {} while (
  123. [[ab]ab] ns::i); // expected-error {{an attribute list cannot appear here}}
  124. do {} while ( // expected-note {{to match this '('}}
  125. alignas(4 ns::i; // expected-note {{to match this '('}}
  126. } // expected-error 2{{expected ')'}} expected-error {{expected expression}}
  127. [[]] using T = int; // expected-error {{an attribute list cannot appear here}}
  128. using T [[]] = int; // ok
  129. template<typename T> using U [[]] = T;
  130. using ns::i [[]]; // expected-error {{an attribute list cannot appear here}}
  131. using [[]] ns::i; // expected-error {{an attribute list cannot appear here}}
  132. using T [[unknown]] = int; // expected-warning {{unknown attribute 'unknown' ignored}}
  133. using T [[noreturn]] = int; // expected-error {{'noreturn' attribute only applies to functions}}
  134. using V = int; // expected-note {{previous}}
  135. using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with different types}}
  136. auto trailing() -> [[]] const int; // expected-error {{an attribute list cannot appear here}}
  137. auto trailing() -> const [[]] int; // expected-error {{an attribute list cannot appear here}}
  138. auto trailing() -> const int [[]];
  139. auto trailing_2() -> struct struct_attr [[]];
  140. namespace N {
  141. struct S {};
  142. };
  143. template<typename> struct Template {};
  144. // FIXME: Improve this diagnostic
  145. struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
  146. struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
  147. struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
  148. template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
  149. template <> struct [[]] Template<void>;
  150. enum [[]] E1 {};
  151. enum [[]] E2; // expected-error {{forbids forward references}}
  152. enum [[]] E1;
  153. enum [[]] E3 : int;
  154. enum [[]] {
  155. k_123 [[]] = 123 // expected-warning {{attributes on an enumerator declaration are incompatible with C++ standards before C++1z}}
  156. };
  157. enum [[]] E1 e; // expected-error {{an attribute list cannot appear here}}
  158. enum [[]] class E4 { }; // expected-error {{an attribute list cannot appear here}}
  159. enum struct [[]] E5;
  160. struct S {
  161. friend int f [[]] (); // expected-FIXME{{an attribute list cannot appear here}}
  162. friend int f1 [[noreturn]] (); //expected-error{{an attribute list cannot appear here}}
  163. friend int f2 [[]] [[noreturn]] () {}
  164. [[]] friend int g(); // expected-error{{an attribute list cannot appear here}}
  165. [[]] friend int h() {
  166. }
  167. [[]] friend int f3(), f4(), f5(); // expected-error{{an attribute list cannot appear here}}
  168. friend int f6 [[noreturn]] (), f7 [[noreturn]] (), f8 [[noreturn]] (); // expected-error3 {{an attribute list cannot appear here}}
  169. friend class [[]] C; // expected-error{{an attribute list cannot appear here}}
  170. [[]] friend class D; // expected-error{{an attribute list cannot appear here}}
  171. [[]] friend int; // expected-error{{an attribute list cannot appear here}}
  172. };
  173. template<typename T> void tmpl(T) {}
  174. template void tmpl [[]] (int); // expected-FIXME {{an attribute list cannot appear here}}
  175. template [[]] void tmpl(char); // expected-error {{an attribute list cannot appear here}}
  176. template void [[]] tmpl(short);
  177. // Argument tests
  178. alignas int aligned_no_params; // expected-error {{expected '('}}
  179. alignas(i) int aligned_nonconst; // expected-error {{'aligned' attribute requires integer constant}} expected-note {{read of non-const variable 'i'}}
  180. // Statement tests
  181. void foo () {
  182. [[]] ;
  183. [[]] { }
  184. [[]] if (0) { }
  185. [[]] for (;;);
  186. [[]] do {
  187. [[]] continue;
  188. } while (0);
  189. [[]] while (0);
  190. [[]] switch (i) {
  191. [[]] case 0:
  192. [[]] default:
  193. [[]] break;
  194. }
  195. [[]] goto there;
  196. [[]] there:
  197. [[]] try {
  198. } [[]] catch (...) { // expected-error {{an attribute list cannot appear here}}
  199. }
  200. struct S { int arr[2]; } s;
  201. (void)s.arr[ [] { return 0; }() ]; // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
  202. int n = __builtin_offsetof(S, arr[ [] { return 0; }() ]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}}
  203. void bar [[noreturn]] ([[]] int i, [[]] int j);
  204. using FuncType = void ([[]] int);
  205. void baz([[]]...); // expected-error {{expected parameter declarator}}
  206. [[]] return;
  207. }
  208. template<typename...Ts> void variadic() {
  209. void bar [[noreturn...]] (); // expected-error {{attribute 'noreturn' cannot be used as an attribute pack}}
  210. }
  211. // Expression tests
  212. void bar () {
  213. // FIXME: GCC accepts [[gnu::noreturn]] on a lambda, even though it appertains
  214. // to the operator()'s type, and GCC does not otherwise accept attributes
  215. // applied to types. Use that to test this.
  216. [] () [[gnu::noreturn]] { return; } (); // expected-warning {{attribute 'noreturn' ignored}} FIXME-error {{should not return}}
  217. [] () [[gnu::noreturn]] { throw; } (); // expected-warning {{attribute 'noreturn' ignored}}
  218. new int[42][[]][5][[]]{};
  219. }
  220. // Condition tests
  221. void baz () {
  222. if ([[unknown]] bool b = true) { // expected-warning {{unknown attribute 'unknown' ignored}}
  223. switch ([[unknown]] int n { 42 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
  224. default:
  225. for ([[unknown]] int n = 0; [[unknown]] char b = n < 5; ++b) { // expected-warning 2{{unknown attribute 'unknown' ignored}}
  226. }
  227. }
  228. }
  229. int x;
  230. // An attribute can be applied to an expression-statement, such as the first
  231. // statement in a for. But it can't be applied to a condition which is an
  232. // expression.
  233. for ([[]] x = 0; ; ) {} // expected-error {{an attribute list cannot appear here}}
  234. for (; [[]] x < 5; ) {} // expected-error {{an attribute list cannot appear here}}
  235. while ([[]] bool k { false }) {
  236. }
  237. while ([[]] true) { // expected-error {{an attribute list cannot appear here}}
  238. }
  239. do {
  240. } while ([[]] false); // expected-error {{an attribute list cannot appear here}}
  241. for ([[unknown]] int n : { 1, 2, 3 }) { // expected-warning {{unknown attribute 'unknown' ignored}}
  242. }
  243. }
  244. enum class __attribute__((visibility("hidden"))) SecretKeepers {
  245. one, /* rest are deprecated */ two, three
  246. };
  247. enum class [[]] EvenMoreSecrets {};
  248. namespace arguments {
  249. void f[[gnu::format(printf, 1, 2)]](const char*, ...);
  250. void g() [[unknown::foo(ignore arguments for unknown attributes, even with symbols!)]]; // expected-warning {{unknown attribute 'foo' ignored}}
  251. [[deprecated("with argument")]] int i;
  252. // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
  253. }
  254. // Forbid attributes on decl specifiers.
  255. unsigned [[gnu::used]] static int [[gnu::unused]] v1; // expected-error {{'unused' attribute cannot be applied to types}} \
  256. expected-error {{an attribute list cannot appear here}}
  257. typedef [[gnu::used]] unsigned long [[gnu::unused]] v2; // expected-error {{'unused' attribute cannot be applied to types}} \
  258. expected-error {{an attribute list cannot appear here}}
  259. int [[carries_dependency]] foo(int [[carries_dependency]] x); // expected-error 2{{'carries_dependency' attribute cannot be applied to types}}
  260. // Forbid [[gnu::...]] attributes on declarator chunks.
  261. int *[[gnu::unused]] v3; // expected-warning {{attribute 'unused' ignored}}
  262. int v4[2][[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
  263. int v5()[[gnu::unused]]; // expected-warning {{attribute 'unused' ignored}}
  264. [[attribute_declaration]]; // expected-warning {{unknown attribute 'attribute_declaration' ignored}}
  265. [[noreturn]]; // expected-error {{'noreturn' attribute only applies to functions}}
  266. [[carries_dependency]]; // expected-error {{'carries_dependency' attribute only applies to functions, methods, and parameters}}
  267. class A {
  268. A([[gnu::unused]] int a);
  269. };
  270. A::A([[gnu::unused]] int a) {}
  271. namespace GccConst {
  272. // GCC's tokenizer treats const and __const as the same token.
  273. [[gnu::const]] int *f1();
  274. [[gnu::__const]] int *f2();
  275. [[gnu::__const__]] int *f3();
  276. void f(const int *);
  277. void g() { f(f1()); f(f2()); }
  278. void h() { f(f3()); }
  279. }
  280. namespace GccASan {
  281. __attribute__((no_address_safety_analysis)) void f1();
  282. __attribute__((no_sanitize_address)) void f2();
  283. [[gnu::no_address_safety_analysis]] void f3();
  284. [[gnu::no_sanitize_address]] void f4();
  285. }
  286. namespace {
  287. [[deprecated]] void bar();
  288. // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
  289. [[deprecated("hello")]] void baz();
  290. // expected-warning@-1 {{use of the 'deprecated' attribute is a C++14 extension}}
  291. [[deprecated()]] void foo();
  292. // expected-error@-1 {{parentheses must be omitted if 'deprecated' attribute's argument list is empty}}
  293. // expected-warning@-2 {{use of the 'deprecated' attribute is a C++14 extension}}
  294. [[gnu::deprecated()]] void quux();
  295. }
  296. namespace {
  297. [[ // expected-error {{expected ']'}}
  298. #pragma pack(pop)
  299. deprecated
  300. ]] void bad();
  301. }
  302. #define attr_name bitand
  303. #define attr_name_2(x) x
  304. #define attr_name_3(x, y) x##y
  305. [[attr_name, attr_name_2(bitor), attr_name_3(com, pl)]] int macro_attrs; // expected-warning {{unknown attribute 'compl' ignored}} \
  306. expected-warning {{unknown attribute 'bitor' ignored}} \
  307. expected-warning {{unknown attribute 'bitand' ignored}}