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