Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wrange-loop-construct.C

raw
1 // PR c++/94695
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wrange-loop-construct" }
4
5 #include <initializer_list>
6
7 struct Small {
8 char arr[64];
9 };
10
11 struct Big_aggr {
12 char arr[65];
13 };
14
15 struct Big_triv_copy {
16 char arr[65];
17 Big_triv_copy() { }
18 };
19
20 struct Big {
21 char arr[65];
22 Big () = default;
23 Big(const Big&);
24 };
25
26 struct Foo { };
27 struct Bar {
28 char arr[100];
29 Bar(Foo);
30 Bar(int);
31 operator int();
32 };
33
34 template<typename T>
35 struct It {
36 T operator*();
37 It operator++();
38 bool operator!=(const It);
39 };
40
41 template<typename T>
42 struct Cont {
43 using I = It<T>;
44 I begin();
45 I end();
46 };
47
48 #define TEST \
49 void fn_macro() \
50 { \
51 Cont<Bar &> cont_bar_ref; \
52 for (const Bar x : cont_bar_ref) { (void) x; } \
53 }
54
55 TEST
56
57 Cont<Bar &>& foo ();
58 Cont<Bar &> foo2 ();
59
60 void
61 fn1 ()
62 {
63 for (const auto x : foo () ) { (void) x; } // { dg-warning "creates a copy" }
64 for (const auto x : foo2 () ) { (void) x; } // { dg-warning "creates a copy" }
65
66 Small s{};
67 Small sa[5] = { };
68 for (const auto x : sa) { (void) x; }
69 for (const auto x : { s, s, s }) { (void) x; }
70
71 Big_aggr b{};
72 Big_aggr ba[5] = { };
73 for (const auto x : ba) { (void) x; } // { dg-warning "creates a copy" }
74 for (const auto x : { b, b, b }) { (void) x; } // { dg-warning "creates a copy" }
75
76 Big_triv_copy bt{};
77 Big_triv_copy bta[5];
78 for (const auto x : bta) { (void) x; } // { dg-warning "creates a copy" }
79 for (const auto x : { bt, bt, bt }) { (void) x; } // { dg-warning "creates a copy" }
80
81 Big b2;
82 Big ba2[5];
83 for (const auto x : ba2) { (void) x; } // { dg-warning "creates a copy" }
84 for (const auto x : { b2, b2, b2 }) { (void) x; } // { dg-warning "creates a copy" }
85 }
86
87 void
88 fn2 ()
89 {
90 Cont<int> cont_int;
91 for (const auto x : cont_int) { (void) x; }
92 for (const int x : cont_int) { (void) x; }
93 for (int x : cont_int) { (void) x; }
94 for (const auto &x : cont_int) { (void) x; }
95 for (double x : cont_int) { (void) x; }
96 for (const double x : cont_int) { (void) x; }
97 for (const Bar x : cont_int) { (void) x; }
98 for (Bar x : cont_int) { (void) x; }
99 }
100
101 void
102 fn3 ()
103 {
104 Cont<int &> cont_int_ref;
105 for (const int x : cont_int_ref) { (void) x; }
106 for (int x : cont_int_ref) { (void) x; }
107 for (const double x : cont_int_ref) { (void) x; }
108 for (double x : cont_int_ref) { (void) x; }
109 for (const Bar x : cont_int_ref) { (void) x; }
110 for (Bar x : cont_int_ref) { (void) x; }
111 }
112
113 void
114 fn4 ()
115 {
116 Cont<Bar> cont_bar;
117 for (const Bar x : cont_bar) { (void) x; }
118 for (Bar x : cont_bar) { (void) x; }
119 for (const int x : cont_bar) { (void) x; }
120 for (int x : cont_bar) { (void) x; }
121 }
122
123 void
124 fn5 ()
125 {
126 Cont<Bar&> cont_bar_ref;
127 for (const Bar x : cont_bar_ref) { (void) x; } // { dg-warning "creates a copy" }
128 for (Bar x : cont_bar_ref) { (void) x; }
129 for (const int x : cont_bar_ref) { (void) x; }
130 for (int x : cont_bar_ref) { (void) x; }
131 }
132
133 void
134 fn6 ()
135 {
136 Cont<Foo> cont_foo;
137 for (const Bar x : cont_foo) { (void) x; }
138 for (Bar x : cont_foo) { (void) x; }
139 }
140
141 void
142 fn7 ()
143 {
144 Cont<Foo &> cont_foo_ref;
145 for (const Bar x : cont_foo_ref) { (void) x; }
146 for (Bar x : cont_foo_ref) { (void) x; }
147 }
148
149 void
150 fn8 ()
151 {
152 double arr[2];
153 for (const double x : arr) { (void) x; }
154 for (double x : arr) { (void) x; }
155 for (const int x : arr) { (void) x; }
156 for (int x : arr) { (void) x; }
157 for (const Bar x : arr) { (void) x; }
158 for (Bar x : arr) { (void) x; }
159 }
160
161 void
162 fn9 ()
163 {
164 Foo foo[2];
165 for (const Foo x : foo) { (void) x; }
166 for (Foo x : foo) { (void) x; }
167 for (const Bar x : foo) { (void) x; }
168 for (Bar x : foo) { (void) x; }
169 }
170
171 void
172 fn10 ()
173 {
174 Bar bar[2] = { 1, 2 };
175 for (const Bar x : bar) { (void) x; } // { dg-warning "creates a copy" }
176 for (Bar x : bar) { (void) x; }
177 for (const int x : bar) { (void) x; }
178 for (int x : bar) { (void) x; }
179 }
180
181 template<typename T>
182 void
183 fn11 ()
184 {
185 Cont<Bar> cont_bar;
186 for (const Bar x : cont_bar) { (void) x; }
187
188 Cont<Bar&> cont_bar_ref;
189 for (const Bar x : cont_bar_ref) { (void) x; } // { dg-warning "creates a copy" }
190
191 Cont<T> cont_dep;
192 for (const T x : cont_dep) { (void) x; }
193 }
194
195 template<typename T>
196 void
197 fn12 ()
198 {
199 for (const auto x : { T{} }) { (void) x; } // { dg-warning "creates a copy" }
200 }
201
202 void
203 invoke ()
204 {
205 fn11<int> ();
206 fn12<Big> ();
207 }