From: Artem Dergachev Date: Tue, 4 Dec 2018 03:38:08 +0000 (+0000) Subject: [analyzer] MoveChecker: Add more common state resetting methods. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=699a1c5dbf2b006d3c94fd1c48ecb7ff3d0024f3;p=clang [analyzer] MoveChecker: Add more common state resetting methods. Includes "resize" and "shrink" because they can reset the object to a known state in certain circumstances. Differential Revision: https://reviews.llvm.org/D54563 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@348235 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/MoveChecker.cpp b/lib/StaticAnalyzer/Checkers/MoveChecker.cpp index d9ea0d967c..c93ba9d3b4 100644 --- a/lib/StaticAnalyzer/Checkers/MoveChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MoveChecker.cpp @@ -343,8 +343,11 @@ bool MoveChecker::isStateResetMethod(const CXXMethodDecl *MethodDec) const { return true; if (MethodDec->getDeclName().isIdentifier()) { std::string MethodName = MethodDec->getName().lower(); + // TODO: Some of these methods (eg., resize) are not always resetting + // the state, so we should consider looking at the arguments. if (MethodName == "reset" || MethodName == "clear" || - MethodName == "destroy") + MethodName == "destroy" || MethodName == "resize" || + MethodName == "shrink") return true; } return false; diff --git a/test/Analysis/use-after-move.cpp b/test/Analysis/use-after-move.cpp index 0e7f5d5ce1..3134720e4d 100644 --- a/test/Analysis/use-after-move.cpp +++ b/test/Analysis/use-after-move.cpp @@ -15,6 +15,8 @@ namespace std { +typedef __typeof(sizeof(int)) size_t; + template struct remove_reference; @@ -110,6 +112,7 @@ public: void reset(); void destroy(); void clear(); + void resize(std::size_t); bool empty() const; bool isEmpty() const; operator bool() const; @@ -403,6 +406,13 @@ void moveStateResetFunctionsTest() { a.foo(); // no-warning a.b.foo(); // no-warning } + { + A a; + A b = std::move(a); + a.resize(0); // no-warning + a.foo(); // no-warning + a.b.foo(); // no-warning + } } // Moves or uses that occur as part of template arguments.