1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wloop-analysis -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wrange-loop-analysis -verify %s
4 template <typename return_type>
6 return_type operator*();
8 bool operator!=(const Iterator);
13 typedef Iterator<T> I;
27 // test0 checks that the full text of the warnings and notes is correct. The
28 // rest of the tests checks a smaller portion of the text.
29 // test1-6 are set in pairs, the odd numbers are the non-reference returning
30 // versions of the even numbers.
31 // test7-9 use an array instead of a range object
32 // tests use all four versions of the loop varaible, const &T, const T, T&, and
33 // T. Versions producing errors and are commented out.
41 // Conversions during tests:
63 Container<int> int_non_ref_container;
64 Container<int&> int_container;
65 Container<Bar&> bar_container;
67 for (const int &x : int_non_ref_container) {}
68 // expected-warning@-1 {{loop variable 'x' is always a copy because the range of type 'Container<int>' does not return a reference}}
69 // expected-note@-2 {{use non-reference type 'int'}}
71 for (const double &x : int_container) {}
72 // expected-warning@-1 {{loop variable 'x' has type 'const double &' but is initialized with type 'int' resulting in a copy}}
73 // expected-note@-2 {{use non-reference type 'double' to keep the copy or type 'const int &' to prevent copying}}
75 for (const Bar x : bar_container) {}
76 // expected-warning@-1 {{loop variable 'x' of type 'const Bar' creates a copy from type 'const Bar'}}
77 // expected-note@-2 {{use reference type 'const Bar &' to prevent copying}}
83 for (const int &x : A) {}
84 // expected-warning@-1 {{always a copy}}
85 // expected-note@-2 {{'int'}}
86 for (const int x : A) {}
87 // No warning, non-reference type indicates copy is made
91 // No warning, non-reference type indicates copy is made
93 for (const double &x : A) {}
94 // expected-warning@-1 {{always a copy}}
95 // expected-note@-2 {{'double'}}
96 for (const double x : A) {}
97 // No warning, non-reference type indicates copy is made
98 //for (double &x : A) {}
100 for (double x : A) {}
101 // No warning, non-reference type indicates copy is made
103 for (const Bar &x : A) {}
104 // expected-warning@-1 {{always a copy}}
105 // expected-note@-2 {{'Bar'}}
106 for (const Bar x : A) {}
107 // No warning, non-reference type indicates copy is made
108 //for (Bar &x : A) {}
111 // No warning, non-reference type indicates copy is made
117 for (const int &x : B) {}
118 // No warning, this reference is not a temporary
119 for (const int x : B) {}
120 // No warning on POD copy
126 for (const double &x : B) {}
127 // expected-warning@-1 {{resulting in a copy}}
128 // expected-note-re@-2 {{'double'{{.*}}'const int &'}}
129 for (const double x : B) {}
130 //for (double &x : B) {}
132 for (double x : B) {}
135 for (const Bar &x : B) {}
136 // expected-warning@-1 {{resulting in a copy}}
137 // expected-note@-2 {{'Bar'}}
138 for (const Bar x : B) {}
139 //for (Bar &x : B) {}
148 for (const Bar &x : C) {}
149 // expected-warning@-1 {{always a copy}}
150 // expected-note@-2 {{'Bar'}}
151 for (const Bar x : C) {}
152 // No warning, non-reference type indicates copy is made
153 //for (Bar &x : C) {}
156 // No warning, non-reference type indicates copy is made
158 for (const int &x : C) {}
159 // expected-warning@-1 {{always a copy}}
160 // expected-note@-2 {{'int'}}
161 for (const int x : C) {}
162 // No warning, copy made
163 //for (int &x : C) {}
166 // No warning, copy made
172 for (const Bar &x : D) {}
173 // No warning, this reference is not a temporary
174 for (const Bar x : D) {}
175 // expected-warning@-1 {{creates a copy}}
176 // expected-note@-2 {{'const Bar &'}}
182 for (const int &x : D) {}
183 // expected-warning@-1 {{resulting in a copy}}
184 // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
185 for (const int x : D) {}
187 //for (int &x : D) {}
196 for (const Bar &x : E) {}
197 // expected-warning@-1 {{always a copy}}
198 // expected-note@-2 {{'Bar'}}
199 for (const Bar x : E) {}
200 // No warning, non-reference type indicates copy is made
201 //for (Bar &x : E) {}
204 // No warning, non-reference type indicates copy is made
210 for (const Bar &x : F) {}
211 // expected-warning@-1 {{resulting in a copy}}
212 // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
213 for (const Bar x : F) {}
215 //for (Bar &x : F) {}
224 for (const double &x : G) {}
226 for (const double x : G) {}
227 // No warning on POD copy
228 for (double &x : G) {}
230 for (double x : G) {}
233 for (const int &x : G) {}
234 // expected-warning@-1 {{resulting in a copy}}
235 // expected-note-re@-2 {{'int'{{.*}}'const double &'}}
236 for (const int x : G) {}
238 //for (int &x : G) {}
243 for (const Bar &x : G) {}
244 // expected-warning@-1 {{resulting in a copy}}
245 // expected-note-re@-2 {{'Bar'{{.*}}'const double &'}}
246 for (const Bar x : G) {}
248 //for (int &Bar : G) {}
257 for (const Foo &x : H) {}
259 for (const Foo x : H) {}
260 // No warning on POD copy
266 for (const Bar &x : H) {}
267 // expected-warning@-1 {{resulting in a copy}}
268 // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
269 for (const Bar x : H) {}
280 for (const Bar &x : I) {}
282 for (const Bar x : I) {}
283 // expected-warning@-1 {{creates a copy}}
284 // expected-note@-2 {{'const Bar &'}}
290 for (const int &x : I) {}
291 // expected-warning@-1 {{resulting in a copy}}
292 // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
293 for (const int x : I) {}
295 //for (int &x : I) {}