1 //===- BugReporterVisitors.cpp - Helpers for reporting bugs ---------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines a set of BugReporter "visitors" which can be used to
10 //  enhance the diagnostics reported for a bug.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/ExprObjC.h"
22 #include "clang/AST/Stmt.h"
23 #include "clang/AST/Type.h"
24 #include "clang/ASTMatchers/ASTMatchFinder.h"
25 #include "clang/Analysis/Analyses/Dominators.h"
26 #include "clang/Analysis/AnalysisDeclContext.h"
27 #include "clang/Analysis/CFG.h"
28 #include "clang/Analysis/CFGStmtMap.h"
29 #include "clang/Analysis/PathDiagnostic.h"
30 #include "clang/Analysis/ProgramPoint.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/SourceLocation.h"
34 #include "clang/Basic/SourceManager.h"
35 #include "clang/Lex/Lexer.h"
36 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
37 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
38 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
39 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
40 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
41 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
42 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
43 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
44 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
45 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h"
46 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
47 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
48 #include "llvm/ADT/ArrayRef.h"
49 #include "llvm/ADT/None.h"
50 #include "llvm/ADT/Optional.h"
51 #include "llvm/ADT/STLExtras.h"
52 #include "llvm/ADT/SmallPtrSet.h"
53 #include "llvm/ADT/SmallString.h"
54 #include "llvm/ADT/SmallVector.h"
55 #include "llvm/ADT/StringExtras.h"
56 #include "llvm/ADT/StringRef.h"
57 #include "llvm/Support/Casting.h"
58 #include "llvm/Support/ErrorHandling.h"
59 #include "llvm/Support/raw_ostream.h"
60 #include <cassert>
61 #include <deque>
62 #include <memory>
63 #include <string>
64 #include <utility>
65 
66 using namespace clang;
67 using namespace ento;
68 using namespace bugreporter;
69 
70 //===----------------------------------------------------------------------===//
71 // Utility functions.
72 //===----------------------------------------------------------------------===//
73 
74 static const Expr *peelOffPointerArithmetic(const BinaryOperator *B) {
75   if (B->isAdditiveOp() && B->getType()->isPointerType()) {
76     if (B->getLHS()->getType()->isPointerType()) {
77       return B->getLHS();
78     } else if (B->getRHS()->getType()->isPointerType()) {
79       return B->getRHS();
80     }
81   }
82   return nullptr;
83 }
84 
85 /// Given that expression S represents a pointer that would be dereferenced,
86 /// try to find a sub-expression from which the pointer came from.
87 /// This is used for tracking down origins of a null or undefined value:
88 /// "this is null because that is null because that is null" etc.
89 /// We wipe away field and element offsets because they merely add offsets.
90 /// We also wipe away all casts except lvalue-to-rvalue casts, because the
91 /// latter represent an actual pointer dereference; however, we remove
92 /// the final lvalue-to-rvalue cast before returning from this function
93 /// because it demonstrates more clearly from where the pointer rvalue was
94 /// loaded. Examples:
95 ///   x->y.z      ==>  x (lvalue)
96 ///   foo()->y.z  ==>  foo() (rvalue)
97 const Expr *bugreporter::getDerefExpr(const Stmt *S) {
98   const auto *E = dyn_cast<Expr>(S);
99   if (!E)
100     return nullptr;
101 
102   while (true) {
103     if (const auto *CE = dyn_cast<CastExpr>(E)) {
104       if (CE->getCastKind() == CK_LValueToRValue) {
105         // This cast represents the load we're looking for.
106         break;
107       }
108       E = CE->getSubExpr();
109     } else if (const auto *B = dyn_cast<BinaryOperator>(E)) {
110       // Pointer arithmetic: '*(x + 2)' -> 'x') etc.
111       if (const Expr *Inner = peelOffPointerArithmetic(B)) {
112         E = Inner;
113       } else {
114         // Probably more arithmetic can be pattern-matched here,
115         // but for now give up.
116         break;
117       }
118     } else if (const auto *U = dyn_cast<UnaryOperator>(E)) {
119       if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf ||
120           (U->isIncrementDecrementOp() && U->getType()->isPointerType())) {
121         // Operators '*' and '&' don't actually mean anything.
122         // We look at casts instead.
123         E = U->getSubExpr();
124       } else {
125         // Probably more arithmetic can be pattern-matched here,
126         // but for now give up.
127         break;
128       }
129     }
130     // Pattern match for a few useful cases: a[0], p->f, *p etc.
131     else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
132       E = ME->getBase();
133     } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
134       E = IvarRef->getBase();
135     } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) {
136       E = AE->getBase();
137     } else if (const auto *PE = dyn_cast<ParenExpr>(E)) {
138       E = PE->getSubExpr();
139     } else if (const auto *FE = dyn_cast<FullExpr>(E)) {
140       E = FE->getSubExpr();
141     } else {
142       // Other arbitrary stuff.
143       break;
144     }
145   }
146 
147   // Special case: remove the final lvalue-to-rvalue cast, but do not recurse
148   // deeper into the sub-expression. This way we return the lvalue from which
149   // our pointer rvalue was loaded.
150   if (const auto *CE = dyn_cast<ImplicitCastExpr>(E))
151     if (CE->getCastKind() == CK_LValueToRValue)
152       E = CE->getSubExpr();
153 
154   return E;
155 }
156 
157 static const MemRegion *
158 getLocationRegionIfReference(const Expr *E, const ExplodedNode *N,
159                              bool LookingForReference = true) {
160   if (const auto *DR = dyn_cast<DeclRefExpr>(E)) {
161     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
162       if (LookingForReference && !VD->getType()->isReferenceType())
163         return nullptr;
164       return N->getState()
165           ->getLValue(VD, N->getLocationContext())
166           .getAsRegion();
167     }
168   }
169 
170   // FIXME: This does not handle other kinds of null references,
171   // for example, references from FieldRegions:
172   //   struct Wrapper { int &ref; };
173   //   Wrapper w = { *(int *)0 };
174   //   w.ref = 1;
175 
176   return nullptr;
177 }
178 
179 /// Comparing internal representations of symbolic values (via
180 /// SVal::operator==()) is a valid way to check if the value was updated,
181 /// unless it's a LazyCompoundVal that may have a different internal
182 /// representation every time it is loaded from the state. In this function we
183 /// do an approximate comparison for lazy compound values, checking that they
184 /// are the immediate snapshots of the tracked region's bindings within the
185 /// node's respective states but not really checking that these snapshots
186 /// actually contain the same set of bindings.
187 static bool hasVisibleUpdate(const ExplodedNode *LeftNode, SVal LeftVal,
188                              const ExplodedNode *RightNode, SVal RightVal) {
189   if (LeftVal == RightVal)
190     return true;
191 
192   const auto LLCV = LeftVal.getAs<nonloc::LazyCompoundVal>();
193   if (!LLCV)
194     return false;
195 
196   const auto RLCV = RightVal.getAs<nonloc::LazyCompoundVal>();
197   if (!RLCV)
198     return false;
199 
200   return LLCV->getRegion() == RLCV->getRegion() &&
201     LLCV->getStore() == LeftNode->getState()->getStore() &&
202     RLCV->getStore() == RightNode->getState()->getStore();
203 }
204 
205 static Optional<SVal> getSValForVar(const Expr *CondVarExpr,
206                                     const ExplodedNode *N) {
207   ProgramStateRef State = N->getState();
208   const LocationContext *LCtx = N->getLocationContext();
209 
210   assert(CondVarExpr);
211   CondVarExpr = CondVarExpr->IgnoreImpCasts();
212 
213   // The declaration of the value may rely on a pointer so take its l-value.
214   // FIXME: As seen in VisitCommonDeclRefExpr, sometimes DeclRefExpr may
215   // evaluate to a FieldRegion when it refers to a declaration of a lambda
216   // capture variable. We most likely need to duplicate that logic here.
217   if (const auto *DRE = dyn_cast<DeclRefExpr>(CondVarExpr))
218     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
219       return State->getSVal(State->getLValue(VD, LCtx));
220 
221   if (const auto *ME = dyn_cast<MemberExpr>(CondVarExpr))
222     if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
223       if (auto FieldL = State->getSVal(ME, LCtx).getAs<Loc>())
224         return State->getRawSVal(*FieldL, FD->getType());
225 
226   return None;
227 }
228 
229 static Optional<const llvm::APSInt *>
230 getConcreteIntegerValue(const Expr *CondVarExpr, const ExplodedNode *N) {
231 
232   if (Optional<SVal> V = getSValForVar(CondVarExpr, N))
233     if (auto CI = V->getAs<nonloc::ConcreteInt>())
234       return &CI->getValue();
235   return None;
236 }
237 
238 static bool isVarAnInterestingCondition(const Expr *CondVarExpr,
239                                         const ExplodedNode *N,
240                                         const PathSensitiveBugReport *B) {
241   // Even if this condition is marked as interesting, it isn't *that*
242   // interesting if it didn't happen in a nested stackframe, the user could just
243   // follow the arrows.
244   if (!B->getErrorNode()->getStackFrame()->isParentOf(N->getStackFrame()))
245     return false;
246 
247   if (Optional<SVal> V = getSValForVar(CondVarExpr, N))
248     if (Optional<bugreporter::TrackingKind> K = B->getInterestingnessKind(*V))
249       return *K == bugreporter::TrackingKind::Condition;
250 
251   return false;
252 }
253 
254 static bool isInterestingExpr(const Expr *E, const ExplodedNode *N,
255                               const PathSensitiveBugReport *B) {
256   if (Optional<SVal> V = getSValForVar(E, N))
257     return B->getInterestingnessKind(*V).hasValue();
258   return false;
259 }
260 
261 /// \return name of the macro inside the location \p Loc.
262 static StringRef getMacroName(SourceLocation Loc,
263     BugReporterContext &BRC) {
264   return Lexer::getImmediateMacroName(
265       Loc,
266       BRC.getSourceManager(),
267       BRC.getASTContext().getLangOpts());
268 }
269 
270 /// \return Whether given spelling location corresponds to an expansion
271 /// of a function-like macro.
272 static bool isFunctionMacroExpansion(SourceLocation Loc,
273                                 const SourceManager &SM) {
274   if (!Loc.isMacroID())
275     return false;
276   while (SM.isMacroArgExpansion(Loc))
277     Loc = SM.getImmediateExpansionRange(Loc).getBegin();
278   std::pair<FileID, unsigned> TLInfo = SM.getDecomposedLoc(Loc);
279   SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first);
280   const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion();
281   return EInfo.isFunctionMacroExpansion();
282 }
283 
284 /// \return Whether \c RegionOfInterest was modified at \p N,
285 /// where \p ValueAfter is \c RegionOfInterest's value at the end of the
286 /// stack frame.
287 static bool wasRegionOfInterestModifiedAt(const SubRegion *RegionOfInterest,
288                                           const ExplodedNode *N,
289                                           SVal ValueAfter) {
290   ProgramStateRef State = N->getState();
291   ProgramStateManager &Mgr = N->getState()->getStateManager();
292 
293   if (!N->getLocationAs<PostStore>() && !N->getLocationAs<PostInitializer>() &&
294       !N->getLocationAs<PostStmt>())
295     return false;
296 
297   // Writing into region of interest.
298   if (auto PS = N->getLocationAs<PostStmt>())
299     if (auto *BO = PS->getStmtAs<BinaryOperator>())
300       if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(
301                                       N->getSVal(BO->getLHS()).getAsRegion()))
302         return true;
303 
304   // SVal after the state is possibly different.
305   SVal ValueAtN = N->getState()->getSVal(RegionOfInterest);
306   if (!Mgr.getSValBuilder()
307            .areEqual(State, ValueAtN, ValueAfter)
308            .isConstrainedTrue() &&
309       (!ValueAtN.isUndef() || !ValueAfter.isUndef()))
310     return true;
311 
312   return false;
313 }
314 
315 //===----------------------------------------------------------------------===//
316 // Implementation of BugReporterVisitor.
317 //===----------------------------------------------------------------------===//
318 
319 PathDiagnosticPieceRef BugReporterVisitor::getEndPath(BugReporterContext &,
320                                                       const ExplodedNode *,
321                                                       PathSensitiveBugReport &) {
322   return nullptr;
323 }
324 
325 void BugReporterVisitor::finalizeVisitor(BugReporterContext &,
326                                          const ExplodedNode *,
327                                          PathSensitiveBugReport &) {}
328 
329 PathDiagnosticPieceRef
330 BugReporterVisitor::getDefaultEndPath(const BugReporterContext &BRC,
331                                       const ExplodedNode *EndPathNode,
332                                       const PathSensitiveBugReport &BR) {
333   PathDiagnosticLocation L = BR.getLocation();
334   const auto &Ranges = BR.getRanges();
335 
336   // Only add the statement itself as a range if we didn't specify any
337   // special ranges for this report.
338   auto P = std::make_shared<PathDiagnosticEventPiece>(
339       L, BR.getDescription(), Ranges.begin() == Ranges.end());
340   for (SourceRange Range : Ranges)
341     P->addRange(Range);
342 
343   return P;
344 }
345 
346 //===----------------------------------------------------------------------===//
347 // Implementation of NoStateChangeFuncVisitor.
348 //===----------------------------------------------------------------------===//
349 
350 bool NoStateChangeFuncVisitor::isModifiedInFrame(const ExplodedNode *N) {
351   const LocationContext *Ctx = N->getLocationContext();
352   const StackFrameContext *SCtx = Ctx->getStackFrame();
353   if (!FramesModifyingCalculated.count(SCtx))
354     findModifyingFrames(N);
355   return FramesModifying.count(SCtx);
356 }
357 
358 void NoStateChangeFuncVisitor::findModifyingFrames(
359     const ExplodedNode *const CallExitBeginN) {
360 
361   assert(CallExitBeginN->getLocationAs<CallExitBegin>());
362   const ExplodedNode *LastReturnN = CallExitBeginN;
363   const StackFrameContext *const OriginalSCtx =
364       CallExitBeginN->getLocationContext()->getStackFrame();
365 
366   const ExplodedNode *CurrN = CallExitBeginN;
367 
368   do {
369     ProgramStateRef State = CurrN->getState();
370     auto CallExitLoc = CurrN->getLocationAs<CallExitBegin>();
371     if (CallExitLoc) {
372       LastReturnN = CurrN;
373     }
374 
375     FramesModifyingCalculated.insert(
376         CurrN->getLocationContext()->getStackFrame());
377 
378     if (wasModifiedBeforeCallExit(CurrN, LastReturnN)) {
379       const StackFrameContext *SCtx = CurrN->getStackFrame();
380       while (!SCtx->inTopFrame()) {
381         auto p = FramesModifying.insert(SCtx);
382         if (!p.second)
383           break; // Frame and all its parents already inserted.
384         SCtx = SCtx->getParent()->getStackFrame();
385       }
386     }
387 
388     // Stop calculation at the call to the current function.
389     if (auto CE = CurrN->getLocationAs<CallEnter>())
390       if (CE->getCalleeContext() == OriginalSCtx)
391         break;
392 
393     CurrN = CurrN->getFirstPred();
394   } while (CurrN);
395 }
396 
397 PathDiagnosticPieceRef NoStateChangeFuncVisitor::VisitNode(
398     const ExplodedNode *N, BugReporterContext &BR, PathSensitiveBugReport &R) {
399 
400   const LocationContext *Ctx = N->getLocationContext();
401   const StackFrameContext *SCtx = Ctx->getStackFrame();
402   ProgramStateRef State = N->getState();
403   auto CallExitLoc = N->getLocationAs<CallExitBegin>();
404 
405   // No diagnostic if region was modified inside the frame.
406   if (!CallExitLoc || isModifiedInFrame(N))
407     return nullptr;
408 
409   CallEventRef<> Call =
410       BR.getStateManager().getCallEventManager().getCaller(SCtx, State);
411 
412   // Optimistically suppress uninitialized value bugs that result
413   // from system headers having a chance to initialize the value
414   // but failing to do so. It's too unlikely a system header's fault.
415   // It's much more likely a situation in which the function has a failure
416   // mode that the user decided not to check. If we want to hunt such
417   // omitted checks, we should provide an explicit function-specific note
418   // describing the precondition under which the function isn't supposed to
419   // initialize its out-parameter, and additionally check that such
420   // precondition can actually be fulfilled on the current path.
421   if (Call->isInSystemHeader()) {
422     // We make an exception for system header functions that have no branches.
423     // Such functions unconditionally fail to initialize the variable.
424     // If they call other functions that have more paths within them,
425     // this suppression would still apply when we visit these inner functions.
426     // One common example of a standard function that doesn't ever initialize
427     // its out parameter is operator placement new; it's up to the follow-up
428     // constructor (if any) to initialize the memory.
429     if (!N->getStackFrame()->getCFG()->isLinear()) {
430       static int i = 0;
431       R.markInvalid(&i, nullptr);
432     }
433     return nullptr;
434   }
435 
436   if (const auto *MC = dyn_cast<ObjCMethodCall>(Call)) {
437     // If we failed to construct a piece for self, we still want to check
438     // whether the entity of interest is in a parameter.
439     if (PathDiagnosticPieceRef Piece = maybeEmitNoteForObjCSelf(R, *MC, N))
440       return Piece;
441   }
442 
443   if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) {
444     // Do not generate diagnostics for not modified parameters in
445     // constructors.
446     return maybeEmitNoteForCXXThis(R, *CCall, N);
447   }
448 
449   return maybeEmitNoteForParameters(R, *Call, N);
450 }
451 
452 //===----------------------------------------------------------------------===//
453 // Implementation of NoStoreFuncVisitor.
454 //===----------------------------------------------------------------------===//
455 
456 namespace {
457 /// Put a diagnostic on return statement of all inlined functions
458 /// for which  the region of interest \p RegionOfInterest was passed into,
459 /// but not written inside, and it has caused an undefined read or a null
460 /// pointer dereference outside.
461 class NoStoreFuncVisitor final : public NoStateChangeFuncVisitor {
462   const SubRegion *RegionOfInterest;
463   MemRegionManager &MmrMgr;
464   const SourceManager &SM;
465   const PrintingPolicy &PP;
466 
467   /// Recursion limit for dereferencing fields when looking for the
468   /// region of interest.
469   /// The limit of two indicates that we will dereference fields only once.
470   static const unsigned DEREFERENCE_LIMIT = 2;
471 
472   using RegionVector = SmallVector<const MemRegion *, 5>;
473 
474 public:
475   NoStoreFuncVisitor(const SubRegion *R, bugreporter::TrackingKind TKind)
476       : NoStateChangeFuncVisitor(TKind), RegionOfInterest(R),
477         MmrMgr(R->getMemRegionManager()),
478         SM(MmrMgr.getContext().getSourceManager()),
479         PP(MmrMgr.getContext().getPrintingPolicy()) {}
480 
481   void Profile(llvm::FoldingSetNodeID &ID) const override {
482     static int Tag = 0;
483     ID.AddPointer(&Tag);
484     ID.AddPointer(RegionOfInterest);
485   }
486 
487   void *getTag() const {
488     static int Tag = 0;
489     return static_cast<void *>(&Tag);
490   }
491 
492 private:
493   /// \return Whether \c RegionOfInterest was modified at \p CurrN compared to
494   /// the value it holds in \p CallExitBeginN.
495   virtual bool
496   wasModifiedBeforeCallExit(const ExplodedNode *CurrN,
497                             const ExplodedNode *CallExitBeginN) override;
498 
499   /// Attempts to find the region of interest in a given record decl,
500   /// by either following the base classes or fields.
501   /// Dereferences fields up to a given recursion limit.
502   /// Note that \p Vec is passed by value, leading to quadratic copying cost,
503   /// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT.
504   /// \return A chain fields leading to the region of interest or None.
505   const Optional<RegionVector>
506   findRegionOfInterestInRecord(const RecordDecl *RD, ProgramStateRef State,
507                                const MemRegion *R, const RegionVector &Vec = {},
508                                int depth = 0);
509 
510   // Region of interest corresponds to an IVar, exiting a method
511   // which could have written into that IVar, but did not.
512   virtual PathDiagnosticPieceRef
513   maybeEmitNoteForObjCSelf(PathSensitiveBugReport &R,
514                            const ObjCMethodCall &Call,
515                            const ExplodedNode *N) override final;
516 
517   virtual PathDiagnosticPieceRef
518   maybeEmitNoteForCXXThis(PathSensitiveBugReport &R,
519                           const CXXConstructorCall &Call,
520                           const ExplodedNode *N) override final;
521 
522   virtual PathDiagnosticPieceRef
523   maybeEmitNoteForParameters(PathSensitiveBugReport &R, const CallEvent &Call,
524                              const ExplodedNode *N) override final;
525 
526   /// Consume the information on the no-store stack frame in order to
527   /// either emit a note or suppress the report enirely.
528   /// \return Diagnostics piece for region not modified in the current function,
529   /// if it decides to emit one.
530   PathDiagnosticPieceRef
531   maybeEmitNote(PathSensitiveBugReport &R, const CallEvent &Call,
532                 const ExplodedNode *N, const RegionVector &FieldChain,
533                 const MemRegion *MatchedRegion, StringRef FirstElement,
534                 bool FirstIsReferenceType, unsigned IndirectionLevel);
535 
536   bool prettyPrintRegionName(const RegionVector &FieldChain,
537                              const MemRegion *MatchedRegion,
538                              StringRef FirstElement, bool FirstIsReferenceType,
539                              unsigned IndirectionLevel,
540                              llvm::raw_svector_ostream &os);
541 
542   StringRef prettyPrintFirstElement(StringRef FirstElement,
543                                     bool MoreItemsExpected,
544                                     int IndirectionLevel,
545                                     llvm::raw_svector_ostream &os);
546 };
547 } // namespace
548 
549 /// \return Whether the method declaration \p Parent
550 /// syntactically has a binary operation writing into the ivar \p Ivar.
551 static bool potentiallyWritesIntoIvar(const Decl *Parent,
552                                       const ObjCIvarDecl *Ivar) {
553   using namespace ast_matchers;
554   const char *IvarBind = "Ivar";
555   if (!Parent || !Parent->hasBody())
556     return false;
557   StatementMatcher WriteIntoIvarM = binaryOperator(
558       hasOperatorName("="),
559       hasLHS(ignoringParenImpCasts(
560           objcIvarRefExpr(hasDeclaration(equalsNode(Ivar))).bind(IvarBind))));
561   StatementMatcher ParentM = stmt(hasDescendant(WriteIntoIvarM));
562   auto Matches = match(ParentM, *Parent->getBody(), Parent->getASTContext());
563   for (BoundNodes &Match : Matches) {
564     auto IvarRef = Match.getNodeAs<ObjCIvarRefExpr>(IvarBind);
565     if (IvarRef->isFreeIvar())
566       return true;
567 
568     const Expr *Base = IvarRef->getBase();
569     if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base))
570       Base = ICE->getSubExpr();
571 
572     if (const auto *DRE = dyn_cast<DeclRefExpr>(Base))
573       if (const auto *ID = dyn_cast<ImplicitParamDecl>(DRE->getDecl()))
574         if (ID->getParameterKind() == ImplicitParamDecl::ObjCSelf)
575           return true;
576 
577     return false;
578   }
579   return false;
580 }
581 
582 /// Attempts to find the region of interest in a given CXX decl,
583 /// by either following the base classes or fields.
584 /// Dereferences fields up to a given recursion limit.
585 /// Note that \p Vec is passed by value, leading to quadratic copying cost,
586 /// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT.
587 /// \return A chain fields leading to the region of interest or None.
588 const Optional<NoStoreFuncVisitor::RegionVector>
589 NoStoreFuncVisitor::findRegionOfInterestInRecord(
590     const RecordDecl *RD, ProgramStateRef State, const MemRegion *R,
591     const NoStoreFuncVisitor::RegionVector &Vec /* = {} */,
592     int depth /* = 0 */) {
593 
594   if (depth == DEREFERENCE_LIMIT) // Limit the recursion depth.
595     return None;
596 
597   if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
598     if (!RDX->hasDefinition())
599       return None;
600 
601   // Recursively examine the base classes.
602   // Note that following base classes does not increase the recursion depth.
603   if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
604     for (const auto &II : RDX->bases())
605       if (const RecordDecl *RRD = II.getType()->getAsRecordDecl())
606         if (Optional<RegionVector> Out =
607                 findRegionOfInterestInRecord(RRD, State, R, Vec, depth))
608           return Out;
609 
610   for (const FieldDecl *I : RD->fields()) {
611     QualType FT = I->getType();
612     const FieldRegion *FR = MmrMgr.getFieldRegion(I, cast<SubRegion>(R));
613     const SVal V = State->getSVal(FR);
614     const MemRegion *VR = V.getAsRegion();
615 
616     RegionVector VecF = Vec;
617     VecF.push_back(FR);
618 
619     if (RegionOfInterest == VR)
620       return VecF;
621 
622     if (const RecordDecl *RRD = FT->getAsRecordDecl())
623       if (auto Out =
624               findRegionOfInterestInRecord(RRD, State, FR, VecF, depth + 1))
625         return Out;
626 
627     QualType PT = FT->getPointeeType();
628     if (PT.isNull() || PT->isVoidType() || !VR)
629       continue;
630 
631     if (const RecordDecl *RRD = PT->getAsRecordDecl())
632       if (Optional<RegionVector> Out =
633               findRegionOfInterestInRecord(RRD, State, VR, VecF, depth + 1))
634         return Out;
635   }
636 
637   return None;
638 }
639 
640 PathDiagnosticPieceRef
641 NoStoreFuncVisitor::maybeEmitNoteForObjCSelf(PathSensitiveBugReport &R,
642                                              const ObjCMethodCall &Call,
643                                              const ExplodedNode *N) {
644   if (const auto *IvarR = dyn_cast<ObjCIvarRegion>(RegionOfInterest)) {
645     const MemRegion *SelfRegion = Call.getReceiverSVal().getAsRegion();
646     if (RegionOfInterest->isSubRegionOf(SelfRegion) &&
647         potentiallyWritesIntoIvar(Call.getRuntimeDefinition().getDecl(),
648                                   IvarR->getDecl()))
649       return maybeEmitNote(R, Call, N, {}, SelfRegion, "self",
650                            /*FirstIsReferenceType=*/false, 1);
651   }
652   return nullptr;
653 }
654 
655 PathDiagnosticPieceRef
656 NoStoreFuncVisitor::maybeEmitNoteForCXXThis(PathSensitiveBugReport &R,
657                                             const CXXConstructorCall &Call,
658                                             const ExplodedNode *N) {
659   const MemRegion *ThisR = Call.getCXXThisVal().getAsRegion();
660   if (RegionOfInterest->isSubRegionOf(ThisR) && !Call.getDecl()->isImplicit())
661     return maybeEmitNote(R, Call, N, {}, ThisR, "this",
662                          /*FirstIsReferenceType=*/false, 1);
663 
664   // Do not generate diagnostics for not modified parameters in
665   // constructors.
666   return nullptr;
667 }
668 
669 /// \return whether \p Ty points to a const type, or is a const reference.
670 static bool isPointerToConst(QualType Ty) {
671   return !Ty->getPointeeType().isNull() &&
672          Ty->getPointeeType().getCanonicalType().isConstQualified();
673 }
674 
675 PathDiagnosticPieceRef NoStoreFuncVisitor::maybeEmitNoteForParameters(
676     PathSensitiveBugReport &R, const CallEvent &Call, const ExplodedNode *N) {
677   ArrayRef<ParmVarDecl *> Parameters = Call.parameters();
678   for (unsigned I = 0; I < Call.getNumArgs() && I < Parameters.size(); ++I) {
679     const ParmVarDecl *PVD = Parameters[I];
680     SVal V = Call.getArgSVal(I);
681     bool ParamIsReferenceType = PVD->getType()->isReferenceType();
682     std::string ParamName = PVD->getNameAsString();
683 
684     unsigned IndirectionLevel = 1;
685     QualType T = PVD->getType();
686     while (const MemRegion *MR = V.getAsRegion()) {
687       if (RegionOfInterest->isSubRegionOf(MR) && !isPointerToConst(T))
688         return maybeEmitNote(R, Call, N, {}, MR, ParamName,
689                              ParamIsReferenceType, IndirectionLevel);
690 
691       QualType PT = T->getPointeeType();
692       if (PT.isNull() || PT->isVoidType())
693         break;
694 
695       ProgramStateRef State = N->getState();
696 
697       if (const RecordDecl *RD = PT->getAsRecordDecl())
698         if (Optional<RegionVector> P =
699                 findRegionOfInterestInRecord(RD, State, MR))
700           return maybeEmitNote(R, Call, N, *P, RegionOfInterest, ParamName,
701                                ParamIsReferenceType, IndirectionLevel);
702 
703       V = State->getSVal(MR, PT);
704       T = PT;
705       IndirectionLevel++;
706     }
707   }
708 
709   return nullptr;
710 }
711 
712 bool NoStoreFuncVisitor::wasModifiedBeforeCallExit(
713     const ExplodedNode *CurrN, const ExplodedNode *CallExitBeginN) {
714   return ::wasRegionOfInterestModifiedAt(
715       RegionOfInterest, CurrN,
716       CallExitBeginN->getState()->getSVal(RegionOfInterest));
717 }
718 
719 static llvm::StringLiteral WillBeUsedForACondition =
720     ", which participates in a condition later";
721 
722 PathDiagnosticPieceRef NoStoreFuncVisitor::maybeEmitNote(
723     PathSensitiveBugReport &R, const CallEvent &Call, const ExplodedNode *N,
724     const RegionVector &FieldChain, const MemRegion *MatchedRegion,
725     StringRef FirstElement, bool FirstIsReferenceType,
726     unsigned IndirectionLevel) {
727 
728   PathDiagnosticLocation L =
729       PathDiagnosticLocation::create(N->getLocation(), SM);
730 
731   // For now this shouldn't trigger, but once it does (as we add more
732   // functions to the body farm), we'll need to decide if these reports
733   // are worth suppressing as well.
734   if (!L.hasValidLocation())
735     return nullptr;
736 
737   SmallString<256> sbuf;
738   llvm::raw_svector_ostream os(sbuf);
739   os << "Returning without writing to '";
740 
741   // Do not generate the note if failed to pretty-print.
742   if (!prettyPrintRegionName(FieldChain, MatchedRegion, FirstElement,
743                              FirstIsReferenceType, IndirectionLevel, os))
744     return nullptr;
745 
746   os << "'";
747   if (TKind == bugreporter::TrackingKind::Condition)
748     os << WillBeUsedForACondition;
749   return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
750 }
751 
752 bool NoStoreFuncVisitor::prettyPrintRegionName(const RegionVector &FieldChain,
753                                                const MemRegion *MatchedRegion,
754                                                StringRef FirstElement,
755                                                bool FirstIsReferenceType,
756                                                unsigned IndirectionLevel,
757                                                llvm::raw_svector_ostream &os) {
758 
759   if (FirstIsReferenceType)
760     IndirectionLevel--;
761 
762   RegionVector RegionSequence;
763 
764   // Add the regions in the reverse order, then reverse the resulting array.
765   assert(RegionOfInterest->isSubRegionOf(MatchedRegion));
766   const MemRegion *R = RegionOfInterest;
767   while (R != MatchedRegion) {
768     RegionSequence.push_back(R);
769     R = cast<SubRegion>(R)->getSuperRegion();
770   }
771   std::reverse(RegionSequence.begin(), RegionSequence.end());
772   RegionSequence.append(FieldChain.begin(), FieldChain.end());
773 
774   StringRef Sep;
775   for (const MemRegion *R : RegionSequence) {
776 
777     // Just keep going up to the base region.
778     // Element regions may appear due to casts.
779     if (isa<CXXBaseObjectRegion>(R) || isa<CXXTempObjectRegion>(R))
780       continue;
781 
782     if (Sep.empty())
783       Sep = prettyPrintFirstElement(FirstElement,
784                                     /*MoreItemsExpected=*/true,
785                                     IndirectionLevel, os);
786 
787     os << Sep;
788 
789     // Can only reasonably pretty-print DeclRegions.
790     if (!isa<DeclRegion>(R))
791       return false;
792 
793     const auto *DR = cast<DeclRegion>(R);
794     Sep = DR->getValueType()->isAnyPointerType() ? "->" : ".";
795     DR->getDecl()->getDeclName().print(os, PP);
796   }
797 
798   if (Sep.empty())
799     prettyPrintFirstElement(FirstElement,
800                             /*MoreItemsExpected=*/false, IndirectionLevel, os);
801   return true;
802 }
803 
804 StringRef NoStoreFuncVisitor::prettyPrintFirstElement(
805     StringRef FirstElement, bool MoreItemsExpected, int IndirectionLevel,
806     llvm::raw_svector_ostream &os) {
807   StringRef Out = ".";
808 
809   if (IndirectionLevel > 0 && MoreItemsExpected) {
810     IndirectionLevel--;
811     Out = "->";
812   }
813 
814   if (IndirectionLevel > 0 && MoreItemsExpected)
815     os << "(";
816 
817   for (int i = 0; i < IndirectionLevel; i++)
818     os << "*";
819   os << FirstElement;
820 
821   if (IndirectionLevel > 0 && MoreItemsExpected)
822     os << ")";
823 
824   return Out;
825 }
826 
827 //===----------------------------------------------------------------------===//
828 // Implementation of MacroNullReturnSuppressionVisitor.
829 //===----------------------------------------------------------------------===//
830 
831 namespace {
832 
833 /// Suppress null-pointer-dereference bugs where dereferenced null was returned
834 /// the macro.
835 class MacroNullReturnSuppressionVisitor final : public BugReporterVisitor {
836   const SubRegion *RegionOfInterest;
837   const SVal ValueAtDereference;
838 
839   // Do not invalidate the reports where the value was modified
840   // after it got assigned to from the macro.
841   bool WasModified = false;
842 
843 public:
844   MacroNullReturnSuppressionVisitor(const SubRegion *R, const SVal V)
845       : RegionOfInterest(R), ValueAtDereference(V) {}
846 
847   PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
848                                    BugReporterContext &BRC,
849                                    PathSensitiveBugReport &BR) override {
850     if (WasModified)
851       return nullptr;
852 
853     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
854     if (!BugPoint)
855       return nullptr;
856 
857     const SourceManager &SMgr = BRC.getSourceManager();
858     if (auto Loc = matchAssignment(N)) {
859       if (isFunctionMacroExpansion(*Loc, SMgr)) {
860         std::string MacroName = std::string(getMacroName(*Loc, BRC));
861         SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
862         if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName)
863           BR.markInvalid(getTag(), MacroName.c_str());
864       }
865     }
866 
867     if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtDereference))
868       WasModified = true;
869 
870     return nullptr;
871   }
872 
873   static void addMacroVisitorIfNecessary(
874         const ExplodedNode *N, const MemRegion *R,
875         bool EnableNullFPSuppression, PathSensitiveBugReport &BR,
876         const SVal V) {
877     AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
878     if (EnableNullFPSuppression && Options.ShouldSuppressNullReturnPaths &&
879         V.getAs<Loc>())
880       BR.addVisitor<MacroNullReturnSuppressionVisitor>(R->getAs<SubRegion>(),
881                                                        V);
882   }
883 
884   void* getTag() const {
885     static int Tag = 0;
886     return static_cast<void *>(&Tag);
887   }
888 
889   void Profile(llvm::FoldingSetNodeID &ID) const override {
890     ID.AddPointer(getTag());
891   }
892 
893 private:
894   /// \return Source location of right hand side of an assignment
895   /// into \c RegionOfInterest, empty optional if none found.
896   Optional<SourceLocation> matchAssignment(const ExplodedNode *N) {
897     const Stmt *S = N->getStmtForDiagnostics();
898     ProgramStateRef State = N->getState();
899     auto *LCtx = N->getLocationContext();
900     if (!S)
901       return None;
902 
903     if (const auto *DS = dyn_cast<DeclStmt>(S)) {
904       if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
905         if (const Expr *RHS = VD->getInit())
906           if (RegionOfInterest->isSubRegionOf(
907                   State->getLValue(VD, LCtx).getAsRegion()))
908             return RHS->getBeginLoc();
909     } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
910       const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion();
911       const Expr *RHS = BO->getRHS();
912       if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) {
913         return RHS->getBeginLoc();
914       }
915     }
916     return None;
917   }
918 };
919 
920 } // end of anonymous namespace
921 
922 namespace {
923 
924 /// Emits an extra note at the return statement of an interesting stack frame.
925 ///
926 /// The returned value is marked as an interesting value, and if it's null,
927 /// adds a visitor to track where it became null.
928 ///
929 /// This visitor is intended to be used when another visitor discovers that an
930 /// interesting value comes from an inlined function call.
931 class ReturnVisitor : public TrackingBugReporterVisitor {
932   const StackFrameContext *CalleeSFC;
933   enum {
934     Initial,
935     MaybeUnsuppress,
936     Satisfied
937   } Mode = Initial;
938 
939   bool EnableNullFPSuppression;
940   bool ShouldInvalidate = true;
941   AnalyzerOptions& Options;
942   bugreporter::TrackingKind TKind;
943 
944 public:
945   ReturnVisitor(TrackerRef ParentTracker, const StackFrameContext *Frame,
946                 bool Suppressed, AnalyzerOptions &Options,
947                 bugreporter::TrackingKind TKind)
948       : TrackingBugReporterVisitor(ParentTracker), CalleeSFC(Frame),
949         EnableNullFPSuppression(Suppressed), Options(Options), TKind(TKind) {}
950 
951   static void *getTag() {
952     static int Tag = 0;
953     return static_cast<void *>(&Tag);
954   }
955 
956   void Profile(llvm::FoldingSetNodeID &ID) const override {
957     ID.AddPointer(ReturnVisitor::getTag());
958     ID.AddPointer(CalleeSFC);
959     ID.AddBoolean(EnableNullFPSuppression);
960   }
961 
962   PathDiagnosticPieceRef visitNodeInitial(const ExplodedNode *N,
963                                           BugReporterContext &BRC,
964                                           PathSensitiveBugReport &BR) {
965     // Only print a message at the interesting return statement.
966     if (N->getLocationContext() != CalleeSFC)
967       return nullptr;
968 
969     Optional<StmtPoint> SP = N->getLocationAs<StmtPoint>();
970     if (!SP)
971       return nullptr;
972 
973     const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
974     if (!Ret)
975       return nullptr;
976 
977     // Okay, we're at the right return statement, but do we have the return
978     // value available?
979     ProgramStateRef State = N->getState();
980     SVal V = State->getSVal(Ret, CalleeSFC);
981     if (V.isUnknownOrUndef())
982       return nullptr;
983 
984     // Don't print any more notes after this one.
985     Mode = Satisfied;
986 
987     const Expr *RetE = Ret->getRetValue();
988     assert(RetE && "Tracking a return value for a void function");
989 
990     // Handle cases where a reference is returned and then immediately used.
991     Optional<Loc> LValue;
992     if (RetE->isGLValue()) {
993       if ((LValue = V.getAs<Loc>())) {
994         SVal RValue = State->getRawSVal(*LValue, RetE->getType());
995         if (RValue.getAs<DefinedSVal>())
996           V = RValue;
997       }
998     }
999 
1000     // Ignore aggregate rvalues.
1001     if (V.getAs<nonloc::LazyCompoundVal>() ||
1002         V.getAs<nonloc::CompoundVal>())
1003       return nullptr;
1004 
1005     RetE = RetE->IgnoreParenCasts();
1006 
1007     // Let's track the return value.
1008     getParentTracker().track(RetE, N, {TKind, EnableNullFPSuppression});
1009 
1010     // Build an appropriate message based on the return value.
1011     SmallString<64> Msg;
1012     llvm::raw_svector_ostream Out(Msg);
1013 
1014     bool WouldEventBeMeaningless = false;
1015 
1016     if (State->isNull(V).isConstrainedTrue()) {
1017       if (V.getAs<Loc>()) {
1018 
1019         // If we have counter-suppression enabled, make sure we keep visiting
1020         // future nodes. We want to emit a path note as well, in case
1021         // the report is resurrected as valid later on.
1022         if (EnableNullFPSuppression &&
1023             Options.ShouldAvoidSuppressingNullArgumentPaths)
1024           Mode = MaybeUnsuppress;
1025 
1026         if (RetE->getType()->isObjCObjectPointerType()) {
1027           Out << "Returning nil";
1028         } else {
1029           Out << "Returning null pointer";
1030         }
1031       } else {
1032         Out << "Returning zero";
1033       }
1034 
1035     } else {
1036       if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
1037         Out << "Returning the value " << CI->getValue();
1038       } else {
1039         // There is nothing interesting about returning a value, when it is
1040         // plain value without any constraints, and the function is guaranteed
1041         // to return that every time. We could use CFG::isLinear() here, but
1042         // constexpr branches are obvious to the compiler, not necesserily to
1043         // the programmer.
1044         if (N->getCFG().size() == 3)
1045           WouldEventBeMeaningless = true;
1046 
1047         if (V.getAs<Loc>())
1048           Out << "Returning pointer";
1049         else
1050           Out << "Returning value";
1051       }
1052     }
1053 
1054     if (LValue) {
1055       if (const MemRegion *MR = LValue->getAsRegion()) {
1056         if (MR->canPrintPretty()) {
1057           Out << " (reference to ";
1058           MR->printPretty(Out);
1059           Out << ")";
1060         }
1061       }
1062     } else {
1063       // FIXME: We should have a more generalized location printing mechanism.
1064       if (const auto *DR = dyn_cast<DeclRefExpr>(RetE))
1065         if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
1066           Out << " (loaded from '" << *DD << "')";
1067     }
1068 
1069     PathDiagnosticLocation L(Ret, BRC.getSourceManager(), CalleeSFC);
1070     if (!L.isValid() || !L.asLocation().isValid())
1071       return nullptr;
1072 
1073     if (TKind == bugreporter::TrackingKind::Condition)
1074       Out << WillBeUsedForACondition;
1075 
1076     auto EventPiece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
1077 
1078     // If we determined that the note is meaningless, make it prunable, and
1079     // don't mark the stackframe interesting.
1080     if (WouldEventBeMeaningless)
1081       EventPiece->setPrunable(true);
1082     else
1083       BR.markInteresting(CalleeSFC);
1084 
1085     return EventPiece;
1086   }
1087 
1088   PathDiagnosticPieceRef visitNodeMaybeUnsuppress(const ExplodedNode *N,
1089                                                   BugReporterContext &BRC,
1090                                                   PathSensitiveBugReport &BR) {
1091     assert(Options.ShouldAvoidSuppressingNullArgumentPaths);
1092 
1093     // Are we at the entry node for this call?
1094     Optional<CallEnter> CE = N->getLocationAs<CallEnter>();
1095     if (!CE)
1096       return nullptr;
1097 
1098     if (CE->getCalleeContext() != CalleeSFC)
1099       return nullptr;
1100 
1101     Mode = Satisfied;
1102 
1103     // Don't automatically suppress a report if one of the arguments is
1104     // known to be a null pointer. Instead, start tracking /that/ null
1105     // value back to its origin.
1106     ProgramStateManager &StateMgr = BRC.getStateManager();
1107     CallEventManager &CallMgr = StateMgr.getCallEventManager();
1108 
1109     ProgramStateRef State = N->getState();
1110     CallEventRef<> Call = CallMgr.getCaller(CalleeSFC, State);
1111     for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
1112       Optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>();
1113       if (!ArgV)
1114         continue;
1115 
1116       const Expr *ArgE = Call->getArgExpr(I);
1117       if (!ArgE)
1118         continue;
1119 
1120       // Is it possible for this argument to be non-null?
1121       if (!State->isNull(*ArgV).isConstrainedTrue())
1122         continue;
1123 
1124       if (getParentTracker()
1125               .track(ArgE, N, {TKind, EnableNullFPSuppression})
1126               .FoundSomethingToTrack)
1127         ShouldInvalidate = false;
1128 
1129       // If we /can't/ track the null pointer, we should err on the side of
1130       // false negatives, and continue towards marking this report invalid.
1131       // (We will still look at the other arguments, though.)
1132     }
1133 
1134     return nullptr;
1135   }
1136 
1137   PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1138                                    BugReporterContext &BRC,
1139                                    PathSensitiveBugReport &BR) override {
1140     switch (Mode) {
1141     case Initial:
1142       return visitNodeInitial(N, BRC, BR);
1143     case MaybeUnsuppress:
1144       return visitNodeMaybeUnsuppress(N, BRC, BR);
1145     case Satisfied:
1146       return nullptr;
1147     }
1148 
1149     llvm_unreachable("Invalid visit mode!");
1150   }
1151 
1152   void finalizeVisitor(BugReporterContext &, const ExplodedNode *,
1153                        PathSensitiveBugReport &BR) override {
1154     if (EnableNullFPSuppression && ShouldInvalidate)
1155       BR.markInvalid(ReturnVisitor::getTag(), CalleeSFC);
1156   }
1157 };
1158 
1159 } // end of anonymous namespace
1160 
1161 //===----------------------------------------------------------------------===//
1162 //                               StoreSiteFinder
1163 //===----------------------------------------------------------------------===//
1164 
1165 /// Finds last store into the given region,
1166 /// which is different from a given symbolic value.
1167 class StoreSiteFinder final : public TrackingBugReporterVisitor {
1168   const MemRegion *R;
1169   SVal V;
1170   bool Satisfied = false;
1171 
1172   TrackingOptions Options;
1173   const StackFrameContext *OriginSFC;
1174 
1175 public:
1176   /// \param V We're searching for the store where \c R received this value.
1177   /// \param R The region we're tracking.
1178   /// \param TKind May limit the amount of notes added to the bug report.
1179   /// \param OriginSFC Only adds notes when the last store happened in a
1180   ///        different stackframe to this one. Disregarded if the tracking kind
1181   ///        is thorough.
1182   ///        This is useful, because for non-tracked regions, notes about
1183   ///        changes to its value in a nested stackframe could be pruned, and
1184   ///        this visitor can prevent that without polluting the bugpath too
1185   ///        much.
1186   StoreSiteFinder(bugreporter::TrackerRef ParentTracker, KnownSVal V,
1187                   const MemRegion *R, TrackingOptions Options,
1188                   const StackFrameContext *OriginSFC = nullptr)
1189       : TrackingBugReporterVisitor(ParentTracker), R(R), V(V), Options(Options),
1190         OriginSFC(OriginSFC) {
1191     assert(R);
1192   }
1193 
1194   void Profile(llvm::FoldingSetNodeID &ID) const override;
1195 
1196   PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1197                                    BugReporterContext &BRC,
1198                                    PathSensitiveBugReport &BR) override;
1199 };
1200 
1201 void StoreSiteFinder::Profile(llvm::FoldingSetNodeID &ID) const {
1202   static int tag = 0;
1203   ID.AddPointer(&tag);
1204   ID.AddPointer(R);
1205   ID.Add(V);
1206   ID.AddInteger(static_cast<int>(Options.Kind));
1207   ID.AddBoolean(Options.EnableNullFPSuppression);
1208 }
1209 
1210 /// Returns true if \p N represents the DeclStmt declaring and initializing
1211 /// \p VR.
1212 static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) {
1213   Optional<PostStmt> P = N->getLocationAs<PostStmt>();
1214   if (!P)
1215     return false;
1216 
1217   const DeclStmt *DS = P->getStmtAs<DeclStmt>();
1218   if (!DS)
1219     return false;
1220 
1221   if (DS->getSingleDecl() != VR->getDecl())
1222     return false;
1223 
1224   const MemSpaceRegion *VarSpace = VR->getMemorySpace();
1225   const auto *FrameSpace = dyn_cast<StackSpaceRegion>(VarSpace);
1226   if (!FrameSpace) {
1227     // If we ever directly evaluate global DeclStmts, this assertion will be
1228     // invalid, but this still seems preferable to silently accepting an
1229     // initialization that may be for a path-sensitive variable.
1230     assert(VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion");
1231     return true;
1232   }
1233 
1234   assert(VR->getDecl()->hasLocalStorage());
1235   const LocationContext *LCtx = N->getLocationContext();
1236   return FrameSpace->getStackFrame() == LCtx->getStackFrame();
1237 }
1238 
1239 static bool isObjCPointer(const MemRegion *R) {
1240   if (R->isBoundable())
1241     if (const auto *TR = dyn_cast<TypedValueRegion>(R))
1242       return TR->getValueType()->isObjCObjectPointerType();
1243 
1244   return false;
1245 }
1246 
1247 static bool isObjCPointer(const ValueDecl *D) {
1248   return D->getType()->isObjCObjectPointerType();
1249 }
1250 
1251 /// Show diagnostics for initializing or declaring a region \p R with a bad value.
1252 static void showBRDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI) {
1253   const bool HasPrefix = SI.Dest->canPrintPretty();
1254 
1255   if (HasPrefix) {
1256     SI.Dest->printPretty(OS);
1257     OS << " ";
1258   }
1259 
1260   const char *Action = nullptr;
1261 
1262   switch (SI.StoreKind) {
1263   case StoreInfo::Initialization:
1264     Action = HasPrefix ? "initialized to " : "Initializing to ";
1265     break;
1266   case StoreInfo::BlockCapture:
1267     Action = HasPrefix ? "captured by block as " : "Captured by block as ";
1268     break;
1269   default:
1270     llvm_unreachable("Unexpected store kind");
1271   }
1272 
1273   if (SI.Value.getAs<loc::ConcreteInt>()) {
1274     OS << Action << (isObjCPointer(SI.Dest) ? "nil" : "a null pointer value");
1275 
1276   } else if (auto CVal = SI.Value.getAs<nonloc::ConcreteInt>()) {
1277     OS << Action << CVal->getValue();
1278 
1279   } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1280     OS << Action << "the value of ";
1281     SI.Origin->printPretty(OS);
1282 
1283   } else if (SI.StoreKind == StoreInfo::Initialization) {
1284     // We don't need to check here, all these conditions were
1285     // checked by StoreSiteFinder, when it figured out that it is
1286     // initialization.
1287     const auto *DS =
1288         cast<DeclStmt>(SI.StoreSite->getLocationAs<PostStmt>()->getStmt());
1289 
1290     if (SI.Value.isUndef()) {
1291       if (isa<VarRegion>(SI.Dest)) {
1292         const auto *VD = cast<VarDecl>(DS->getSingleDecl());
1293 
1294         if (VD->getInit()) {
1295           OS << (HasPrefix ? "initialized" : "Initializing")
1296              << " to a garbage value";
1297         } else {
1298           OS << (HasPrefix ? "declared" : "Declaring")
1299              << " without an initial value";
1300         }
1301       }
1302     } else {
1303       OS << (HasPrefix ? "initialized" : "Initialized") << " here";
1304     }
1305   }
1306 }
1307 
1308 /// Display diagnostics for passing bad region as a parameter.
1309 static void showBRParamDiagnostics(llvm::raw_svector_ostream &OS,
1310                                    StoreInfo SI) {
1311   const auto *VR = cast<VarRegion>(SI.Dest);
1312   const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1313 
1314   OS << "Passing ";
1315 
1316   if (SI.Value.getAs<loc::ConcreteInt>()) {
1317     OS << (isObjCPointer(Param) ? "nil object reference"
1318                                 : "null pointer value");
1319 
1320   } else if (SI.Value.isUndef()) {
1321     OS << "uninitialized value";
1322 
1323   } else if (auto CI = SI.Value.getAs<nonloc::ConcreteInt>()) {
1324     OS << "the value " << CI->getValue();
1325 
1326   } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1327     SI.Origin->printPretty(OS);
1328 
1329   } else {
1330     OS << "value";
1331   }
1332 
1333   // Printed parameter indexes are 1-based, not 0-based.
1334   unsigned Idx = Param->getFunctionScopeIndex() + 1;
1335   OS << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
1336   if (VR->canPrintPretty()) {
1337     OS << " ";
1338     VR->printPretty(OS);
1339   }
1340 }
1341 
1342 /// Show default diagnostics for storing bad region.
1343 static void showBRDefaultDiagnostics(llvm::raw_svector_ostream &OS,
1344                                      StoreInfo SI) {
1345   const bool HasSuffix = SI.Dest->canPrintPretty();
1346 
1347   if (SI.Value.getAs<loc::ConcreteInt>()) {
1348     OS << (isObjCPointer(SI.Dest) ? "nil object reference stored"
1349                                   : (HasSuffix ? "Null pointer value stored"
1350                                                : "Storing null pointer value"));
1351 
1352   } else if (SI.Value.isUndef()) {
1353     OS << (HasSuffix ? "Uninitialized value stored"
1354                      : "Storing uninitialized value");
1355 
1356   } else if (auto CV = SI.Value.getAs<nonloc::ConcreteInt>()) {
1357     if (HasSuffix)
1358       OS << "The value " << CV->getValue() << " is assigned";
1359     else
1360       OS << "Assigning " << CV->getValue();
1361 
1362   } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1363     if (HasSuffix) {
1364       OS << "The value of ";
1365       SI.Origin->printPretty(OS);
1366       OS << " is assigned";
1367     } else {
1368       OS << "Assigning the value of ";
1369       SI.Origin->printPretty(OS);
1370     }
1371 
1372   } else {
1373     OS << (HasSuffix ? "Value assigned" : "Assigning value");
1374   }
1375 
1376   if (HasSuffix) {
1377     OS << " to ";
1378     SI.Dest->printPretty(OS);
1379   }
1380 }
1381 
1382 PathDiagnosticPieceRef StoreSiteFinder::VisitNode(const ExplodedNode *Succ,
1383                                                   BugReporterContext &BRC,
1384                                                   PathSensitiveBugReport &BR) {
1385   if (Satisfied)
1386     return nullptr;
1387 
1388   const ExplodedNode *StoreSite = nullptr;
1389   const ExplodedNode *Pred = Succ->getFirstPred();
1390   const Expr *InitE = nullptr;
1391   bool IsParam = false;
1392 
1393   // First see if we reached the declaration of the region.
1394   if (const auto *VR = dyn_cast<VarRegion>(R)) {
1395     if (isInitializationOfVar(Pred, VR)) {
1396       StoreSite = Pred;
1397       InitE = VR->getDecl()->getInit();
1398     }
1399   }
1400 
1401   // If this is a post initializer expression, initializing the region, we
1402   // should track the initializer expression.
1403   if (Optional<PostInitializer> PIP = Pred->getLocationAs<PostInitializer>()) {
1404     const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue();
1405     if (FieldReg == R) {
1406       StoreSite = Pred;
1407       InitE = PIP->getInitializer()->getInit();
1408     }
1409   }
1410 
1411   // Otherwise, see if this is the store site:
1412   // (1) Succ has this binding and Pred does not, i.e. this is
1413   //     where the binding first occurred.
1414   // (2) Succ has this binding and is a PostStore node for this region, i.e.
1415   //     the same binding was re-assigned here.
1416   if (!StoreSite) {
1417     if (Succ->getState()->getSVal(R) != V)
1418       return nullptr;
1419 
1420     if (hasVisibleUpdate(Pred, Pred->getState()->getSVal(R), Succ, V)) {
1421       Optional<PostStore> PS = Succ->getLocationAs<PostStore>();
1422       if (!PS || PS->getLocationValue() != R)
1423         return nullptr;
1424     }
1425 
1426     StoreSite = Succ;
1427 
1428     // If this is an assignment expression, we can track the value
1429     // being assigned.
1430     if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
1431       if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
1432         if (BO->isAssignmentOp())
1433           InitE = BO->getRHS();
1434 
1435     // If this is a call entry, the variable should be a parameter.
1436     // FIXME: Handle CXXThisRegion as well. (This is not a priority because
1437     // 'this' should never be NULL, but this visitor isn't just for NULL and
1438     // UndefinedVal.)
1439     if (Optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) {
1440       if (const auto *VR = dyn_cast<VarRegion>(R)) {
1441 
1442         if (const auto *Param = dyn_cast<ParmVarDecl>(VR->getDecl())) {
1443           ProgramStateManager &StateMgr = BRC.getStateManager();
1444           CallEventManager &CallMgr = StateMgr.getCallEventManager();
1445 
1446           CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(),
1447                                                   Succ->getState());
1448           InitE = Call->getArgExpr(Param->getFunctionScopeIndex());
1449         } else {
1450           // Handle Objective-C 'self'.
1451           assert(isa<ImplicitParamDecl>(VR->getDecl()));
1452           InitE = cast<ObjCMessageExpr>(CE->getCalleeContext()->getCallSite())
1453                       ->getInstanceReceiver()->IgnoreParenCasts();
1454         }
1455         IsParam = true;
1456       }
1457     }
1458 
1459     // If this is a CXXTempObjectRegion, the Expr responsible for its creation
1460     // is wrapped inside of it.
1461     if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R))
1462       InitE = TmpR->getExpr();
1463   }
1464 
1465   if (!StoreSite)
1466     return nullptr;
1467 
1468   Satisfied = true;
1469 
1470   // If we have an expression that provided the value, try to track where it
1471   // came from.
1472   if (InitE) {
1473     if (!IsParam)
1474       InitE = InitE->IgnoreParenCasts();
1475 
1476     getParentTracker().track(InitE, StoreSite, Options);
1477   }
1478 
1479   // Let's try to find the region where the value came from.
1480   const MemRegion *OldRegion = nullptr;
1481 
1482   // If we have init expression, it might be simply a reference
1483   // to a variable, so we can use it.
1484   if (InitE) {
1485     // That region might still be not exactly what we are looking for.
1486     // In situations like `int &ref = val;`, we can't say that
1487     // `ref` is initialized with `val`, rather refers to `val`.
1488     //
1489     // In order, to mitigate situations like this, we check if the last
1490     // stored value in that region is the value that we track.
1491     //
1492     // TODO: support other situations better.
1493     if (const MemRegion *Candidate =
1494             getLocationRegionIfReference(InitE, Succ, false)) {
1495       const StoreManager &SM = BRC.getStateManager().getStoreManager();
1496 
1497       // Here we traverse the graph up to find the last node where the
1498       // candidate region is still in the store.
1499       for (const ExplodedNode *N = StoreSite; N; N = N->getFirstPred()) {
1500         if (SM.includedInBindings(N->getState()->getStore(), Candidate)) {
1501           // And if it was bound to the target value, we can use it.
1502           if (N->getState()->getSVal(Candidate) == V) {
1503             OldRegion = Candidate;
1504           }
1505           break;
1506         }
1507       }
1508     }
1509   }
1510 
1511   // Otherwise, if the current region does indeed contain the value
1512   // we are looking for, we can look for a region where this value
1513   // was before.
1514   //
1515   // It can be useful for situations like:
1516   //     new = identity(old)
1517   // where the analyzer knows that 'identity' returns the value of its
1518   // first argument.
1519   //
1520   // NOTE: If the region R is not a simple var region, it can contain
1521   //       V in one of its subregions.
1522   if (!OldRegion && StoreSite->getState()->getSVal(R) == V) {
1523     // Let's go up the graph to find the node where the region is
1524     // bound to V.
1525     const ExplodedNode *NodeWithoutBinding = StoreSite->getFirstPred();
1526     for (;
1527          NodeWithoutBinding && NodeWithoutBinding->getState()->getSVal(R) == V;
1528          NodeWithoutBinding = NodeWithoutBinding->getFirstPred()) {
1529     }
1530 
1531     if (NodeWithoutBinding) {
1532       // Let's try to find a unique binding for the value in that node.
1533       // We want to use this to find unique bindings because of the following
1534       // situations:
1535       //     b = a;
1536       //     c = identity(b);
1537       //
1538       // Telling the user that the value of 'a' is assigned to 'c', while
1539       // correct, can be confusing.
1540       StoreManager::FindUniqueBinding FB(V.getAsLocSymbol());
1541       BRC.getStateManager().iterBindings(NodeWithoutBinding->getState(), FB);
1542       if (FB)
1543         OldRegion = FB.getRegion();
1544     }
1545   }
1546 
1547   if (Options.Kind == TrackingKind::Condition && OriginSFC &&
1548       !OriginSFC->isParentOf(StoreSite->getStackFrame()))
1549     return nullptr;
1550 
1551   // Okay, we've found the binding. Emit an appropriate message.
1552   SmallString<256> sbuf;
1553   llvm::raw_svector_ostream os(sbuf);
1554 
1555   StoreInfo SI = {StoreInfo::Assignment, // default kind
1556                   StoreSite,
1557                   InitE,
1558                   V,
1559                   R,
1560                   OldRegion};
1561 
1562   if (Optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) {
1563     const Stmt *S = PS->getStmt();
1564     const auto *DS = dyn_cast<DeclStmt>(S);
1565     const auto *VR = dyn_cast<VarRegion>(R);
1566 
1567     if (DS) {
1568       SI.StoreKind = StoreInfo::Initialization;
1569     } else if (isa<BlockExpr>(S)) {
1570       SI.StoreKind = StoreInfo::BlockCapture;
1571       if (VR) {
1572         // See if we can get the BlockVarRegion.
1573         ProgramStateRef State = StoreSite->getState();
1574         SVal V = StoreSite->getSVal(S);
1575         if (const auto *BDR =
1576                 dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
1577           if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) {
1578             getParentTracker().track(State->getSVal(OriginalR), OriginalR,
1579                                      Options, OriginSFC);
1580           }
1581         }
1582       }
1583     }
1584   } else if (SI.StoreSite->getLocation().getAs<CallEnter>() &&
1585              isa<VarRegion>(SI.Dest)) {
1586     SI.StoreKind = StoreInfo::CallArgument;
1587   }
1588 
1589   return getParentTracker().handle(SI, BRC, Options);
1590 }
1591 
1592 //===----------------------------------------------------------------------===//
1593 // Implementation of TrackConstraintBRVisitor.
1594 //===----------------------------------------------------------------------===//
1595 
1596 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1597   static int tag = 0;
1598   ID.AddPointer(&tag);
1599   ID.AddBoolean(Assumption);
1600   ID.Add(Constraint);
1601 }
1602 
1603 /// Return the tag associated with this visitor.  This tag will be used
1604 /// to make all PathDiagnosticPieces created by this visitor.
1605 const char *TrackConstraintBRVisitor::getTag() {
1606   return "TrackConstraintBRVisitor";
1607 }
1608 
1609 bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const {
1610   if (IsZeroCheck)
1611     return N->getState()->isNull(Constraint).isUnderconstrained();
1612   return (bool)N->getState()->assume(Constraint, !Assumption);
1613 }
1614 
1615 PathDiagnosticPieceRef TrackConstraintBRVisitor::VisitNode(
1616     const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &) {
1617   const ExplodedNode *PrevN = N->getFirstPred();
1618   if (IsSatisfied)
1619     return nullptr;
1620 
1621   // Start tracking after we see the first state in which the value is
1622   // constrained.
1623   if (!IsTrackingTurnedOn)
1624     if (!isUnderconstrained(N))
1625       IsTrackingTurnedOn = true;
1626   if (!IsTrackingTurnedOn)
1627     return nullptr;
1628 
1629   // Check if in the previous state it was feasible for this constraint
1630   // to *not* be true.
1631   if (isUnderconstrained(PrevN)) {
1632     IsSatisfied = true;
1633 
1634     // As a sanity check, make sure that the negation of the constraint
1635     // was infeasible in the current state.  If it is feasible, we somehow
1636     // missed the transition point.
1637     assert(!isUnderconstrained(N));
1638 
1639     // We found the transition point for the constraint.  We now need to
1640     // pretty-print the constraint. (work-in-progress)
1641     SmallString<64> sbuf;
1642     llvm::raw_svector_ostream os(sbuf);
1643 
1644     if (Constraint.getAs<Loc>()) {
1645       os << "Assuming pointer value is ";
1646       os << (Assumption ? "non-null" : "null");
1647     }
1648 
1649     if (os.str().empty())
1650       return nullptr;
1651 
1652     // Construct a new PathDiagnosticPiece.
1653     ProgramPoint P = N->getLocation();
1654     PathDiagnosticLocation L =
1655       PathDiagnosticLocation::create(P, BRC.getSourceManager());
1656     if (!L.isValid())
1657       return nullptr;
1658 
1659     auto X = std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1660     X->setTag(getTag());
1661     return std::move(X);
1662   }
1663 
1664   return nullptr;
1665 }
1666 
1667 //===----------------------------------------------------------------------===//
1668 // Implementation of SuppressInlineDefensiveChecksVisitor.
1669 //===----------------------------------------------------------------------===//
1670 
1671 SuppressInlineDefensiveChecksVisitor::
1672 SuppressInlineDefensiveChecksVisitor(DefinedSVal Value, const ExplodedNode *N)
1673     : V(Value) {
1674   // Check if the visitor is disabled.
1675   AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
1676   if (!Options.ShouldSuppressInlinedDefensiveChecks)
1677     IsSatisfied = true;
1678 }
1679 
1680 void SuppressInlineDefensiveChecksVisitor::Profile(
1681     llvm::FoldingSetNodeID &ID) const {
1682   static int id = 0;
1683   ID.AddPointer(&id);
1684   ID.Add(V);
1685 }
1686 
1687 const char *SuppressInlineDefensiveChecksVisitor::getTag() {
1688   return "IDCVisitor";
1689 }
1690 
1691 PathDiagnosticPieceRef
1692 SuppressInlineDefensiveChecksVisitor::VisitNode(const ExplodedNode *Succ,
1693                                                 BugReporterContext &BRC,
1694                                                 PathSensitiveBugReport &BR) {
1695   const ExplodedNode *Pred = Succ->getFirstPred();
1696   if (IsSatisfied)
1697     return nullptr;
1698 
1699   // Start tracking after we see the first state in which the value is null.
1700   if (!IsTrackingTurnedOn)
1701     if (Succ->getState()->isNull(V).isConstrainedTrue())
1702       IsTrackingTurnedOn = true;
1703   if (!IsTrackingTurnedOn)
1704     return nullptr;
1705 
1706   // Check if in the previous state it was feasible for this value
1707   // to *not* be null.
1708   if (!Pred->getState()->isNull(V).isConstrainedTrue() &&
1709       Succ->getState()->isNull(V).isConstrainedTrue()) {
1710     IsSatisfied = true;
1711 
1712     // Check if this is inlined defensive checks.
1713     const LocationContext *CurLC = Succ->getLocationContext();
1714     const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext();
1715     if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) {
1716       BR.markInvalid("Suppress IDC", CurLC);
1717       return nullptr;
1718     }
1719 
1720     // Treat defensive checks in function-like macros as if they were an inlined
1721     // defensive check. If the bug location is not in a macro and the
1722     // terminator for the current location is in a macro then suppress the
1723     // warning.
1724     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
1725 
1726     if (!BugPoint)
1727       return nullptr;
1728 
1729     ProgramPoint CurPoint = Succ->getLocation();
1730     const Stmt *CurTerminatorStmt = nullptr;
1731     if (auto BE = CurPoint.getAs<BlockEdge>()) {
1732       CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt();
1733     } else if (auto SP = CurPoint.getAs<StmtPoint>()) {
1734       const Stmt *CurStmt = SP->getStmt();
1735       if (!CurStmt->getBeginLoc().isMacroID())
1736         return nullptr;
1737 
1738       CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap();
1739       CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminatorStmt();
1740     } else {
1741       return nullptr;
1742     }
1743 
1744     if (!CurTerminatorStmt)
1745       return nullptr;
1746 
1747     SourceLocation TerminatorLoc = CurTerminatorStmt->getBeginLoc();
1748     if (TerminatorLoc.isMacroID()) {
1749       SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
1750 
1751       // Suppress reports unless we are in that same macro.
1752       if (!BugLoc.isMacroID() ||
1753           getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) {
1754         BR.markInvalid("Suppress Macro IDC", CurLC);
1755       }
1756       return nullptr;
1757     }
1758   }
1759   return nullptr;
1760 }
1761 
1762 //===----------------------------------------------------------------------===//
1763 // TrackControlDependencyCondBRVisitor.
1764 //===----------------------------------------------------------------------===//
1765 
1766 namespace {
1767 /// Tracks the expressions that are a control dependency of the node that was
1768 /// supplied to the constructor.
1769 /// For example:
1770 ///
1771 ///   cond = 1;
1772 ///   if (cond)
1773 ///     10 / 0;
1774 ///
1775 /// An error is emitted at line 3. This visitor realizes that the branch
1776 /// on line 2 is a control dependency of line 3, and tracks it's condition via
1777 /// trackExpressionValue().
1778 class TrackControlDependencyCondBRVisitor final
1779     : public TrackingBugReporterVisitor {
1780   const ExplodedNode *Origin;
1781   ControlDependencyCalculator ControlDeps;
1782   llvm::SmallSet<const CFGBlock *, 32> VisitedBlocks;
1783 
1784 public:
1785   TrackControlDependencyCondBRVisitor(TrackerRef ParentTracker,
1786                                       const ExplodedNode *O)
1787       : TrackingBugReporterVisitor(ParentTracker), Origin(O),
1788         ControlDeps(&O->getCFG()) {}
1789 
1790   void Profile(llvm::FoldingSetNodeID &ID) const override {
1791     static int x = 0;
1792     ID.AddPointer(&x);
1793   }
1794 
1795   PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1796                                    BugReporterContext &BRC,
1797                                    PathSensitiveBugReport &BR) override;
1798 };
1799 } // end of anonymous namespace
1800 
1801 static std::shared_ptr<PathDiagnosticEventPiece>
1802 constructDebugPieceForTrackedCondition(const Expr *Cond,
1803                                        const ExplodedNode *N,
1804                                        BugReporterContext &BRC) {
1805 
1806   if (BRC.getAnalyzerOptions().AnalysisDiagOpt == PD_NONE ||
1807       !BRC.getAnalyzerOptions().ShouldTrackConditionsDebug)
1808     return nullptr;
1809 
1810   std::string ConditionText = std::string(Lexer::getSourceText(
1811       CharSourceRange::getTokenRange(Cond->getSourceRange()),
1812       BRC.getSourceManager(), BRC.getASTContext().getLangOpts()));
1813 
1814   return std::make_shared<PathDiagnosticEventPiece>(
1815       PathDiagnosticLocation::createBegin(
1816           Cond, BRC.getSourceManager(), N->getLocationContext()),
1817           (Twine() + "Tracking condition '" + ConditionText + "'").str());
1818 }
1819 
1820 static bool isAssertlikeBlock(const CFGBlock *B, ASTContext &Context) {
1821   if (B->succ_size() != 2)
1822     return false;
1823 
1824   const CFGBlock *Then = B->succ_begin()->getReachableBlock();
1825   const CFGBlock *Else = (B->succ_begin() + 1)->getReachableBlock();
1826 
1827   if (!Then || !Else)
1828     return false;
1829 
1830   if (Then->isInevitablySinking() != Else->isInevitablySinking())
1831     return true;
1832 
1833   // For the following condition the following CFG would be built:
1834   //
1835   //                          ------------->
1836   //                         /              \
1837   //                       [B1] -> [B2] -> [B3] -> [sink]
1838   // assert(A && B || C);            \       \
1839   //                                  -----------> [go on with the execution]
1840   //
1841   // It so happens that CFGBlock::getTerminatorCondition returns 'A' for block
1842   // B1, 'A && B' for B2, and 'A && B || C' for B3. Let's check whether we
1843   // reached the end of the condition!
1844   if (const Stmt *ElseCond = Else->getTerminatorCondition())
1845     if (const auto *BinOp = dyn_cast<BinaryOperator>(ElseCond))
1846       if (BinOp->isLogicalOp())
1847         return isAssertlikeBlock(Else, Context);
1848 
1849   return false;
1850 }
1851 
1852 PathDiagnosticPieceRef
1853 TrackControlDependencyCondBRVisitor::VisitNode(const ExplodedNode *N,
1854                                                BugReporterContext &BRC,
1855                                                PathSensitiveBugReport &BR) {
1856   // We can only reason about control dependencies within the same stack frame.
1857   if (Origin->getStackFrame() != N->getStackFrame())
1858     return nullptr;
1859 
1860   CFGBlock *NB = const_cast<CFGBlock *>(N->getCFGBlock());
1861 
1862   // Skip if we already inspected this block.
1863   if (!VisitedBlocks.insert(NB).second)
1864     return nullptr;
1865 
1866   CFGBlock *OriginB = const_cast<CFGBlock *>(Origin->getCFGBlock());
1867 
1868   // TODO: Cache CFGBlocks for each ExplodedNode.
1869   if (!OriginB || !NB)
1870     return nullptr;
1871 
1872   if (isAssertlikeBlock(NB, BRC.getASTContext()))
1873     return nullptr;
1874 
1875   if (ControlDeps.isControlDependent(OriginB, NB)) {
1876     // We don't really want to explain for range loops. Evidence suggests that
1877     // the only thing that leads to is the addition of calls to operator!=.
1878     if (llvm::isa_and_nonnull<CXXForRangeStmt>(NB->getTerminatorStmt()))
1879       return nullptr;
1880 
1881     if (const Expr *Condition = NB->getLastCondition()) {
1882       // Keeping track of the already tracked conditions on a visitor level
1883       // isn't sufficient, because a new visitor is created for each tracked
1884       // expression, hence the BugReport level set.
1885       if (BR.addTrackedCondition(N)) {
1886         getParentTracker().track(Condition, N,
1887                                  {bugreporter::TrackingKind::Condition,
1888                                   /*EnableNullFPSuppression=*/false});
1889         return constructDebugPieceForTrackedCondition(Condition, N, BRC);
1890       }
1891     }
1892   }
1893 
1894   return nullptr;
1895 }
1896 
1897 //===----------------------------------------------------------------------===//
1898 // Implementation of trackExpressionValue.
1899 //===----------------------------------------------------------------------===//
1900 
1901 /// \return A subexpression of @c Ex which represents the
1902 /// expression-of-interest.
1903 static const Expr *peelOffOuterExpr(const Expr *Ex,
1904                                     const ExplodedNode *N) {
1905   Ex = Ex->IgnoreParenCasts();
1906   if (const auto *FE = dyn_cast<FullExpr>(Ex))
1907     return peelOffOuterExpr(FE->getSubExpr(), N);
1908   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex))
1909     return peelOffOuterExpr(OVE->getSourceExpr(), N);
1910   if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) {
1911     const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
1912     if (PropRef && PropRef->isMessagingGetter()) {
1913       const Expr *GetterMessageSend =
1914           POE->getSemanticExpr(POE->getNumSemanticExprs() - 1);
1915       assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts()));
1916       return peelOffOuterExpr(GetterMessageSend, N);
1917     }
1918   }
1919 
1920   // Peel off the ternary operator.
1921   if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) {
1922     // Find a node where the branching occurred and find out which branch
1923     // we took (true/false) by looking at the ExplodedGraph.
1924     const ExplodedNode *NI = N;
1925     do {
1926       ProgramPoint ProgPoint = NI->getLocation();
1927       if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
1928         const CFGBlock *srcBlk = BE->getSrc();
1929         if (const Stmt *term = srcBlk->getTerminatorStmt()) {
1930           if (term == CO) {
1931             bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst());
1932             if (TookTrueBranch)
1933               return peelOffOuterExpr(CO->getTrueExpr(), N);
1934             else
1935               return peelOffOuterExpr(CO->getFalseExpr(), N);
1936           }
1937         }
1938       }
1939       NI = NI->getFirstPred();
1940     } while (NI);
1941   }
1942 
1943   if (auto *BO = dyn_cast<BinaryOperator>(Ex))
1944     if (const Expr *SubEx = peelOffPointerArithmetic(BO))
1945       return peelOffOuterExpr(SubEx, N);
1946 
1947   if (auto *UO = dyn_cast<UnaryOperator>(Ex)) {
1948     if (UO->getOpcode() == UO_LNot)
1949       return peelOffOuterExpr(UO->getSubExpr(), N);
1950 
1951     // FIXME: There's a hack in our Store implementation that always computes
1952     // field offsets around null pointers as if they are always equal to 0.
1953     // The idea here is to report accesses to fields as null dereferences
1954     // even though the pointer value that's being dereferenced is actually
1955     // the offset of the field rather than exactly 0.
1956     // See the FIXME in StoreManager's getLValueFieldOrIvar() method.
1957     // This code interacts heavily with this hack; otherwise the value
1958     // would not be null at all for most fields, so we'd be unable to track it.
1959     if (UO->getOpcode() == UO_AddrOf && UO->getSubExpr()->isLValue())
1960       if (const Expr *DerefEx = bugreporter::getDerefExpr(UO->getSubExpr()))
1961         return peelOffOuterExpr(DerefEx, N);
1962   }
1963 
1964   return Ex;
1965 }
1966 
1967 /// Find the ExplodedNode where the lvalue (the value of 'Ex')
1968 /// was computed.
1969 static const ExplodedNode* findNodeForExpression(const ExplodedNode *N,
1970                                                  const Expr *Inner) {
1971   while (N) {
1972     if (N->getStmtForDiagnostics() == Inner)
1973       return N;
1974     N = N->getFirstPred();
1975   }
1976   return N;
1977 }
1978 
1979 //===----------------------------------------------------------------------===//
1980 //                            Tracker implementation
1981 //===----------------------------------------------------------------------===//
1982 
1983 PathDiagnosticPieceRef StoreHandler::constructNote(StoreInfo SI,
1984                                                    BugReporterContext &BRC,
1985                                                    StringRef NodeText) {
1986   // Construct a new PathDiagnosticPiece.
1987   ProgramPoint P = SI.StoreSite->getLocation();
1988   PathDiagnosticLocation L;
1989   if (P.getAs<CallEnter>() && SI.SourceOfTheValue)
1990     L = PathDiagnosticLocation(SI.SourceOfTheValue, BRC.getSourceManager(),
1991                                P.getLocationContext());
1992 
1993   if (!L.isValid() || !L.asLocation().isValid())
1994     L = PathDiagnosticLocation::create(P, BRC.getSourceManager());
1995 
1996   if (!L.isValid() || !L.asLocation().isValid())
1997     return nullptr;
1998 
1999   return std::make_shared<PathDiagnosticEventPiece>(L, NodeText);
2000 }
2001 
2002 class DefaultStoreHandler final : public StoreHandler {
2003 public:
2004   using StoreHandler::StoreHandler;
2005 
2006   PathDiagnosticPieceRef handle(StoreInfo SI, BugReporterContext &BRC,
2007                                 TrackingOptions Opts) override {
2008     // Okay, we've found the binding. Emit an appropriate message.
2009     SmallString<256> Buffer;
2010     llvm::raw_svector_ostream OS(Buffer);
2011 
2012     switch (SI.StoreKind) {
2013     case StoreInfo::Initialization:
2014     case StoreInfo::BlockCapture:
2015       showBRDiagnostics(OS, SI);
2016       break;
2017     case StoreInfo::CallArgument:
2018       showBRParamDiagnostics(OS, SI);
2019       break;
2020     case StoreInfo::Assignment:
2021       showBRDefaultDiagnostics(OS, SI);
2022       break;
2023     }
2024 
2025     if (Opts.Kind == bugreporter::TrackingKind::Condition)
2026       OS << WillBeUsedForACondition;
2027 
2028     return constructNote(SI, BRC, OS.str());
2029   }
2030 };
2031 
2032 class ControlDependencyHandler final : public ExpressionHandler {
2033 public:
2034   using ExpressionHandler::ExpressionHandler;
2035 
2036   Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2037                          const ExplodedNode *LVNode,
2038                          TrackingOptions Opts) override {
2039     PathSensitiveBugReport &Report = getParentTracker().getReport();
2040 
2041     // We only track expressions if we believe that they are important. Chances
2042     // are good that control dependencies to the tracking point are also
2043     // important because of this, let's explain why we believe control reached
2044     // this point.
2045     // TODO: Shouldn't we track control dependencies of every bug location,
2046     // rather than only tracked expressions?
2047     if (LVNode->getState()
2048             ->getAnalysisManager()
2049             .getAnalyzerOptions()
2050             .ShouldTrackConditions) {
2051       Report.addVisitor<TrackControlDependencyCondBRVisitor>(
2052           &getParentTracker(), InputNode);
2053       return {/*FoundSomethingToTrack=*/true};
2054     }
2055 
2056     return {};
2057   }
2058 };
2059 
2060 class NilReceiverHandler final : public ExpressionHandler {
2061 public:
2062   using ExpressionHandler::ExpressionHandler;
2063 
2064   Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2065                          const ExplodedNode *LVNode,
2066                          TrackingOptions Opts) override {
2067     // The message send could be nil due to the receiver being nil.
2068     // At this point in the path, the receiver should be live since we are at
2069     // the message send expr. If it is nil, start tracking it.
2070     if (const Expr *Receiver =
2071             NilReceiverBRVisitor::getNilReceiver(Inner, LVNode))
2072       return getParentTracker().track(Receiver, LVNode, Opts);
2073 
2074     return {};
2075   }
2076 };
2077 
2078 class ArrayIndexHandler final : public ExpressionHandler {
2079 public:
2080   using ExpressionHandler::ExpressionHandler;
2081 
2082   Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2083                          const ExplodedNode *LVNode,
2084                          TrackingOptions Opts) override {
2085     // Track the index if this is an array subscript.
2086     if (const auto *Arr = dyn_cast<ArraySubscriptExpr>(Inner))
2087       return getParentTracker().track(
2088           Arr->getIdx(), LVNode,
2089           {Opts.Kind, /*EnableNullFPSuppression*/ false});
2090 
2091     return {};
2092   }
2093 };
2094 
2095 // TODO: extract it into more handlers
2096 class InterestingLValueHandler final : public ExpressionHandler {
2097 public:
2098   using ExpressionHandler::ExpressionHandler;
2099 
2100   Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2101                          const ExplodedNode *LVNode,
2102                          TrackingOptions Opts) override {
2103     ProgramStateRef LVState = LVNode->getState();
2104     const StackFrameContext *SFC = LVNode->getStackFrame();
2105     PathSensitiveBugReport &Report = getParentTracker().getReport();
2106     Tracker::Result Result;
2107 
2108     // See if the expression we're interested refers to a variable.
2109     // If so, we can track both its contents and constraints on its value.
2110     if (ExplodedGraph::isInterestingLValueExpr(Inner)) {
2111       SVal LVal = LVNode->getSVal(Inner);
2112 
2113       const MemRegion *RR = getLocationRegionIfReference(Inner, LVNode);
2114       bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue();
2115 
2116       // If this is a C++ reference to a null pointer, we are tracking the
2117       // pointer. In addition, we should find the store at which the reference
2118       // got initialized.
2119       if (RR && !LVIsNull)
2120         Result.combineWith(getParentTracker().track(LVal, RR, Opts, SFC));
2121 
2122       // In case of C++ references, we want to differentiate between a null
2123       // reference and reference to null pointer.
2124       // If the LVal is null, check if we are dealing with null reference.
2125       // For those, we want to track the location of the reference.
2126       const MemRegion *R =
2127           (RR && LVIsNull) ? RR : LVNode->getSVal(Inner).getAsRegion();
2128 
2129       if (R) {
2130 
2131         // Mark both the variable region and its contents as interesting.
2132         SVal V = LVState->getRawSVal(loc::MemRegionVal(R));
2133         Report.addVisitor<NoStoreFuncVisitor>(cast<SubRegion>(R), Opts.Kind);
2134 
2135         // When we got here, we do have something to track, and we will
2136         // interrupt.
2137         Result.FoundSomethingToTrack = true;
2138         Result.WasInterrupted = true;
2139 
2140         MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary(
2141             LVNode, R, Opts.EnableNullFPSuppression, Report, V);
2142 
2143         Report.markInteresting(V, Opts.Kind);
2144         Report.addVisitor<UndefOrNullArgVisitor>(R);
2145 
2146         // If the contents are symbolic and null, find out when they became
2147         // null.
2148         if (V.getAsLocSymbol(/*IncludeBaseRegions=*/true))
2149           if (LVState->isNull(V).isConstrainedTrue())
2150             Report.addVisitor<TrackConstraintBRVisitor>(V.castAs<DefinedSVal>(),
2151                                                         false);
2152 
2153         // Add visitor, which will suppress inline defensive checks.
2154         if (auto DV = V.getAs<DefinedSVal>())
2155           if (!DV->isZeroConstant() && Opts.EnableNullFPSuppression)
2156             // Note that LVNode may be too late (i.e., too far from the
2157             // InputNode) because the lvalue may have been computed before the
2158             // inlined call was evaluated. InputNode may as well be too early
2159             // here, because the symbol is already dead; this, however, is fine
2160             // because we can still find the node in which it collapsed to null
2161             // previously.
2162             Report.addVisitor<SuppressInlineDefensiveChecksVisitor>(*DV,
2163                                                                     InputNode);
2164         getParentTracker().track(V, R, Opts, SFC);
2165       }
2166     }
2167 
2168     return Result;
2169   }
2170 };
2171 
2172 /// Adds a ReturnVisitor if the given statement represents a call that was
2173 /// inlined.
2174 ///
2175 /// This will search back through the ExplodedGraph, starting from the given
2176 /// node, looking for when the given statement was processed. If it turns out
2177 /// the statement is a call that was inlined, we add the visitor to the
2178 /// bug report, so it can print a note later.
2179 class InlinedFunctionCallHandler final : public ExpressionHandler {
2180   using ExpressionHandler::ExpressionHandler;
2181 
2182   Tracker::Result handle(const Expr *E, const ExplodedNode *InputNode,
2183                          const ExplodedNode *ExprNode,
2184                          TrackingOptions Opts) override {
2185     if (!CallEvent::isCallStmt(E))
2186       return {};
2187 
2188     // First, find when we processed the statement.
2189     // If we work with a 'CXXNewExpr' that is going to be purged away before
2190     // its call take place. We would catch that purge in the last condition
2191     // as a 'StmtPoint' so we have to bypass it.
2192     const bool BypassCXXNewExprEval = isa<CXXNewExpr>(E);
2193 
2194     // This is moving forward when we enter into another context.
2195     const StackFrameContext *CurrentSFC = ExprNode->getStackFrame();
2196 
2197     do {
2198       // If that is satisfied we found our statement as an inlined call.
2199       if (Optional<CallExitEnd> CEE = ExprNode->getLocationAs<CallExitEnd>())
2200         if (CEE->getCalleeContext()->getCallSite() == E)
2201           break;
2202 
2203       // Try to move forward to the end of the call-chain.
2204       ExprNode = ExprNode->getFirstPred();
2205       if (!ExprNode)
2206         break;
2207 
2208       const StackFrameContext *PredSFC = ExprNode->getStackFrame();
2209 
2210       // If that is satisfied we found our statement.
2211       // FIXME: This code currently bypasses the call site for the
2212       //        conservatively evaluated allocator.
2213       if (!BypassCXXNewExprEval)
2214         if (Optional<StmtPoint> SP = ExprNode->getLocationAs<StmtPoint>())
2215           // See if we do not enter into another context.
2216           if (SP->getStmt() == E && CurrentSFC == PredSFC)
2217             break;
2218 
2219       CurrentSFC = PredSFC;
2220     } while (ExprNode->getStackFrame() == CurrentSFC);
2221 
2222     // Next, step over any post-statement checks.
2223     while (ExprNode && ExprNode->getLocation().getAs<PostStmt>())
2224       ExprNode = ExprNode->getFirstPred();
2225     if (!ExprNode)
2226       return {};
2227 
2228     // Finally, see if we inlined the call.
2229     Optional<CallExitEnd> CEE = ExprNode->getLocationAs<CallExitEnd>();
2230     if (!CEE)
2231       return {};
2232 
2233     const StackFrameContext *CalleeContext = CEE->getCalleeContext();
2234     if (CalleeContext->getCallSite() != E)
2235       return {};
2236 
2237     // Check the return value.
2238     ProgramStateRef State = ExprNode->getState();
2239     SVal RetVal = ExprNode->getSVal(E);
2240 
2241     // Handle cases where a reference is returned and then immediately used.
2242     if (cast<Expr>(E)->isGLValue())
2243       if (Optional<Loc> LValue = RetVal.getAs<Loc>())
2244         RetVal = State->getSVal(*LValue);
2245 
2246     // See if the return value is NULL. If so, suppress the report.
2247     AnalyzerOptions &Options = State->getAnalysisManager().options;
2248 
2249     bool EnableNullFPSuppression = false;
2250     if (Opts.EnableNullFPSuppression && Options.ShouldSuppressNullReturnPaths)
2251       if (Optional<Loc> RetLoc = RetVal.getAs<Loc>())
2252         EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue();
2253 
2254     PathSensitiveBugReport &Report = getParentTracker().getReport();
2255     Report.addVisitor<ReturnVisitor>(&getParentTracker(), CalleeContext,
2256                                      EnableNullFPSuppression, Options,
2257                                      Opts.Kind);
2258     return {true};
2259   }
2260 };
2261 
2262 class DefaultExpressionHandler final : public ExpressionHandler {
2263 public:
2264   using ExpressionHandler::ExpressionHandler;
2265 
2266   Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2267                          const ExplodedNode *LVNode,
2268                          TrackingOptions Opts) override {
2269     ProgramStateRef LVState = LVNode->getState();
2270     const StackFrameContext *SFC = LVNode->getStackFrame();
2271     PathSensitiveBugReport &Report = getParentTracker().getReport();
2272     Tracker::Result Result;
2273 
2274     // If the expression is not an "lvalue expression", we can still
2275     // track the constraints on its contents.
2276     SVal V = LVState->getSValAsScalarOrLoc(Inner, LVNode->getLocationContext());
2277 
2278     // Is it a symbolic value?
2279     if (auto L = V.getAs<loc::MemRegionVal>()) {
2280       // FIXME: this is a hack for fixing a later crash when attempting to
2281       // dereference a void* pointer.
2282       // We should not try to dereference pointers at all when we don't care
2283       // what is written inside the pointer.
2284       bool CanDereference = true;
2285       if (const auto *SR = L->getRegionAs<SymbolicRegion>()) {
2286         if (SR->getSymbol()->getType()->getPointeeType()->isVoidType())
2287           CanDereference = false;
2288       } else if (L->getRegionAs<AllocaRegion>())
2289         CanDereference = false;
2290 
2291       // At this point we are dealing with the region's LValue.
2292       // However, if the rvalue is a symbolic region, we should track it as
2293       // well. Try to use the correct type when looking up the value.
2294       SVal RVal;
2295       if (ExplodedGraph::isInterestingLValueExpr(Inner))
2296         RVal = LVState->getRawSVal(L.getValue(), Inner->getType());
2297       else if (CanDereference)
2298         RVal = LVState->getSVal(L->getRegion());
2299 
2300       if (CanDereference) {
2301         Report.addVisitor<UndefOrNullArgVisitor>(L->getRegion());
2302         Result.FoundSomethingToTrack = true;
2303 
2304         if (auto KV = RVal.getAs<KnownSVal>())
2305           Result.combineWith(
2306               getParentTracker().track(*KV, L->getRegion(), Opts, SFC));
2307       }
2308 
2309       const MemRegion *RegionRVal = RVal.getAsRegion();
2310       if (isa_and_nonnull<SymbolicRegion>(RegionRVal)) {
2311         Report.markInteresting(RegionRVal, Opts.Kind);
2312         Report.addVisitor<TrackConstraintBRVisitor>(
2313             loc::MemRegionVal(RegionRVal),
2314             /*assumption=*/false);
2315         Result.FoundSomethingToTrack = true;
2316       }
2317     }
2318 
2319     return Result;
2320   }
2321 };
2322 
2323 /// Attempts to add visitors to track an RValue expression back to its point of
2324 /// origin.
2325 class PRValueHandler final : public ExpressionHandler {
2326 public:
2327   using ExpressionHandler::ExpressionHandler;
2328 
2329   Tracker::Result handle(const Expr *E, const ExplodedNode *InputNode,
2330                          const ExplodedNode *ExprNode,
2331                          TrackingOptions Opts) override {
2332     if (!E->isPRValue())
2333       return {};
2334 
2335     const ExplodedNode *RVNode = findNodeForExpression(ExprNode, E);
2336     if (!RVNode)
2337       return {};
2338 
2339     ProgramStateRef RVState = RVNode->getState();
2340     SVal V = RVState->getSValAsScalarOrLoc(E, RVNode->getLocationContext());
2341     const auto *BO = dyn_cast<BinaryOperator>(E);
2342 
2343     if (!BO || !BO->isMultiplicativeOp() || !V.isZeroConstant())
2344       return {};
2345 
2346     SVal RHSV = RVState->getSVal(BO->getRHS(), RVNode->getLocationContext());
2347     SVal LHSV = RVState->getSVal(BO->getLHS(), RVNode->getLocationContext());
2348 
2349     // Track both LHS and RHS of a multiplication.
2350     Tracker::Result CombinedResult;
2351     Tracker &Parent = getParentTracker();
2352 
2353     const auto track = [&CombinedResult, &Parent, ExprNode, Opts](Expr *Inner) {
2354       CombinedResult.combineWith(Parent.track(Inner, ExprNode, Opts));
2355     };
2356 
2357     if (BO->getOpcode() == BO_Mul) {
2358       if (LHSV.isZeroConstant())
2359         track(BO->getLHS());
2360       if (RHSV.isZeroConstant())
2361         track(BO->getRHS());
2362     } else { // Track only the LHS of a division or a modulo.
2363       if (LHSV.isZeroConstant())
2364         track(BO->getLHS());
2365     }
2366 
2367     return CombinedResult;
2368   }
2369 };
2370 
2371 Tracker::Tracker(PathSensitiveBugReport &Report) : Report(Report) {
2372   // Default expression handlers.
2373   addLowPriorityHandler<ControlDependencyHandler>();
2374   addLowPriorityHandler<NilReceiverHandler>();
2375   addLowPriorityHandler<ArrayIndexHandler>();
2376   addLowPriorityHandler<InterestingLValueHandler>();
2377   addLowPriorityHandler<InlinedFunctionCallHandler>();
2378   addLowPriorityHandler<DefaultExpressionHandler>();
2379   addLowPriorityHandler<PRValueHandler>();
2380   // Default store handlers.
2381   addHighPriorityHandler<DefaultStoreHandler>();
2382 }
2383 
2384 Tracker::Result Tracker::track(const Expr *E, const ExplodedNode *N,
2385                                TrackingOptions Opts) {
2386   if (!E || !N)
2387     return {};
2388 
2389   const Expr *Inner = peelOffOuterExpr(E, N);
2390   const ExplodedNode *LVNode = findNodeForExpression(N, Inner);
2391   if (!LVNode)
2392     return {};
2393 
2394   Result CombinedResult;
2395   // Iterate through the handlers in the order according to their priorities.
2396   for (ExpressionHandlerPtr &Handler : ExpressionHandlers) {
2397     CombinedResult.combineWith(Handler->handle(Inner, N, LVNode, Opts));
2398     if (CombinedResult.WasInterrupted) {
2399       // There is no need to confuse our users here.
2400       // We got interrupted, but our users don't need to know about it.
2401       CombinedResult.WasInterrupted = false;
2402       break;
2403     }
2404   }
2405 
2406   return CombinedResult;
2407 }
2408 
2409 Tracker::Result Tracker::track(SVal V, const MemRegion *R, TrackingOptions Opts,
2410                                const StackFrameContext *Origin) {
2411   if (auto KV = V.getAs<KnownSVal>()) {
2412     Report.addVisitor<StoreSiteFinder>(this, *KV, R, Opts, Origin);
2413     return {true};
2414   }
2415   return {};
2416 }
2417 
2418 PathDiagnosticPieceRef Tracker::handle(StoreInfo SI, BugReporterContext &BRC,
2419                                        TrackingOptions Opts) {
2420   // Iterate through the handlers in the order according to their priorities.
2421   for (StoreHandlerPtr &Handler : StoreHandlers) {
2422     if (PathDiagnosticPieceRef Result = Handler->handle(SI, BRC, Opts))
2423       // If the handler produced a non-null piece, return it.
2424       // There is no need in asking other handlers.
2425       return Result;
2426   }
2427   return {};
2428 }
2429 
2430 bool bugreporter::trackExpressionValue(const ExplodedNode *InputNode,
2431                                        const Expr *E,
2432 
2433                                        PathSensitiveBugReport &Report,
2434                                        TrackingOptions Opts) {
2435   return Tracker::create(Report)
2436       ->track(E, InputNode, Opts)
2437       .FoundSomethingToTrack;
2438 }
2439 
2440 void bugreporter::trackStoredValue(KnownSVal V, const MemRegion *R,
2441                                    PathSensitiveBugReport &Report,
2442                                    TrackingOptions Opts,
2443                                    const StackFrameContext *Origin) {
2444   Tracker::create(Report)->track(V, R, Opts, Origin);
2445 }
2446 
2447 //===----------------------------------------------------------------------===//
2448 // Implementation of NulReceiverBRVisitor.
2449 //===----------------------------------------------------------------------===//
2450 
2451 const Expr *NilReceiverBRVisitor::getNilReceiver(const Stmt *S,
2452                                                  const ExplodedNode *N) {
2453   const auto *ME = dyn_cast<ObjCMessageExpr>(S);
2454   if (!ME)
2455     return nullptr;
2456   if (const Expr *Receiver = ME->getInstanceReceiver()) {
2457     ProgramStateRef state = N->getState();
2458     SVal V = N->getSVal(Receiver);
2459     if (state->isNull(V).isConstrainedTrue())
2460       return Receiver;
2461   }
2462   return nullptr;
2463 }
2464 
2465 PathDiagnosticPieceRef
2466 NilReceiverBRVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
2467                                 PathSensitiveBugReport &BR) {
2468   Optional<PreStmt> P = N->getLocationAs<PreStmt>();
2469   if (!P)
2470     return nullptr;
2471 
2472   const Stmt *S = P->getStmt();
2473   const Expr *Receiver = getNilReceiver(S, N);
2474   if (!Receiver)
2475     return nullptr;
2476 
2477   llvm::SmallString<256> Buf;
2478   llvm::raw_svector_ostream OS(Buf);
2479 
2480   if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
2481     OS << "'";
2482     ME->getSelector().print(OS);
2483     OS << "' not called";
2484   }
2485   else {
2486     OS << "No method is called";
2487   }
2488   OS << " because the receiver is nil";
2489 
2490   // The receiver was nil, and hence the method was skipped.
2491   // Register a BugReporterVisitor to issue a message telling us how
2492   // the receiver was null.
2493   bugreporter::trackExpressionValue(N, Receiver, BR,
2494                                     {bugreporter::TrackingKind::Thorough,
2495                                      /*EnableNullFPSuppression*/ false});
2496   // Issue a message saying that the method was skipped.
2497   PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
2498                                      N->getLocationContext());
2499   return std::make_shared<PathDiagnosticEventPiece>(L, OS.str());
2500 }
2501 
2502 //===----------------------------------------------------------------------===//
2503 // Visitor that tries to report interesting diagnostics from conditions.
2504 //===----------------------------------------------------------------------===//
2505 
2506 /// Return the tag associated with this visitor.  This tag will be used
2507 /// to make all PathDiagnosticPieces created by this visitor.
2508 const char *ConditionBRVisitor::getTag() { return "ConditionBRVisitor"; }
2509 
2510 PathDiagnosticPieceRef
2511 ConditionBRVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
2512                               PathSensitiveBugReport &BR) {
2513   auto piece = VisitNodeImpl(N, BRC, BR);
2514   if (piece) {
2515     piece->setTag(getTag());
2516     if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get()))
2517       ev->setPrunable(true, /* override */ false);
2518   }
2519   return piece;
2520 }
2521 
2522 PathDiagnosticPieceRef
2523 ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
2524                                   BugReporterContext &BRC,
2525                                   PathSensitiveBugReport &BR) {
2526   ProgramPoint ProgPoint = N->getLocation();
2527   const std::pair<const ProgramPointTag *, const ProgramPointTag *> &Tags =
2528       ExprEngine::geteagerlyAssumeBinOpBifurcationTags();
2529 
2530   // If an assumption was made on a branch, it should be caught
2531   // here by looking at the state transition.
2532   if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
2533     const CFGBlock *SrcBlock = BE->getSrc();
2534     if (const Stmt *Term = SrcBlock->getTerminatorStmt()) {
2535       // If the tag of the previous node is 'Eagerly Assume...' the current
2536       // 'BlockEdge' has the same constraint information. We do not want to
2537       // report the value as it is just an assumption on the predecessor node
2538       // which will be caught in the next VisitNode() iteration as a 'PostStmt'.
2539       const ProgramPointTag *PreviousNodeTag =
2540           N->getFirstPred()->getLocation().getTag();
2541       if (PreviousNodeTag == Tags.first || PreviousNodeTag == Tags.second)
2542         return nullptr;
2543 
2544       return VisitTerminator(Term, N, SrcBlock, BE->getDst(), BR, BRC);
2545     }
2546     return nullptr;
2547   }
2548 
2549   if (Optional<PostStmt> PS = ProgPoint.getAs<PostStmt>()) {
2550     const ProgramPointTag *CurrentNodeTag = PS->getTag();
2551     if (CurrentNodeTag != Tags.first && CurrentNodeTag != Tags.second)
2552       return nullptr;
2553 
2554     bool TookTrue = CurrentNodeTag == Tags.first;
2555     return VisitTrueTest(cast<Expr>(PS->getStmt()), BRC, BR, N, TookTrue);
2556   }
2557 
2558   return nullptr;
2559 }
2560 
2561 PathDiagnosticPieceRef ConditionBRVisitor::VisitTerminator(
2562     const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk,
2563     const CFGBlock *dstBlk, PathSensitiveBugReport &R,
2564     BugReporterContext &BRC) {
2565   const Expr *Cond = nullptr;
2566 
2567   // In the code below, Term is a CFG terminator and Cond is a branch condition
2568   // expression upon which the decision is made on this terminator.
2569   //
2570   // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator,
2571   // and "x == 0" is the respective condition.
2572   //
2573   // Another example: in "if (x && y)", we've got two terminators and two
2574   // conditions due to short-circuit nature of operator "&&":
2575   // 1. The "if (x && y)" statement is a terminator,
2576   //    and "y" is the respective condition.
2577   // 2. Also "x && ..." is another terminator,
2578   //    and "x" is its condition.
2579 
2580   switch (Term->getStmtClass()) {
2581   // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit
2582   // more tricky because there are more than two branches to account for.
2583   default:
2584     return nullptr;
2585   case Stmt::IfStmtClass:
2586     Cond = cast<IfStmt>(Term)->getCond();
2587     break;
2588   case Stmt::ConditionalOperatorClass:
2589     Cond = cast<ConditionalOperator>(Term)->getCond();
2590     break;
2591   case Stmt::BinaryOperatorClass:
2592     // When we encounter a logical operator (&& or ||) as a CFG terminator,
2593     // then the condition is actually its LHS; otherwise, we'd encounter
2594     // the parent, such as if-statement, as a terminator.
2595     const auto *BO = cast<BinaryOperator>(Term);
2596     assert(BO->isLogicalOp() &&
2597            "CFG terminator is not a short-circuit operator!");
2598     Cond = BO->getLHS();
2599     break;
2600   }
2601 
2602   Cond = Cond->IgnoreParens();
2603 
2604   // However, when we encounter a logical operator as a branch condition,
2605   // then the condition is actually its RHS, because LHS would be
2606   // the condition for the logical operator terminator.
2607   while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) {
2608     if (!InnerBO->isLogicalOp())
2609       break;
2610     Cond = InnerBO->getRHS()->IgnoreParens();
2611   }
2612 
2613   assert(Cond);
2614   assert(srcBlk->succ_size() == 2);
2615   const bool TookTrue = *(srcBlk->succ_begin()) == dstBlk;
2616   return VisitTrueTest(Cond, BRC, R, N, TookTrue);
2617 }
2618 
2619 PathDiagnosticPieceRef
2620 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, BugReporterContext &BRC,
2621                                   PathSensitiveBugReport &R,
2622                                   const ExplodedNode *N, bool TookTrue) {
2623   ProgramStateRef CurrentState = N->getState();
2624   ProgramStateRef PrevState = N->getFirstPred()->getState();
2625   const LocationContext *LCtx = N->getLocationContext();
2626 
2627   // If the constraint information is changed between the current and the
2628   // previous program state we assuming the newly seen constraint information.
2629   // If we cannot evaluate the condition (and the constraints are the same)
2630   // the analyzer has no information about the value and just assuming it.
2631   bool IsAssuming =
2632       !BRC.getStateManager().haveEqualConstraints(CurrentState, PrevState) ||
2633       CurrentState->getSVal(Cond, LCtx).isUnknownOrUndef();
2634 
2635   // These will be modified in code below, but we need to preserve the original
2636   //  values in case we want to throw the generic message.
2637   const Expr *CondTmp = Cond;
2638   bool TookTrueTmp = TookTrue;
2639 
2640   while (true) {
2641     CondTmp = CondTmp->IgnoreParenCasts();
2642     switch (CondTmp->getStmtClass()) {
2643       default:
2644         break;
2645       case Stmt::BinaryOperatorClass:
2646         if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp),
2647                                    BRC, R, N, TookTrueTmp, IsAssuming))
2648           return P;
2649         break;
2650       case Stmt::DeclRefExprClass:
2651         if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp),
2652                                    BRC, R, N, TookTrueTmp, IsAssuming))
2653           return P;
2654         break;
2655       case Stmt::MemberExprClass:
2656         if (auto P = VisitTrueTest(Cond, cast<MemberExpr>(CondTmp),
2657                                    BRC, R, N, TookTrueTmp, IsAssuming))
2658           return P;
2659         break;
2660       case Stmt::UnaryOperatorClass: {
2661         const auto *UO = cast<UnaryOperator>(CondTmp);
2662         if (UO->getOpcode() == UO_LNot) {
2663           TookTrueTmp = !TookTrueTmp;
2664           CondTmp = UO->getSubExpr();
2665           continue;
2666         }
2667         break;
2668       }
2669     }
2670     break;
2671   }
2672 
2673   // Condition too complex to explain? Just say something so that the user
2674   // knew we've made some path decision at this point.
2675   // If it is too complex and we know the evaluation of the condition do not
2676   // repeat the note from 'BugReporter.cpp'
2677   if (!IsAssuming)
2678     return nullptr;
2679 
2680   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2681   if (!Loc.isValid() || !Loc.asLocation().isValid())
2682     return nullptr;
2683 
2684   return std::make_shared<PathDiagnosticEventPiece>(
2685       Loc, TookTrue ? GenericTrueMessage : GenericFalseMessage);
2686 }
2687 
2688 bool ConditionBRVisitor::patternMatch(const Expr *Ex,
2689                                       const Expr *ParentEx,
2690                                       raw_ostream &Out,
2691                                       BugReporterContext &BRC,
2692                                       PathSensitiveBugReport &report,
2693                                       const ExplodedNode *N,
2694                                       Optional<bool> &prunable,
2695                                       bool IsSameFieldName) {
2696   const Expr *OriginalExpr = Ex;
2697   Ex = Ex->IgnoreParenCasts();
2698 
2699   if (isa<GNUNullExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
2700       isa<CXXBoolLiteralExpr>(Ex) || isa<IntegerLiteral>(Ex) ||
2701       isa<FloatingLiteral>(Ex)) {
2702     // Use heuristics to determine if the expression is a macro
2703     // expanding to a literal and if so, use the macro's name.
2704     SourceLocation BeginLoc = OriginalExpr->getBeginLoc();
2705     SourceLocation EndLoc = OriginalExpr->getEndLoc();
2706     if (BeginLoc.isMacroID() && EndLoc.isMacroID()) {
2707       const SourceManager &SM = BRC.getSourceManager();
2708       const LangOptions &LO = BRC.getASTContext().getLangOpts();
2709       if (Lexer::isAtStartOfMacroExpansion(BeginLoc, SM, LO) &&
2710           Lexer::isAtEndOfMacroExpansion(EndLoc, SM, LO)) {
2711         CharSourceRange R = Lexer::getAsCharRange({BeginLoc, EndLoc}, SM, LO);
2712         Out << Lexer::getSourceText(R, SM, LO);
2713         return false;
2714       }
2715     }
2716   }
2717 
2718   if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) {
2719     const bool quotes = isa<VarDecl>(DR->getDecl());
2720     if (quotes) {
2721       Out << '\'';
2722       const LocationContext *LCtx = N->getLocationContext();
2723       const ProgramState *state = N->getState().get();
2724       if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
2725                                                 LCtx).getAsRegion()) {
2726         if (report.isInteresting(R))
2727           prunable = false;
2728         else {
2729           const ProgramState *state = N->getState().get();
2730           SVal V = state->getSVal(R);
2731           if (report.isInteresting(V))
2732             prunable = false;
2733         }
2734       }
2735     }
2736     Out << DR->getDecl()->getDeclName().getAsString();
2737     if (quotes)
2738       Out << '\'';
2739     return quotes;
2740   }
2741 
2742   if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) {
2743     QualType OriginalTy = OriginalExpr->getType();
2744     if (OriginalTy->isPointerType()) {
2745       if (IL->getValue() == 0) {
2746         Out << "null";
2747         return false;
2748       }
2749     }
2750     else if (OriginalTy->isObjCObjectPointerType()) {
2751       if (IL->getValue() == 0) {
2752         Out << "nil";
2753         return false;
2754       }
2755     }
2756 
2757     Out << IL->getValue();
2758     return false;
2759   }
2760 
2761   if (const auto *ME = dyn_cast<MemberExpr>(Ex)) {
2762     if (!IsSameFieldName)
2763       Out << "field '" << ME->getMemberDecl()->getName() << '\'';
2764     else
2765       Out << '\''
2766           << Lexer::getSourceText(
2767                  CharSourceRange::getTokenRange(Ex->getSourceRange()),
2768                  BRC.getSourceManager(), BRC.getASTContext().getLangOpts(), 0)
2769           << '\'';
2770   }
2771 
2772   return false;
2773 }
2774 
2775 PathDiagnosticPieceRef ConditionBRVisitor::VisitTrueTest(
2776     const Expr *Cond, const BinaryOperator *BExpr, BugReporterContext &BRC,
2777     PathSensitiveBugReport &R, const ExplodedNode *N, bool TookTrue,
2778     bool IsAssuming) {
2779   bool shouldInvert = false;
2780   Optional<bool> shouldPrune;
2781 
2782   // Check if the field name of the MemberExprs is ambiguous. Example:
2783   // " 'a.d' is equal to 'h.d' " in 'test/Analysis/null-deref-path-notes.cpp'.
2784   bool IsSameFieldName = false;
2785   const auto *LhsME = dyn_cast<MemberExpr>(BExpr->getLHS()->IgnoreParenCasts());
2786   const auto *RhsME = dyn_cast<MemberExpr>(BExpr->getRHS()->IgnoreParenCasts());
2787 
2788   if (LhsME && RhsME)
2789     IsSameFieldName =
2790         LhsME->getMemberDecl()->getName() == RhsME->getMemberDecl()->getName();
2791 
2792   SmallString<128> LhsString, RhsString;
2793   {
2794     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
2795     const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS, BRC, R,
2796                                        N, shouldPrune, IsSameFieldName);
2797     const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS, BRC, R,
2798                                        N, shouldPrune, IsSameFieldName);
2799 
2800     shouldInvert = !isVarLHS && isVarRHS;
2801   }
2802 
2803   BinaryOperator::Opcode Op = BExpr->getOpcode();
2804 
2805   if (BinaryOperator::isAssignmentOp(Op)) {
2806     // For assignment operators, all that we care about is that the LHS
2807     // evaluates to "true" or "false".
2808     return VisitConditionVariable(LhsString, BExpr->getLHS(), BRC, R, N,
2809                                   TookTrue);
2810   }
2811 
2812   // For non-assignment operations, we require that we can understand
2813   // both the LHS and RHS.
2814   if (LhsString.empty() || RhsString.empty() ||
2815       !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp)
2816     return nullptr;
2817 
2818   // Should we invert the strings if the LHS is not a variable name?
2819   SmallString<256> buf;
2820   llvm::raw_svector_ostream Out(buf);
2821   Out << (IsAssuming ? "Assuming " : "")
2822       << (shouldInvert ? RhsString : LhsString) << " is ";
2823 
2824   // Do we need to invert the opcode?
2825   if (shouldInvert)
2826     switch (Op) {
2827       default: break;
2828       case BO_LT: Op = BO_GT; break;
2829       case BO_GT: Op = BO_LT; break;
2830       case BO_LE: Op = BO_GE; break;
2831       case BO_GE: Op = BO_LE; break;
2832     }
2833 
2834   if (!TookTrue)
2835     switch (Op) {
2836       case BO_EQ: Op = BO_NE; break;
2837       case BO_NE: Op = BO_EQ; break;
2838       case BO_LT: Op = BO_GE; break;
2839       case BO_GT: Op = BO_LE; break;
2840       case BO_LE: Op = BO_GT; break;
2841       case BO_GE: Op = BO_LT; break;
2842       default:
2843         return nullptr;
2844     }
2845 
2846   switch (Op) {
2847     case BO_EQ:
2848       Out << "equal to ";
2849       break;
2850     case BO_NE:
2851       Out << "not equal to ";
2852       break;
2853     default:
2854       Out << BinaryOperator::getOpcodeStr(Op) << ' ';
2855       break;
2856   }
2857 
2858   Out << (shouldInvert ? LhsString : RhsString);
2859   const LocationContext *LCtx = N->getLocationContext();
2860   const SourceManager &SM = BRC.getSourceManager();
2861 
2862   if (isVarAnInterestingCondition(BExpr->getLHS(), N, &R) ||
2863       isVarAnInterestingCondition(BExpr->getRHS(), N, &R))
2864     Out << WillBeUsedForACondition;
2865 
2866   // Convert 'field ...' to 'Field ...' if it is a MemberExpr.
2867   std::string Message = std::string(Out.str());
2868   Message[0] = toupper(Message[0]);
2869 
2870   // If we know the value create a pop-up note to the value part of 'BExpr'.
2871   if (!IsAssuming) {
2872     PathDiagnosticLocation Loc;
2873     if (!shouldInvert) {
2874       if (LhsME && LhsME->getMemberLoc().isValid())
2875         Loc = PathDiagnosticLocation(LhsME->getMemberLoc(), SM);
2876       else
2877         Loc = PathDiagnosticLocation(BExpr->getLHS(), SM, LCtx);
2878     } else {
2879       if (RhsME && RhsME->getMemberLoc().isValid())
2880         Loc = PathDiagnosticLocation(RhsME->getMemberLoc(), SM);
2881       else
2882         Loc = PathDiagnosticLocation(BExpr->getRHS(), SM, LCtx);
2883     }
2884 
2885     return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Message);
2886   }
2887 
2888   PathDiagnosticLocation Loc(Cond, SM, LCtx);
2889   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Message);
2890   if (shouldPrune.hasValue())
2891     event->setPrunable(shouldPrune.getValue());
2892   return event;
2893 }
2894 
2895 PathDiagnosticPieceRef ConditionBRVisitor::VisitConditionVariable(
2896     StringRef LhsString, const Expr *CondVarExpr, BugReporterContext &BRC,
2897     PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue) {
2898   // FIXME: If there's already a constraint tracker for this variable,
2899   // we shouldn't emit anything here (c.f. the double note in
2900   // test/Analysis/inlining/path-notes.c)
2901   SmallString<256> buf;
2902   llvm::raw_svector_ostream Out(buf);
2903   Out << "Assuming " << LhsString << " is ";
2904 
2905   if (!printValue(CondVarExpr, Out, N, TookTrue, /*IsAssuming=*/true))
2906     return nullptr;
2907 
2908   const LocationContext *LCtx = N->getLocationContext();
2909   PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
2910 
2911   if (isVarAnInterestingCondition(CondVarExpr, N, &report))
2912     Out << WillBeUsedForACondition;
2913 
2914   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2915 
2916   if (isInterestingExpr(CondVarExpr, N, &report))
2917     event->setPrunable(false);
2918 
2919   return event;
2920 }
2921 
2922 PathDiagnosticPieceRef ConditionBRVisitor::VisitTrueTest(
2923     const Expr *Cond, const DeclRefExpr *DRE, BugReporterContext &BRC,
2924     PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue,
2925     bool IsAssuming) {
2926   const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2927   if (!VD)
2928     return nullptr;
2929 
2930   SmallString<256> Buf;
2931   llvm::raw_svector_ostream Out(Buf);
2932 
2933   Out << (IsAssuming ? "Assuming '" : "'") << VD->getDeclName() << "' is ";
2934 
2935   if (!printValue(DRE, Out, N, TookTrue, IsAssuming))
2936     return nullptr;
2937 
2938   const LocationContext *LCtx = N->getLocationContext();
2939 
2940   if (isVarAnInterestingCondition(DRE, N, &report))
2941     Out << WillBeUsedForACondition;
2942 
2943   // If we know the value create a pop-up note to the 'DRE'.
2944   if (!IsAssuming) {
2945     PathDiagnosticLocation Loc(DRE, BRC.getSourceManager(), LCtx);
2946     return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Out.str());
2947   }
2948 
2949   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2950   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2951 
2952   if (isInterestingExpr(DRE, N, &report))
2953     event->setPrunable(false);
2954 
2955   return std::move(event);
2956 }
2957 
2958 PathDiagnosticPieceRef ConditionBRVisitor::VisitTrueTest(
2959     const Expr *Cond, const MemberExpr *ME, BugReporterContext &BRC,
2960     PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue,
2961     bool IsAssuming) {
2962   SmallString<256> Buf;
2963   llvm::raw_svector_ostream Out(Buf);
2964 
2965   Out << (IsAssuming ? "Assuming field '" : "Field '")
2966       << ME->getMemberDecl()->getName() << "' is ";
2967 
2968   if (!printValue(ME, Out, N, TookTrue, IsAssuming))
2969     return nullptr;
2970 
2971   const LocationContext *LCtx = N->getLocationContext();
2972   PathDiagnosticLocation Loc;
2973 
2974   // If we know the value create a pop-up note to the member of the MemberExpr.
2975   if (!IsAssuming && ME->getMemberLoc().isValid())
2976     Loc = PathDiagnosticLocation(ME->getMemberLoc(), BRC.getSourceManager());
2977   else
2978     Loc = PathDiagnosticLocation(Cond, BRC.getSourceManager(), LCtx);
2979 
2980   if (!Loc.isValid() || !Loc.asLocation().isValid())
2981     return nullptr;
2982 
2983   if (isVarAnInterestingCondition(ME, N, &report))
2984     Out << WillBeUsedForACondition;
2985 
2986   // If we know the value create a pop-up note.
2987   if (!IsAssuming)
2988     return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Out.str());
2989 
2990   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2991   if (isInterestingExpr(ME, N, &report))
2992     event->setPrunable(false);
2993   return event;
2994 }
2995 
2996 bool ConditionBRVisitor::printValue(const Expr *CondVarExpr, raw_ostream &Out,
2997                                     const ExplodedNode *N, bool TookTrue,
2998                                     bool IsAssuming) {
2999   QualType Ty = CondVarExpr->getType();
3000 
3001   if (Ty->isPointerType()) {
3002     Out << (TookTrue ? "non-null" : "null");
3003     return true;
3004   }
3005 
3006   if (Ty->isObjCObjectPointerType()) {
3007     Out << (TookTrue ? "non-nil" : "nil");
3008     return true;
3009   }
3010 
3011   if (!Ty->isIntegralOrEnumerationType())
3012     return false;
3013 
3014   Optional<const llvm::APSInt *> IntValue;
3015   if (!IsAssuming)
3016     IntValue = getConcreteIntegerValue(CondVarExpr, N);
3017 
3018   if (IsAssuming || !IntValue.hasValue()) {
3019     if (Ty->isBooleanType())
3020       Out << (TookTrue ? "true" : "false");
3021     else
3022       Out << (TookTrue ? "not equal to 0" : "0");
3023   } else {
3024     if (Ty->isBooleanType())
3025       Out << (IntValue.getValue()->getBoolValue() ? "true" : "false");
3026     else
3027       Out << *IntValue.getValue();
3028   }
3029 
3030   return true;
3031 }
3032 
3033 constexpr llvm::StringLiteral ConditionBRVisitor::GenericTrueMessage;
3034 constexpr llvm::StringLiteral ConditionBRVisitor::GenericFalseMessage;
3035 
3036 bool ConditionBRVisitor::isPieceMessageGeneric(
3037     const PathDiagnosticPiece *Piece) {
3038   return Piece->getString() == GenericTrueMessage ||
3039          Piece->getString() == GenericFalseMessage;
3040 }
3041 
3042 //===----------------------------------------------------------------------===//
3043 // Implementation of LikelyFalsePositiveSuppressionBRVisitor.
3044 //===----------------------------------------------------------------------===//
3045 
3046 void LikelyFalsePositiveSuppressionBRVisitor::finalizeVisitor(
3047     BugReporterContext &BRC, const ExplodedNode *N,
3048     PathSensitiveBugReport &BR) {
3049   // Here we suppress false positives coming from system headers. This list is
3050   // based on known issues.
3051   const AnalyzerOptions &Options = BRC.getAnalyzerOptions();
3052   const Decl *D = N->getLocationContext()->getDecl();
3053 
3054   if (AnalysisDeclContext::isInStdNamespace(D)) {
3055     // Skip reports within the 'std' namespace. Although these can sometimes be
3056     // the user's fault, we currently don't report them very well, and
3057     // Note that this will not help for any other data structure libraries, like
3058     // TR1, Boost, or llvm/ADT.
3059     if (Options.ShouldSuppressFromCXXStandardLibrary) {
3060       BR.markInvalid(getTag(), nullptr);
3061       return;
3062     } else {
3063       // If the complete 'std' suppression is not enabled, suppress reports
3064       // from the 'std' namespace that are known to produce false positives.
3065 
3066       // The analyzer issues a false use-after-free when std::list::pop_front
3067       // or std::list::pop_back are called multiple times because we cannot
3068       // reason about the internal invariants of the data structure.
3069       if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3070         const CXXRecordDecl *CD = MD->getParent();
3071         if (CD->getName() == "list") {
3072           BR.markInvalid(getTag(), nullptr);
3073           return;
3074         }
3075       }
3076 
3077       // The analyzer issues a false positive when the constructor of
3078       // std::__independent_bits_engine from algorithms is used.
3079       if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) {
3080         const CXXRecordDecl *CD = MD->getParent();
3081         if (CD->getName() == "__independent_bits_engine") {
3082           BR.markInvalid(getTag(), nullptr);
3083           return;
3084         }
3085       }
3086 
3087       for (const LocationContext *LCtx = N->getLocationContext(); LCtx;
3088            LCtx = LCtx->getParent()) {
3089         const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
3090         if (!MD)
3091           continue;
3092 
3093         const CXXRecordDecl *CD = MD->getParent();
3094         // The analyzer issues a false positive on
3095         //   std::basic_string<uint8_t> v; v.push_back(1);
3096         // and
3097         //   std::u16string s; s += u'a';
3098         // because we cannot reason about the internal invariants of the
3099         // data structure.
3100         if (CD->getName() == "basic_string") {
3101           BR.markInvalid(getTag(), nullptr);
3102           return;
3103         }
3104 
3105         // The analyzer issues a false positive on
3106         //    std::shared_ptr<int> p(new int(1)); p = nullptr;
3107         // because it does not reason properly about temporary destructors.
3108         if (CD->getName() == "shared_ptr") {
3109           BR.markInvalid(getTag(), nullptr);
3110           return;
3111         }
3112       }
3113     }
3114   }
3115 
3116   // Skip reports within the sys/queue.h macros as we do not have the ability to
3117   // reason about data structure shapes.
3118   const SourceManager &SM = BRC.getSourceManager();
3119   FullSourceLoc Loc = BR.getLocation().asLocation();
3120   while (Loc.isMacroID()) {
3121     Loc = Loc.getSpellingLoc();
3122     if (SM.getFilename(Loc).endswith("sys/queue.h")) {
3123       BR.markInvalid(getTag(), nullptr);
3124       return;
3125     }
3126   }
3127 }
3128 
3129 //===----------------------------------------------------------------------===//
3130 // Implementation of UndefOrNullArgVisitor.
3131 //===----------------------------------------------------------------------===//
3132 
3133 PathDiagnosticPieceRef
3134 UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
3135                                  PathSensitiveBugReport &BR) {
3136   ProgramStateRef State = N->getState();
3137   ProgramPoint ProgLoc = N->getLocation();
3138 
3139   // We are only interested in visiting CallEnter nodes.
3140   Optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>();
3141   if (!CEnter)
3142     return nullptr;
3143 
3144   // Check if one of the arguments is the region the visitor is tracking.
3145   CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager();
3146   CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
3147   unsigned Idx = 0;
3148   ArrayRef<ParmVarDecl *> parms = Call->parameters();
3149 
3150   for (const auto ParamDecl : parms) {
3151     const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
3152     ++Idx;
3153 
3154     // Are we tracking the argument or its subregion?
3155     if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts()))
3156       continue;
3157 
3158     // Check the function parameter type.
3159     assert(ParamDecl && "Formal parameter has no decl?");
3160     QualType T = ParamDecl->getType();
3161 
3162     if (!(T->isAnyPointerType() || T->isReferenceType())) {
3163       // Function can only change the value passed in by address.
3164       continue;
3165     }
3166 
3167     // If it is a const pointer value, the function does not intend to
3168     // change the value.
3169     if (T->getPointeeType().isConstQualified())
3170       continue;
3171 
3172     // Mark the call site (LocationContext) as interesting if the value of the
3173     // argument is undefined or '0'/'NULL'.
3174     SVal BoundVal = State->getSVal(R);
3175     if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
3176       BR.markInteresting(CEnter->getCalleeContext());
3177       return nullptr;
3178     }
3179   }
3180   return nullptr;
3181 }
3182 
3183 //===----------------------------------------------------------------------===//
3184 // Implementation of FalsePositiveRefutationBRVisitor.
3185 //===----------------------------------------------------------------------===//
3186 
3187 FalsePositiveRefutationBRVisitor::FalsePositiveRefutationBRVisitor()
3188     : Constraints(ConstraintMap::Factory().getEmptyMap()) {}
3189 
3190 void FalsePositiveRefutationBRVisitor::finalizeVisitor(
3191     BugReporterContext &BRC, const ExplodedNode *EndPathNode,
3192     PathSensitiveBugReport &BR) {
3193   // Collect new constraints
3194   addConstraints(EndPathNode, /*OverwriteConstraintsOnExistingSyms=*/true);
3195 
3196   // Create a refutation manager
3197   llvm::SMTSolverRef RefutationSolver = llvm::CreateZ3Solver();
3198   ASTContext &Ctx = BRC.getASTContext();
3199 
3200   // Add constraints to the solver
3201   for (const auto &I : Constraints) {
3202     const SymbolRef Sym = I.first;
3203     auto RangeIt = I.second.begin();
3204 
3205     llvm::SMTExprRef SMTConstraints = SMTConv::getRangeExpr(
3206         RefutationSolver, Ctx, Sym, RangeIt->From(), RangeIt->To(),
3207         /*InRange=*/true);
3208     while ((++RangeIt) != I.second.end()) {
3209       SMTConstraints = RefutationSolver->mkOr(
3210           SMTConstraints, SMTConv::getRangeExpr(RefutationSolver, Ctx, Sym,
3211                                                 RangeIt->From(), RangeIt->To(),
3212                                                 /*InRange=*/true));
3213     }
3214 
3215     RefutationSolver->addConstraint(SMTConstraints);
3216   }
3217 
3218   // And check for satisfiability
3219   Optional<bool> IsSAT = RefutationSolver->check();
3220   if (!IsSAT.hasValue())
3221     return;
3222 
3223   if (!IsSAT.getValue())
3224     BR.markInvalid("Infeasible constraints", EndPathNode->getLocationContext());
3225 }
3226 
3227 void FalsePositiveRefutationBRVisitor::addConstraints(
3228     const ExplodedNode *N, bool OverwriteConstraintsOnExistingSyms) {
3229   // Collect new constraints
3230   ConstraintMap NewCs = getConstraintMap(N->getState());
3231   ConstraintMap::Factory &CF = N->getState()->get_context<ConstraintMap>();
3232 
3233   // Add constraints if we don't have them yet
3234   for (auto const &C : NewCs) {
3235     const SymbolRef &Sym = C.first;
3236     if (!Constraints.contains(Sym)) {
3237       // This symbol is new, just add the constraint.
3238       Constraints = CF.add(Constraints, Sym, C.second);
3239     } else if (OverwriteConstraintsOnExistingSyms) {
3240       // Overwrite the associated constraint of the Symbol.
3241       Constraints = CF.remove(Constraints, Sym);
3242       Constraints = CF.add(Constraints, Sym, C.second);
3243     }
3244   }
3245 }
3246 
3247 PathDiagnosticPieceRef FalsePositiveRefutationBRVisitor::VisitNode(
3248     const ExplodedNode *N, BugReporterContext &, PathSensitiveBugReport &) {
3249   addConstraints(N, /*OverwriteConstraintsOnExistingSyms=*/false);
3250   return nullptr;
3251 }
3252 
3253 void FalsePositiveRefutationBRVisitor::Profile(
3254     llvm::FoldingSetNodeID &ID) const {
3255   static int Tag = 0;
3256   ID.AddPointer(&Tag);
3257 }
3258 
3259 //===----------------------------------------------------------------------===//
3260 // Implementation of TagVisitor.
3261 //===----------------------------------------------------------------------===//
3262 
3263 int NoteTag::Kind = 0;
3264 
3265 void TagVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
3266   static int Tag = 0;
3267   ID.AddPointer(&Tag);
3268 }
3269 
3270 PathDiagnosticPieceRef TagVisitor::VisitNode(const ExplodedNode *N,
3271                                              BugReporterContext &BRC,
3272                                              PathSensitiveBugReport &R) {
3273   ProgramPoint PP = N->getLocation();
3274   const NoteTag *T = dyn_cast_or_null<NoteTag>(PP.getTag());
3275   if (!T)
3276     return nullptr;
3277 
3278   if (Optional<std::string> Msg = T->generateMessage(BRC, R)) {
3279     PathDiagnosticLocation Loc =
3280         PathDiagnosticLocation::create(PP, BRC.getSourceManager());
3281     auto Piece = std::make_shared<PathDiagnosticEventPiece>(Loc, *Msg);
3282     Piece->setPrunable(T->isPrunable());
3283     return Piece;
3284   }
3285 
3286   return nullptr;
3287 }
3288