warn-range-loop-analysis.cpp 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wall -Wrange-loop-bind-reference -Wno-unused -verify %s
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wloop-analysis -verify %s
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wrange-loop-analysis -verify %s
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wloop-analysis -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s

template <typename return_type>
struct Iterator {
  return_type operator*();
  Iterator operator++();
  bool operator!=(const Iterator);
};

template <typename T>
struct Container {
  typedef Iterator<T> I;

  I begin();
  I end();
};

struct Foo {};
struct Bar {
  // Small trivially copyable types do not show a warning when copied in a
  // range-based for loop. This size ensures the object is not considered
  // small.
  char s[128];
  Bar(Foo);
  Bar(int);
  operator int();
};

// Testing notes:
// test0 checks that the full text of the warnings and notes is correct.  The
//   rest of the tests checks a smaller portion of the text.
// test1-6 are set in pairs, the odd numbers are the non-reference returning
//   versions of the even numbers.
// test7-9 use an array instead of a range object
// tests use all four versions of the loop variable, const &T, const T, T&, and
//   T.  Versions producing errors and are commented out.
//
// Conversion chart:
//   double <=> int
//   int    <=> Bar
//   double  => Bar
//   Foo     => Bar
//
// Conversions during tests:
// test1-2
//   int => int
//   int => double
//   int => Bar
// test3-4
//   Bar => Bar
//   Bar => int
// test5-6
//   Foo => Bar
// test7
//   double => double
//   double => int
//   double => Bar
// test8
//   Foo => Foo
//   Foo => Bar
// test9
//   Bar => Bar
//   Bar => int

void test0() {
  Container<int> int_non_ref_container;
  Container<int&> int_container;
  Container<Bar&> bar_container;

  for (const int &x : int_non_ref_container) {}
  // expected-warning@-1 {{loop variable 'x' binds to a temporary value produced by a range of type 'Container<int>'}}
  // expected-note@-2 {{use non-reference type 'int'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""

  for (const double &x : int_container) {}
  // expected-warning@-1 {{loop variable 'x' of type 'const double &' binds to a temporary constructed from type 'int &'}}
  // expected-note@-2 {{use non-reference type 'double' to make construction explicit or type 'const int &' to prevent copying}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:22}:""

  for (const Bar x : bar_container) {}
  // expected-warning@-1 {{loop variable 'x' creates a copy from type 'const Bar'}}
  // expected-note@-2 {{use reference type 'const Bar &' to prevent copying}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:18}:"&"
}

void test1() {
  Container<int> A;

  for (const int &&x : A) {}
  // No warning, rvalue-reference to the temporary
  for (const int &x : A) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'int'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const int x : A) {}
  // No warning, non-reference type indicates copy is made
  for (int&& x : A) {}
  // No warning, rvalue-reference to the temporary
  //for (int &x : A) {}
  // Binding error
  for (int x : A) {}
  // No warning, non-reference type indicates copy is made

  for (const double &&x : A) {}
  // No warning, rvalue-reference to the temporary
  for (const double &x : A) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'double'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:22}:""
  for (const double x : A) {}
  // No warning, non-reference type indicates copy is made
  for (double &&x : A) {}
  // No warning, rvalue-reference to the temporary
  //for (double &x : A) {}
  // Binding error
  for (double x : A) {}
  // No warning, non-reference type indicates copy is made

  for (const Bar &&x : A) {}
  // No warning, rvalue-reference to the temporary
  for (const Bar &x : A) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : A) {}
  // No warning, non-reference type indicates copy is made
  for (Bar &&x : A) {}
  // No warning, rvalue-reference to the temporary
  //for (Bar &x : A) {}
  // Binding error
  for (Bar x : A) {}
  // No warning, non-reference type indicates copy is made
}

void test2() {
  Container<int&> B;

  //for (const int &&x : B) {}
  // Binding error
  for (const int &x : B) {}
  // No warning, this reference is not a temporary
  for (const int x : B) {}
  // No warning on POD copy
  //for (int &x : B) {}
  // Binding error
  for (int &x : B) {}
  // No warning
  for (int x : B) {}
  // No warning

  for (const double &&x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'double'{{.*}}'const int &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:23}:""
  for (const double &x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'double'{{.*}}'const int &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:21-[[@LINE-3]]:22}:""
  for (const double x : B) {}
  for (double &&x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'double'{{.*}}'const int &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:17}:""
  //for (double &x : B) {}
  // Binding error
  for (double x : B) {}
  // No warning

  for (const Bar &&x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const Bar &x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : B) {}
  for (Bar &&x : B) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (Bar &x : B) {}
  // Binding error
  for (Bar x : B) {}
  // No warning
}

void test3() {
  Container<Bar> C;

  for (const Bar &&x : C) {}
  // No warning, rvalue-reference to the temporary
  for (const Bar &x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : C) {}
  // No warning, non-reference type indicates copy is made
  for (Bar &&x : C) {}
  // No warning, rvalue-reference to the temporary
  //for (Bar &x : C) {}
  // Binding error
  for (Bar x : C) {}
  // No warning, non-reference type indicates copy is made

  for (const int &&x : C) {}
  // No warning, rvalue-reference to the temporary
  for (const int &x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'int'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const int x : C) {}
  // No warning, copy made
  for (int &&x : C) {}
  // No warning, rvalue-reference to the temporary
  //for (int &x : C) {}
  // Binding error
  for (int x : C) {}
  // No warning, copy made
}

void test4() {
  Container<Bar&> D;

  //for (const Bar &&x : D) {}
  // Binding error
  for (const Bar &x : D) {}
  // No warning, this reference is not a temporary
  for (const Bar x : D) {}
  // expected-warning@-1 {{creates a copy}}
  // expected-note@-2 {{'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:18}:"&"
  //for (Bar &&x : D) {}
  // Binding error
  for (Bar &x : D) {}
  // No warning
  for (Bar x : D) {}
  // No warning

  for (const int &&x : D) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const int &x : D) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const int x : D) {}
  // No warning
  for (int &&x : D) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (int &x : D) {}
  // Binding error
  for (int x : D) {}
  // No warning
}

void test5() {
  Container<Foo> E;

  for (const Bar &&x : E) {}
  // No warning, rvalue-reference to the temporary
  for (const Bar &x : E) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : E) {}
  // No warning, non-reference type indicates copy is made
  for (Bar &&x : E) {}
  // No warning, rvalue-reference to the temporary
  //for (Bar &x : E) {}
  // Binding error
  for (Bar x : E) {}
  // No warning, non-reference type indicates copy is made
}

void test6() {
  Container<Foo&> F;

  for (const Bar &&x : F) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const Bar &x : F) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : F) {}
  // No warning.
  for (Bar &&x : F) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (Bar &x : F) {}
  // Binding error
  for (Bar x : F) {}
  // No warning
}

void test7() {
  double G[2];

  //for (const double &&x : G) {}
  // Binding error
  for (const double &x : G) {}
  // No warning
  for (const double x : G) {}
  // No warning on POD copy
  //for (double &&x : G) {}
  // Binding error
  for (double &x : G) {}
  // No warning
  for (double x : G) {}
  // No warning

  for (const int &&x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const int &x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const int x : G) {}
  // No warning
  for (int &&x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (int &x : G) {}
  // Binding error
  for (int x : G) {}
  // No warning

  for (const Bar &&x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const Bar &x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : G) {}
  // No warning
  for (Bar &&x : G) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const double &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (Bar &x : G) {}
  // Binding error
  for (Bar x : G) {}
  // No warning
}

void test8() {
  Foo H[2];

  //for (const Foo &&x : H) {}
  // Binding error
  for (const Foo &x : H) {}
  // No warning
  for (const Foo x : H) {}
  // No warning on POD copy
  //for (Foo &&x : H) {}
  // Binding error
  for (Foo &x : H) {}
  // No warning
  for (Foo x : H) {}
  // No warning

  for (const Bar &&x : H) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const Bar &x : H) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const Bar x : H) {}
  // No warning
  for (Bar &&x: H) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'Bar'{{.*}}'const Foo &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (Bar &x: H) {}
  // Binding error
  for (Bar x: H) {}
  // No warning
}

void test9() {
  Bar I[2] = {1,2};

  //for (const Bar &&x : I) {}
  // Binding error
  for (const Bar &x : I) {}
  // No warning
  for (const Bar x : I) {}
  // expected-warning@-1 {{creates a copy}}
  // expected-note@-2 {{'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:18}:"&"
  //for (Bar &&x : I) {}
  // Binding error
  for (Bar &x : I) {}
  // No warning
  for (Bar x : I) {}
  // No warning

  for (const int &&x : I) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""
  for (const int &x : I) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""
  for (const int x : I) {}
  // No warning
  for (int &&x : I) {}
  // expected-warning@-1 {{binds to a temporary constructed from}}
  // expected-note-re@-2 {{'int'{{.*}}'const Bar &'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:14}:""
  //for (int &x : I) {}
  // Binding error
  for (int x : I) {}
  // No warning
}

void test10() {
  Container<Bar> C;

  for (const Bar &x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""

  for (const Bar& x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:""

  for (const Bar & x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:20}:""

  for (const Bar&x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:18}:" "
}

template <class T>
void test_template_function() {
  // In a template instantiation the diagnostics should not be emitted for
  // loops with dependent types.
  Container<Bar> C;
  for (const Bar &x : C) {}
  // expected-warning@-1 {{binds to a temporary value produced by a range}}
  // expected-note@-2 {{'Bar'}}
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:18-[[@LINE-3]]:19}:""

  Container<T> Dependent;
  for (const T &x : Dependent) {}
}
template void test_template_function<Bar>();

template <class T>
struct test_template_struct {
  static void static_member() {
    Container<Bar> C;
    for (const Bar &x : C) {}
    // expected-warning@-1 {{binds to a temporary value produced by a range}}
    // expected-note@-2 {{'Bar'}}
    // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:21}:""

    Container<T> Dependent;
    for (const T &x : Dependent) {}
  }

  void member() {
    Container<Bar> C;
    for (const Bar &x : C) {}
    // expected-warning@-1 {{binds to a temporary value produced by a range}}
    // expected-note@-2 {{'Bar'}}
    // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:21}:""

    Container<T> Dependent;
    for (const T &x : Dependent) {}
  }
};
template struct test_template_struct<Bar>;

struct test_struct_with_templated_member {
  void member() {
    Container<Bar> C;
    for (const Bar &x : C) {}
    // expected-warning@-1 {{binds to a temporary value produced by a range}}
    // expected-note@-2 {{'Bar'}}
    // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:21}:""
  }

  template <class T>
  void template_member() {
    Container<Bar> C;
    for (const Bar &x : C) {}
    // expected-warning@-1 {{binds to a temporary value produced by a range}}
    // expected-note@-2 {{'Bar'}}
    // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:21}:""

    Container<T> Dependent;
    for (const T &x : Dependent) {}
  }
};
template void test_struct_with_templated_member::template_member<Bar>();

#define TEST_MACRO            \
  void test_macro() {         \
    Container<Bar> C;         \
    for (const Bar &x : C) {} \
  }

TEST_MACRO