Skip to content

Commit

Permalink
[analyzer] MisusedMovedObject: Fix state-resetting a base-class sub-o…
Browse files Browse the repository at this point in the history
…bject.

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

llvm-svn: 315301
  • Loading branch information
haoNoQ committed Oct 10, 2017
1 parent c06bb16 commit 0f22a06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -416,7 +416,14 @@ void MisusedMovedObjectChecker::checkPreCall(const CallEvent &Call,
return;

if (isStateResetMethod(MethodDecl)) {
State = State->remove<TrackedRegionMap>(ThisRegion);
// A state reset method resets the whole object, not only sub-object
// of a parent class in which it is defined.
const MemRegion *WholeObjectRegion = ThisRegion;
while (const CXXBaseObjectRegion *BR =
dyn_cast<CXXBaseObjectRegion>(WholeObjectRegion))
WholeObjectRegion = BR->getSuperRegion();

State = State->remove<TrackedRegionMap>(WholeObjectRegion);
C.addTransition(State);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Analysis/MisusedMovedObject.cpp
Expand Up @@ -617,3 +617,11 @@ void subRegionMoveTest() {
a.b.foo(); // no-warning
}
}

class C: public A {};
void resetSuperClass() {
C c;
C c1 = std::move(c);
c.clear();
C c2 = c; // no-warning
}

0 comments on commit 0f22a06

Please sign in to comment.