[clang] c74a204 - [analyzer] Fix false positive in use-after-move checker
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 9 17:32:28 PDT 2022
Author: malavikasamak
Date: 2022-08-09T17:26:30-07:00
New Revision: c74a204826da331cb18023c626b0717d5b8a927d
URL: https://github.com/llvm/llvm-project/commit/c74a204826da331cb18023c626b0717d5b8a927d
DIFF: https://github.com/llvm/llvm-project/commit/c74a204826da331cb18023c626b0717d5b8a927d.diff
LOG: [analyzer] Fix false positive in use-after-move checker
Differential Revision: https://reviews.llvm.org/D131525
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp
clang/test/Analysis/use-after-move.cpp
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp
index 3376453d59880..c8ddf3b2c14f1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp
@@ -618,10 +618,6 @@ void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
if (!IC)
return;
- // Calling a destructor on a moved object is fine.
- if (isa<CXXDestructorCall>(IC))
- return;
-
const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion();
if (!ThisRegion)
return;
@@ -631,6 +627,10 @@ void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
if (!MethodDecl)
return;
+ // Calling a destructor on a moved object is fine.
+ if (isa<CXXDestructorDecl>(MethodDecl))
+ return;
+
// We want to investigate the whole object, not only sub-object of a parent
// class in which the encountered method defined.
ThisRegion = ThisRegion->getMostDerivedObjectRegion();
diff --git a/clang/test/Analysis/use-after-move.cpp b/clang/test/Analysis/use-after-move.cpp
index ce9b26f15360e..8a8a89ba13ead 100644
--- a/clang/test/Analysis/use-after-move.cpp
+++ b/clang/test/Analysis/use-after-move.cpp
@@ -900,6 +900,28 @@ void checkMoreLoopZombies4(bool flag) {
}
}
+void checkExplicitDestructorCalls() {
+ // The below code segments invoke the destructor twice (explicit and
+ // implicit). While this is not a desired code behavior, it is
+ // not the use-after-move checker's responsibility to issue such a warning.
+ {
+ B* b = new B;
+ B a = std::move(*b);
+ b->~B(); // no-warning
+ delete b;
+ }
+ {
+ B a, b;
+ new (&a) B(reinterpret_cast<B &&>(b));
+ (&b)->~B(); // no-warning
+ }
+ {
+ B b;
+ B a = std::move(b);
+ b.~B(); // no-warning
+ }
+}
+
struct MoveOnlyWithDestructor {
MoveOnlyWithDestructor();
~MoveOnlyWithDestructor();
More information about the cfe-commits
mailing list