| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.Move %s -std=c++11\ |
| 2 |
// RUN: -analyzer-output=text |
| 3 |
|
| 4 |
#include "Inputs/system-header-simulator-cxx.h" |
| 5 |
|
| 6 |
class C { |
| 7 |
int n; |
| 8 |
public: |
| 9 |
void memberFun(); |
| 10 |
}; |
| 11 |
|
| 12 |
template <class... Ts> void opaqueFreeFun(Ts...); |
| 13 |
|
| 14 |
class Foo { |
| 15 |
std::unique_ptr<C> up; |
| 16 |
void opaqueMemberFun(); |
| 17 |
|
| 18 |
void testOpaqueMemberFun() { |
| 19 |
auto tmp = std::move(up); |
| 20 |
opaqueMemberFun(); // clears region state |
| 21 |
(void)up->memberFun(); // no-warning |
| 22 |
} |
| 23 |
|
| 24 |
void testOpaqueFreeFun() { |
| 25 |
auto tmp = std::move(up); // expected-note{{Smart pointer 'up' of type 'std::unique_ptr' is reset to null when moved from}} |
| 26 |
opaqueFreeFun(); // does not clear region state |
| 27 |
(void)up->memberFun(); // expected-warning{{Dereference of null smart pointer 'up' of type 'std::unique_ptr'}} |
| 28 |
// expected-note@-1{{Dereference of null smart pointer 'up' of type 'std::unique_ptr'}} |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
void testInstanceMemberFun(C c) { |
| 33 |
auto tmp1 = std::move(c); // expected-note{{Object 'c' is moved}} |
| 34 |
auto tmp2 = std::move(c); // expected-warning{{Moved-from object 'c' is moved}} |
| 35 |
// expected-note@-1{{Moved-from object 'c' is moved}} |
| 36 |
auto tmp3 = std::move(c); |
| 37 |
c.memberFun(); // does not clear region state |
| 38 |
auto tmp4 = std::move(c); |
| 39 |
auto tmp5 = std::move(c); // no-warning |
| 40 |
} |
| 41 |
|
| 42 |
void testPassByRef(C c) { |
| 43 |
auto tmp1 = std::move(c); // expected-note{{Object 'c' is moved}} |
| 44 |
auto tmp2 = std::move(c); // expected-warning{{Moved-from object 'c' is moved}} |
| 45 |
// expected-note@-1{{Moved-from object 'c' is moved}} |
| 46 |
auto tmp3 = std::move(c); |
| 47 |
opaqueFreeFun<C&>(c); // clears region state |
| 48 |
auto tmp4 = std::move(c); // expected-note{{Object 'c' is moved}} |
| 49 |
auto tmp5 = std::move(c); // expected-warning{{Moved-from object 'c' is moved}} |
| 50 |
// expected-note@-1{{Moved-from object 'c' is moved}} |
| 51 |
} |