Debian Codegraph @

gcc/testsuite/g++.dg/cpp0x/attr-nodiscard1.C

raw
1 // C++ 26 P2552R3 - On the ignorability of standard attributes
2 // { dg-do compile { target c++11 } }
3
4 int arr[2];
5 struct S { int a, b; };
6 S arr2[2];
7
8 void
9 foo (int n)
10 {
11 struct [[nodiscard]] S1 {};
12 struct [[nodiscard ("foobar")]] S2 {};
13 struct [[nodiscard (0)]] S3 {}; // { dg-error "'nodiscard' attribute argument must be a string constant" }
14 struct [[nodiscard ("foo", "bar", "baz")]] S4 {}; // { dg-error "wrong number of arguments specified for 'nodiscard' attribute" }
15 struct [[nodiscard (0, 1, 2)]] S5 {}; // { dg-error "wrong number of arguments specified for 'nodiscard' attribute" }
16
17 auto a = [] [[nodiscard]] () {};
18 auto b = [] constexpr [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
19 // { dg-error "parameter declaration before lambda declaration specifiers only optional with" "" { target c++20_down } .-1 }
20 // { dg-error "'constexpr' lambda only available with" "" { target c++14_down } .-2 }
21 auto c = [] noexcept [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
22 // { dg-error "parameter declaration before lambda exception specification only optional with" "" { target c++20_down } .-1 }
23 auto d = [] () [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
24 auto e = new int [n] [[nodiscard]]; // { dg-warning "attributes ignored on outermost array type in new expression" }
25 auto e2 = new int [n] [[nodiscard]] [42]; // { dg-warning "attributes ignored on outermost array type in new expression" }
26 auto f = new int [n][42] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
27 [[nodiscard]]; // { dg-warning "attributes at the beginning of statement are ignored" }
28 [[nodiscard]] {} // { dg-warning "attributes at the beginning of statement are ignored" }
29 [[nodiscard]] if (true) {} // { dg-warning "attributes at the beginning of statement are ignored" }
30 [[nodiscard]] while (false) {} // { dg-warning "attributes at the beginning of statement are ignored" }
31 [[nodiscard]] goto lab; // { dg-warning "attributes at the beginning of statement are ignored" }
32 [[nodiscard]] lab:; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
33 [[nodiscard]] try {} catch (int) {} // { dg-warning "attributes at the beginning of statement are ignored" }
34 if ([[nodiscard]] int x = 0) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
35 switch (n)
36 {
37 [[nodiscard]] case 1: // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
38 [[nodiscard]] break; // { dg-warning "attributes at the beginning of statement are ignored" }
39 [[nodiscard]] default: // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
40 break;
41 }
42 for ([[nodiscard]] auto a : arr) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
43 for ([[nodiscard]] auto [a, b] : arr2) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
44 // { dg-error "structured bindings only available with" "" { target c++14_down } .-1 }
45 [[nodiscard]] asm (""); // { dg-warning "attributes ignored on 'asm' declaration" }
46 try {} catch ([[nodiscard]] int x) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
47 try {} catch ([[nodiscard]] int) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
48 try {} catch (int [[nodiscard]] x) {} // { dg-warning "attribute ignored" }
49 try {} catch (int [[nodiscard]]) {} // { dg-warning "attribute ignored" }
50 try {} catch (int x [[nodiscard]]) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
51 }
52
53 [[nodiscard]] int bar ();
54 using foobar [[nodiscard]] = int; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
55 [[nodiscard]] int a; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
56 [[nodiscard]] auto [b, c] = arr; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
57 // { dg-error "structured bindings only available with" "" { target c++14_down } .-1 }
58 [[nodiscard]]; // { dg-warning "attribute ignored" }
59 inline [[nodiscard]] void baz () {} // { dg-warning "attribute ignored" }
60 // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 }
61 constexpr [[nodiscard]] int qux () { return 0; } // { dg-warning "attribute ignored" }
62 // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 }
63 int [[nodiscard]] d; // { dg-warning "attribute ignored" }
64 int const [[nodiscard]] e = 1; // { dg-warning "attribute ignored" }
65 struct A {} [[nodiscard]]; // { dg-warning "attribute ignored in declaration of 'struct A'" }
66 struct A [[nodiscard]]; // { dg-warning "attribute ignored" }
67 struct A [[nodiscard]] a1; // { dg-warning "attribute ignored" }
68 A [[nodiscard]] a2; // { dg-warning "attribute ignored" }
69 enum B { B0 } [[nodiscard]]; // { dg-warning "attribute ignored in declaration of 'enum B'" }
70 enum B [[nodiscard]]; // { dg-warning "attribute ignored" }
71 enum B [[nodiscard]] b1; // { dg-warning "attribute ignored" }
72 B [[nodiscard]] b2; // { dg-warning "attribute ignored" }
73 struct [[nodiscard]] C {};
74 int f [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
75 int g[2] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
76 int g2 [[nodiscard]] [2]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
77 int corge () [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
78 int *[[nodiscard]] h; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
79 int & [[nodiscard]] i = f; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
80 int && [[nodiscard]] j = 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
81 int S::* [[nodiscard]] k; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
82 auto l = sizeof (int [2] [[nodiscard]]); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
83 int freddy ([[nodiscard]] int a, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
84 [[nodiscard]] int, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
85 [[nodiscard]] int c = 0, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
86 [[nodiscard]] int = 0); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
87 void
88 corge ([[nodiscard]] int a, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
89 [[nodiscard]] int, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
90 [[nodiscard]] int c = 0, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
91 [[nodiscard]] int = 0) // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
92 {
93 }
94 [[nodiscard]] void
95 garply () // { dg-warning "'nodiscard' attribute applied to 'void garply\\\(\\\)' with void return type" }
96 {
97 }
98 [[nodiscard]] int
99 xyzzyy ()
100 {
101 return 0;
102 }
103 int grault (int [[nodiscard]] a, // { dg-warning "attribute ignored" }
104 int [[nodiscard]], // { dg-warning "attribute ignored" }
105 int [[nodiscard]] c = 0, // { dg-warning "attribute ignored" }
106 int [[nodiscard]] = 0); // { dg-warning "attribute ignored" }
107 void
108 waldo (int [[nodiscard]] a, // { dg-warning "attribute ignored" }
109 int [[nodiscard]], // { dg-warning "attribute ignored" }
110 int [[nodiscard]] c = 0, // { dg-warning "attribute ignored" }
111 int [[nodiscard]] = 0) // { dg-warning "attribute ignored" }
112 {
113 }
114 int plugh (int a [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
115 int b [[nodiscard]] = 0); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
116 void
117 thud (int a [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
118 int b [[nodiscard]] = 0) // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
119 {
120 }
121 enum [[nodiscard]] D { D0 };
122 enum class [[nodiscard]] E { E0 };
123 enum F {};
124 enum [[nodiscard]] F; // { dg-warning "type attributes ignored after type is already defined" }
125 enum G {
126 G0 [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
127 G1 [[nodiscard]] = 2 // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
128 };
129 namespace [[nodiscard]] H { using H0 = int; } // { dg-warning "'nodiscard' attribute directive ignored" }
130 namespace [[nodiscard]] {} // { dg-warning "'nodiscard' attribute directive ignored" }
131 [[nodiscard]] using namespace H; // { dg-warning "'nodiscard' attribute directive ignored" }
132 struct [[nodiscard]] I
133 {
134 [[nodiscard]]; // { dg-error "declaration does not declare anything" }
135 [[nodiscard]] int i; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
136 [[nodiscard]] int foo ();
137 [[nodiscard]] int bar () { return 1; }
138 [[nodiscard]] int : 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
139 [[nodiscard]] int i2 : 5; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
140 [[nodiscard]] static int i3; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
141 static int i4;
142 };
143 [[nodiscard]] int I::i4 = 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
144 struct J : [[nodiscard]] C {}; // { dg-warning "attributes on base specifiers are ignored" }
145 #if __cpp_concepts >= 201907L
146 template <typename T>
147 concept K [[nodiscard]] = requires { true; }; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" "" { target c++20 } }
148 #endif
149 typedef int L [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
150 template <typename T>
151 struct M {};
152 template <>
153 struct [[nodiscard]] M<int> { int m; };
154 typedef int N[2] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }
155 typedef int O [[nodiscard]] [2]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }