xref: /openbsd-src/gnu/llvm/clang/lib/StaticAnalyzer/Checkers/MoveChecker.cpp (revision ec727ea710c91afd8ce4f788c5aaa8482b7b69b2)
1e5dd7070Spatrick // MoveChecker.cpp - Check use of moved-from objects. - C++ ---------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This defines checker which checks for potential misuses of a moved-from
10e5dd7070Spatrick // object. That means method calls on the object or copying it in moved-from
11e5dd7070Spatrick // state.
12e5dd7070Spatrick //
13e5dd7070Spatrick //===----------------------------------------------------------------------===//
14e5dd7070Spatrick 
15e5dd7070Spatrick #include "clang/AST/Attr.h"
16e5dd7070Spatrick #include "clang/AST/ExprCXX.h"
17e5dd7070Spatrick #include "clang/Driver/DriverDiagnostic.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
19e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
21e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
22e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
23e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
24e5dd7070Spatrick #include "llvm/ADT/StringSet.h"
25e5dd7070Spatrick 
26e5dd7070Spatrick using namespace clang;
27e5dd7070Spatrick using namespace ento;
28e5dd7070Spatrick 
29e5dd7070Spatrick namespace {
30e5dd7070Spatrick struct RegionState {
31e5dd7070Spatrick private:
32e5dd7070Spatrick   enum Kind { Moved, Reported } K;
33e5dd7070Spatrick   RegionState(Kind InK) : K(InK) {}
34e5dd7070Spatrick 
35e5dd7070Spatrick public:
36e5dd7070Spatrick   bool isReported() const { return K == Reported; }
37e5dd7070Spatrick   bool isMoved() const { return K == Moved; }
38e5dd7070Spatrick 
39e5dd7070Spatrick   static RegionState getReported() { return RegionState(Reported); }
40e5dd7070Spatrick   static RegionState getMoved() { return RegionState(Moved); }
41e5dd7070Spatrick 
42e5dd7070Spatrick   bool operator==(const RegionState &X) const { return K == X.K; }
43e5dd7070Spatrick   void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
44e5dd7070Spatrick };
45e5dd7070Spatrick } // end of anonymous namespace
46e5dd7070Spatrick 
47e5dd7070Spatrick namespace {
48e5dd7070Spatrick class MoveChecker
49e5dd7070Spatrick     : public Checker<check::PreCall, check::PostCall,
50e5dd7070Spatrick                      check::DeadSymbols, check::RegionChanges> {
51e5dd7070Spatrick public:
52e5dd7070Spatrick   void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
53e5dd7070Spatrick   void checkPreCall(const CallEvent &MC, CheckerContext &C) const;
54e5dd7070Spatrick   void checkPostCall(const CallEvent &MC, CheckerContext &C) const;
55e5dd7070Spatrick   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
56e5dd7070Spatrick   ProgramStateRef
57e5dd7070Spatrick   checkRegionChanges(ProgramStateRef State,
58e5dd7070Spatrick                      const InvalidatedSymbols *Invalidated,
59e5dd7070Spatrick                      ArrayRef<const MemRegion *> RequestedRegions,
60e5dd7070Spatrick                      ArrayRef<const MemRegion *> InvalidatedRegions,
61e5dd7070Spatrick                      const LocationContext *LCtx, const CallEvent *Call) const;
62e5dd7070Spatrick   void printState(raw_ostream &Out, ProgramStateRef State,
63e5dd7070Spatrick                   const char *NL, const char *Sep) const override;
64e5dd7070Spatrick 
65e5dd7070Spatrick private:
66e5dd7070Spatrick   enum MisuseKind { MK_FunCall, MK_Copy, MK_Move, MK_Dereference };
67e5dd7070Spatrick   enum StdObjectKind { SK_NonStd, SK_Unsafe, SK_Safe, SK_SmartPtr };
68e5dd7070Spatrick 
69e5dd7070Spatrick   enum AggressivenessKind { // In any case, don't warn after a reset.
70e5dd7070Spatrick     AK_Invalid = -1,
71e5dd7070Spatrick     AK_KnownsOnly = 0,      // Warn only about known move-unsafe classes.
72e5dd7070Spatrick     AK_KnownsAndLocals = 1, // Also warn about all local objects.
73e5dd7070Spatrick     AK_All = 2,             // Warn on any use-after-move.
74e5dd7070Spatrick     AK_NumKinds = AK_All
75e5dd7070Spatrick   };
76e5dd7070Spatrick 
77e5dd7070Spatrick   static bool misuseCausesCrash(MisuseKind MK) {
78e5dd7070Spatrick     return MK == MK_Dereference;
79e5dd7070Spatrick   }
80e5dd7070Spatrick 
81e5dd7070Spatrick   struct ObjectKind {
82e5dd7070Spatrick     // Is this a local variable or a local rvalue reference?
83e5dd7070Spatrick     bool IsLocal;
84e5dd7070Spatrick     // Is this an STL object? If so, of what kind?
85e5dd7070Spatrick     StdObjectKind StdKind;
86e5dd7070Spatrick   };
87e5dd7070Spatrick 
88e5dd7070Spatrick   // STL smart pointers are automatically re-initialized to null when moved
89e5dd7070Spatrick   // from. So we can't warn on many methods, but we can warn when it is
90e5dd7070Spatrick   // dereferenced, which is UB even if the resulting lvalue never gets read.
91e5dd7070Spatrick   const llvm::StringSet<> StdSmartPtrClasses = {
92e5dd7070Spatrick       "shared_ptr",
93e5dd7070Spatrick       "unique_ptr",
94e5dd7070Spatrick       "weak_ptr",
95e5dd7070Spatrick   };
96e5dd7070Spatrick 
97e5dd7070Spatrick   // Not all of these are entirely move-safe, but they do provide *some*
98e5dd7070Spatrick   // guarantees, and it means that somebody is using them after move
99e5dd7070Spatrick   // in a valid manner.
100e5dd7070Spatrick   // TODO: We can still try to identify *unsafe* use after move,
101e5dd7070Spatrick   // like we did with smart pointers.
102e5dd7070Spatrick   const llvm::StringSet<> StdSafeClasses = {
103e5dd7070Spatrick       "basic_filebuf",
104e5dd7070Spatrick       "basic_ios",
105e5dd7070Spatrick       "future",
106e5dd7070Spatrick       "optional",
107e5dd7070Spatrick       "packaged_task"
108e5dd7070Spatrick       "promise",
109e5dd7070Spatrick       "shared_future",
110e5dd7070Spatrick       "shared_lock",
111e5dd7070Spatrick       "thread",
112e5dd7070Spatrick       "unique_lock",
113e5dd7070Spatrick   };
114e5dd7070Spatrick 
115e5dd7070Spatrick   // Should we bother tracking the state of the object?
116e5dd7070Spatrick   bool shouldBeTracked(ObjectKind OK) const {
117e5dd7070Spatrick     // In non-aggressive mode, only warn on use-after-move of local variables
118e5dd7070Spatrick     // (or local rvalue references) and of STL objects. The former is possible
119e5dd7070Spatrick     // because local variables (or local rvalue references) are not tempting
120e5dd7070Spatrick     // their user to re-use the storage. The latter is possible because STL
121e5dd7070Spatrick     // objects are known to end up in a valid but unspecified state after the
122e5dd7070Spatrick     // move and their state-reset methods are also known, which allows us to
123e5dd7070Spatrick     // predict precisely when use-after-move is invalid.
124e5dd7070Spatrick     // Some STL objects are known to conform to additional contracts after move,
125e5dd7070Spatrick     // so they are not tracked. However, smart pointers specifically are tracked
126e5dd7070Spatrick     // because we can perform extra checking over them.
127e5dd7070Spatrick     // In aggressive mode, warn on any use-after-move because the user has
128e5dd7070Spatrick     // intentionally asked us to completely eliminate use-after-move
129e5dd7070Spatrick     // in his code.
130e5dd7070Spatrick     return (Aggressiveness == AK_All) ||
131e5dd7070Spatrick            (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
132e5dd7070Spatrick            OK.StdKind == SK_Unsafe || OK.StdKind == SK_SmartPtr;
133e5dd7070Spatrick   }
134e5dd7070Spatrick 
135e5dd7070Spatrick   // Some objects only suffer from some kinds of misuses, but we need to track
136e5dd7070Spatrick   // them anyway because we cannot know in advance what misuse will we find.
137e5dd7070Spatrick   bool shouldWarnAbout(ObjectKind OK, MisuseKind MK) const {
138e5dd7070Spatrick     // Additionally, only warn on smart pointers when they are dereferenced (or
139e5dd7070Spatrick     // local or we are aggressive).
140e5dd7070Spatrick     return shouldBeTracked(OK) &&
141e5dd7070Spatrick            ((Aggressiveness == AK_All) ||
142e5dd7070Spatrick             (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
143e5dd7070Spatrick             OK.StdKind != SK_SmartPtr || MK == MK_Dereference);
144e5dd7070Spatrick   }
145e5dd7070Spatrick 
146e5dd7070Spatrick   // Obtains ObjectKind of an object. Because class declaration cannot always
147e5dd7070Spatrick   // be easily obtained from the memory region, it is supplied separately.
148e5dd7070Spatrick   ObjectKind classifyObject(const MemRegion *MR, const CXXRecordDecl *RD) const;
149e5dd7070Spatrick 
150e5dd7070Spatrick   // Classifies the object and dumps a user-friendly description string to
151e5dd7070Spatrick   // the stream.
152e5dd7070Spatrick   void explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
153e5dd7070Spatrick                      const CXXRecordDecl *RD, MisuseKind MK) const;
154e5dd7070Spatrick 
155e5dd7070Spatrick   bool belongsTo(const CXXRecordDecl *RD, const llvm::StringSet<> &Set) const;
156e5dd7070Spatrick 
157e5dd7070Spatrick   class MovedBugVisitor : public BugReporterVisitor {
158e5dd7070Spatrick   public:
159e5dd7070Spatrick     MovedBugVisitor(const MoveChecker &Chk, const MemRegion *R,
160e5dd7070Spatrick                     const CXXRecordDecl *RD, MisuseKind MK)
161e5dd7070Spatrick         : Chk(Chk), Region(R), RD(RD), MK(MK), Found(false) {}
162e5dd7070Spatrick 
163e5dd7070Spatrick     void Profile(llvm::FoldingSetNodeID &ID) const override {
164e5dd7070Spatrick       static int X = 0;
165e5dd7070Spatrick       ID.AddPointer(&X);
166e5dd7070Spatrick       ID.AddPointer(Region);
167e5dd7070Spatrick       // Don't add RD because it's, in theory, uniquely determined by
168e5dd7070Spatrick       // the region. In practice though, it's not always possible to obtain
169e5dd7070Spatrick       // the declaration directly from the region, that's why we store it
170e5dd7070Spatrick       // in the first place.
171e5dd7070Spatrick     }
172e5dd7070Spatrick 
173e5dd7070Spatrick     PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
174e5dd7070Spatrick                                      BugReporterContext &BRC,
175e5dd7070Spatrick                                      PathSensitiveBugReport &BR) override;
176e5dd7070Spatrick 
177e5dd7070Spatrick   private:
178e5dd7070Spatrick     const MoveChecker &Chk;
179e5dd7070Spatrick     // The tracked region.
180e5dd7070Spatrick     const MemRegion *Region;
181e5dd7070Spatrick     // The class of the tracked object.
182e5dd7070Spatrick     const CXXRecordDecl *RD;
183e5dd7070Spatrick     // How exactly the object was misused.
184e5dd7070Spatrick     const MisuseKind MK;
185e5dd7070Spatrick     bool Found;
186e5dd7070Spatrick   };
187e5dd7070Spatrick 
188e5dd7070Spatrick   AggressivenessKind Aggressiveness;
189e5dd7070Spatrick 
190e5dd7070Spatrick public:
191e5dd7070Spatrick   void setAggressiveness(StringRef Str, CheckerManager &Mgr) {
192e5dd7070Spatrick     Aggressiveness =
193e5dd7070Spatrick         llvm::StringSwitch<AggressivenessKind>(Str)
194e5dd7070Spatrick             .Case("KnownsOnly", AK_KnownsOnly)
195e5dd7070Spatrick             .Case("KnownsAndLocals", AK_KnownsAndLocals)
196e5dd7070Spatrick             .Case("All", AK_All)
197e5dd7070Spatrick             .Default(AK_Invalid);
198e5dd7070Spatrick 
199e5dd7070Spatrick     if (Aggressiveness == AK_Invalid)
200e5dd7070Spatrick       Mgr.reportInvalidCheckerOptionValue(this, "WarnOn",
201e5dd7070Spatrick           "either \"KnownsOnly\", \"KnownsAndLocals\" or \"All\" string value");
202e5dd7070Spatrick   };
203e5dd7070Spatrick 
204e5dd7070Spatrick private:
205e5dd7070Spatrick   mutable std::unique_ptr<BugType> BT;
206e5dd7070Spatrick 
207e5dd7070Spatrick   // Check if the given form of potential misuse of a given object
208e5dd7070Spatrick   // should be reported. If so, get it reported. The callback from which
209e5dd7070Spatrick   // this function was called should immediately return after the call
210e5dd7070Spatrick   // because this function adds one or two transitions.
211e5dd7070Spatrick   void modelUse(ProgramStateRef State, const MemRegion *Region,
212e5dd7070Spatrick                 const CXXRecordDecl *RD, MisuseKind MK,
213e5dd7070Spatrick                 CheckerContext &C) const;
214e5dd7070Spatrick 
215e5dd7070Spatrick   // Returns the exploded node against which the report was emitted.
216e5dd7070Spatrick   // The caller *must* add any further transitions against this node.
217e5dd7070Spatrick   ExplodedNode *reportBug(const MemRegion *Region, const CXXRecordDecl *RD,
218e5dd7070Spatrick                           CheckerContext &C, MisuseKind MK) const;
219e5dd7070Spatrick 
220e5dd7070Spatrick   bool isInMoveSafeContext(const LocationContext *LC) const;
221e5dd7070Spatrick   bool isStateResetMethod(const CXXMethodDecl *MethodDec) const;
222e5dd7070Spatrick   bool isMoveSafeMethod(const CXXMethodDecl *MethodDec) const;
223e5dd7070Spatrick   const ExplodedNode *getMoveLocation(const ExplodedNode *N,
224e5dd7070Spatrick                                       const MemRegion *Region,
225e5dd7070Spatrick                                       CheckerContext &C) const;
226e5dd7070Spatrick };
227e5dd7070Spatrick } // end anonymous namespace
228e5dd7070Spatrick 
229e5dd7070Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(TrackedRegionMap, const MemRegion *, RegionState)
230e5dd7070Spatrick 
231e5dd7070Spatrick // Define the inter-checker API.
232e5dd7070Spatrick namespace clang {
233e5dd7070Spatrick namespace ento {
234e5dd7070Spatrick namespace move {
235e5dd7070Spatrick bool isMovedFrom(ProgramStateRef State, const MemRegion *Region) {
236e5dd7070Spatrick   const RegionState *RS = State->get<TrackedRegionMap>(Region);
237e5dd7070Spatrick   return RS && (RS->isMoved() || RS->isReported());
238e5dd7070Spatrick }
239e5dd7070Spatrick } // namespace move
240e5dd7070Spatrick } // namespace ento
241e5dd7070Spatrick } // namespace clang
242e5dd7070Spatrick 
243e5dd7070Spatrick // If a region is removed all of the subregions needs to be removed too.
244e5dd7070Spatrick static ProgramStateRef removeFromState(ProgramStateRef State,
245e5dd7070Spatrick                                        const MemRegion *Region) {
246e5dd7070Spatrick   if (!Region)
247e5dd7070Spatrick     return State;
248e5dd7070Spatrick   for (auto &E : State->get<TrackedRegionMap>()) {
249e5dd7070Spatrick     if (E.first->isSubRegionOf(Region))
250e5dd7070Spatrick       State = State->remove<TrackedRegionMap>(E.first);
251e5dd7070Spatrick   }
252e5dd7070Spatrick   return State;
253e5dd7070Spatrick }
254e5dd7070Spatrick 
255e5dd7070Spatrick static bool isAnyBaseRegionReported(ProgramStateRef State,
256e5dd7070Spatrick                                     const MemRegion *Region) {
257e5dd7070Spatrick   for (auto &E : State->get<TrackedRegionMap>()) {
258e5dd7070Spatrick     if (Region->isSubRegionOf(E.first) && E.second.isReported())
259e5dd7070Spatrick       return true;
260e5dd7070Spatrick   }
261e5dd7070Spatrick   return false;
262e5dd7070Spatrick }
263e5dd7070Spatrick 
264e5dd7070Spatrick static const MemRegion *unwrapRValueReferenceIndirection(const MemRegion *MR) {
265e5dd7070Spatrick   if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(MR)) {
266e5dd7070Spatrick     SymbolRef Sym = SR->getSymbol();
267e5dd7070Spatrick     if (Sym->getType()->isRValueReferenceType())
268e5dd7070Spatrick       if (const MemRegion *OriginMR = Sym->getOriginRegion())
269e5dd7070Spatrick         return OriginMR;
270e5dd7070Spatrick   }
271e5dd7070Spatrick   return MR;
272e5dd7070Spatrick }
273e5dd7070Spatrick 
274e5dd7070Spatrick PathDiagnosticPieceRef
275e5dd7070Spatrick MoveChecker::MovedBugVisitor::VisitNode(const ExplodedNode *N,
276e5dd7070Spatrick                                         BugReporterContext &BRC,
277e5dd7070Spatrick                                         PathSensitiveBugReport &BR) {
278e5dd7070Spatrick   // We need only the last move of the reported object's region.
279e5dd7070Spatrick   // The visitor walks the ExplodedGraph backwards.
280e5dd7070Spatrick   if (Found)
281e5dd7070Spatrick     return nullptr;
282e5dd7070Spatrick   ProgramStateRef State = N->getState();
283e5dd7070Spatrick   ProgramStateRef StatePrev = N->getFirstPred()->getState();
284e5dd7070Spatrick   const RegionState *TrackedObject = State->get<TrackedRegionMap>(Region);
285e5dd7070Spatrick   const RegionState *TrackedObjectPrev =
286e5dd7070Spatrick       StatePrev->get<TrackedRegionMap>(Region);
287e5dd7070Spatrick   if (!TrackedObject)
288e5dd7070Spatrick     return nullptr;
289e5dd7070Spatrick   if (TrackedObjectPrev && TrackedObject)
290e5dd7070Spatrick     return nullptr;
291e5dd7070Spatrick 
292e5dd7070Spatrick   // Retrieve the associated statement.
293e5dd7070Spatrick   const Stmt *S = N->getStmtForDiagnostics();
294e5dd7070Spatrick   if (!S)
295e5dd7070Spatrick     return nullptr;
296e5dd7070Spatrick   Found = true;
297e5dd7070Spatrick 
298e5dd7070Spatrick   SmallString<128> Str;
299e5dd7070Spatrick   llvm::raw_svector_ostream OS(Str);
300e5dd7070Spatrick 
301e5dd7070Spatrick   ObjectKind OK = Chk.classifyObject(Region, RD);
302e5dd7070Spatrick   switch (OK.StdKind) {
303e5dd7070Spatrick     case SK_SmartPtr:
304e5dd7070Spatrick       if (MK == MK_Dereference) {
305e5dd7070Spatrick         OS << "Smart pointer";
306e5dd7070Spatrick         Chk.explainObject(OS, Region, RD, MK);
307e5dd7070Spatrick         OS << " is reset to null when moved from";
308e5dd7070Spatrick         break;
309e5dd7070Spatrick       }
310e5dd7070Spatrick 
311e5dd7070Spatrick       // If it's not a dereference, we don't care if it was reset to null
312e5dd7070Spatrick       // or that it is even a smart pointer.
313e5dd7070Spatrick       LLVM_FALLTHROUGH;
314e5dd7070Spatrick     case SK_NonStd:
315e5dd7070Spatrick     case SK_Safe:
316e5dd7070Spatrick       OS << "Object";
317e5dd7070Spatrick       Chk.explainObject(OS, Region, RD, MK);
318e5dd7070Spatrick       OS << " is moved";
319e5dd7070Spatrick       break;
320e5dd7070Spatrick     case SK_Unsafe:
321e5dd7070Spatrick       OS << "Object";
322e5dd7070Spatrick       Chk.explainObject(OS, Region, RD, MK);
323e5dd7070Spatrick       OS << " is left in a valid but unspecified state after move";
324e5dd7070Spatrick       break;
325e5dd7070Spatrick   }
326e5dd7070Spatrick 
327e5dd7070Spatrick   // Generate the extra diagnostic.
328e5dd7070Spatrick   PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
329e5dd7070Spatrick                              N->getLocationContext());
330e5dd7070Spatrick   return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
331e5dd7070Spatrick }
332e5dd7070Spatrick 
333e5dd7070Spatrick const ExplodedNode *MoveChecker::getMoveLocation(const ExplodedNode *N,
334e5dd7070Spatrick                                                  const MemRegion *Region,
335e5dd7070Spatrick                                                  CheckerContext &C) const {
336e5dd7070Spatrick   // Walk the ExplodedGraph backwards and find the first node that referred to
337e5dd7070Spatrick   // the tracked region.
338e5dd7070Spatrick   const ExplodedNode *MoveNode = N;
339e5dd7070Spatrick 
340e5dd7070Spatrick   while (N) {
341e5dd7070Spatrick     ProgramStateRef State = N->getState();
342e5dd7070Spatrick     if (!State->get<TrackedRegionMap>(Region))
343e5dd7070Spatrick       break;
344e5dd7070Spatrick     MoveNode = N;
345e5dd7070Spatrick     N = N->pred_empty() ? nullptr : *(N->pred_begin());
346e5dd7070Spatrick   }
347e5dd7070Spatrick   return MoveNode;
348e5dd7070Spatrick }
349e5dd7070Spatrick 
350e5dd7070Spatrick void MoveChecker::modelUse(ProgramStateRef State, const MemRegion *Region,
351e5dd7070Spatrick                            const CXXRecordDecl *RD, MisuseKind MK,
352e5dd7070Spatrick                            CheckerContext &C) const {
353e5dd7070Spatrick   assert(!C.isDifferent() && "No transitions should have been made by now");
354e5dd7070Spatrick   const RegionState *RS = State->get<TrackedRegionMap>(Region);
355e5dd7070Spatrick   ObjectKind OK = classifyObject(Region, RD);
356e5dd7070Spatrick 
357e5dd7070Spatrick   // Just in case: if it's not a smart pointer but it does have operator *,
358e5dd7070Spatrick   // we shouldn't call the bug a dereference.
359e5dd7070Spatrick   if (MK == MK_Dereference && OK.StdKind != SK_SmartPtr)
360e5dd7070Spatrick     MK = MK_FunCall;
361e5dd7070Spatrick 
362e5dd7070Spatrick   if (!RS || !shouldWarnAbout(OK, MK)
363e5dd7070Spatrick           || isInMoveSafeContext(C.getLocationContext())) {
364e5dd7070Spatrick     // Finalize changes made by the caller.
365e5dd7070Spatrick     C.addTransition(State);
366e5dd7070Spatrick     return;
367e5dd7070Spatrick   }
368e5dd7070Spatrick 
369e5dd7070Spatrick   // Don't report it in case if any base region is already reported.
370e5dd7070Spatrick   // But still generate a sink in case of UB.
371e5dd7070Spatrick   // And still finalize changes made by the caller.
372e5dd7070Spatrick   if (isAnyBaseRegionReported(State, Region)) {
373e5dd7070Spatrick     if (misuseCausesCrash(MK)) {
374e5dd7070Spatrick       C.generateSink(State, C.getPredecessor());
375e5dd7070Spatrick     } else {
376e5dd7070Spatrick       C.addTransition(State);
377e5dd7070Spatrick     }
378e5dd7070Spatrick     return;
379e5dd7070Spatrick   }
380e5dd7070Spatrick 
381e5dd7070Spatrick   ExplodedNode *N = reportBug(Region, RD, C, MK);
382e5dd7070Spatrick 
383e5dd7070Spatrick   // If the program has already crashed on this path, don't bother.
384e5dd7070Spatrick   if (N->isSink())
385e5dd7070Spatrick     return;
386e5dd7070Spatrick 
387e5dd7070Spatrick   State = State->set<TrackedRegionMap>(Region, RegionState::getReported());
388e5dd7070Spatrick   C.addTransition(State, N);
389e5dd7070Spatrick }
390e5dd7070Spatrick 
391e5dd7070Spatrick ExplodedNode *MoveChecker::reportBug(const MemRegion *Region,
392e5dd7070Spatrick                                      const CXXRecordDecl *RD, CheckerContext &C,
393e5dd7070Spatrick                                      MisuseKind MK) const {
394e5dd7070Spatrick   if (ExplodedNode *N = misuseCausesCrash(MK) ? C.generateErrorNode()
395e5dd7070Spatrick                                               : C.generateNonFatalErrorNode()) {
396e5dd7070Spatrick 
397e5dd7070Spatrick     if (!BT)
398e5dd7070Spatrick       BT.reset(new BugType(this, "Use-after-move",
399e5dd7070Spatrick                            "C++ move semantics"));
400e5dd7070Spatrick 
401e5dd7070Spatrick     // Uniqueing report to the same object.
402e5dd7070Spatrick     PathDiagnosticLocation LocUsedForUniqueing;
403e5dd7070Spatrick     const ExplodedNode *MoveNode = getMoveLocation(N, Region, C);
404e5dd7070Spatrick 
405e5dd7070Spatrick     if (const Stmt *MoveStmt = MoveNode->getStmtForDiagnostics())
406e5dd7070Spatrick       LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
407e5dd7070Spatrick           MoveStmt, C.getSourceManager(), MoveNode->getLocationContext());
408e5dd7070Spatrick 
409e5dd7070Spatrick     // Creating the error message.
410e5dd7070Spatrick     llvm::SmallString<128> Str;
411e5dd7070Spatrick     llvm::raw_svector_ostream OS(Str);
412e5dd7070Spatrick     switch(MK) {
413e5dd7070Spatrick       case MK_FunCall:
414e5dd7070Spatrick         OS << "Method called on moved-from object";
415e5dd7070Spatrick         explainObject(OS, Region, RD, MK);
416e5dd7070Spatrick         break;
417e5dd7070Spatrick       case MK_Copy:
418e5dd7070Spatrick         OS << "Moved-from object";
419e5dd7070Spatrick         explainObject(OS, Region, RD, MK);
420e5dd7070Spatrick         OS << " is copied";
421e5dd7070Spatrick         break;
422e5dd7070Spatrick       case MK_Move:
423e5dd7070Spatrick         OS << "Moved-from object";
424e5dd7070Spatrick         explainObject(OS, Region, RD, MK);
425e5dd7070Spatrick         OS << " is moved";
426e5dd7070Spatrick         break;
427e5dd7070Spatrick       case MK_Dereference:
428e5dd7070Spatrick         OS << "Dereference of null smart pointer";
429e5dd7070Spatrick         explainObject(OS, Region, RD, MK);
430e5dd7070Spatrick         break;
431e5dd7070Spatrick     }
432e5dd7070Spatrick 
433e5dd7070Spatrick     auto R = std::make_unique<PathSensitiveBugReport>(
434e5dd7070Spatrick         *BT, OS.str(), N, LocUsedForUniqueing,
435e5dd7070Spatrick         MoveNode->getLocationContext()->getDecl());
436e5dd7070Spatrick     R->addVisitor(std::make_unique<MovedBugVisitor>(*this, Region, RD, MK));
437e5dd7070Spatrick     C.emitReport(std::move(R));
438e5dd7070Spatrick     return N;
439e5dd7070Spatrick   }
440e5dd7070Spatrick   return nullptr;
441e5dd7070Spatrick }
442e5dd7070Spatrick 
443e5dd7070Spatrick void MoveChecker::checkPostCall(const CallEvent &Call,
444e5dd7070Spatrick                                 CheckerContext &C) const {
445e5dd7070Spatrick   const auto *AFC = dyn_cast<AnyFunctionCall>(&Call);
446e5dd7070Spatrick   if (!AFC)
447e5dd7070Spatrick     return;
448e5dd7070Spatrick 
449e5dd7070Spatrick   ProgramStateRef State = C.getState();
450e5dd7070Spatrick   const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(AFC->getDecl());
451e5dd7070Spatrick   if (!MethodDecl)
452e5dd7070Spatrick     return;
453e5dd7070Spatrick 
454e5dd7070Spatrick   // Check if an object became moved-from.
455e5dd7070Spatrick   // Object can become moved from after a call to move assignment operator or
456e5dd7070Spatrick   // move constructor .
457e5dd7070Spatrick   const auto *ConstructorDecl = dyn_cast<CXXConstructorDecl>(MethodDecl);
458e5dd7070Spatrick   if (ConstructorDecl && !ConstructorDecl->isMoveConstructor())
459e5dd7070Spatrick     return;
460e5dd7070Spatrick 
461e5dd7070Spatrick   if (!ConstructorDecl && !MethodDecl->isMoveAssignmentOperator())
462e5dd7070Spatrick     return;
463e5dd7070Spatrick 
464e5dd7070Spatrick   const auto ArgRegion = AFC->getArgSVal(0).getAsRegion();
465e5dd7070Spatrick   if (!ArgRegion)
466e5dd7070Spatrick     return;
467e5dd7070Spatrick 
468e5dd7070Spatrick   // Skip moving the object to itself.
469e5dd7070Spatrick   const auto *CC = dyn_cast_or_null<CXXConstructorCall>(&Call);
470e5dd7070Spatrick   if (CC && CC->getCXXThisVal().getAsRegion() == ArgRegion)
471e5dd7070Spatrick     return;
472e5dd7070Spatrick 
473e5dd7070Spatrick   if (const auto *IC = dyn_cast<CXXInstanceCall>(AFC))
474e5dd7070Spatrick     if (IC->getCXXThisVal().getAsRegion() == ArgRegion)
475e5dd7070Spatrick       return;
476e5dd7070Spatrick 
477e5dd7070Spatrick   const MemRegion *BaseRegion = ArgRegion->getBaseRegion();
478e5dd7070Spatrick   // Skip temp objects because of their short lifetime.
479e5dd7070Spatrick   if (BaseRegion->getAs<CXXTempObjectRegion>() ||
480e5dd7070Spatrick       AFC->getArgExpr(0)->isRValue())
481e5dd7070Spatrick     return;
482e5dd7070Spatrick   // If it has already been reported do not need to modify the state.
483e5dd7070Spatrick 
484e5dd7070Spatrick   if (State->get<TrackedRegionMap>(ArgRegion))
485e5dd7070Spatrick     return;
486e5dd7070Spatrick 
487e5dd7070Spatrick   const CXXRecordDecl *RD = MethodDecl->getParent();
488e5dd7070Spatrick   ObjectKind OK = classifyObject(ArgRegion, RD);
489e5dd7070Spatrick   if (shouldBeTracked(OK)) {
490e5dd7070Spatrick     // Mark object as moved-from.
491e5dd7070Spatrick     State = State->set<TrackedRegionMap>(ArgRegion, RegionState::getMoved());
492e5dd7070Spatrick     C.addTransition(State);
493e5dd7070Spatrick     return;
494e5dd7070Spatrick   }
495e5dd7070Spatrick   assert(!C.isDifferent() && "Should not have made transitions on this path!");
496e5dd7070Spatrick }
497e5dd7070Spatrick 
498e5dd7070Spatrick bool MoveChecker::isMoveSafeMethod(const CXXMethodDecl *MethodDec) const {
499e5dd7070Spatrick   // We abandon the cases where bool/void/void* conversion happens.
500e5dd7070Spatrick   if (const auto *ConversionDec =
501e5dd7070Spatrick           dyn_cast_or_null<CXXConversionDecl>(MethodDec)) {
502e5dd7070Spatrick     const Type *Tp = ConversionDec->getConversionType().getTypePtrOrNull();
503e5dd7070Spatrick     if (!Tp)
504e5dd7070Spatrick       return false;
505e5dd7070Spatrick     if (Tp->isBooleanType() || Tp->isVoidType() || Tp->isVoidPointerType())
506e5dd7070Spatrick       return true;
507e5dd7070Spatrick   }
508e5dd7070Spatrick   // Function call `empty` can be skipped.
509e5dd7070Spatrick   return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
510e5dd7070Spatrick       (MethodDec->getName().lower() == "empty" ||
511e5dd7070Spatrick        MethodDec->getName().lower() == "isempty"));
512e5dd7070Spatrick }
513e5dd7070Spatrick 
514e5dd7070Spatrick bool MoveChecker::isStateResetMethod(const CXXMethodDecl *MethodDec) const {
515e5dd7070Spatrick   if (!MethodDec)
516e5dd7070Spatrick       return false;
517e5dd7070Spatrick   if (MethodDec->hasAttr<ReinitializesAttr>())
518e5dd7070Spatrick       return true;
519e5dd7070Spatrick   if (MethodDec->getDeclName().isIdentifier()) {
520e5dd7070Spatrick     std::string MethodName = MethodDec->getName().lower();
521e5dd7070Spatrick     // TODO: Some of these methods (eg., resize) are not always resetting
522e5dd7070Spatrick     // the state, so we should consider looking at the arguments.
523e5dd7070Spatrick     if (MethodName == "assign" || MethodName == "clear" ||
524e5dd7070Spatrick         MethodName == "destroy" || MethodName == "reset" ||
525e5dd7070Spatrick         MethodName == "resize" || MethodName == "shrink")
526e5dd7070Spatrick       return true;
527e5dd7070Spatrick   }
528e5dd7070Spatrick   return false;
529e5dd7070Spatrick }
530e5dd7070Spatrick 
531e5dd7070Spatrick // Don't report an error inside a move related operation.
532e5dd7070Spatrick // We assume that the programmer knows what she does.
533e5dd7070Spatrick bool MoveChecker::isInMoveSafeContext(const LocationContext *LC) const {
534e5dd7070Spatrick   do {
535e5dd7070Spatrick     const auto *CtxDec = LC->getDecl();
536e5dd7070Spatrick     auto *CtorDec = dyn_cast_or_null<CXXConstructorDecl>(CtxDec);
537e5dd7070Spatrick     auto *DtorDec = dyn_cast_or_null<CXXDestructorDecl>(CtxDec);
538e5dd7070Spatrick     auto *MethodDec = dyn_cast_or_null<CXXMethodDecl>(CtxDec);
539e5dd7070Spatrick     if (DtorDec || (CtorDec && CtorDec->isCopyOrMoveConstructor()) ||
540e5dd7070Spatrick         (MethodDec && MethodDec->isOverloadedOperator() &&
541e5dd7070Spatrick          MethodDec->getOverloadedOperator() == OO_Equal) ||
542e5dd7070Spatrick         isStateResetMethod(MethodDec) || isMoveSafeMethod(MethodDec))
543e5dd7070Spatrick       return true;
544e5dd7070Spatrick   } while ((LC = LC->getParent()));
545e5dd7070Spatrick   return false;
546e5dd7070Spatrick }
547e5dd7070Spatrick 
548e5dd7070Spatrick bool MoveChecker::belongsTo(const CXXRecordDecl *RD,
549e5dd7070Spatrick                             const llvm::StringSet<> &Set) const {
550e5dd7070Spatrick   const IdentifierInfo *II = RD->getIdentifier();
551e5dd7070Spatrick   return II && Set.count(II->getName());
552e5dd7070Spatrick }
553e5dd7070Spatrick 
554e5dd7070Spatrick MoveChecker::ObjectKind
555e5dd7070Spatrick MoveChecker::classifyObject(const MemRegion *MR,
556e5dd7070Spatrick                             const CXXRecordDecl *RD) const {
557e5dd7070Spatrick   // Local variables and local rvalue references are classified as "Local".
558e5dd7070Spatrick   // For the purposes of this checker, we classify move-safe STL types
559e5dd7070Spatrick   // as not-"STL" types, because that's how the checker treats them.
560e5dd7070Spatrick   MR = unwrapRValueReferenceIndirection(MR);
561e5dd7070Spatrick   bool IsLocal =
562e5dd7070Spatrick       MR && isa<VarRegion>(MR) && isa<StackSpaceRegion>(MR->getMemorySpace());
563e5dd7070Spatrick 
564e5dd7070Spatrick   if (!RD || !RD->getDeclContext()->isStdNamespace())
565e5dd7070Spatrick     return { IsLocal, SK_NonStd };
566e5dd7070Spatrick 
567e5dd7070Spatrick   if (belongsTo(RD, StdSmartPtrClasses))
568e5dd7070Spatrick     return { IsLocal, SK_SmartPtr };
569e5dd7070Spatrick 
570e5dd7070Spatrick   if (belongsTo(RD, StdSafeClasses))
571e5dd7070Spatrick     return { IsLocal, SK_Safe };
572e5dd7070Spatrick 
573e5dd7070Spatrick   return { IsLocal, SK_Unsafe };
574e5dd7070Spatrick }
575e5dd7070Spatrick 
576e5dd7070Spatrick void MoveChecker::explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
577e5dd7070Spatrick                                 const CXXRecordDecl *RD, MisuseKind MK) const {
578e5dd7070Spatrick   // We may need a leading space every time we actually explain anything,
579e5dd7070Spatrick   // and we never know if we are to explain anything until we try.
580e5dd7070Spatrick   if (const auto DR =
581e5dd7070Spatrick           dyn_cast_or_null<DeclRegion>(unwrapRValueReferenceIndirection(MR))) {
582e5dd7070Spatrick     const auto *RegionDecl = cast<NamedDecl>(DR->getDecl());
583e5dd7070Spatrick     OS << " '" << RegionDecl->getNameAsString() << "'";
584e5dd7070Spatrick   }
585e5dd7070Spatrick 
586e5dd7070Spatrick   ObjectKind OK = classifyObject(MR, RD);
587e5dd7070Spatrick   switch (OK.StdKind) {
588e5dd7070Spatrick     case SK_NonStd:
589e5dd7070Spatrick     case SK_Safe:
590e5dd7070Spatrick       break;
591e5dd7070Spatrick     case SK_SmartPtr:
592e5dd7070Spatrick       if (MK != MK_Dereference)
593e5dd7070Spatrick         break;
594e5dd7070Spatrick 
595e5dd7070Spatrick       // We only care about the type if it's a dereference.
596e5dd7070Spatrick       LLVM_FALLTHROUGH;
597e5dd7070Spatrick     case SK_Unsafe:
598e5dd7070Spatrick       OS << " of type '" << RD->getQualifiedNameAsString() << "'";
599e5dd7070Spatrick       break;
600e5dd7070Spatrick   };
601e5dd7070Spatrick }
602e5dd7070Spatrick 
603e5dd7070Spatrick void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
604e5dd7070Spatrick   ProgramStateRef State = C.getState();
605e5dd7070Spatrick 
606e5dd7070Spatrick   // Remove the MemRegions from the map on which a ctor/dtor call or assignment
607e5dd7070Spatrick   // happened.
608e5dd7070Spatrick 
609e5dd7070Spatrick   // Checking constructor calls.
610e5dd7070Spatrick   if (const auto *CC = dyn_cast<CXXConstructorCall>(&Call)) {
611e5dd7070Spatrick     State = removeFromState(State, CC->getCXXThisVal().getAsRegion());
612e5dd7070Spatrick     auto CtorDec = CC->getDecl();
613e5dd7070Spatrick     // Check for copying a moved-from object and report the bug.
614e5dd7070Spatrick     if (CtorDec && CtorDec->isCopyOrMoveConstructor()) {
615e5dd7070Spatrick       const MemRegion *ArgRegion = CC->getArgSVal(0).getAsRegion();
616e5dd7070Spatrick       const CXXRecordDecl *RD = CtorDec->getParent();
617e5dd7070Spatrick       MisuseKind MK = CtorDec->isMoveConstructor() ? MK_Move : MK_Copy;
618e5dd7070Spatrick       modelUse(State, ArgRegion, RD, MK, C);
619e5dd7070Spatrick       return;
620e5dd7070Spatrick     }
621e5dd7070Spatrick   }
622e5dd7070Spatrick 
623e5dd7070Spatrick   const auto IC = dyn_cast<CXXInstanceCall>(&Call);
624e5dd7070Spatrick   if (!IC)
625e5dd7070Spatrick     return;
626e5dd7070Spatrick 
627e5dd7070Spatrick   // Calling a destructor on a moved object is fine.
628e5dd7070Spatrick   if (isa<CXXDestructorCall>(IC))
629e5dd7070Spatrick     return;
630e5dd7070Spatrick 
631e5dd7070Spatrick   const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion();
632e5dd7070Spatrick   if (!ThisRegion)
633e5dd7070Spatrick     return;
634e5dd7070Spatrick 
635e5dd7070Spatrick   // The remaining part is check only for method call on a moved-from object.
636e5dd7070Spatrick   const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(IC->getDecl());
637e5dd7070Spatrick   if (!MethodDecl)
638e5dd7070Spatrick     return;
639e5dd7070Spatrick 
640e5dd7070Spatrick   // We want to investigate the whole object, not only sub-object of a parent
641e5dd7070Spatrick   // class in which the encountered method defined.
642e5dd7070Spatrick   ThisRegion = ThisRegion->getMostDerivedObjectRegion();
643e5dd7070Spatrick 
644e5dd7070Spatrick   if (isStateResetMethod(MethodDecl)) {
645e5dd7070Spatrick     State = removeFromState(State, ThisRegion);
646e5dd7070Spatrick     C.addTransition(State);
647e5dd7070Spatrick     return;
648e5dd7070Spatrick   }
649e5dd7070Spatrick 
650e5dd7070Spatrick   if (isMoveSafeMethod(MethodDecl))
651e5dd7070Spatrick     return;
652e5dd7070Spatrick 
653e5dd7070Spatrick   // Store class declaration as well, for bug reporting purposes.
654e5dd7070Spatrick   const CXXRecordDecl *RD = MethodDecl->getParent();
655e5dd7070Spatrick 
656e5dd7070Spatrick   if (MethodDecl->isOverloadedOperator()) {
657e5dd7070Spatrick     OverloadedOperatorKind OOK = MethodDecl->getOverloadedOperator();
658e5dd7070Spatrick 
659e5dd7070Spatrick     if (OOK == OO_Equal) {
660e5dd7070Spatrick       // Remove the tracked object for every assignment operator, but report bug
661e5dd7070Spatrick       // only for move or copy assignment's argument.
662e5dd7070Spatrick       State = removeFromState(State, ThisRegion);
663e5dd7070Spatrick 
664e5dd7070Spatrick       if (MethodDecl->isCopyAssignmentOperator() ||
665e5dd7070Spatrick           MethodDecl->isMoveAssignmentOperator()) {
666e5dd7070Spatrick         const MemRegion *ArgRegion = IC->getArgSVal(0).getAsRegion();
667e5dd7070Spatrick         MisuseKind MK =
668e5dd7070Spatrick             MethodDecl->isMoveAssignmentOperator() ? MK_Move : MK_Copy;
669e5dd7070Spatrick         modelUse(State, ArgRegion, RD, MK, C);
670e5dd7070Spatrick         return;
671e5dd7070Spatrick       }
672e5dd7070Spatrick       C.addTransition(State);
673e5dd7070Spatrick       return;
674e5dd7070Spatrick     }
675e5dd7070Spatrick 
676e5dd7070Spatrick     if (OOK == OO_Star || OOK == OO_Arrow) {
677e5dd7070Spatrick       modelUse(State, ThisRegion, RD, MK_Dereference, C);
678e5dd7070Spatrick       return;
679e5dd7070Spatrick     }
680e5dd7070Spatrick   }
681e5dd7070Spatrick 
682e5dd7070Spatrick   modelUse(State, ThisRegion, RD, MK_FunCall, C);
683e5dd7070Spatrick }
684e5dd7070Spatrick 
685e5dd7070Spatrick void MoveChecker::checkDeadSymbols(SymbolReaper &SymReaper,
686e5dd7070Spatrick                                    CheckerContext &C) const {
687e5dd7070Spatrick   ProgramStateRef State = C.getState();
688e5dd7070Spatrick   TrackedRegionMapTy TrackedRegions = State->get<TrackedRegionMap>();
689e5dd7070Spatrick   for (auto E : TrackedRegions) {
690e5dd7070Spatrick     const MemRegion *Region = E.first;
691e5dd7070Spatrick     bool IsRegDead = !SymReaper.isLiveRegion(Region);
692e5dd7070Spatrick 
693e5dd7070Spatrick     // Remove the dead regions from the region map.
694e5dd7070Spatrick     if (IsRegDead) {
695e5dd7070Spatrick       State = State->remove<TrackedRegionMap>(Region);
696e5dd7070Spatrick     }
697e5dd7070Spatrick   }
698e5dd7070Spatrick   C.addTransition(State);
699e5dd7070Spatrick }
700e5dd7070Spatrick 
701e5dd7070Spatrick ProgramStateRef MoveChecker::checkRegionChanges(
702e5dd7070Spatrick     ProgramStateRef State, const InvalidatedSymbols *Invalidated,
703e5dd7070Spatrick     ArrayRef<const MemRegion *> RequestedRegions,
704e5dd7070Spatrick     ArrayRef<const MemRegion *> InvalidatedRegions,
705e5dd7070Spatrick     const LocationContext *LCtx, const CallEvent *Call) const {
706e5dd7070Spatrick   if (Call) {
707e5dd7070Spatrick     // Relax invalidation upon function calls: only invalidate parameters
708e5dd7070Spatrick     // that are passed directly via non-const pointers or non-const references
709e5dd7070Spatrick     // or rvalue references.
710e5dd7070Spatrick     // In case of an InstanceCall don't invalidate the this-region since
711e5dd7070Spatrick     // it is fully handled in checkPreCall and checkPostCall.
712e5dd7070Spatrick     const MemRegion *ThisRegion = nullptr;
713e5dd7070Spatrick     if (const auto *IC = dyn_cast<CXXInstanceCall>(Call))
714e5dd7070Spatrick       ThisRegion = IC->getCXXThisVal().getAsRegion();
715e5dd7070Spatrick 
716e5dd7070Spatrick     // Requested ("explicit") regions are the regions passed into the call
717e5dd7070Spatrick     // directly, but not all of them end up being invalidated.
718e5dd7070Spatrick     // But when they do, they appear in the InvalidatedRegions array as well.
719e5dd7070Spatrick     for (const auto *Region : RequestedRegions) {
720e5dd7070Spatrick       if (ThisRegion != Region) {
721e5dd7070Spatrick         if (llvm::find(InvalidatedRegions, Region) !=
722e5dd7070Spatrick             std::end(InvalidatedRegions)) {
723e5dd7070Spatrick           State = removeFromState(State, Region);
724e5dd7070Spatrick         }
725e5dd7070Spatrick       }
726e5dd7070Spatrick     }
727e5dd7070Spatrick   } else {
728e5dd7070Spatrick     // For invalidations that aren't caused by calls, assume nothing. In
729e5dd7070Spatrick     // particular, direct write into an object's field invalidates the status.
730e5dd7070Spatrick     for (const auto *Region : InvalidatedRegions)
731e5dd7070Spatrick       State = removeFromState(State, Region->getBaseRegion());
732e5dd7070Spatrick   }
733e5dd7070Spatrick 
734e5dd7070Spatrick   return State;
735e5dd7070Spatrick }
736e5dd7070Spatrick 
737e5dd7070Spatrick void MoveChecker::printState(raw_ostream &Out, ProgramStateRef State,
738e5dd7070Spatrick                              const char *NL, const char *Sep) const {
739e5dd7070Spatrick 
740e5dd7070Spatrick   TrackedRegionMapTy RS = State->get<TrackedRegionMap>();
741e5dd7070Spatrick 
742e5dd7070Spatrick   if (!RS.isEmpty()) {
743e5dd7070Spatrick     Out << Sep << "Moved-from objects :" << NL;
744e5dd7070Spatrick     for (auto I: RS) {
745e5dd7070Spatrick       I.first->dumpToStream(Out);
746e5dd7070Spatrick       if (I.second.isMoved())
747e5dd7070Spatrick         Out << ": moved";
748e5dd7070Spatrick       else
749e5dd7070Spatrick         Out << ": moved and reported";
750e5dd7070Spatrick       Out << NL;
751e5dd7070Spatrick     }
752e5dd7070Spatrick   }
753e5dd7070Spatrick }
754e5dd7070Spatrick void ento::registerMoveChecker(CheckerManager &mgr) {
755e5dd7070Spatrick   MoveChecker *chk = mgr.registerChecker<MoveChecker>();
756e5dd7070Spatrick   chk->setAggressiveness(
757e5dd7070Spatrick       mgr.getAnalyzerOptions().getCheckerStringOption(chk, "WarnOn"), mgr);
758e5dd7070Spatrick }
759e5dd7070Spatrick 
760*ec727ea7Spatrick bool ento::shouldRegisterMoveChecker(const CheckerManager &mgr) {
761e5dd7070Spatrick   return true;
762e5dd7070Spatrick }
763