Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 18e9d04

Browse files
committed
[analyzer] MisusedMovedObject: Fix state-resetting a base-class sub-object.
If a method is resetting the state of an object that was moved from, it should be safe to use this object again. However if the method was defined in a parent class, but used in a child class, the reset didn't happen from the checker's perspective. Differential Revision: https://reviews.llvm.org/D31538 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315301 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c91d03c commit 18e9d04

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,14 @@ void MisusedMovedObjectChecker::checkPreCall(const CallEvent &Call,
416416
return;
417417

418418
if (isStateResetMethod(MethodDecl)) {
419-
State = State->remove<TrackedRegionMap>(ThisRegion);
419+
// A state reset method resets the whole object, not only sub-object
420+
// of a parent class in which it is defined.
421+
const MemRegion *WholeObjectRegion = ThisRegion;
422+
while (const CXXBaseObjectRegion *BR =
423+
dyn_cast<CXXBaseObjectRegion>(WholeObjectRegion))
424+
WholeObjectRegion = BR->getSuperRegion();
425+
426+
State = State->remove<TrackedRegionMap>(WholeObjectRegion);
420427
C.addTransition(State);
421428
return;
422429
}

test/Analysis/MisusedMovedObject.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,11 @@ void subRegionMoveTest() {
617617
a.b.foo(); // no-warning
618618
}
619619
}
620+
621+
class C: public A {};
622+
void resetSuperClass() {
623+
C c;
624+
C c1 = std::move(c);
625+
c.clear();
626+
C c2 = c; // no-warning
627+
}

0 commit comments

Comments
 (0)