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