1 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 2 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 3 // RUN: -analyzer-config exploration_strategy=unexplored_first_queue\ 4 // RUN: -analyzer-checker debug.ExprInspection 5 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 6 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 7 // RUN: -analyzer-config exploration_strategy=dfs -DDFS=1\ 8 // RUN: -analyzer-checker debug.ExprInspection 9 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 10 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 11 // RUN: -analyzer-config exploration_strategy=unexplored_first_queue\ 12 // RUN: -analyzer-config cplusplus.Move:WarnOn=KnownsOnly -DPEACEFUL\ 13 // RUN: -analyzer-checker debug.ExprInspection 14 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 15 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 16 // RUN: -analyzer-config exploration_strategy=dfs -DDFS=1\ 17 // RUN: -analyzer-config cplusplus.Move:WarnOn=KnownsOnly -DPEACEFUL\ 18 // RUN: -analyzer-checker debug.ExprInspection 19 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 20 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 21 // RUN: -analyzer-config exploration_strategy=unexplored_first_queue\ 22 // RUN: -analyzer-config cplusplus.Move:WarnOn=All -DAGGRESSIVE\ 23 // RUN: -analyzer-checker debug.ExprInspection 24 // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move -verify %s\ 25 // RUN: -std=c++11 -analyzer-output=text -analyzer-config eagerly-assume=false\ 26 // RUN: -analyzer-config exploration_strategy=dfs -DDFS=1\ 27 // RUN: -analyzer-config cplusplus.Move:WarnOn=All -DAGGRESSIVE\ 28 // RUN: -analyzer-checker debug.ExprInspection 29 30 #include "Inputs/system-header-simulator-cxx.h" 31 32 void clang_analyzer_warnIfReached(); 33 34 class B { 35 public: 36 B() = default; 37 B(const B &) = default; 38 B(B &&) = default; 39 B& operator=(const B &q) = default; 40 void operator=(B &&b) { 41 return; 42 } 43 void foo() { return; } 44 }; 45 46 class A { 47 int i; 48 double d; 49 50 public: 51 B b; 52 A(int ii = 42, double dd = 1.0) : d(dd), i(ii), b(B()) {} 53 void moveconstruct(A &&other) { 54 std::swap(b, other.b); 55 std::swap(d, other.d); 56 std::swap(i, other.i); 57 return; 58 } 59 static A get() { 60 A v(12, 13); 61 return v; 62 } 63 A(A *a) { 64 moveconstruct(std::move(*a)); 65 } 66 A(const A &other) : i(other.i), d(other.d), b(other.b) {} 67 A(A &&other) : i(other.i), d(other.d), b(std::move(other.b)) { 68 #ifdef AGGRESSIVE 69 // expected-note@-2{{Object 'b' is moved}} 70 #endif 71 } 72 A(A &&other, char *k) { 73 moveconstruct(std::move(other)); 74 } 75 void operator=(const A &other) { 76 i = other.i; 77 d = other.d; 78 b = other.b; 79 return; 80 } 81 void operator=(A &&other) { 82 moveconstruct(std::move(other)); 83 return; 84 } 85 int getI() { return i; } 86 int foo() const; 87 void bar() const; 88 void reset(); 89 void destroy(); 90 void clear(); 91 void resize(std::size_t); 92 void assign(const A &); 93 bool empty() const; 94 bool isEmpty() const; 95 operator bool() const; 96 97 void testUpdateField() { 98 A a; 99 A b = std::move(a); 100 a.i = 1; 101 a.foo(); // no-warning 102 } 103 void testUpdateFieldDouble() { 104 A a; 105 A b = std::move(a); 106 a.d = 1.0; 107 a.foo(); // no-warning 108 } 109 }; 110 111 int bignum(); 112 113 void moveInsideFunctionCall(A a) { 114 A b = std::move(a); 115 } 116 void leftRefCall(A &a) { 117 a.foo(); 118 } 119 void rightRefCall(A &&a) { 120 a.foo(); 121 } 122 void constCopyOrMoveCall(const A a) { 123 a.foo(); 124 } 125 126 void copyOrMoveCall(A a) { 127 a.foo(); 128 } 129 130 void simpleMoveCtorTest() { 131 { 132 A a; 133 A b = std::move(a); 134 a.foo(); 135 #ifndef PEACEFUL 136 // expected-note@-3 {{Object 'a' is moved}} 137 // expected-warning@-3 {{Method called on moved-from object 'a'}} 138 // expected-note@-4 {{Method called on moved-from object 'a'}} 139 #endif 140 } 141 { 142 A a; 143 A b = std::move(a); 144 b = a; 145 #ifndef PEACEFUL 146 // expected-note@-3 {{Object 'a' is moved}} 147 // expected-warning@-3 {{Moved-from object 'a' is copied}} 148 // expected-note@-4 {{Moved-from object 'a' is copied}} 149 #endif 150 } 151 { 152 A a; 153 A b = std::move(a); 154 b = std::move(a); 155 #ifndef PEACEFUL 156 // expected-note@-3 {{Object 'a' is moved}} 157 // expected-warning@-3 {{Moved-from object 'a' is moved}} 158 // expected-note@-4 {{Moved-from object 'a' is moved}} 159 #endif 160 } 161 } 162 163 void simpleMoveAssignementTest() { 164 { 165 A a; 166 A b; 167 b = std::move(a); 168 a.foo(); 169 #ifndef PEACEFUL 170 // expected-note@-3 {{Object 'a' is moved}} 171 // expected-warning@-3 {{Method called on moved-from object 'a'}} 172 // expected-note@-4 {{Method called on moved-from object 'a'}} 173 #endif 174 } 175 { 176 A a; 177 A b; 178 b = std::move(a); 179 A c(a); 180 #ifndef PEACEFUL 181 // expected-note@-3 {{Object 'a' is moved}} 182 // expected-warning@-3 {{Moved-from object 'a' is copied}} 183 // expected-note@-4 {{Moved-from object 'a' is copied}} 184 #endif 185 } 186 { 187 A a; 188 A b; 189 b = std::move(a); 190 A c(std::move(a)); 191 #ifndef PEACEFUL 192 // expected-note@-3 {{Object 'a' is moved}} 193 // expected-warning@-3 {{Moved-from object 'a' is moved}} 194 // expected-note@-4 {{Moved-from object 'a' is moved}} 195 #endif 196 } 197 } 198 199 void moveInInitListTest() { 200 struct S { 201 A a; 202 }; 203 A a; 204 S s{std::move(a)}; 205 a.foo(); 206 #ifndef PEACEFUL 207 // expected-note@-3 {{Object 'a' is moved}} 208 // expected-warning@-3 {{Method called on moved-from object 'a'}} 209 // expected-note@-4 {{Method called on moved-from object 'a'}} 210 #endif 211 } 212 213 // Don't report a bug if the variable was assigned to in the meantime. 214 void reinitializationTest(int i) { 215 { 216 A a; 217 A b; 218 b = std::move(a); 219 a = A(); 220 a.foo(); 221 } 222 { 223 A a; 224 if (i == 1) { 225 #ifndef PEACEFUL 226 // expected-note@-2 {{Assuming 'i' is not equal to 1}} 227 // expected-note@-3 {{Taking false branch}} 228 // And the other report: 229 // expected-note@-5 {{Assuming 'i' is not equal to 1}} 230 // expected-note@-6 {{Taking false branch}} 231 #endif 232 A b; 233 b = std::move(a); 234 a = A(); 235 } 236 if (i == 2) { 237 #ifndef PEACEFUL 238 // expected-note@-2 {{Assuming 'i' is not equal to 2}} 239 // expected-note@-3 {{Taking false branch}} 240 // And the other report: 241 // expected-note@-5 {{Assuming 'i' is not equal to 2}} 242 // expected-note@-6 {{Taking false branch}} 243 #endif 244 a.foo(); // no-warning 245 } 246 } 247 { 248 A a; 249 if (i == 1) { 250 #ifndef PEACEFUL 251 // expected-note@-2 {{Taking false branch}} 252 // expected-note@-3 {{Taking false branch}} 253 #endif 254 std::move(a); 255 } 256 if (i == 2) { 257 #ifndef PEACEFUL 258 // expected-note@-2 {{Taking false branch}} 259 // expected-note@-3 {{Taking false branch}} 260 #endif 261 a = A(); 262 a.foo(); 263 } 264 } 265 // The built-in assignment operator should also be recognized as a 266 // reinitialization. (std::move() may be called on built-in types in template 267 // code.) 268 { 269 int a1 = 1, a2 = 2; 270 std::swap(a1, a2); 271 } 272 // A std::move() after the assignment makes the variable invalid again. 273 { 274 A a; 275 A b; 276 b = std::move(a); 277 a = A(); 278 b = std::move(a); 279 a.foo(); 280 #ifndef PEACEFUL 281 // expected-note@-3 {{Object 'a' is moved}} 282 // expected-warning@-3 {{Method called on moved-from object 'a'}} 283 // expected-note@-4 {{Method called on moved-from object 'a'}} 284 #endif 285 } 286 // If a path exist where we not reinitialize the variable we report a bug. 287 { 288 A a; 289 A b; 290 b = std::move(a); 291 #ifndef PEACEFUL 292 // expected-note@-2 {{Object 'a' is moved}} 293 #endif 294 if (i < 10) { 295 #ifndef PEACEFUL 296 // expected-note@-2 {{Assuming 'i' is >= 10}} 297 // expected-note@-3 {{Taking false branch}} 298 #endif 299 a = A(); 300 } 301 if (i > 5) { 302 a.foo(); 303 #ifndef PEACEFUL 304 // expected-note@-3 {{Taking true branch}} 305 // expected-warning@-3 {{Method called on moved-from object 'a'}} 306 // expected-note@-4 {{Method called on moved-from object 'a'}} 307 #endif 308 } 309 } 310 } 311 312 // Using decltype on an expression is not a use. 313 void decltypeIsNotUseTest() { 314 A a; 315 // A b(std::move(a)); 316 decltype(a) other_a; // no-warning 317 } 318 319 void loopTest() { 320 { 321 A a; 322 for (int i = 0; i < bignum(); i++) { 323 #ifndef PEACEFUL 324 // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}} 325 #endif 326 rightRefCall(std::move(a)); // no-warning 327 } 328 } 329 { 330 A a; 331 for (int i = 0; i < 2; i++) { 332 #ifndef PEACEFUL 333 // expected-note@-2 {{Loop condition is true. Entering loop body}} 334 // expected-note@-3 {{Loop condition is true. Entering loop body}} 335 // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}} 336 #endif 337 rightRefCall(std::move(a)); // no-warning 338 } 339 } 340 { 341 A a; 342 for (int i = 0; i < bignum(); i++) { 343 #ifndef PEACEFUL 344 // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}} 345 #endif 346 leftRefCall(a); // no-warning 347 } 348 } 349 { 350 A a; 351 for (int i = 0; i < 2; i++) { 352 #ifndef PEACEFUL 353 // expected-note@-2 {{Loop condition is true. Entering loop body}} 354 // expected-note@-3 {{Loop condition is true. Entering loop body}} 355 // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}} 356 #endif 357 leftRefCall(a); // no-warning 358 } 359 } 360 { 361 A a; 362 for (int i = 0; i < bignum(); i++) { 363 #ifndef PEACEFUL 364 // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}} 365 #endif 366 constCopyOrMoveCall(a); // no-warning 367 } 368 } 369 { 370 A a; 371 for (int i = 0; i < 2; i++) { 372 #ifndef PEACEFUL 373 // expected-note@-2 {{Loop condition is true. Entering loop body}} 374 // expected-note@-3 {{Loop condition is true. Entering loop body}} 375 // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}} 376 #endif 377 constCopyOrMoveCall(a); // no-warning 378 } 379 } 380 { 381 A a; 382 for (int i = 0; i < bignum(); i++) { 383 #ifndef PEACEFUL 384 // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}} 385 #endif 386 moveInsideFunctionCall(a); // no-warning 387 } 388 } 389 { 390 A a; 391 for (int i = 0; i < 2; i++) { 392 #ifndef PEACEFUL 393 // expected-note@-2 {{Loop condition is true. Entering loop body}} 394 // expected-note@-3 {{Loop condition is true. Entering loop body}} 395 // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}} 396 #endif 397 moveInsideFunctionCall(a); // no-warning 398 } 399 } 400 { 401 A a; 402 for (int i = 0; i < bignum(); i++) { 403 #ifndef PEACEFUL 404 // expected-note@-2 {{Loop condition is false. Execution jumps to the end of the function}} 405 #endif 406 copyOrMoveCall(a); // no-warning 407 } 408 } 409 { 410 A a; 411 for (int i = 0; i < 2; i++) { 412 #ifndef PEACEFUL 413 // expected-note@-2 {{Loop condition is true. Entering loop body}} 414 // expected-note@-3 {{Loop condition is true. Entering loop body}} 415 // expected-note@-4 {{Loop condition is false. Execution jumps to the end of the function}} 416 #endif 417 copyOrMoveCall(a); // no-warning 418 } 419 } 420 { 421 A a; 422 for (int i = 0; i < bignum(); i++) { 423 #ifndef PEACEFUL 424 // expected-note@-2 {{Loop condition is true. Entering loop body}} 425 // expected-note@-3 {{Loop condition is true. Entering loop body}} 426 #endif 427 constCopyOrMoveCall(std::move(a)); 428 #ifndef PEACEFUL 429 // expected-note@-2 {{Object 'a' is moved}} 430 // expected-warning@-3 {{Moved-from object 'a' is moved}} 431 // expected-note@-4 {{Moved-from object 'a' is moved}} 432 #endif 433 } 434 } 435 436 // Don't warn if we return after the move. 437 { 438 A a; 439 for (int i = 0; i < 3; ++i) { 440 a.bar(); 441 if (a.foo() > 0) { 442 A b; 443 b = std::move(a); // no-warning 444 return; 445 } 446 } 447 } 448 } 449 450 // Report a usage of a moved-from object only at the first use. 451 void uniqueTest(bool cond) { 452 A a(42, 42.0); 453 A b; 454 b = std::move(a); 455 456 if (cond) { 457 a.foo(); 458 #ifndef PEACEFUL 459 // expected-note@-5 {{Object 'a' is moved}} 460 // expected-note@-4 {{Assuming 'cond' is not equal to 0}} 461 // expected-note@-5 {{Taking true branch}} 462 // expected-warning@-5 {{Method called on moved-from object 'a'}} 463 // expected-note@-6 {{Method called on moved-from object 'a'}} 464 #endif 465 } 466 if (cond) { 467 a.bar(); // no-warning 468 } 469 470 a.bar(); // no-warning 471 } 472 473 void uniqueTest2() { 474 A a; 475 A a1 = std::move(a); 476 a.foo(); 477 #ifndef PEACEFUL 478 // expected-note@-3 {{Object 'a' is moved}} 479 // expected-warning@-3 {{Method called on moved-from object 'a'}} 480 // expected-note@-4 {{Method called on moved-from object 'a'}} 481 #endif 482 483 A a2 = std::move(a); // no-warning 484 a.foo(); // no-warning 485 } 486 487 // There are exceptions where we assume in general that the method works fine 488 //even on moved-from objects. 489 void moveSafeFunctionsTest() { 490 A a; 491 A b = std::move(a); 492 #ifndef PEACEFUL 493 // expected-note@-2 {{Object 'a' is moved}} 494 #endif 495 a.empty(); // no-warning 496 a.isEmpty(); // no-warning 497 (void)a; // no-warning 498 (bool)a; // expected-warning {{expression result unused}} 499 a.foo(); 500 #ifndef PEACEFUL 501 // expected-warning@-2 {{Method called on moved-from object 'a'}} 502 // expected-note@-3 {{Method called on moved-from object 'a'}} 503 #endif 504 } 505 506 void moveStateResetFunctionsTest() { 507 { 508 A a; 509 A b = std::move(a); 510 a.reset(); // no-warning 511 a.foo(); // no-warning 512 // Test if resets the state of subregions as well. 513 a.b.foo(); // no-warning 514 } 515 { 516 A a; 517 A b = std::move(a); 518 a.destroy(); // no-warning 519 a.foo(); // no-warning 520 } 521 { 522 A a; 523 A b = std::move(a); 524 a.clear(); // no-warning 525 a.foo(); // no-warning 526 a.b.foo(); // no-warning 527 } 528 { 529 A a; 530 A b = std::move(a); 531 a.resize(0); // no-warning 532 a.foo(); // no-warning 533 a.b.foo(); // no-warning 534 } 535 { 536 A a; 537 A b = std::move(a); 538 a.assign(A()); // no-warning 539 a.foo(); // no-warning 540 a.b.foo(); // no-warning 541 } 542 } 543 544 // Moves or uses that occur as part of template arguments. 545 template <int> 546 class ClassTemplate { 547 public: 548 void foo(A a); 549 }; 550 551 template <int> 552 void functionTemplate(A a); 553 554 void templateArgIsNotUseTest() { 555 { 556 // A pattern like this occurs in the EXPECT_EQ and ASSERT_EQ macros in 557 // Google Test. 558 A a; 559 ClassTemplate<sizeof(A(std::move(a)))>().foo(std::move(a)); // no-warning 560 } 561 { 562 A a; 563 functionTemplate<sizeof(A(std::move(a)))>(std::move(a)); // no-warning 564 } 565 } 566 567 // Moves of global variables are not reported. 568 A global_a; 569 void globalVariablesTest() { 570 std::move(global_a); 571 global_a.foo(); // no-warning 572 } 573 574 // Moves of member variables. 575 class memberVariablesTest { 576 A a; 577 static A static_a; 578 579 void f() { 580 A b; 581 b = std::move(a); 582 a.foo(); 583 #ifdef AGGRESSIVE 584 // expected-note@-3{{Object 'a' is moved}} 585 // expected-warning@-3 {{Method called on moved-from object 'a'}} 586 // expected-note@-4{{Method called on moved-from object 'a'}} 587 #endif 588 589 b = std::move(static_a); 590 static_a.foo(); 591 #ifdef AGGRESSIVE 592 // expected-note@-3{{Object 'static_a' is moved}} 593 // expected-warning@-3{{Method called on moved-from object 'static_a'}} 594 // expected-note@-4{{Method called on moved-from object 'static_a'}} 595 #endif 596 } 597 }; 598 599 void PtrAndArrayTest() { 600 A *Ptr = new A(1, 1.5); 601 A Arr[10]; 602 Arr[2] = std::move(*Ptr); 603 (*Ptr).foo(); 604 #ifdef AGGRESSIVE 605 // expected-note@-3{{Object is moved}} 606 // expected-warning@-3{{Method called on moved-from object}} 607 // expected-note@-4{{Method called on moved-from object}} 608 #endif 609 610 Ptr = &Arr[1]; 611 Arr[3] = std::move(Arr[1]); 612 Ptr->foo(); 613 #ifdef AGGRESSIVE 614 // expected-note@-3{{Object is moved}} 615 // expected-warning@-3{{Method called on moved-from object}} 616 // expected-note@-4{{Method called on moved-from object}} 617 #endif 618 619 Arr[3] = std::move(Arr[2]); 620 Arr[2].foo(); 621 #ifdef AGGRESSIVE 622 // expected-note@-3{{Object is moved}} 623 // expected-warning@-3{{Method called on moved-from object}} 624 // expected-note@-4{{Method called on moved-from object}} 625 #endif 626 627 Arr[2] = std::move(Arr[3]); // reinitialization 628 Arr[2].foo(); // no-warning 629 } 630 631 void exclusiveConditionsTest(bool cond) { 632 A a; 633 if (cond) { 634 A b; 635 b = std::move(a); 636 } 637 if (!cond) { 638 a.bar(); // no-warning 639 } 640 } 641 642 void differentBranchesTest(int i) { 643 // Don't warn if the use is in a different branch from the move. 644 { 645 A a; 646 if (i > 0) { 647 #ifndef PEACEFUL 648 // expected-note@-2 {{Assuming 'i' is > 0}} 649 // expected-note@-3 {{Taking true branch}} 650 #endif 651 A b; 652 b = std::move(a); 653 } else { 654 a.foo(); // no-warning 655 } 656 } 657 // Same thing, but with a ternary operator. 658 { 659 A a, b; 660 i > 0 ? (void)(b = std::move(a)) : a.bar(); // no-warning 661 #ifndef PEACEFUL 662 // expected-note@-2 {{'?' condition is true}} 663 #endif 664 } 665 // A variation on the theme above. 666 { 667 A a; 668 a.foo() > 0 ? a.foo() : A(std::move(a)).foo(); 669 #ifdef DFS 670 #ifndef PEACEFUL 671 // expected-note@-3 {{Assuming the condition is false}} 672 // expected-note@-4 {{'?' condition is false}} 673 #endif 674 #else 675 #ifndef PEACEFUL 676 // expected-note@-8 {{Assuming the condition is true}} 677 // expected-note@-9 {{'?' condition is true}} 678 #endif 679 #endif 680 } 681 // Same thing, but with a switch statement. 682 { 683 A a, b; 684 switch (i) { 685 #ifndef PEACEFUL 686 // expected-note@-2 {{Control jumps to 'case 1:'}} 687 #endif 688 case 1: 689 b = std::move(a); // no-warning 690 break; 691 #ifndef PEACEFUL 692 // expected-note@-2 {{Execution jumps to the end of the function}} 693 #endif 694 case 2: 695 a.foo(); // no-warning 696 break; 697 } 698 } 699 // However, if there's a fallthrough, we do warn. 700 { 701 A a, b; 702 switch (i) { 703 #ifndef PEACEFUL 704 // expected-note@-2 {{Control jumps to 'case 1:'}} 705 #endif 706 case 1: 707 b = std::move(a); 708 #ifndef PEACEFUL 709 // expected-note@-2 {{Object 'a' is moved}} 710 #endif 711 case 2: 712 a.foo(); 713 #ifndef PEACEFUL 714 // expected-warning@-2 {{Method called on moved-from object}} 715 // expected-note@-3 {{Method called on moved-from object 'a'}} 716 #endif 717 break; 718 } 719 } 720 } 721 722 void tempTest() { 723 A a = A::get(); 724 A::get().foo(); // no-warning 725 for (int i = 0; i < bignum(); i++) { 726 A::get().foo(); // no-warning 727 } 728 } 729 730 void interFunTest1(A &a) { 731 a.bar(); 732 #ifndef PEACEFUL 733 // expected-warning@-2 {{Method called on moved-from object 'a'}} 734 // expected-note@-3 {{Method called on moved-from object 'a'}} 735 #endif 736 } 737 738 void interFunTest2() { 739 A a; 740 A b; 741 b = std::move(a); 742 interFunTest1(a); 743 #ifndef PEACEFUL 744 // expected-note@-3 {{Object 'a' is moved}} 745 // expected-note@-3 {{Calling 'interFunTest1'}} 746 #endif 747 } 748 749 void foobar(A a, int i); 750 void foobar(int i, A a); 751 752 void paramEvaluateOrderTest() { 753 A a; 754 foobar(std::move(a), a.getI()); 755 #ifndef PEACEFUL 756 // expected-note@-2 {{Object 'a' is moved}} 757 // expected-warning@-3 {{Method called on moved-from object 'a'}} 758 // expected-note@-4 {{Method called on moved-from object 'a'}} 759 #endif 760 761 //FALSE NEGATIVE since parameters evaluate order is undefined 762 foobar(a.getI(), std::move(a)); //no-warning 763 } 764 765 void not_known_pass_by_ref(A &a); 766 void not_known_pass_by_const_ref(const A &a); 767 void not_known_pass_by_rvalue_ref(A &&a); 768 void not_known_pass_by_ptr(A *a); 769 void not_known_pass_by_const_ptr(const A *a); 770 771 void regionAndPointerEscapeTest() { 772 { 773 A a; 774 A b; 775 b = std::move(a); 776 not_known_pass_by_ref(a); 777 a.foo(); // no-warning 778 } 779 { 780 A a; 781 A b; 782 b = std::move(a); 783 not_known_pass_by_const_ref(a); 784 a.foo(); 785 #ifndef PEACEFUL 786 // expected-note@-4{{Object 'a' is moved}} 787 // expected-warning@-3{{Method called on moved-from object 'a'}} 788 // expected-note@-4 {{Method called on moved-from object 'a'}} 789 #endif 790 } 791 { 792 A a; 793 A b; 794 b = std::move(a); 795 not_known_pass_by_rvalue_ref(std::move(a)); 796 a.foo(); // no-warning 797 } 798 { 799 A a; 800 A b; 801 b = std::move(a); 802 not_known_pass_by_ptr(&a); 803 a.foo(); // no-warning 804 } 805 { 806 A a; 807 A b; 808 b = std::move(a); 809 not_known_pass_by_const_ptr(&a); 810 a.foo(); 811 #ifndef PEACEFUL 812 // expected-note@-4{{Object 'a' is moved}} 813 // expected-warning@-3{{Method called on moved-from object 'a'}} 814 // expected-note@-4 {{Method called on moved-from object 'a'}} 815 #endif 816 } 817 } 818 819 // A declaration statement containing multiple declarations sequences the 820 // initializer expressions. 821 void declarationSequenceTest() { 822 { 823 A a; 824 A a1 = a, a2 = std::move(a); // no-warning 825 } 826 { 827 A a; 828 A a1 = std::move(a), a2 = a; 829 #ifndef PEACEFUL 830 // expected-note@-2 {{Object 'a' is moved}} 831 // expected-warning@-3 {{Moved-from object 'a' is copied}} 832 // expected-note@-4 {{Moved-from object 'a' is copied}} 833 #endif 834 } 835 } 836 837 // The logical operators && and || sequence their operands. 838 void logicalOperatorsSequenceTest() { 839 { 840 A a; 841 if (a.foo() > 0 && A(std::move(a)).foo() > 0) { 842 #ifndef PEACEFUL 843 // expected-note@-2 {{Assuming the condition is false}} 844 // expected-note@-3 {{Left side of '&&' is false}} 845 // expected-note@-4 {{Taking false branch}} 846 // And the other report: 847 // expected-note@-6 {{Assuming the condition is false}} 848 // expected-note@-7 {{Left side of '&&' is false}} 849 // expected-note@-8 {{Taking false branch}} 850 A().bar(); 851 #endif 852 } 853 } 854 // A variation: Negate the result of the && (which pushes the && further down 855 // into the AST). 856 { 857 A a; 858 if (!(a.foo() > 0 && A(std::move(a)).foo() > 0)) { 859 #ifndef PEACEFUL 860 // expected-note@-2 {{Assuming the condition is false}} 861 // expected-note@-3 {{Left side of '&&' is false}} 862 // expected-note@-4 {{Taking true branch}} 863 // And the other report: 864 // expected-note@-6 {{Assuming the condition is false}} 865 // expected-note@-7 {{Left side of '&&' is false}} 866 // expected-note@-8 {{Taking true branch}} 867 #endif 868 A().bar(); 869 } 870 } 871 { 872 A a; 873 if (A(std::move(a)).foo() > 0 && a.foo() > 0) { 874 #ifndef PEACEFUL 875 // expected-note@-2 {{Object 'a' is moved}} 876 // expected-note@-3 {{Assuming the condition is true}} 877 // expected-note@-4 {{Left side of '&&' is true}} 878 // expected-warning@-5 {{Method called on moved-from object 'a'}} 879 // expected-note@-6 {{Method called on moved-from object 'a'}} 880 // And the other report: 881 // expected-note@-8 {{Assuming the condition is false}} 882 // expected-note@-9 {{Left side of '&&' is false}} 883 // expected-note@-10{{Taking false branch}} 884 #endif 885 A().bar(); 886 } 887 } 888 { 889 A a; 890 if (a.foo() > 0 || A(std::move(a)).foo() > 0) { 891 #ifndef PEACEFUL 892 // expected-note@-2 {{Assuming the condition is true}} 893 // expected-note@-3 {{Left side of '||' is true}} 894 // expected-note@-4 {{Taking true branch}} 895 #endif 896 A().bar(); 897 } 898 } 899 { 900 A a; 901 if (A(std::move(a)).foo() > 0 || a.foo() > 0) { 902 #ifndef PEACEFUL 903 // expected-note@-2 {{Object 'a' is moved}} 904 // expected-note@-3 {{Assuming the condition is false}} 905 // expected-note@-4 {{Left side of '||' is false}} 906 // expected-warning@-5 {{Method called on moved-from object 'a'}} 907 // expected-note@-6 {{Method called on moved-from object 'a'}} 908 #endif 909 A().bar(); 910 } 911 } 912 } 913 914 // A range-based for sequences the loop variable declaration before the body. 915 void forRangeSequencesTest() { 916 A v[2] = {A(), A()}; 917 for (A &a : v) { 918 A b; 919 b = std::move(a); // no-warning 920 } 921 } 922 923 // If a variable is declared in an if statement, the declaration of the variable 924 // (which is treated like a reinitialization by the check) is sequenced before 925 // the evaluation of the condition (which constitutes a use). 926 void ifStmtSequencesDeclAndConditionTest() { 927 for (int i = 0; i < 3; ++i) { 928 if (A a = A()) { 929 A b; 930 b = std::move(a); // no-warning 931 } 932 } 933 } 934 935 struct C : public A { 936 [[clang::reinitializes]] void reinit(); 937 }; 938 939 void subRegionMoveTest() { 940 { 941 A a; 942 B b = std::move(a.b); 943 a.b.foo(); 944 #ifdef AGGRESSIVE 945 // expected-note@-3{{Object 'b' is moved}} 946 // expected-warning@-3{{Method called on moved-from object 'b'}} 947 // expected-note@-4 {{Method called on moved-from object 'b'}} 948 #endif 949 } 950 { 951 A a; 952 A a1 = std::move(a); 953 a.b.foo(); 954 #ifdef AGGRESSIVE 955 // expected-note@-3{{Calling move constructor for 'A'}} 956 // expected-note@-4{{Returning from move constructor for 'A'}} 957 // expected-warning@-4{{Method called on moved-from object 'b'}} 958 // expected-note@-5{{Method called on moved-from object 'b'}} 959 #endif 960 } 961 // Don't report a misuse if any SuperRegion is already reported. 962 { 963 A a; 964 A a1 = std::move(a); 965 a.foo(); 966 #ifndef PEACEFUL 967 // expected-note@-3 {{Object 'a' is moved}} 968 // expected-warning@-3 {{Method called on moved-from object 'a'}} 969 // expected-note@-4 {{Method called on moved-from object 'a'}} 970 #endif 971 a.b.foo(); // no-warning 972 } 973 { 974 C c; 975 C c1 = std::move(c); 976 c.foo(); 977 #ifndef PEACEFUL 978 // expected-note@-3 {{Object 'c' is moved}} 979 // expected-warning@-3 {{Method called on moved-from object 'c'}} 980 // expected-note@-4 {{Method called on moved-from object 'c'}} 981 #endif 982 c.b.foo(); // no-warning 983 } 984 } 985 986 void resetSuperClass() { 987 C c; 988 C c1 = std::move(c); 989 c.clear(); 990 C c2 = c; // no-warning 991 } 992 993 void resetSuperClass2() { 994 C c; 995 C c1 = std::move(c); 996 c.reinit(); 997 C c2 = c; // no-warning 998 } 999 1000 void reportSuperClass() { 1001 C c; 1002 C c1 = std::move(c); 1003 c.foo(); 1004 #ifndef PEACEFUL 1005 // expected-note@-3 {{Object 'c' is moved}} 1006 // expected-warning@-3 {{Method called on moved-from object 'c'}} 1007 // expected-note@-4 {{Method called on moved-from object 'c'}} 1008 #endif 1009 C c2 = c; // no-warning 1010 } 1011 1012 struct Empty {}; 1013 1014 Empty inlinedCall() { 1015 // Used to warn because region 'e' failed to be cleaned up because no symbols 1016 // have ever died during the analysis and the checkDeadSymbols callback 1017 // was skipped entirely. 1018 Empty e{}; 1019 return e; // no-warning 1020 } 1021 1022 void checkInlinedCallZombies() { 1023 while (true) 1024 inlinedCall(); 1025 } 1026 1027 void checkLoopZombies() { 1028 while (true) { 1029 Empty e{}; 1030 Empty f = std::move(e); // no-warning 1031 } 1032 } 1033 1034 void checkMoreLoopZombies1(bool flag) { 1035 while (flag) { 1036 Empty e{}; 1037 if (true) 1038 e; // expected-warning {{expression result unused}} 1039 Empty f = std::move(e); // no-warning 1040 } 1041 } 1042 1043 bool coin(); 1044 1045 void checkMoreLoopZombies2(bool flag) { 1046 while (flag) { 1047 Empty e{}; 1048 while (coin()) 1049 e; // expected-warning {{expression result unused}} 1050 Empty f = std::move(e); // no-warning 1051 } 1052 } 1053 1054 void checkMoreLoopZombies3(bool flag) { 1055 while (flag) { 1056 Empty e{}; 1057 do 1058 e; // expected-warning {{expression result unused}} 1059 while (coin()); 1060 Empty f = std::move(e); // no-warning 1061 } 1062 } 1063 1064 void checkMoreLoopZombies4(bool flag) { 1065 while (flag) { 1066 Empty e{}; 1067 for (; coin();) 1068 e; // expected-warning {{expression result unused}} 1069 Empty f = std::move(e); // no-warning 1070 } 1071 } 1072 1073 struct MoveOnlyWithDestructor { 1074 MoveOnlyWithDestructor(); 1075 ~MoveOnlyWithDestructor(); 1076 MoveOnlyWithDestructor(const MoveOnlyWithDestructor &m) = delete; 1077 MoveOnlyWithDestructor(MoveOnlyWithDestructor &&m); 1078 }; 1079 1080 MoveOnlyWithDestructor foo() { 1081 MoveOnlyWithDestructor m; 1082 return m; 1083 } 1084 1085 class HasSTLField { 1086 std::vector<int> V; 1087 void testVector() { 1088 // Warn even in non-aggressive mode when it comes to STL, because 1089 // in STL the object is left in "valid but unspecified state" after move. 1090 std::vector<int> W = std::move(V); // expected-note{{Object 'V' of type 'std::vector' is left in a valid but unspecified state after move}} 1091 V.push_back(123); // expected-warning{{Method called on moved-from object 'V'}} 1092 // expected-note@-1{{Method called on moved-from object 'V'}} 1093 } 1094 1095 std::unique_ptr<int> P; 1096 void testUniquePtr() { 1097 // unique_ptr remains in a well-defined state after move. 1098 std::unique_ptr<int> Q = std::move(P); 1099 P.get(); 1100 #ifdef AGGRESSIVE 1101 // expected-warning@-2{{Method called on moved-from object 'P'}} 1102 // expected-note@-4{{Object 'P' is moved}} 1103 // expected-note@-4{{Method called on moved-from object 'P'}} 1104 #endif 1105 1106 // Because that well-defined state is null, dereference is still UB. 1107 // Note that in aggressive mode we already warned about 'P', 1108 // so no extra warning is generated. 1109 *P += 1; 1110 #ifndef AGGRESSIVE 1111 // expected-warning@-2{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}} 1112 // expected-note@-14{{Smart pointer 'P' of type 'std::unique_ptr' is reset to null when moved from}} 1113 // expected-note@-4{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}} 1114 #endif 1115 1116 // The program should have crashed by now. 1117 clang_analyzer_warnIfReached(); // no-warning 1118 } 1119 }; 1120 1121 void localRValueMove(A &&a) { 1122 A b = std::move(a); 1123 a.foo(); 1124 #ifndef PEACEFUL 1125 // expected-note@-3 {{Object 'a' is moved}} 1126 // expected-warning@-3 {{Method called on moved-from object 'a'}} 1127 // expected-note@-4 {{Method called on moved-from object 'a'}} 1128 #endif 1129 } 1130 1131 void localUniquePtr(std::unique_ptr<int> P) { 1132 // Even though unique_ptr is safe to use after move, 1133 // reusing a local variable this way usually indicates a bug. 1134 std::unique_ptr<int> Q = std::move(P); 1135 P.get(); 1136 #ifndef PEACEFUL 1137 // expected-note@-3 {{Object 'P' is moved}} 1138 // expected-warning@-3 {{Method called on moved-from object 'P'}} 1139 // expected-note@-4 {{Method called on moved-from object 'P'}} 1140 #endif 1141 } 1142 1143 void localUniquePtrWithArrow(std::unique_ptr<A> P) { 1144 std::unique_ptr<A> Q = std::move(P); // expected-note{{Smart pointer 'P' of type 'std::unique_ptr' is reset to null when moved from}} 1145 P->foo(); // expected-warning{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}} 1146 // expected-note@-1{{Dereference of null smart pointer 'P' of type 'std::unique_ptr'}} 1147 } 1148