1 // MoveChecker.cpp - Check use of moved-from objects. - C++ ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This defines checker which checks for potential misuses of a moved-from
10 // object. That means method calls on the object or copying it in moved-from
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/Driver/DriverDiagnostic.h"
17 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
20 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "llvm/ADT/StringSet.h"
25 using namespace clang;
31 enum Kind { Moved, Reported } K;
32 RegionState(Kind InK) : K(InK) {}
35 bool isReported() const { return K == Reported; }
36 bool isMoved() const { return K == Moved; }
38 static RegionState getReported() { return RegionState(Reported); }
39 static RegionState getMoved() { return RegionState(Moved); }
41 bool operator==(const RegionState &X) const { return K == X.K; }
42 void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
44 } // end of anonymous namespace
48 : public Checker<check::PreCall, check::PostCall,
49 check::DeadSymbols, check::RegionChanges> {
51 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
52 void checkPreCall(const CallEvent &MC, CheckerContext &C) const;
53 void checkPostCall(const CallEvent &MC, CheckerContext &C) const;
54 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
56 checkRegionChanges(ProgramStateRef State,
57 const InvalidatedSymbols *Invalidated,
58 ArrayRef<const MemRegion *> RequestedRegions,
59 ArrayRef<const MemRegion *> InvalidatedRegions,
60 const LocationContext *LCtx, const CallEvent *Call) const;
61 void printState(raw_ostream &Out, ProgramStateRef State,
62 const char *NL, const char *Sep) const override;
65 enum MisuseKind { MK_FunCall, MK_Copy, MK_Move, MK_Dereference };
66 enum StdObjectKind { SK_NonStd, SK_Unsafe, SK_Safe, SK_SmartPtr };
68 enum AggressivenessKind { // In any case, don't warn after a reset.
70 AK_KnownsOnly = 0, // Warn only about known move-unsafe classes.
71 AK_KnownsAndLocals = 1, // Also warn about all local objects.
72 AK_All = 2, // Warn on any use-after-move.
76 static bool misuseCausesCrash(MisuseKind MK) {
77 return MK == MK_Dereference;
81 // Is this a local variable or a local rvalue reference?
83 // Is this an STL object? If so, of what kind?
84 StdObjectKind StdKind;
87 // STL smart pointers are automatically re-initialized to null when moved
88 // from. So we can't warn on many methods, but we can warn when it is
89 // dereferenced, which is UB even if the resulting lvalue never gets read.
90 const llvm::StringSet<> StdSmartPtrClasses = {
96 // Not all of these are entirely move-safe, but they do provide *some*
97 // guarantees, and it means that somebody is using them after move
99 // TODO: We can still try to identify *unsafe* use after move,
100 // like we did with smart pointers.
101 const llvm::StringSet<> StdSafeClasses = {
114 // Should we bother tracking the state of the object?
115 bool shouldBeTracked(ObjectKind OK) const {
116 // In non-aggressive mode, only warn on use-after-move of local variables
117 // (or local rvalue references) and of STL objects. The former is possible
118 // because local variables (or local rvalue references) are not tempting
119 // their user to re-use the storage. The latter is possible because STL
120 // objects are known to end up in a valid but unspecified state after the
121 // move and their state-reset methods are also known, which allows us to
122 // predict precisely when use-after-move is invalid.
123 // Some STL objects are known to conform to additional contracts after move,
124 // so they are not tracked. However, smart pointers specifically are tracked
125 // because we can perform extra checking over them.
126 // In aggressive mode, warn on any use-after-move because the user has
127 // intentionally asked us to completely eliminate use-after-move
129 return (Aggressiveness == AK_All) ||
130 (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
131 OK.StdKind == SK_Unsafe || OK.StdKind == SK_SmartPtr;
134 // Some objects only suffer from some kinds of misuses, but we need to track
135 // them anyway because we cannot know in advance what misuse will we find.
136 bool shouldWarnAbout(ObjectKind OK, MisuseKind MK) const {
137 // Additionally, only warn on smart pointers when they are dereferenced (or
138 // local or we are aggressive).
139 return shouldBeTracked(OK) &&
140 ((Aggressiveness == AK_All) ||
141 (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
142 OK.StdKind != SK_SmartPtr || MK == MK_Dereference);
145 // Obtains ObjectKind of an object. Because class declaration cannot always
146 // be easily obtained from the memory region, it is supplied separately.
147 ObjectKind classifyObject(const MemRegion *MR, const CXXRecordDecl *RD) const;
149 // Classifies the object and dumps a user-friendly description string to
151 void explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
152 const CXXRecordDecl *RD, MisuseKind MK) const;
154 bool belongsTo(const CXXRecordDecl *RD, const llvm::StringSet<> &Set) const;
156 class MovedBugVisitor : public BugReporterVisitor {
158 MovedBugVisitor(const MoveChecker &Chk, const MemRegion *R,
159 const CXXRecordDecl *RD, MisuseKind MK)
160 : Chk(Chk), Region(R), RD(RD), MK(MK), Found(false) {}
162 void Profile(llvm::FoldingSetNodeID &ID) const override {
165 ID.AddPointer(Region);
166 // Don't add RD because it's, in theory, uniquely determined by
167 // the region. In practice though, it's not always possible to obtain
168 // the declaration directly from the region, that's why we store it
169 // in the first place.
172 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
173 BugReporterContext &BRC,
174 BugReport &BR) override;
177 const MoveChecker &Chk;
178 // The tracked region.
179 const MemRegion *Region;
180 // The class of the tracked object.
181 const CXXRecordDecl *RD;
182 // How exactly the object was misused.
187 AggressivenessKind Aggressiveness;
190 void setAggressiveness(StringRef Str, CheckerManager &Mgr) {
192 llvm::StringSwitch<AggressivenessKind>(Str)
193 .Case("KnownsOnly", AK_KnownsOnly)
194 .Case("KnownsAndLocals", AK_KnownsAndLocals)
196 .Default(AK_Invalid);
198 if (Aggressiveness == AK_Invalid)
199 Mgr.reportInvalidCheckerOptionValue(this, "WarnOn",
200 "either \"KnownsOnly\", \"KnownsAndLocals\" or \"All\" string value");
204 mutable std::unique_ptr<BugType> BT;
206 // Check if the given form of potential misuse of a given object
207 // should be reported. If so, get it reported. The callback from which
208 // this function was called should immediately return after the call
209 // because this function adds one or two transitions.
210 void modelUse(ProgramStateRef State, const MemRegion *Region,
211 const CXXRecordDecl *RD, MisuseKind MK,
212 CheckerContext &C) const;
214 // Returns the exploded node against which the report was emitted.
215 // The caller *must* add any further transitions against this node.
216 ExplodedNode *reportBug(const MemRegion *Region, const CXXRecordDecl *RD,
217 CheckerContext &C, MisuseKind MK) const;
219 bool isInMoveSafeContext(const LocationContext *LC) const;
220 bool isStateResetMethod(const CXXMethodDecl *MethodDec) const;
221 bool isMoveSafeMethod(const CXXMethodDecl *MethodDec) const;
222 const ExplodedNode *getMoveLocation(const ExplodedNode *N,
223 const MemRegion *Region,
224 CheckerContext &C) const;
226 } // end anonymous namespace
228 REGISTER_MAP_WITH_PROGRAMSTATE(TrackedRegionMap, const MemRegion *, RegionState)
230 // Define the inter-checker API.
234 bool isMovedFrom(ProgramStateRef State, const MemRegion *Region) {
235 const RegionState *RS = State->get<TrackedRegionMap>(Region);
236 return RS && (RS->isMoved() || RS->isReported());
242 // If a region is removed all of the subregions needs to be removed too.
243 static ProgramStateRef removeFromState(ProgramStateRef State,
244 const MemRegion *Region) {
247 for (auto &E : State->get<TrackedRegionMap>()) {
248 if (E.first->isSubRegionOf(Region))
249 State = State->remove<TrackedRegionMap>(E.first);
254 static bool isAnyBaseRegionReported(ProgramStateRef State,
255 const MemRegion *Region) {
256 for (auto &E : State->get<TrackedRegionMap>()) {
257 if (Region->isSubRegionOf(E.first) && E.second.isReported())
263 static const MemRegion *unwrapRValueReferenceIndirection(const MemRegion *MR) {
264 if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(MR)) {
265 SymbolRef Sym = SR->getSymbol();
266 if (Sym->getType()->isRValueReferenceType())
267 if (const MemRegion *OriginMR = Sym->getOriginRegion())
273 std::shared_ptr<PathDiagnosticPiece>
274 MoveChecker::MovedBugVisitor::VisitNode(const ExplodedNode *N,
275 BugReporterContext &BRC, BugReport &BR) {
276 // We need only the last move of the reported object's region.
277 // The visitor walks the ExplodedGraph backwards.
280 ProgramStateRef State = N->getState();
281 ProgramStateRef StatePrev = N->getFirstPred()->getState();
282 const RegionState *TrackedObject = State->get<TrackedRegionMap>(Region);
283 const RegionState *TrackedObjectPrev =
284 StatePrev->get<TrackedRegionMap>(Region);
287 if (TrackedObjectPrev && TrackedObject)
290 // Retrieve the associated statement.
291 const Stmt *S = PathDiagnosticLocation::getStmt(N);
296 SmallString<128> Str;
297 llvm::raw_svector_ostream OS(Str);
299 ObjectKind OK = Chk.classifyObject(Region, RD);
300 switch (OK.StdKind) {
302 if (MK == MK_Dereference) {
303 OS << "Smart pointer";
304 Chk.explainObject(OS, Region, RD, MK);
305 OS << " is reset to null when moved from";
309 // If it's not a dereference, we don't care if it was reset to null
310 // or that it is even a smart pointer.
315 Chk.explainObject(OS, Region, RD, MK);
320 Chk.explainObject(OS, Region, RD, MK);
321 OS << " is left in a valid but unspecified state after move";
325 // Generate the extra diagnostic.
326 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
327 N->getLocationContext());
328 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
331 const ExplodedNode *MoveChecker::getMoveLocation(const ExplodedNode *N,
332 const MemRegion *Region,
333 CheckerContext &C) const {
334 // Walk the ExplodedGraph backwards and find the first node that referred to
335 // the tracked region.
336 const ExplodedNode *MoveNode = N;
339 ProgramStateRef State = N->getState();
340 if (!State->get<TrackedRegionMap>(Region))
343 N = N->pred_empty() ? nullptr : *(N->pred_begin());
348 void MoveChecker::modelUse(ProgramStateRef State, const MemRegion *Region,
349 const CXXRecordDecl *RD, MisuseKind MK,
350 CheckerContext &C) const {
351 assert(!C.isDifferent() && "No transitions should have been made by now");
352 const RegionState *RS = State->get<TrackedRegionMap>(Region);
353 ObjectKind OK = classifyObject(Region, RD);
355 // Just in case: if it's not a smart pointer but it does have operator *,
356 // we shouldn't call the bug a dereference.
357 if (MK == MK_Dereference && OK.StdKind != SK_SmartPtr)
360 if (!RS || !shouldWarnAbout(OK, MK)
361 || isInMoveSafeContext(C.getLocationContext())) {
362 // Finalize changes made by the caller.
363 C.addTransition(State);
367 // Don't report it in case if any base region is already reported.
368 // But still generate a sink in case of UB.
369 // And still finalize changes made by the caller.
370 if (isAnyBaseRegionReported(State, Region)) {
371 if (misuseCausesCrash(MK)) {
372 C.generateSink(State, C.getPredecessor());
374 C.addTransition(State);
379 ExplodedNode *N = reportBug(Region, RD, C, MK);
381 // If the program has already crashed on this path, don't bother.
385 State = State->set<TrackedRegionMap>(Region, RegionState::getReported());
386 C.addTransition(State, N);
389 ExplodedNode *MoveChecker::reportBug(const MemRegion *Region,
390 const CXXRecordDecl *RD, CheckerContext &C,
391 MisuseKind MK) const {
392 if (ExplodedNode *N = misuseCausesCrash(MK) ? C.generateErrorNode()
393 : C.generateNonFatalErrorNode()) {
396 BT.reset(new BugType(this, "Use-after-move",
397 "C++ move semantics"));
399 // Uniqueing report to the same object.
400 PathDiagnosticLocation LocUsedForUniqueing;
401 const ExplodedNode *MoveNode = getMoveLocation(N, Region, C);
403 if (const Stmt *MoveStmt = PathDiagnosticLocation::getStmt(MoveNode))
404 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
405 MoveStmt, C.getSourceManager(), MoveNode->getLocationContext());
407 // Creating the error message.
408 llvm::SmallString<128> Str;
409 llvm::raw_svector_ostream OS(Str);
412 OS << "Method called on moved-from object";
413 explainObject(OS, Region, RD, MK);
416 OS << "Moved-from object";
417 explainObject(OS, Region, RD, MK);
421 OS << "Moved-from object";
422 explainObject(OS, Region, RD, MK);
426 OS << "Dereference of null smart pointer";
427 explainObject(OS, Region, RD, MK);
432 llvm::make_unique<BugReport>(*BT, OS.str(), N, LocUsedForUniqueing,
433 MoveNode->getLocationContext()->getDecl());
434 R->addVisitor(llvm::make_unique<MovedBugVisitor>(*this, Region, RD, MK));
435 C.emitReport(std::move(R));
441 void MoveChecker::checkPostCall(const CallEvent &Call,
442 CheckerContext &C) const {
443 const auto *AFC = dyn_cast<AnyFunctionCall>(&Call);
447 ProgramStateRef State = C.getState();
448 const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(AFC->getDecl());
452 // Check if an object became moved-from.
453 // Object can become moved from after a call to move assignment operator or
454 // move constructor .
455 const auto *ConstructorDecl = dyn_cast<CXXConstructorDecl>(MethodDecl);
456 if (ConstructorDecl && !ConstructorDecl->isMoveConstructor())
459 if (!ConstructorDecl && !MethodDecl->isMoveAssignmentOperator())
462 const auto ArgRegion = AFC->getArgSVal(0).getAsRegion();
466 // Skip moving the object to itself.
467 const auto *CC = dyn_cast_or_null<CXXConstructorCall>(&Call);
468 if (CC && CC->getCXXThisVal().getAsRegion() == ArgRegion)
471 if (const auto *IC = dyn_cast<CXXInstanceCall>(AFC))
472 if (IC->getCXXThisVal().getAsRegion() == ArgRegion)
475 const MemRegion *BaseRegion = ArgRegion->getBaseRegion();
476 // Skip temp objects because of their short lifetime.
477 if (BaseRegion->getAs<CXXTempObjectRegion>() ||
478 AFC->getArgExpr(0)->isRValue())
480 // If it has already been reported do not need to modify the state.
482 if (State->get<TrackedRegionMap>(ArgRegion))
485 const CXXRecordDecl *RD = MethodDecl->getParent();
486 ObjectKind OK = classifyObject(ArgRegion, RD);
487 if (shouldBeTracked(OK)) {
488 // Mark object as moved-from.
489 State = State->set<TrackedRegionMap>(ArgRegion, RegionState::getMoved());
490 C.addTransition(State);
493 assert(!C.isDifferent() && "Should not have made transitions on this path!");
496 bool MoveChecker::isMoveSafeMethod(const CXXMethodDecl *MethodDec) const {
497 // We abandon the cases where bool/void/void* conversion happens.
498 if (const auto *ConversionDec =
499 dyn_cast_or_null<CXXConversionDecl>(MethodDec)) {
500 const Type *Tp = ConversionDec->getConversionType().getTypePtrOrNull();
503 if (Tp->isBooleanType() || Tp->isVoidType() || Tp->isVoidPointerType())
506 // Function call `empty` can be skipped.
507 return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
508 (MethodDec->getName().lower() == "empty" ||
509 MethodDec->getName().lower() == "isempty"));
512 bool MoveChecker::isStateResetMethod(const CXXMethodDecl *MethodDec) const {
515 if (MethodDec->hasAttr<ReinitializesAttr>())
517 if (MethodDec->getDeclName().isIdentifier()) {
518 std::string MethodName = MethodDec->getName().lower();
519 // TODO: Some of these methods (eg., resize) are not always resetting
520 // the state, so we should consider looking at the arguments.
521 if (MethodName == "assign" || MethodName == "clear" ||
522 MethodName == "destroy" || MethodName == "reset" ||
523 MethodName == "resize" || MethodName == "shrink")
529 // Don't report an error inside a move related operation.
530 // We assume that the programmer knows what she does.
531 bool MoveChecker::isInMoveSafeContext(const LocationContext *LC) const {
533 const auto *CtxDec = LC->getDecl();
534 auto *CtorDec = dyn_cast_or_null<CXXConstructorDecl>(CtxDec);
535 auto *DtorDec = dyn_cast_or_null<CXXDestructorDecl>(CtxDec);
536 auto *MethodDec = dyn_cast_or_null<CXXMethodDecl>(CtxDec);
537 if (DtorDec || (CtorDec && CtorDec->isCopyOrMoveConstructor()) ||
538 (MethodDec && MethodDec->isOverloadedOperator() &&
539 MethodDec->getOverloadedOperator() == OO_Equal) ||
540 isStateResetMethod(MethodDec) || isMoveSafeMethod(MethodDec))
542 } while ((LC = LC->getParent()));
546 bool MoveChecker::belongsTo(const CXXRecordDecl *RD,
547 const llvm::StringSet<> &Set) const {
548 const IdentifierInfo *II = RD->getIdentifier();
549 return II && Set.count(II->getName());
552 MoveChecker::ObjectKind
553 MoveChecker::classifyObject(const MemRegion *MR,
554 const CXXRecordDecl *RD) const {
555 // Local variables and local rvalue references are classified as "Local".
556 // For the purposes of this checker, we classify move-safe STL types
557 // as not-"STL" types, because that's how the checker treats them.
558 MR = unwrapRValueReferenceIndirection(MR);
560 MR && isa<VarRegion>(MR) && isa<StackSpaceRegion>(MR->getMemorySpace());
562 if (!RD || !RD->getDeclContext()->isStdNamespace())
563 return { IsLocal, SK_NonStd };
565 if (belongsTo(RD, StdSmartPtrClasses))
566 return { IsLocal, SK_SmartPtr };
568 if (belongsTo(RD, StdSafeClasses))
569 return { IsLocal, SK_Safe };
571 return { IsLocal, SK_Unsafe };
574 void MoveChecker::explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
575 const CXXRecordDecl *RD, MisuseKind MK) const {
576 // We may need a leading space every time we actually explain anything,
577 // and we never know if we are to explain anything until we try.
579 dyn_cast_or_null<DeclRegion>(unwrapRValueReferenceIndirection(MR))) {
580 const auto *RegionDecl = cast<NamedDecl>(DR->getDecl());
581 OS << " '" << RegionDecl->getNameAsString() << "'";
584 ObjectKind OK = classifyObject(MR, RD);
585 switch (OK.StdKind) {
590 if (MK != MK_Dereference)
593 // We only care about the type if it's a dereference.
596 OS << " of type '" << RD->getQualifiedNameAsString() << "'";
601 void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
602 ProgramStateRef State = C.getState();
604 // Remove the MemRegions from the map on which a ctor/dtor call or assignment
607 // Checking constructor calls.
608 if (const auto *CC = dyn_cast<CXXConstructorCall>(&Call)) {
609 State = removeFromState(State, CC->getCXXThisVal().getAsRegion());
610 auto CtorDec = CC->getDecl();
611 // Check for copying a moved-from object and report the bug.
612 if (CtorDec && CtorDec->isCopyOrMoveConstructor()) {
613 const MemRegion *ArgRegion = CC->getArgSVal(0).getAsRegion();
614 const CXXRecordDecl *RD = CtorDec->getParent();
615 MisuseKind MK = CtorDec->isMoveConstructor() ? MK_Move : MK_Copy;
616 modelUse(State, ArgRegion, RD, MK, C);
621 const auto IC = dyn_cast<CXXInstanceCall>(&Call);
625 // Calling a destructor on a moved object is fine.
626 if (isa<CXXDestructorCall>(IC))
629 const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion();
633 // The remaining part is check only for method call on a moved-from object.
634 const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(IC->getDecl());
638 // We want to investigate the whole object, not only sub-object of a parent
639 // class in which the encountered method defined.
640 ThisRegion = ThisRegion->getMostDerivedObjectRegion();
642 if (isStateResetMethod(MethodDecl)) {
643 State = removeFromState(State, ThisRegion);
644 C.addTransition(State);
648 if (isMoveSafeMethod(MethodDecl))
651 // Store class declaration as well, for bug reporting purposes.
652 const CXXRecordDecl *RD = MethodDecl->getParent();
654 if (MethodDecl->isOverloadedOperator()) {
655 OverloadedOperatorKind OOK = MethodDecl->getOverloadedOperator();
657 if (OOK == OO_Equal) {
658 // Remove the tracked object for every assignment operator, but report bug
659 // only for move or copy assignment's argument.
660 State = removeFromState(State, ThisRegion);
662 if (MethodDecl->isCopyAssignmentOperator() ||
663 MethodDecl->isMoveAssignmentOperator()) {
664 const MemRegion *ArgRegion = IC->getArgSVal(0).getAsRegion();
666 MethodDecl->isMoveAssignmentOperator() ? MK_Move : MK_Copy;
667 modelUse(State, ArgRegion, RD, MK, C);
670 C.addTransition(State);
674 if (OOK == OO_Star || OOK == OO_Arrow) {
675 modelUse(State, ThisRegion, RD, MK_Dereference, C);
680 modelUse(State, ThisRegion, RD, MK_FunCall, C);
683 void MoveChecker::checkDeadSymbols(SymbolReaper &SymReaper,
684 CheckerContext &C) const {
685 ProgramStateRef State = C.getState();
686 TrackedRegionMapTy TrackedRegions = State->get<TrackedRegionMap>();
687 for (TrackedRegionMapTy::value_type E : TrackedRegions) {
688 const MemRegion *Region = E.first;
689 bool IsRegDead = !SymReaper.isLiveRegion(Region);
691 // Remove the dead regions from the region map.
693 State = State->remove<TrackedRegionMap>(Region);
696 C.addTransition(State);
699 ProgramStateRef MoveChecker::checkRegionChanges(
700 ProgramStateRef State, const InvalidatedSymbols *Invalidated,
701 ArrayRef<const MemRegion *> RequestedRegions,
702 ArrayRef<const MemRegion *> InvalidatedRegions,
703 const LocationContext *LCtx, const CallEvent *Call) const {
705 // Relax invalidation upon function calls: only invalidate parameters
706 // that are passed directly via non-const pointers or non-const references
707 // or rvalue references.
708 // In case of an InstanceCall don't invalidate the this-region since
709 // it is fully handled in checkPreCall and checkPostCall.
710 const MemRegion *ThisRegion = nullptr;
711 if (const auto *IC = dyn_cast<CXXInstanceCall>(Call))
712 ThisRegion = IC->getCXXThisVal().getAsRegion();
714 // Requested ("explicit") regions are the regions passed into the call
715 // directly, but not all of them end up being invalidated.
716 // But when they do, they appear in the InvalidatedRegions array as well.
717 for (const auto *Region : RequestedRegions) {
718 if (ThisRegion != Region) {
719 if (llvm::find(InvalidatedRegions, Region) !=
720 std::end(InvalidatedRegions)) {
721 State = removeFromState(State, Region);
726 // For invalidations that aren't caused by calls, assume nothing. In
727 // particular, direct write into an object's field invalidates the status.
728 for (const auto *Region : InvalidatedRegions)
729 State = removeFromState(State, Region->getBaseRegion());
735 void MoveChecker::printState(raw_ostream &Out, ProgramStateRef State,
736 const char *NL, const char *Sep) const {
738 TrackedRegionMapTy RS = State->get<TrackedRegionMap>();
741 Out << Sep << "Moved-from objects :" << NL;
743 I.first->dumpToStream(Out);
744 if (I.second.isMoved())
747 Out << ": moved and reported";
752 void ento::registerMoveChecker(CheckerManager &mgr) {
753 MoveChecker *chk = mgr.registerChecker<MoveChecker>();
754 chk->setAggressiveness(
755 mgr.getAnalyzerOptions().getCheckerStringOption(chk, "WarnOn"), mgr);
758 bool ento::shouldRegisterMoveChecker(const LangOptions &LO) {