Debian Codegraph @

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