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/AnalysisDeclContext.h"
26 #include "clang/Analysis/CFG.h"
27 #include "clang/Analysis/CFGStmtMap.h"
28 #include "clang/Analysis/ProgramPoint.h"
29 #include "clang/Basic/IdentifierTable.h"
30 #include "clang/Basic/LLVM.h"
31 #include "clang/Basic/SourceLocation.h"
32 #include "clang/Basic/SourceManager.h"
33 #include "clang/Lex/Lexer.h"
34 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
35 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
37 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
38 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
39 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
40 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
41 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
42 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
43 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
44 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h"
45 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
46 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
47 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.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 
69 //===----------------------------------------------------------------------===//
70 // Utility functions.
71 //===----------------------------------------------------------------------===//
72 
73 static const Expr *peelOffPointerArithmetic(const BinaryOperator *B) {
74   if (B->isAdditiveOp() && B->getType()->isPointerType()) {
75     if (B->getLHS()->getType()->isPointerType()) {
76       return B->getLHS();
77     } else if (B->getRHS()->getType()->isPointerType()) {
78       return B->getRHS();
79     }
80   }
81   return nullptr;
82 }
83 
84 /// Given that expression S represents a pointer that would be dereferenced,
85 /// try to find a sub-expression from which the pointer came from.
86 /// This is used for tracking down origins of a null or undefined value:
87 /// "this is null because that is null because that is null" etc.
88 /// We wipe away field and element offsets because they merely add offsets.
89 /// We also wipe away all casts except lvalue-to-rvalue casts, because the
90 /// latter represent an actual pointer dereference; however, we remove
91 /// the final lvalue-to-rvalue cast before returning from this function
92 /// because it demonstrates more clearly from where the pointer rvalue was
93 /// loaded. Examples:
94 ///   x->y.z      ==>  x (lvalue)
95 ///   foo()->y.z  ==>  foo() (rvalue)
96 const Expr *bugreporter::getDerefExpr(const Stmt *S) {
97   const auto *E = dyn_cast<Expr>(S);
98   if (!E)
99     return nullptr;
100 
101   while (true) {
102     if (const auto *CE = dyn_cast<CastExpr>(E)) {
103       if (CE->getCastKind() == CK_LValueToRValue) {
104         // This cast represents the load we're looking for.
105         break;
106       }
107       E = CE->getSubExpr();
108     } else if (const auto *B = dyn_cast<BinaryOperator>(E)) {
109       // Pointer arithmetic: '*(x + 2)' -> 'x') etc.
110       if (const Expr *Inner = peelOffPointerArithmetic(B)) {
111         E = Inner;
112       } else {
113         // Probably more arithmetic can be pattern-matched here,
114         // but for now give up.
115         break;
116       }
117     } else if (const auto *U = dyn_cast<UnaryOperator>(E)) {
118       if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf ||
119           (U->isIncrementDecrementOp() && U->getType()->isPointerType())) {
120         // Operators '*' and '&' don't actually mean anything.
121         // We look at casts instead.
122         E = U->getSubExpr();
123       } else {
124         // Probably more arithmetic can be pattern-matched here,
125         // but for now give up.
126         break;
127       }
128     }
129     // Pattern match for a few useful cases: a[0], p->f, *p etc.
130     else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
131       E = ME->getBase();
132     } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
133       E = IvarRef->getBase();
134     } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) {
135       E = AE->getBase();
136     } else if (const auto *PE = dyn_cast<ParenExpr>(E)) {
137       E = PE->getSubExpr();
138     } else if (const auto *FE = dyn_cast<FullExpr>(E)) {
139       E = FE->getSubExpr();
140     } else {
141       // Other arbitrary stuff.
142       break;
143     }
144   }
145 
146   // Special case: remove the final lvalue-to-rvalue cast, but do not recurse
147   // deeper into the sub-expression. This way we return the lvalue from which
148   // our pointer rvalue was loaded.
149   if (const auto *CE = dyn_cast<ImplicitCastExpr>(E))
150     if (CE->getCastKind() == CK_LValueToRValue)
151       E = CE->getSubExpr();
152 
153   return E;
154 }
155 
156 /// Comparing internal representations of symbolic values (via
157 /// SVal::operator==()) is a valid way to check if the value was updated,
158 /// unless it's a LazyCompoundVal that may have a different internal
159 /// representation every time it is loaded from the state. In this function we
160 /// do an approximate comparison for lazy compound values, checking that they
161 /// are the immediate snapshots of the tracked region's bindings within the
162 /// node's respective states but not really checking that these snapshots
163 /// actually contain the same set of bindings.
164 static bool hasVisibleUpdate(const ExplodedNode *LeftNode, SVal LeftVal,
165                              const ExplodedNode *RightNode, SVal RightVal) {
166   if (LeftVal == RightVal)
167     return true;
168 
169   const auto LLCV = LeftVal.getAs<nonloc::LazyCompoundVal>();
170   if (!LLCV)
171     return false;
172 
173   const auto RLCV = RightVal.getAs<nonloc::LazyCompoundVal>();
174   if (!RLCV)
175     return false;
176 
177   return LLCV->getRegion() == RLCV->getRegion() &&
178     LLCV->getStore() == LeftNode->getState()->getStore() &&
179     RLCV->getStore() == RightNode->getState()->getStore();
180 }
181 
182 //===----------------------------------------------------------------------===//
183 // Definitions for bug reporter visitors.
184 //===----------------------------------------------------------------------===//
185 
186 std::shared_ptr<PathDiagnosticPiece>
187 BugReporterVisitor::getEndPath(BugReporterContext &,
188                                const ExplodedNode *, BugReport &) {
189   return nullptr;
190 }
191 
192 void
193 BugReporterVisitor::finalizeVisitor(BugReporterContext &,
194                                     const ExplodedNode *, BugReport &) {}
195 
196 std::shared_ptr<PathDiagnosticPiece> BugReporterVisitor::getDefaultEndPath(
197     BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) {
198   PathDiagnosticLocation L =
199     PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
200 
201   const auto &Ranges = BR.getRanges();
202 
203   // Only add the statement itself as a range if we didn't specify any
204   // special ranges for this report.
205   auto P = std::make_shared<PathDiagnosticEventPiece>(
206       L, BR.getDescription(), Ranges.begin() == Ranges.end());
207   for (SourceRange Range : Ranges)
208     P->addRange(Range);
209 
210   return P;
211 }
212 
213 /// \return name of the macro inside the location \p Loc.
214 static StringRef getMacroName(SourceLocation Loc,
215     BugReporterContext &BRC) {
216   return Lexer::getImmediateMacroName(
217       Loc,
218       BRC.getSourceManager(),
219       BRC.getASTContext().getLangOpts());
220 }
221 
222 /// \return Whether given spelling location corresponds to an expansion
223 /// of a function-like macro.
224 static bool isFunctionMacroExpansion(SourceLocation Loc,
225                                 const SourceManager &SM) {
226   if (!Loc.isMacroID())
227     return false;
228   while (SM.isMacroArgExpansion(Loc))
229     Loc = SM.getImmediateExpansionRange(Loc).getBegin();
230   std::pair<FileID, unsigned> TLInfo = SM.getDecomposedLoc(Loc);
231   SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first);
232   const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion();
233   return EInfo.isFunctionMacroExpansion();
234 }
235 
236 /// \return Whether \c RegionOfInterest was modified at \p N,
237 /// where \p ReturnState is a state associated with the return
238 /// from the current frame.
239 static bool wasRegionOfInterestModifiedAt(
240         const SubRegion *RegionOfInterest,
241         const ExplodedNode *N,
242         SVal ValueAfter) {
243   ProgramStateRef State = N->getState();
244   ProgramStateManager &Mgr = N->getState()->getStateManager();
245 
246   if (!N->getLocationAs<PostStore>()
247       && !N->getLocationAs<PostInitializer>()
248       && !N->getLocationAs<PostStmt>())
249     return false;
250 
251   // Writing into region of interest.
252   if (auto PS = N->getLocationAs<PostStmt>())
253     if (auto *BO = PS->getStmtAs<BinaryOperator>())
254       if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(
255             N->getSVal(BO->getLHS()).getAsRegion()))
256         return true;
257 
258   // SVal after the state is possibly different.
259   SVal ValueAtN = N->getState()->getSVal(RegionOfInterest);
260   if (!Mgr.getSValBuilder().areEqual(State, ValueAtN, ValueAfter).isConstrainedTrue() &&
261       (!ValueAtN.isUndef() || !ValueAfter.isUndef()))
262     return true;
263 
264   return false;
265 }
266 
267 
268 namespace {
269 
270 /// Put a diagnostic on return statement of all inlined functions
271 /// for which  the region of interest \p RegionOfInterest was passed into,
272 /// but not written inside, and it has caused an undefined read or a null
273 /// pointer dereference outside.
274 class NoStoreFuncVisitor final : public BugReporterVisitor {
275   const SubRegion *RegionOfInterest;
276   MemRegionManager &MmrMgr;
277   const SourceManager &SM;
278   const PrintingPolicy &PP;
279 
280   /// Recursion limit for dereferencing fields when looking for the
281   /// region of interest.
282   /// The limit of two indicates that we will dereference fields only once.
283   static const unsigned DEREFERENCE_LIMIT = 2;
284 
285   /// Frames writing into \c RegionOfInterest.
286   /// This visitor generates a note only if a function does not write into
287   /// a region of interest. This information is not immediately available
288   /// by looking at the node associated with the exit from the function
289   /// (usually the return statement). To avoid recomputing the same information
290   /// many times (going up the path for each node and checking whether the
291   /// region was written into) we instead lazily compute the
292   /// stack frames along the path which write into the region of interest.
293   llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingRegion;
294   llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingCalculated;
295 
296   using RegionVector = SmallVector<const MemRegion *, 5>;
297 public:
298   NoStoreFuncVisitor(const SubRegion *R)
299       : RegionOfInterest(R), MmrMgr(*R->getMemRegionManager()),
300         SM(MmrMgr.getContext().getSourceManager()),
301         PP(MmrMgr.getContext().getPrintingPolicy()) {}
302 
303   void Profile(llvm::FoldingSetNodeID &ID) const override {
304     static int Tag = 0;
305     ID.AddPointer(&Tag);
306     ID.AddPointer(RegionOfInterest);
307   }
308 
309   void *getTag() const {
310     static int Tag = 0;
311     return static_cast<void *>(&Tag);
312   }
313 
314   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
315                                                  BugReporterContext &BR,
316                                                  BugReport &R) override {
317 
318     const LocationContext *Ctx = N->getLocationContext();
319     const StackFrameContext *SCtx = Ctx->getStackFrame();
320     ProgramStateRef State = N->getState();
321     auto CallExitLoc = N->getLocationAs<CallExitBegin>();
322 
323     // No diagnostic if region was modified inside the frame.
324     if (!CallExitLoc || isRegionOfInterestModifiedInFrame(N))
325       return nullptr;
326 
327     CallEventRef<> Call =
328         BR.getStateManager().getCallEventManager().getCaller(SCtx, State);
329 
330     // Region of interest corresponds to an IVar, exiting a method
331     // which could have written into that IVar, but did not.
332     if (const auto *MC = dyn_cast<ObjCMethodCall>(Call)) {
333       if (const auto *IvarR = dyn_cast<ObjCIvarRegion>(RegionOfInterest)) {
334         const MemRegion *SelfRegion = MC->getReceiverSVal().getAsRegion();
335         if (RegionOfInterest->isSubRegionOf(SelfRegion) &&
336             potentiallyWritesIntoIvar(Call->getRuntimeDefinition().getDecl(),
337                                       IvarR->getDecl()))
338           return maybeEmitNote(R, *Call, N, {}, SelfRegion, "self",
339                                /*FirstIsReferenceType=*/false, 1);
340       }
341     }
342 
343     if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) {
344       const MemRegion *ThisR = CCall->getCXXThisVal().getAsRegion();
345       if (RegionOfInterest->isSubRegionOf(ThisR)
346           && !CCall->getDecl()->isImplicit())
347         return maybeEmitNote(R, *Call, N, {}, ThisR, "this",
348                              /*FirstIsReferenceType=*/false, 1);
349 
350       // Do not generate diagnostics for not modified parameters in
351       // constructors.
352       return nullptr;
353     }
354 
355     ArrayRef<ParmVarDecl *> parameters = getCallParameters(Call);
356     for (unsigned I = 0; I < Call->getNumArgs() && I < parameters.size(); ++I) {
357       const ParmVarDecl *PVD = parameters[I];
358       SVal V = Call->getArgSVal(I);
359       bool ParamIsReferenceType = PVD->getType()->isReferenceType();
360       std::string ParamName = PVD->getNameAsString();
361 
362       int IndirectionLevel = 1;
363       QualType T = PVD->getType();
364       while (const MemRegion *MR = V.getAsRegion()) {
365         if (RegionOfInterest->isSubRegionOf(MR) && !isPointerToConst(T))
366           return maybeEmitNote(R, *Call, N, {}, MR, ParamName,
367                                ParamIsReferenceType, IndirectionLevel);
368 
369         QualType PT = T->getPointeeType();
370         if (PT.isNull() || PT->isVoidType()) break;
371 
372         if (const RecordDecl *RD = PT->getAsRecordDecl())
373           if (auto P = findRegionOfInterestInRecord(RD, State, MR))
374             return maybeEmitNote(R, *Call, N, *P, RegionOfInterest, ParamName,
375                                  ParamIsReferenceType, IndirectionLevel);
376 
377         V = State->getSVal(MR, PT);
378         T = PT;
379         IndirectionLevel++;
380       }
381     }
382 
383     return nullptr;
384   }
385 
386 private:
387   /// Attempts to find the region of interest in a given CXX decl,
388   /// by either following the base classes or fields.
389   /// Dereferences fields up to a given recursion limit.
390   /// Note that \p Vec is passed by value, leading to quadratic copying cost,
391   /// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT.
392   /// \return A chain fields leading to the region of interest or None.
393   const Optional<RegionVector>
394   findRegionOfInterestInRecord(const RecordDecl *RD, ProgramStateRef State,
395                                const MemRegion *R,
396                                const RegionVector &Vec = {},
397                                int depth = 0) {
398 
399     if (depth == DEREFERENCE_LIMIT) // Limit the recursion depth.
400       return None;
401 
402     if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
403       if (!RDX->hasDefinition())
404         return None;
405 
406     // Recursively examine the base classes.
407     // Note that following base classes does not increase the recursion depth.
408     if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
409       for (const auto II : RDX->bases())
410         if (const RecordDecl *RRD = II.getType()->getAsRecordDecl())
411           if (auto Out = findRegionOfInterestInRecord(RRD, State, R, Vec, depth))
412             return Out;
413 
414     for (const FieldDecl *I : RD->fields()) {
415       QualType FT = I->getType();
416       const FieldRegion *FR = MmrMgr.getFieldRegion(I, cast<SubRegion>(R));
417       const SVal V = State->getSVal(FR);
418       const MemRegion *VR = V.getAsRegion();
419 
420       RegionVector VecF = Vec;
421       VecF.push_back(FR);
422 
423       if (RegionOfInterest == VR)
424         return VecF;
425 
426       if (const RecordDecl *RRD = FT->getAsRecordDecl())
427         if (auto Out =
428                 findRegionOfInterestInRecord(RRD, State, FR, VecF, depth + 1))
429           return Out;
430 
431       QualType PT = FT->getPointeeType();
432       if (PT.isNull() || PT->isVoidType() || !VR) continue;
433 
434       if (const RecordDecl *RRD = PT->getAsRecordDecl())
435         if (auto Out =
436                 findRegionOfInterestInRecord(RRD, State, VR, VecF, depth + 1))
437           return Out;
438 
439     }
440 
441     return None;
442   }
443 
444   /// \return Whether the method declaration \p Parent
445   /// syntactically has a binary operation writing into the ivar \p Ivar.
446   bool potentiallyWritesIntoIvar(const Decl *Parent,
447                                  const ObjCIvarDecl *Ivar) {
448     using namespace ast_matchers;
449     const char * IvarBind = "Ivar";
450     if (!Parent || !Parent->hasBody())
451       return false;
452     StatementMatcher WriteIntoIvarM = binaryOperator(
453         hasOperatorName("="),
454         hasLHS(ignoringParenImpCasts(
455             objcIvarRefExpr(hasDeclaration(equalsNode(Ivar))).bind(IvarBind))));
456     StatementMatcher ParentM = stmt(hasDescendant(WriteIntoIvarM));
457     auto Matches = match(ParentM, *Parent->getBody(), Parent->getASTContext());
458     for (BoundNodes &Match : Matches) {
459       auto IvarRef = Match.getNodeAs<ObjCIvarRefExpr>(IvarBind);
460       if (IvarRef->isFreeIvar())
461         return true;
462 
463       const Expr *Base = IvarRef->getBase();
464       if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base))
465         Base = ICE->getSubExpr();
466 
467       if (const auto *DRE = dyn_cast<DeclRefExpr>(Base))
468         if (const auto *ID = dyn_cast<ImplicitParamDecl>(DRE->getDecl()))
469           if (ID->getParameterKind() == ImplicitParamDecl::ObjCSelf)
470             return true;
471 
472       return false;
473     }
474     return false;
475   }
476 
477   /// Check and lazily calculate whether the region of interest is
478   /// modified in the stack frame to which \p N belongs.
479   /// The calculation is cached in FramesModifyingRegion.
480   bool isRegionOfInterestModifiedInFrame(const ExplodedNode *N) {
481     const LocationContext *Ctx = N->getLocationContext();
482     const StackFrameContext *SCtx = Ctx->getStackFrame();
483     if (!FramesModifyingCalculated.count(SCtx))
484       findModifyingFrames(N);
485     return FramesModifyingRegion.count(SCtx);
486   }
487 
488 
489   /// Write to \c FramesModifyingRegion all stack frames along
490   /// the path in the current stack frame which modify \c RegionOfInterest.
491   void findModifyingFrames(const ExplodedNode *N) {
492     assert(N->getLocationAs<CallExitBegin>());
493     ProgramStateRef LastReturnState = N->getState();
494     SVal ValueAtReturn = LastReturnState->getSVal(RegionOfInterest);
495     const LocationContext *Ctx = N->getLocationContext();
496     const StackFrameContext *OriginalSCtx = Ctx->getStackFrame();
497 
498     do {
499       ProgramStateRef State = N->getState();
500       auto CallExitLoc = N->getLocationAs<CallExitBegin>();
501       if (CallExitLoc) {
502         LastReturnState = State;
503         ValueAtReturn = LastReturnState->getSVal(RegionOfInterest);
504       }
505 
506       FramesModifyingCalculated.insert(
507         N->getLocationContext()->getStackFrame());
508 
509       if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtReturn)) {
510         const StackFrameContext *SCtx = N->getStackFrame();
511         while (!SCtx->inTopFrame()) {
512           auto p = FramesModifyingRegion.insert(SCtx);
513           if (!p.second)
514             break; // Frame and all its parents already inserted.
515           SCtx = SCtx->getParent()->getStackFrame();
516         }
517       }
518 
519       // Stop calculation at the call to the current function.
520       if (auto CE = N->getLocationAs<CallEnter>())
521         if (CE->getCalleeContext() == OriginalSCtx)
522           break;
523 
524       N = N->getFirstPred();
525     } while (N);
526   }
527 
528   /// Get parameters associated with runtime definition in order
529   /// to get the correct parameter name.
530   ArrayRef<ParmVarDecl *> getCallParameters(CallEventRef<> Call) {
531     // Use runtime definition, if available.
532     RuntimeDefinition RD = Call->getRuntimeDefinition();
533     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(RD.getDecl()))
534       return FD->parameters();
535     if (const auto *MD = dyn_cast_or_null<ObjCMethodDecl>(RD.getDecl()))
536       return MD->parameters();
537 
538     return Call->parameters();
539   }
540 
541   /// \return whether \p Ty points to a const type, or is a const reference.
542   bool isPointerToConst(QualType Ty) {
543     return !Ty->getPointeeType().isNull() &&
544            Ty->getPointeeType().getCanonicalType().isConstQualified();
545   }
546 
547   /// Consume the information on the no-store stack frame in order to
548   /// either emit a note or suppress the report enirely.
549   /// \return Diagnostics piece for region not modified in the current function,
550   /// if it decides to emit one.
551   std::shared_ptr<PathDiagnosticPiece>
552   maybeEmitNote(BugReport &R, const CallEvent &Call, const ExplodedNode *N,
553                 const RegionVector &FieldChain, const MemRegion *MatchedRegion,
554                 StringRef FirstElement, bool FirstIsReferenceType,
555                 unsigned IndirectionLevel) {
556     // Optimistically suppress uninitialized value bugs that result
557     // from system headers having a chance to initialize the value
558     // but failing to do so. It's too unlikely a system header's fault.
559     // It's much more likely a situation in which the function has a failure
560     // mode that the user decided not to check. If we want to hunt such
561     // omitted checks, we should provide an explicit function-specific note
562     // describing the precondition under which the function isn't supposed to
563     // initialize its out-parameter, and additionally check that such
564     // precondition can actually be fulfilled on the current path.
565     if (Call.isInSystemHeader()) {
566       // We make an exception for system header functions that have no branches,
567       // i.e. have exactly 3 CFG blocks: begin, all its code, end. Such
568       // functions unconditionally fail to initialize the variable.
569       // If they call other functions that have more paths within them,
570       // this suppression would still apply when we visit these inner functions.
571       // One common example of a standard function that doesn't ever initialize
572       // its out parameter is operator placement new; it's up to the follow-up
573       // constructor (if any) to initialize the memory.
574       if (N->getStackFrame()->getCFG()->size() > 3)
575         R.markInvalid(getTag(), nullptr);
576       return nullptr;
577     }
578 
579     PathDiagnosticLocation L =
580         PathDiagnosticLocation::create(N->getLocation(), SM);
581 
582     // For now this shouldn't trigger, but once it does (as we add more
583     // functions to the body farm), we'll need to decide if these reports
584     // are worth suppressing as well.
585     if (!L.hasValidLocation())
586       return nullptr;
587 
588     SmallString<256> sbuf;
589     llvm::raw_svector_ostream os(sbuf);
590     os << "Returning without writing to '";
591 
592     // Do not generate the note if failed to pretty-print.
593     if (!prettyPrintRegionName(FirstElement, FirstIsReferenceType,
594                                MatchedRegion, FieldChain, IndirectionLevel, os))
595       return nullptr;
596 
597     os << "'";
598     return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
599   }
600 
601   /// Pretty-print region \p MatchedRegion to \p os.
602   /// \return Whether printing succeeded.
603   bool prettyPrintRegionName(StringRef FirstElement, bool FirstIsReferenceType,
604                              const MemRegion *MatchedRegion,
605                              const RegionVector &FieldChain,
606                              int IndirectionLevel,
607                              llvm::raw_svector_ostream &os) {
608 
609     if (FirstIsReferenceType)
610       IndirectionLevel--;
611 
612     RegionVector RegionSequence;
613 
614     // Add the regions in the reverse order, then reverse the resulting array.
615     assert(RegionOfInterest->isSubRegionOf(MatchedRegion));
616     const MemRegion *R = RegionOfInterest;
617     while (R != MatchedRegion) {
618       RegionSequence.push_back(R);
619       R = cast<SubRegion>(R)->getSuperRegion();
620     }
621     std::reverse(RegionSequence.begin(), RegionSequence.end());
622     RegionSequence.append(FieldChain.begin(), FieldChain.end());
623 
624     StringRef Sep;
625     for (const MemRegion *R : RegionSequence) {
626 
627       // Just keep going up to the base region.
628       // Element regions may appear due to casts.
629       if (isa<CXXBaseObjectRegion>(R) || isa<CXXTempObjectRegion>(R))
630         continue;
631 
632       if (Sep.empty())
633         Sep = prettyPrintFirstElement(FirstElement,
634                                       /*MoreItemsExpected=*/true,
635                                       IndirectionLevel, os);
636 
637       os << Sep;
638 
639       // Can only reasonably pretty-print DeclRegions.
640       if (!isa<DeclRegion>(R))
641         return false;
642 
643       const auto *DR = cast<DeclRegion>(R);
644       Sep = DR->getValueType()->isAnyPointerType() ? "->" : ".";
645       DR->getDecl()->getDeclName().print(os, PP);
646     }
647 
648     if (Sep.empty())
649       prettyPrintFirstElement(FirstElement,
650                               /*MoreItemsExpected=*/false, IndirectionLevel,
651                               os);
652     return true;
653   }
654 
655   /// Print first item in the chain, return new separator.
656   StringRef prettyPrintFirstElement(StringRef FirstElement,
657                        bool MoreItemsExpected,
658                        int IndirectionLevel,
659                        llvm::raw_svector_ostream &os) {
660     StringRef Out = ".";
661 
662     if (IndirectionLevel > 0 && MoreItemsExpected) {
663       IndirectionLevel--;
664       Out = "->";
665     }
666 
667     if (IndirectionLevel > 0 && MoreItemsExpected)
668       os << "(";
669 
670     for (int i=0; i<IndirectionLevel; i++)
671       os << "*";
672     os << FirstElement;
673 
674     if (IndirectionLevel > 0 && MoreItemsExpected)
675       os << ")";
676 
677     return Out;
678   }
679 };
680 
681 /// Suppress null-pointer-dereference bugs where dereferenced null was returned
682 /// the macro.
683 class MacroNullReturnSuppressionVisitor final : public BugReporterVisitor {
684   const SubRegion *RegionOfInterest;
685   const SVal ValueAtDereference;
686 
687   // Do not invalidate the reports where the value was modified
688   // after it got assigned to from the macro.
689   bool WasModified = false;
690 
691 public:
692   MacroNullReturnSuppressionVisitor(const SubRegion *R,
693                                     const SVal V) : RegionOfInterest(R),
694                                                     ValueAtDereference(V) {}
695 
696   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
697                                                  BugReporterContext &BRC,
698                                                  BugReport &BR) override {
699     if (WasModified)
700       return nullptr;
701 
702     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
703     if (!BugPoint)
704       return nullptr;
705 
706     const SourceManager &SMgr = BRC.getSourceManager();
707     if (auto Loc = matchAssignment(N)) {
708       if (isFunctionMacroExpansion(*Loc, SMgr)) {
709         std::string MacroName = getMacroName(*Loc, BRC);
710         SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
711         if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName)
712           BR.markInvalid(getTag(), MacroName.c_str());
713       }
714     }
715 
716     if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtDereference))
717       WasModified = true;
718 
719     return nullptr;
720   }
721 
722   static void addMacroVisitorIfNecessary(
723         const ExplodedNode *N, const MemRegion *R,
724         bool EnableNullFPSuppression, BugReport &BR,
725         const SVal V) {
726     AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
727     if (EnableNullFPSuppression &&
728         Options.ShouldSuppressNullReturnPaths && V.getAs<Loc>())
729       BR.addVisitor(llvm::make_unique<MacroNullReturnSuppressionVisitor>(
730               R->getAs<SubRegion>(), V));
731   }
732 
733   void* getTag() const {
734     static int Tag = 0;
735     return static_cast<void *>(&Tag);
736   }
737 
738   void Profile(llvm::FoldingSetNodeID &ID) const override {
739     ID.AddPointer(getTag());
740   }
741 
742 private:
743   /// \return Source location of right hand side of an assignment
744   /// into \c RegionOfInterest, empty optional if none found.
745   Optional<SourceLocation> matchAssignment(const ExplodedNode *N) {
746     const Stmt *S = PathDiagnosticLocation::getStmt(N);
747     ProgramStateRef State = N->getState();
748     auto *LCtx = N->getLocationContext();
749     if (!S)
750       return None;
751 
752     if (const auto *DS = dyn_cast<DeclStmt>(S)) {
753       if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
754         if (const Expr *RHS = VD->getInit())
755           if (RegionOfInterest->isSubRegionOf(
756                   State->getLValue(VD, LCtx).getAsRegion()))
757             return RHS->getBeginLoc();
758     } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
759       const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion();
760       const Expr *RHS = BO->getRHS();
761       if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) {
762         return RHS->getBeginLoc();
763       }
764     }
765     return None;
766   }
767 };
768 
769 /// Emits an extra note at the return statement of an interesting stack frame.
770 ///
771 /// The returned value is marked as an interesting value, and if it's null,
772 /// adds a visitor to track where it became null.
773 ///
774 /// This visitor is intended to be used when another visitor discovers that an
775 /// interesting value comes from an inlined function call.
776 class ReturnVisitor : public BugReporterVisitor {
777   const StackFrameContext *StackFrame;
778   enum {
779     Initial,
780     MaybeUnsuppress,
781     Satisfied
782   } Mode = Initial;
783 
784   bool EnableNullFPSuppression;
785   bool ShouldInvalidate = true;
786   AnalyzerOptions& Options;
787 
788 public:
789   ReturnVisitor(const StackFrameContext *Frame,
790                 bool Suppressed,
791                 AnalyzerOptions &Options)
792       : StackFrame(Frame), EnableNullFPSuppression(Suppressed),
793         Options(Options) {}
794 
795   static void *getTag() {
796     static int Tag = 0;
797     return static_cast<void *>(&Tag);
798   }
799 
800   void Profile(llvm::FoldingSetNodeID &ID) const override {
801     ID.AddPointer(ReturnVisitor::getTag());
802     ID.AddPointer(StackFrame);
803     ID.AddBoolean(EnableNullFPSuppression);
804   }
805 
806   /// Adds a ReturnVisitor if the given statement represents a call that was
807   /// inlined.
808   ///
809   /// This will search back through the ExplodedGraph, starting from the given
810   /// node, looking for when the given statement was processed. If it turns out
811   /// the statement is a call that was inlined, we add the visitor to the
812   /// bug report, so it can print a note later.
813   static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S,
814                                     BugReport &BR,
815                                     bool InEnableNullFPSuppression) {
816     if (!CallEvent::isCallStmt(S))
817       return;
818 
819     // First, find when we processed the statement.
820     do {
821       if (auto CEE = Node->getLocationAs<CallExitEnd>())
822         if (CEE->getCalleeContext()->getCallSite() == S)
823           break;
824       if (auto SP = Node->getLocationAs<StmtPoint>())
825         if (SP->getStmt() == S)
826           break;
827 
828       Node = Node->getFirstPred();
829     } while (Node);
830 
831     // Next, step over any post-statement checks.
832     while (Node && Node->getLocation().getAs<PostStmt>())
833       Node = Node->getFirstPred();
834     if (!Node)
835       return;
836 
837     // Finally, see if we inlined the call.
838     Optional<CallExitEnd> CEE = Node->getLocationAs<CallExitEnd>();
839     if (!CEE)
840       return;
841 
842     const StackFrameContext *CalleeContext = CEE->getCalleeContext();
843     if (CalleeContext->getCallSite() != S)
844       return;
845 
846     // Check the return value.
847     ProgramStateRef State = Node->getState();
848     SVal RetVal = Node->getSVal(S);
849 
850     // Handle cases where a reference is returned and then immediately used.
851     if (cast<Expr>(S)->isGLValue())
852       if (Optional<Loc> LValue = RetVal.getAs<Loc>())
853         RetVal = State->getSVal(*LValue);
854 
855     // See if the return value is NULL. If so, suppress the report.
856     AnalyzerOptions &Options = State->getAnalysisManager().options;
857 
858     bool EnableNullFPSuppression = false;
859     if (InEnableNullFPSuppression &&
860         Options.ShouldSuppressNullReturnPaths)
861       if (Optional<Loc> RetLoc = RetVal.getAs<Loc>())
862         EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue();
863 
864     BR.markInteresting(CalleeContext);
865     BR.addVisitor(llvm::make_unique<ReturnVisitor>(CalleeContext,
866                                                    EnableNullFPSuppression,
867                                                    Options));
868   }
869 
870   std::shared_ptr<PathDiagnosticPiece>
871   visitNodeInitial(const ExplodedNode *N,
872                    BugReporterContext &BRC, BugReport &BR) {
873     // Only print a message at the interesting return statement.
874     if (N->getLocationContext() != StackFrame)
875       return nullptr;
876 
877     Optional<StmtPoint> SP = N->getLocationAs<StmtPoint>();
878     if (!SP)
879       return nullptr;
880 
881     const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
882     if (!Ret)
883       return nullptr;
884 
885     // Okay, we're at the right return statement, but do we have the return
886     // value available?
887     ProgramStateRef State = N->getState();
888     SVal V = State->getSVal(Ret, StackFrame);
889     if (V.isUnknownOrUndef())
890       return nullptr;
891 
892     // Don't print any more notes after this one.
893     Mode = Satisfied;
894 
895     const Expr *RetE = Ret->getRetValue();
896     assert(RetE && "Tracking a return value for a void function");
897 
898     // Handle cases where a reference is returned and then immediately used.
899     Optional<Loc> LValue;
900     if (RetE->isGLValue()) {
901       if ((LValue = V.getAs<Loc>())) {
902         SVal RValue = State->getRawSVal(*LValue, RetE->getType());
903         if (RValue.getAs<DefinedSVal>())
904           V = RValue;
905       }
906     }
907 
908     // Ignore aggregate rvalues.
909     if (V.getAs<nonloc::LazyCompoundVal>() ||
910         V.getAs<nonloc::CompoundVal>())
911       return nullptr;
912 
913     RetE = RetE->IgnoreParenCasts();
914 
915     // If we're returning 0, we should track where that 0 came from.
916     bugreporter::trackExpressionValue(N, RetE, BR, EnableNullFPSuppression);
917 
918     // Build an appropriate message based on the return value.
919     SmallString<64> Msg;
920     llvm::raw_svector_ostream Out(Msg);
921 
922     if (State->isNull(V).isConstrainedTrue()) {
923       if (V.getAs<Loc>()) {
924 
925         // If we have counter-suppression enabled, make sure we keep visiting
926         // future nodes. We want to emit a path note as well, in case
927         // the report is resurrected as valid later on.
928         if (EnableNullFPSuppression &&
929             Options.ShouldAvoidSuppressingNullArgumentPaths)
930           Mode = MaybeUnsuppress;
931 
932         if (RetE->getType()->isObjCObjectPointerType()) {
933           Out << "Returning nil";
934         } else {
935           Out << "Returning null pointer";
936         }
937       } else {
938         Out << "Returning zero";
939       }
940 
941     } else {
942       if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
943         Out << "Returning the value " << CI->getValue();
944       } else if (V.getAs<Loc>()) {
945         Out << "Returning pointer";
946       } else {
947         Out << "Returning value";
948       }
949     }
950 
951     if (LValue) {
952       if (const MemRegion *MR = LValue->getAsRegion()) {
953         if (MR->canPrintPretty()) {
954           Out << " (reference to ";
955           MR->printPretty(Out);
956           Out << ")";
957         }
958       }
959     } else {
960       // FIXME: We should have a more generalized location printing mechanism.
961       if (const auto *DR = dyn_cast<DeclRefExpr>(RetE))
962         if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
963           Out << " (loaded from '" << *DD << "')";
964     }
965 
966     PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame);
967     if (!L.isValid() || !L.asLocation().isValid())
968       return nullptr;
969 
970     return std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
971   }
972 
973   std::shared_ptr<PathDiagnosticPiece>
974   visitNodeMaybeUnsuppress(const ExplodedNode *N,
975                            BugReporterContext &BRC, BugReport &BR) {
976 #ifndef NDEBUG
977     assert(Options.ShouldAvoidSuppressingNullArgumentPaths);
978 #endif
979 
980     // Are we at the entry node for this call?
981     Optional<CallEnter> CE = N->getLocationAs<CallEnter>();
982     if (!CE)
983       return nullptr;
984 
985     if (CE->getCalleeContext() != StackFrame)
986       return nullptr;
987 
988     Mode = Satisfied;
989 
990     // Don't automatically suppress a report if one of the arguments is
991     // known to be a null pointer. Instead, start tracking /that/ null
992     // value back to its origin.
993     ProgramStateManager &StateMgr = BRC.getStateManager();
994     CallEventManager &CallMgr = StateMgr.getCallEventManager();
995 
996     ProgramStateRef State = N->getState();
997     CallEventRef<> Call = CallMgr.getCaller(StackFrame, State);
998     for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
999       Optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>();
1000       if (!ArgV)
1001         continue;
1002 
1003       const Expr *ArgE = Call->getArgExpr(I);
1004       if (!ArgE)
1005         continue;
1006 
1007       // Is it possible for this argument to be non-null?
1008       if (!State->isNull(*ArgV).isConstrainedTrue())
1009         continue;
1010 
1011       if (bugreporter::trackExpressionValue(N, ArgE, BR, EnableNullFPSuppression))
1012         ShouldInvalidate = false;
1013 
1014       // If we /can't/ track the null pointer, we should err on the side of
1015       // false negatives, and continue towards marking this report invalid.
1016       // (We will still look at the other arguments, though.)
1017     }
1018 
1019     return nullptr;
1020   }
1021 
1022   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
1023                                                  BugReporterContext &BRC,
1024                                                  BugReport &BR) override {
1025     switch (Mode) {
1026     case Initial:
1027       return visitNodeInitial(N, BRC, BR);
1028     case MaybeUnsuppress:
1029       return visitNodeMaybeUnsuppress(N, BRC, BR);
1030     case Satisfied:
1031       return nullptr;
1032     }
1033 
1034     llvm_unreachable("Invalid visit mode!");
1035   }
1036 
1037   void finalizeVisitor(BugReporterContext &, const ExplodedNode *,
1038                        BugReport &BR) override {
1039     if (EnableNullFPSuppression && ShouldInvalidate)
1040       BR.markInvalid(ReturnVisitor::getTag(), StackFrame);
1041   }
1042 };
1043 
1044 } // namespace
1045 
1046 void FindLastStoreBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1047   static int tag = 0;
1048   ID.AddPointer(&tag);
1049   ID.AddPointer(R);
1050   ID.Add(V);
1051   ID.AddBoolean(EnableNullFPSuppression);
1052 }
1053 
1054 /// Returns true if \p N represents the DeclStmt declaring and initializing
1055 /// \p VR.
1056 static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) {
1057   Optional<PostStmt> P = N->getLocationAs<PostStmt>();
1058   if (!P)
1059     return false;
1060 
1061   const DeclStmt *DS = P->getStmtAs<DeclStmt>();
1062   if (!DS)
1063     return false;
1064 
1065   if (DS->getSingleDecl() != VR->getDecl())
1066     return false;
1067 
1068   const MemSpaceRegion *VarSpace = VR->getMemorySpace();
1069   const auto *FrameSpace = dyn_cast<StackSpaceRegion>(VarSpace);
1070   if (!FrameSpace) {
1071     // If we ever directly evaluate global DeclStmts, this assertion will be
1072     // invalid, but this still seems preferable to silently accepting an
1073     // initialization that may be for a path-sensitive variable.
1074     assert(VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion");
1075     return true;
1076   }
1077 
1078   assert(VR->getDecl()->hasLocalStorage());
1079   const LocationContext *LCtx = N->getLocationContext();
1080   return FrameSpace->getStackFrame() == LCtx->getStackFrame();
1081 }
1082 
1083 /// Show diagnostics for initializing or declaring a region \p R with a bad value.
1084 static void showBRDiagnostics(const char *action, llvm::raw_svector_ostream &os,
1085                               const MemRegion *R, SVal V, const DeclStmt *DS) {
1086   if (R->canPrintPretty()) {
1087     R->printPretty(os);
1088     os << " ";
1089   }
1090 
1091   if (V.getAs<loc::ConcreteInt>()) {
1092     bool b = false;
1093     if (R->isBoundable()) {
1094       if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
1095         if (TR->getValueType()->isObjCObjectPointerType()) {
1096           os << action << "nil";
1097           b = true;
1098         }
1099       }
1100     }
1101     if (!b)
1102       os << action << "a null pointer value";
1103 
1104   } else if (auto CVal = V.getAs<nonloc::ConcreteInt>()) {
1105     os << action << CVal->getValue();
1106   } else if (DS) {
1107     if (V.isUndef()) {
1108       if (isa<VarRegion>(R)) {
1109         const auto *VD = cast<VarDecl>(DS->getSingleDecl());
1110         if (VD->getInit()) {
1111           os << (R->canPrintPretty() ? "initialized" : "Initializing")
1112             << " to a garbage value";
1113         } else {
1114           os << (R->canPrintPretty() ? "declared" : "Declaring")
1115             << " without an initial value";
1116         }
1117       }
1118     } else {
1119       os << (R->canPrintPretty() ? "initialized" : "Initialized")
1120         << " here";
1121     }
1122   }
1123 }
1124 
1125 /// Display diagnostics for passing bad region as a parameter.
1126 static void showBRParamDiagnostics(llvm::raw_svector_ostream& os,
1127     const VarRegion *VR,
1128     SVal V) {
1129   const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1130 
1131   os << "Passing ";
1132 
1133   if (V.getAs<loc::ConcreteInt>()) {
1134     if (Param->getType()->isObjCObjectPointerType())
1135       os << "nil object reference";
1136     else
1137       os << "null pointer value";
1138   } else if (V.isUndef()) {
1139     os << "uninitialized value";
1140   } else if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
1141     os << "the value " << CI->getValue();
1142   } else {
1143     os << "value";
1144   }
1145 
1146   // Printed parameter indexes are 1-based, not 0-based.
1147   unsigned Idx = Param->getFunctionScopeIndex() + 1;
1148   os << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
1149   if (VR->canPrintPretty()) {
1150     os << " ";
1151     VR->printPretty(os);
1152   }
1153 }
1154 
1155 /// Show default diagnostics for storing bad region.
1156 static void showBRDefaultDiagnostics(llvm::raw_svector_ostream& os,
1157     const MemRegion *R,
1158     SVal V) {
1159   if (V.getAs<loc::ConcreteInt>()) {
1160     bool b = false;
1161     if (R->isBoundable()) {
1162       if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
1163         if (TR->getValueType()->isObjCObjectPointerType()) {
1164           os << "nil object reference stored";
1165           b = true;
1166         }
1167       }
1168     }
1169     if (!b) {
1170       if (R->canPrintPretty())
1171         os << "Null pointer value stored";
1172       else
1173         os << "Storing null pointer value";
1174     }
1175 
1176   } else if (V.isUndef()) {
1177     if (R->canPrintPretty())
1178       os << "Uninitialized value stored";
1179     else
1180       os << "Storing uninitialized value";
1181 
1182   } else if (auto CV = V.getAs<nonloc::ConcreteInt>()) {
1183     if (R->canPrintPretty())
1184       os << "The value " << CV->getValue() << " is assigned";
1185     else
1186       os << "Assigning " << CV->getValue();
1187 
1188   } else {
1189     if (R->canPrintPretty())
1190       os << "Value assigned";
1191     else
1192       os << "Assigning value";
1193   }
1194 
1195   if (R->canPrintPretty()) {
1196     os << " to ";
1197     R->printPretty(os);
1198   }
1199 }
1200 
1201 std::shared_ptr<PathDiagnosticPiece>
1202 FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ,
1203                                   BugReporterContext &BRC, BugReport &BR) {
1204   if (Satisfied)
1205     return nullptr;
1206 
1207   const ExplodedNode *StoreSite = nullptr;
1208   const ExplodedNode *Pred = Succ->getFirstPred();
1209   const Expr *InitE = nullptr;
1210   bool IsParam = false;
1211 
1212   // First see if we reached the declaration of the region.
1213   if (const auto *VR = dyn_cast<VarRegion>(R)) {
1214     if (isInitializationOfVar(Pred, VR)) {
1215       StoreSite = Pred;
1216       InitE = VR->getDecl()->getInit();
1217     }
1218   }
1219 
1220   // If this is a post initializer expression, initializing the region, we
1221   // should track the initializer expression.
1222   if (Optional<PostInitializer> PIP = Pred->getLocationAs<PostInitializer>()) {
1223     const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue();
1224     if (FieldReg && FieldReg == R) {
1225       StoreSite = Pred;
1226       InitE = PIP->getInitializer()->getInit();
1227     }
1228   }
1229 
1230   // Otherwise, see if this is the store site:
1231   // (1) Succ has this binding and Pred does not, i.e. this is
1232   //     where the binding first occurred.
1233   // (2) Succ has this binding and is a PostStore node for this region, i.e.
1234   //     the same binding was re-assigned here.
1235   if (!StoreSite) {
1236     if (Succ->getState()->getSVal(R) != V)
1237       return nullptr;
1238 
1239     if (hasVisibleUpdate(Pred, Pred->getState()->getSVal(R), Succ, V)) {
1240       Optional<PostStore> PS = Succ->getLocationAs<PostStore>();
1241       if (!PS || PS->getLocationValue() != R)
1242         return nullptr;
1243     }
1244 
1245     StoreSite = Succ;
1246 
1247     // If this is an assignment expression, we can track the value
1248     // being assigned.
1249     if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
1250       if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
1251         if (BO->isAssignmentOp())
1252           InitE = BO->getRHS();
1253 
1254     // If this is a call entry, the variable should be a parameter.
1255     // FIXME: Handle CXXThisRegion as well. (This is not a priority because
1256     // 'this' should never be NULL, but this visitor isn't just for NULL and
1257     // UndefinedVal.)
1258     if (Optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) {
1259       if (const auto *VR = dyn_cast<VarRegion>(R)) {
1260 
1261         const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1262 
1263         ProgramStateManager &StateMgr = BRC.getStateManager();
1264         CallEventManager &CallMgr = StateMgr.getCallEventManager();
1265 
1266         CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(),
1267                                                 Succ->getState());
1268         InitE = Call->getArgExpr(Param->getFunctionScopeIndex());
1269         IsParam = true;
1270       }
1271     }
1272 
1273     // If this is a CXXTempObjectRegion, the Expr responsible for its creation
1274     // is wrapped inside of it.
1275     if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R))
1276       InitE = TmpR->getExpr();
1277   }
1278 
1279   if (!StoreSite)
1280     return nullptr;
1281   Satisfied = true;
1282 
1283   // If we have an expression that provided the value, try to track where it
1284   // came from.
1285   if (InitE) {
1286     if (V.isUndef() ||
1287         V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1288       if (!IsParam)
1289         InitE = InitE->IgnoreParenCasts();
1290       bugreporter::trackExpressionValue(StoreSite, InitE, BR,
1291                                    EnableNullFPSuppression);
1292     }
1293     ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE->IgnoreParenCasts(),
1294                                          BR, EnableNullFPSuppression);
1295   }
1296 
1297   // Okay, we've found the binding. Emit an appropriate message.
1298   SmallString<256> sbuf;
1299   llvm::raw_svector_ostream os(sbuf);
1300 
1301   if (Optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) {
1302     const Stmt *S = PS->getStmt();
1303     const char *action = nullptr;
1304     const auto *DS = dyn_cast<DeclStmt>(S);
1305     const auto *VR = dyn_cast<VarRegion>(R);
1306 
1307     if (DS) {
1308       action = R->canPrintPretty() ? "initialized to " :
1309                                      "Initializing to ";
1310     } else if (isa<BlockExpr>(S)) {
1311       action = R->canPrintPretty() ? "captured by block as " :
1312                                      "Captured by block as ";
1313       if (VR) {
1314         // See if we can get the BlockVarRegion.
1315         ProgramStateRef State = StoreSite->getState();
1316         SVal V = StoreSite->getSVal(S);
1317         if (const auto *BDR =
1318               dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
1319           if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) {
1320             if (auto KV = State->getSVal(OriginalR).getAs<KnownSVal>())
1321               BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1322                   *KV, OriginalR, EnableNullFPSuppression));
1323           }
1324         }
1325       }
1326     }
1327     if (action)
1328       showBRDiagnostics(action, os, R, V, DS);
1329 
1330   } else if (StoreSite->getLocation().getAs<CallEnter>()) {
1331     if (const auto *VR = dyn_cast<VarRegion>(R))
1332       showBRParamDiagnostics(os, VR, V);
1333   }
1334 
1335   if (os.str().empty())
1336     showBRDefaultDiagnostics(os, R, V);
1337 
1338   // Construct a new PathDiagnosticPiece.
1339   ProgramPoint P = StoreSite->getLocation();
1340   PathDiagnosticLocation L;
1341   if (P.getAs<CallEnter>() && InitE)
1342     L = PathDiagnosticLocation(InitE, BRC.getSourceManager(),
1343                                P.getLocationContext());
1344 
1345   if (!L.isValid() || !L.asLocation().isValid())
1346     L = PathDiagnosticLocation::create(P, BRC.getSourceManager());
1347 
1348   if (!L.isValid() || !L.asLocation().isValid())
1349     return nullptr;
1350 
1351   return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1352 }
1353 
1354 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1355   static int tag = 0;
1356   ID.AddPointer(&tag);
1357   ID.AddBoolean(Assumption);
1358   ID.Add(Constraint);
1359 }
1360 
1361 /// Return the tag associated with this visitor.  This tag will be used
1362 /// to make all PathDiagnosticPieces created by this visitor.
1363 const char *TrackConstraintBRVisitor::getTag() {
1364   return "TrackConstraintBRVisitor";
1365 }
1366 
1367 bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const {
1368   if (IsZeroCheck)
1369     return N->getState()->isNull(Constraint).isUnderconstrained();
1370   return (bool)N->getState()->assume(Constraint, !Assumption);
1371 }
1372 
1373 std::shared_ptr<PathDiagnosticPiece>
1374 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
1375                                     BugReporterContext &BRC, BugReport &) {
1376   const ExplodedNode *PrevN = N->getFirstPred();
1377   if (IsSatisfied)
1378     return nullptr;
1379 
1380   // Start tracking after we see the first state in which the value is
1381   // constrained.
1382   if (!IsTrackingTurnedOn)
1383     if (!isUnderconstrained(N))
1384       IsTrackingTurnedOn = true;
1385   if (!IsTrackingTurnedOn)
1386     return nullptr;
1387 
1388   // Check if in the previous state it was feasible for this constraint
1389   // to *not* be true.
1390   if (isUnderconstrained(PrevN)) {
1391     IsSatisfied = true;
1392 
1393     // As a sanity check, make sure that the negation of the constraint
1394     // was infeasible in the current state.  If it is feasible, we somehow
1395     // missed the transition point.
1396     assert(!isUnderconstrained(N));
1397 
1398     // We found the transition point for the constraint.  We now need to
1399     // pretty-print the constraint. (work-in-progress)
1400     SmallString<64> sbuf;
1401     llvm::raw_svector_ostream os(sbuf);
1402 
1403     if (Constraint.getAs<Loc>()) {
1404       os << "Assuming pointer value is ";
1405       os << (Assumption ? "non-null" : "null");
1406     }
1407 
1408     if (os.str().empty())
1409       return nullptr;
1410 
1411     // Construct a new PathDiagnosticPiece.
1412     ProgramPoint P = N->getLocation();
1413     PathDiagnosticLocation L =
1414       PathDiagnosticLocation::create(P, BRC.getSourceManager());
1415     if (!L.isValid())
1416       return nullptr;
1417 
1418     auto X = std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1419     X->setTag(getTag());
1420     return std::move(X);
1421   }
1422 
1423   return nullptr;
1424 }
1425 
1426 SuppressInlineDefensiveChecksVisitor::
1427 SuppressInlineDefensiveChecksVisitor(DefinedSVal Value, const ExplodedNode *N)
1428     : V(Value) {
1429   // Check if the visitor is disabled.
1430   AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
1431   if (!Options.ShouldSuppressInlinedDefensiveChecks)
1432     IsSatisfied = true;
1433 
1434   assert(N->getState()->isNull(V).isConstrainedTrue() &&
1435          "The visitor only tracks the cases where V is constrained to 0");
1436 }
1437 
1438 void SuppressInlineDefensiveChecksVisitor::Profile(
1439     llvm::FoldingSetNodeID &ID) const {
1440   static int id = 0;
1441   ID.AddPointer(&id);
1442   ID.Add(V);
1443 }
1444 
1445 const char *SuppressInlineDefensiveChecksVisitor::getTag() {
1446   return "IDCVisitor";
1447 }
1448 
1449 std::shared_ptr<PathDiagnosticPiece>
1450 SuppressInlineDefensiveChecksVisitor::VisitNode(const ExplodedNode *Succ,
1451                                                 BugReporterContext &BRC,
1452                                                 BugReport &BR) {
1453   const ExplodedNode *Pred = Succ->getFirstPred();
1454   if (IsSatisfied)
1455     return nullptr;
1456 
1457   // Start tracking after we see the first state in which the value is null.
1458   if (!IsTrackingTurnedOn)
1459     if (Succ->getState()->isNull(V).isConstrainedTrue())
1460       IsTrackingTurnedOn = true;
1461   if (!IsTrackingTurnedOn)
1462     return nullptr;
1463 
1464   // Check if in the previous state it was feasible for this value
1465   // to *not* be null.
1466   if (!Pred->getState()->isNull(V).isConstrainedTrue()) {
1467     IsSatisfied = true;
1468 
1469     assert(Succ->getState()->isNull(V).isConstrainedTrue());
1470 
1471     // Check if this is inlined defensive checks.
1472     const LocationContext *CurLC =Succ->getLocationContext();
1473     const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext();
1474     if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) {
1475       BR.markInvalid("Suppress IDC", CurLC);
1476       return nullptr;
1477     }
1478 
1479     // Treat defensive checks in function-like macros as if they were an inlined
1480     // defensive check. If the bug location is not in a macro and the
1481     // terminator for the current location is in a macro then suppress the
1482     // warning.
1483     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
1484 
1485     if (!BugPoint)
1486       return nullptr;
1487 
1488     ProgramPoint CurPoint = Succ->getLocation();
1489     const Stmt *CurTerminatorStmt = nullptr;
1490     if (auto BE = CurPoint.getAs<BlockEdge>()) {
1491       CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt();
1492     } else if (auto SP = CurPoint.getAs<StmtPoint>()) {
1493       const Stmt *CurStmt = SP->getStmt();
1494       if (!CurStmt->getBeginLoc().isMacroID())
1495         return nullptr;
1496 
1497       CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap();
1498       CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminator();
1499     } else {
1500       return nullptr;
1501     }
1502 
1503     if (!CurTerminatorStmt)
1504       return nullptr;
1505 
1506     SourceLocation TerminatorLoc = CurTerminatorStmt->getBeginLoc();
1507     if (TerminatorLoc.isMacroID()) {
1508       SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
1509 
1510       // Suppress reports unless we are in that same macro.
1511       if (!BugLoc.isMacroID() ||
1512           getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) {
1513         BR.markInvalid("Suppress Macro IDC", CurLC);
1514       }
1515       return nullptr;
1516     }
1517   }
1518   return nullptr;
1519 }
1520 
1521 static const MemRegion *getLocationRegionIfReference(const Expr *E,
1522                                                      const ExplodedNode *N) {
1523   if (const auto *DR = dyn_cast<DeclRefExpr>(E)) {
1524     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1525       if (!VD->getType()->isReferenceType())
1526         return nullptr;
1527       ProgramStateManager &StateMgr = N->getState()->getStateManager();
1528       MemRegionManager &MRMgr = StateMgr.getRegionManager();
1529       return MRMgr.getVarRegion(VD, N->getLocationContext());
1530     }
1531   }
1532 
1533   // FIXME: This does not handle other kinds of null references,
1534   // for example, references from FieldRegions:
1535   //   struct Wrapper { int &ref; };
1536   //   Wrapper w = { *(int *)0 };
1537   //   w.ref = 1;
1538 
1539   return nullptr;
1540 }
1541 
1542 /// \return A subexpression of {@code Ex} which represents the
1543 /// expression-of-interest.
1544 static const Expr *peelOffOuterExpr(const Expr *Ex,
1545                                     const ExplodedNode *N) {
1546   Ex = Ex->IgnoreParenCasts();
1547   if (const auto *FE = dyn_cast<FullExpr>(Ex))
1548     return peelOffOuterExpr(FE->getSubExpr(), N);
1549   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex))
1550     return peelOffOuterExpr(OVE->getSourceExpr(), N);
1551   if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) {
1552     const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
1553     if (PropRef && PropRef->isMessagingGetter()) {
1554       const Expr *GetterMessageSend =
1555           POE->getSemanticExpr(POE->getNumSemanticExprs() - 1);
1556       assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts()));
1557       return peelOffOuterExpr(GetterMessageSend, N);
1558     }
1559   }
1560 
1561   // Peel off the ternary operator.
1562   if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) {
1563     // Find a node where the branching occurred and find out which branch
1564     // we took (true/false) by looking at the ExplodedGraph.
1565     const ExplodedNode *NI = N;
1566     do {
1567       ProgramPoint ProgPoint = NI->getLocation();
1568       if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
1569         const CFGBlock *srcBlk = BE->getSrc();
1570         if (const Stmt *term = srcBlk->getTerminator()) {
1571           if (term == CO) {
1572             bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst());
1573             if (TookTrueBranch)
1574               return peelOffOuterExpr(CO->getTrueExpr(), N);
1575             else
1576               return peelOffOuterExpr(CO->getFalseExpr(), N);
1577           }
1578         }
1579       }
1580       NI = NI->getFirstPred();
1581     } while (NI);
1582   }
1583 
1584   if (auto *BO = dyn_cast<BinaryOperator>(Ex))
1585     if (const Expr *SubEx = peelOffPointerArithmetic(BO))
1586       return peelOffOuterExpr(SubEx, N);
1587 
1588   if (auto *UO = dyn_cast<UnaryOperator>(Ex)) {
1589     if (UO->getOpcode() == UO_LNot)
1590       return peelOffOuterExpr(UO->getSubExpr(), N);
1591 
1592     // FIXME: There's a hack in our Store implementation that always computes
1593     // field offsets around null pointers as if they are always equal to 0.
1594     // The idea here is to report accesses to fields as null dereferences
1595     // even though the pointer value that's being dereferenced is actually
1596     // the offset of the field rather than exactly 0.
1597     // See the FIXME in StoreManager's getLValueFieldOrIvar() method.
1598     // This code interacts heavily with this hack; otherwise the value
1599     // would not be null at all for most fields, so we'd be unable to track it.
1600     if (UO->getOpcode() == UO_AddrOf && UO->getSubExpr()->isLValue())
1601       if (const Expr *DerefEx = bugreporter::getDerefExpr(UO->getSubExpr()))
1602         return peelOffOuterExpr(DerefEx, N);
1603   }
1604 
1605   return Ex;
1606 }
1607 
1608 /// Find the ExplodedNode where the lvalue (the value of 'Ex')
1609 /// was computed.
1610 static const ExplodedNode* findNodeForExpression(const ExplodedNode *N,
1611                                                  const Expr *Inner) {
1612   while (N) {
1613     if (PathDiagnosticLocation::getStmt(N) == Inner)
1614       return N;
1615     N = N->getFirstPred();
1616   }
1617   return N;
1618 }
1619 
1620 bool bugreporter::trackExpressionValue(const ExplodedNode *InputNode,
1621                                        const Expr *E, BugReport &report,
1622                                        bool EnableNullFPSuppression) {
1623   if (!E || !InputNode)
1624     return false;
1625 
1626   const Expr *Inner = peelOffOuterExpr(E, InputNode);
1627   const ExplodedNode *LVNode = findNodeForExpression(InputNode, Inner);
1628   if (!LVNode)
1629     return false;
1630 
1631   ProgramStateRef LVState = LVNode->getState();
1632 
1633   // The message send could be nil due to the receiver being nil.
1634   // At this point in the path, the receiver should be live since we are at the
1635   // message send expr. If it is nil, start tracking it.
1636   if (const Expr *Receiver = NilReceiverBRVisitor::getNilReceiver(Inner, LVNode))
1637     trackExpressionValue(LVNode, Receiver, report, EnableNullFPSuppression);
1638 
1639   // See if the expression we're interested refers to a variable.
1640   // If so, we can track both its contents and constraints on its value.
1641   if (ExplodedGraph::isInterestingLValueExpr(Inner)) {
1642     SVal LVal = LVNode->getSVal(Inner);
1643 
1644     const MemRegion *RR = getLocationRegionIfReference(Inner, LVNode);
1645     bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue();
1646 
1647     // If this is a C++ reference to a null pointer, we are tracking the
1648     // pointer. In addition, we should find the store at which the reference
1649     // got initialized.
1650     if (RR && !LVIsNull)
1651       if (auto KV = LVal.getAs<KnownSVal>())
1652         report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1653               *KV, RR, EnableNullFPSuppression));
1654 
1655     // In case of C++ references, we want to differentiate between a null
1656     // reference and reference to null pointer.
1657     // If the LVal is null, check if we are dealing with null reference.
1658     // For those, we want to track the location of the reference.
1659     const MemRegion *R = (RR && LVIsNull) ? RR :
1660         LVNode->getSVal(Inner).getAsRegion();
1661 
1662     if (R) {
1663 
1664       // Mark both the variable region and its contents as interesting.
1665       SVal V = LVState->getRawSVal(loc::MemRegionVal(R));
1666       report.addVisitor(
1667           llvm::make_unique<NoStoreFuncVisitor>(cast<SubRegion>(R)));
1668 
1669       MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary(
1670           LVNode, R, EnableNullFPSuppression, report, V);
1671 
1672       report.markInteresting(V);
1673       report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(R));
1674 
1675       // If the contents are symbolic, find out when they became null.
1676       if (V.getAsLocSymbol(/*IncludeBaseRegions*/ true))
1677         report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1678               V.castAs<DefinedSVal>(), false));
1679 
1680       // Add visitor, which will suppress inline defensive checks.
1681       if (auto DV = V.getAs<DefinedSVal>())
1682         if (!DV->isZeroConstant() && LVState->isNull(*DV).isConstrainedTrue() &&
1683             EnableNullFPSuppression)
1684           report.addVisitor(
1685               llvm::make_unique<SuppressInlineDefensiveChecksVisitor>(*DV,
1686                                                                       LVNode));
1687 
1688       if (auto KV = V.getAs<KnownSVal>())
1689         report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1690               *KV, R, EnableNullFPSuppression));
1691       return true;
1692     }
1693   }
1694 
1695   // If the expression is not an "lvalue expression", we can still
1696   // track the constraints on its contents.
1697   SVal V = LVState->getSValAsScalarOrLoc(Inner, LVNode->getLocationContext());
1698 
1699   ReturnVisitor::addVisitorIfNecessary(
1700     LVNode, Inner, report, EnableNullFPSuppression);
1701 
1702   // Is it a symbolic value?
1703   if (auto L = V.getAs<loc::MemRegionVal>()) {
1704     report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(L->getRegion()));
1705 
1706     // FIXME: this is a hack for fixing a later crash when attempting to
1707     // dereference a void* pointer.
1708     // We should not try to dereference pointers at all when we don't care
1709     // what is written inside the pointer.
1710     bool CanDereference = true;
1711     if (const auto *SR = dyn_cast<SymbolicRegion>(L->getRegion()))
1712       if (SR->getSymbol()->getType()->getPointeeType()->isVoidType())
1713         CanDereference = false;
1714 
1715     // At this point we are dealing with the region's LValue.
1716     // However, if the rvalue is a symbolic region, we should track it as well.
1717     // Try to use the correct type when looking up the value.
1718     SVal RVal;
1719     if (ExplodedGraph::isInterestingLValueExpr(Inner)) {
1720       RVal = LVState->getRawSVal(L.getValue(), Inner->getType());
1721     } else if (CanDereference) {
1722       RVal = LVState->getSVal(L->getRegion());
1723     }
1724 
1725     if (CanDereference)
1726       if (auto KV = RVal.getAs<KnownSVal>())
1727         report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1728             *KV, L->getRegion(), EnableNullFPSuppression));
1729 
1730     const MemRegion *RegionRVal = RVal.getAsRegion();
1731     if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) {
1732       report.markInteresting(RegionRVal);
1733       report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1734             loc::MemRegionVal(RegionRVal), /*assumption=*/false));
1735     }
1736   }
1737   return true;
1738 }
1739 
1740 const Expr *NilReceiverBRVisitor::getNilReceiver(const Stmt *S,
1741                                                  const ExplodedNode *N) {
1742   const auto *ME = dyn_cast<ObjCMessageExpr>(S);
1743   if (!ME)
1744     return nullptr;
1745   if (const Expr *Receiver = ME->getInstanceReceiver()) {
1746     ProgramStateRef state = N->getState();
1747     SVal V = N->getSVal(Receiver);
1748     if (state->isNull(V).isConstrainedTrue())
1749       return Receiver;
1750   }
1751   return nullptr;
1752 }
1753 
1754 std::shared_ptr<PathDiagnosticPiece>
1755 NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
1756                                 BugReporterContext &BRC, BugReport &BR) {
1757   Optional<PreStmt> P = N->getLocationAs<PreStmt>();
1758   if (!P)
1759     return nullptr;
1760 
1761   const Stmt *S = P->getStmt();
1762   const Expr *Receiver = getNilReceiver(S, N);
1763   if (!Receiver)
1764     return nullptr;
1765 
1766   llvm::SmallString<256> Buf;
1767   llvm::raw_svector_ostream OS(Buf);
1768 
1769   if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
1770     OS << "'";
1771     ME->getSelector().print(OS);
1772     OS << "' not called";
1773   }
1774   else {
1775     OS << "No method is called";
1776   }
1777   OS << " because the receiver is nil";
1778 
1779   // The receiver was nil, and hence the method was skipped.
1780   // Register a BugReporterVisitor to issue a message telling us how
1781   // the receiver was null.
1782   bugreporter::trackExpressionValue(N, Receiver, BR,
1783                                /*EnableNullFPSuppression*/ false);
1784   // Issue a message saying that the method was skipped.
1785   PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
1786                                      N->getLocationContext());
1787   return std::make_shared<PathDiagnosticEventPiece>(L, OS.str());
1788 }
1789 
1790 // Registers every VarDecl inside a Stmt with a last store visitor.
1791 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
1792                                                 const Stmt *S,
1793                                                 bool EnableNullFPSuppression) {
1794   const ExplodedNode *N = BR.getErrorNode();
1795   std::deque<const Stmt *> WorkList;
1796   WorkList.push_back(S);
1797 
1798   while (!WorkList.empty()) {
1799     const Stmt *Head = WorkList.front();
1800     WorkList.pop_front();
1801 
1802     ProgramStateManager &StateMgr = N->getState()->getStateManager();
1803 
1804     if (const auto *DR = dyn_cast<DeclRefExpr>(Head)) {
1805       if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1806         const VarRegion *R =
1807         StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
1808 
1809         // What did we load?
1810         SVal V = N->getSVal(S);
1811 
1812         if (V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1813           // Register a new visitor with the BugReport.
1814           BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1815               V.castAs<KnownSVal>(), R, EnableNullFPSuppression));
1816         }
1817       }
1818     }
1819 
1820     for (const Stmt *SubStmt : Head->children())
1821       WorkList.push_back(SubStmt);
1822   }
1823 }
1824 
1825 //===----------------------------------------------------------------------===//
1826 // Visitor that tries to report interesting diagnostics from conditions.
1827 //===----------------------------------------------------------------------===//
1828 
1829 /// Return the tag associated with this visitor.  This tag will be used
1830 /// to make all PathDiagnosticPieces created by this visitor.
1831 const char *ConditionBRVisitor::getTag() {
1832   return "ConditionBRVisitor";
1833 }
1834 
1835 std::shared_ptr<PathDiagnosticPiece>
1836 ConditionBRVisitor::VisitNode(const ExplodedNode *N,
1837                               BugReporterContext &BRC, BugReport &BR) {
1838   auto piece = VisitNodeImpl(N, BRC, BR);
1839   if (piece) {
1840     piece->setTag(getTag());
1841     if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get()))
1842       ev->setPrunable(true, /* override */ false);
1843   }
1844   return piece;
1845 }
1846 
1847 std::shared_ptr<PathDiagnosticPiece>
1848 ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
1849                                   BugReporterContext &BRC, BugReport &BR) {
1850   ProgramPoint progPoint = N->getLocation();
1851 
1852   // If an assumption was made on a branch, it should be caught
1853   // here by looking at the state transition.
1854   if (Optional<BlockEdge> BE = progPoint.getAs<BlockEdge>()) {
1855     const CFGBlock *srcBlk = BE->getSrc();
1856     if (const Stmt *term = srcBlk->getTerminator())
1857       return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
1858     return nullptr;
1859   }
1860 
1861   if (Optional<PostStmt> PS = progPoint.getAs<PostStmt>()) {
1862     const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
1863         ExprEngine::geteagerlyAssumeBinOpBifurcationTags();
1864 
1865     const ProgramPointTag *tag = PS->getTag();
1866     if (tag == tags.first)
1867       return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
1868                            BRC, BR, N);
1869     if (tag == tags.second)
1870       return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
1871                            BRC, BR, N);
1872 
1873     return nullptr;
1874   }
1875 
1876   return nullptr;
1877 }
1878 
1879 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitTerminator(
1880     const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk,
1881     const CFGBlock *dstBlk, BugReport &R, BugReporterContext &BRC) {
1882   const Expr *Cond = nullptr;
1883 
1884   // In the code below, Term is a CFG terminator and Cond is a branch condition
1885   // expression upon which the decision is made on this terminator.
1886   //
1887   // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator,
1888   // and "x == 0" is the respective condition.
1889   //
1890   // Another example: in "if (x && y)", we've got two terminators and two
1891   // conditions due to short-circuit nature of operator "&&":
1892   // 1. The "if (x && y)" statement is a terminator,
1893   //    and "y" is the respective condition.
1894   // 2. Also "x && ..." is another terminator,
1895   //    and "x" is its condition.
1896 
1897   switch (Term->getStmtClass()) {
1898   // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit
1899   // more tricky because there are more than two branches to account for.
1900   default:
1901     return nullptr;
1902   case Stmt::IfStmtClass:
1903     Cond = cast<IfStmt>(Term)->getCond();
1904     break;
1905   case Stmt::ConditionalOperatorClass:
1906     Cond = cast<ConditionalOperator>(Term)->getCond();
1907     break;
1908   case Stmt::BinaryOperatorClass:
1909     // When we encounter a logical operator (&& or ||) as a CFG terminator,
1910     // then the condition is actually its LHS; otherwise, we'd encounter
1911     // the parent, such as if-statement, as a terminator.
1912     const auto *BO = cast<BinaryOperator>(Term);
1913     assert(BO->isLogicalOp() &&
1914            "CFG terminator is not a short-circuit operator!");
1915     Cond = BO->getLHS();
1916     break;
1917   }
1918 
1919   Cond = Cond->IgnoreParens();
1920 
1921   // However, when we encounter a logical operator as a branch condition,
1922   // then the condition is actually its RHS, because LHS would be
1923   // the condition for the logical operator terminator.
1924   while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) {
1925     if (!InnerBO->isLogicalOp())
1926       break;
1927     Cond = InnerBO->getRHS()->IgnoreParens();
1928   }
1929 
1930   assert(Cond);
1931   assert(srcBlk->succ_size() == 2);
1932   const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
1933   return VisitTrueTest(Cond, tookTrue, BRC, R, N);
1934 }
1935 
1936 std::shared_ptr<PathDiagnosticPiece>
1937 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, bool tookTrue,
1938                                   BugReporterContext &BRC, BugReport &R,
1939                                   const ExplodedNode *N) {
1940   ProgramStateRef CurrentState = N->getState();
1941   ProgramStateRef PreviousState = N->getFirstPred()->getState();
1942   const LocationContext *LCtx = N->getLocationContext();
1943 
1944   // If the constraint information is changed between the current and the
1945   // previous program state we assuming the newly seen constraint information.
1946   // If we cannot evaluate the condition (and the constraints are the same)
1947   // the analyzer has no information about the value and just assuming it.
1948   if (BRC.getStateManager().haveEqualConstraints(CurrentState, PreviousState) &&
1949       CurrentState->getSVal(Cond, LCtx).isValid())
1950     return nullptr;
1951 
1952   // These will be modified in code below, but we need to preserve the original
1953   //  values in case we want to throw the generic message.
1954   const Expr *CondTmp = Cond;
1955   bool tookTrueTmp = tookTrue;
1956 
1957   while (true) {
1958     CondTmp = CondTmp->IgnoreParenCasts();
1959     switch (CondTmp->getStmtClass()) {
1960       default:
1961         break;
1962       case Stmt::BinaryOperatorClass:
1963         if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp),
1964                                    tookTrueTmp, BRC, R, N))
1965           return P;
1966         break;
1967       case Stmt::DeclRefExprClass:
1968         if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp),
1969                                    tookTrueTmp, BRC, R, N))
1970           return P;
1971         break;
1972       case Stmt::UnaryOperatorClass: {
1973         const auto *UO = cast<UnaryOperator>(CondTmp);
1974         if (UO->getOpcode() == UO_LNot) {
1975           tookTrueTmp = !tookTrueTmp;
1976           CondTmp = UO->getSubExpr();
1977           continue;
1978         }
1979         break;
1980       }
1981     }
1982     break;
1983   }
1984 
1985   // Condition too complex to explain? Just say something so that the user
1986   // knew we've made some path decision at this point.
1987   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
1988   if (!Loc.isValid() || !Loc.asLocation().isValid())
1989     return nullptr;
1990 
1991   return std::make_shared<PathDiagnosticEventPiece>(
1992       Loc, tookTrue ? GenericTrueMessage : GenericFalseMessage);
1993 }
1994 
1995 bool ConditionBRVisitor::patternMatch(const Expr *Ex,
1996                                       const Expr *ParentEx,
1997                                       raw_ostream &Out,
1998                                       BugReporterContext &BRC,
1999                                       BugReport &report,
2000                                       const ExplodedNode *N,
2001                                       Optional<bool> &prunable) {
2002   const Expr *OriginalExpr = Ex;
2003   Ex = Ex->IgnoreParenCasts();
2004 
2005   if (isa<GNUNullExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
2006       isa<CXXBoolLiteralExpr>(Ex) || isa<IntegerLiteral>(Ex) ||
2007       isa<FloatingLiteral>(Ex)) {
2008     // Use heuristics to determine if the expression is a macro
2009     // expanding to a literal and if so, use the macro's name.
2010     SourceLocation BeginLoc = OriginalExpr->getBeginLoc();
2011     SourceLocation EndLoc = OriginalExpr->getEndLoc();
2012     if (BeginLoc.isMacroID() && EndLoc.isMacroID()) {
2013       SourceManager &SM = BRC.getSourceManager();
2014       const LangOptions &LO = BRC.getASTContext().getLangOpts();
2015       if (Lexer::isAtStartOfMacroExpansion(BeginLoc, SM, LO) &&
2016           Lexer::isAtEndOfMacroExpansion(EndLoc, SM, LO)) {
2017         CharSourceRange R = Lexer::getAsCharRange({BeginLoc, EndLoc}, SM, LO);
2018         Out << Lexer::getSourceText(R, SM, LO);
2019         return false;
2020       }
2021     }
2022   }
2023 
2024   if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) {
2025     const bool quotes = isa<VarDecl>(DR->getDecl());
2026     if (quotes) {
2027       Out << '\'';
2028       const LocationContext *LCtx = N->getLocationContext();
2029       const ProgramState *state = N->getState().get();
2030       if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
2031                                                 LCtx).getAsRegion()) {
2032         if (report.isInteresting(R))
2033           prunable = false;
2034         else {
2035           const ProgramState *state = N->getState().get();
2036           SVal V = state->getSVal(R);
2037           if (report.isInteresting(V))
2038             prunable = false;
2039         }
2040       }
2041     }
2042     Out << DR->getDecl()->getDeclName().getAsString();
2043     if (quotes)
2044       Out << '\'';
2045     return quotes;
2046   }
2047 
2048   if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) {
2049     QualType OriginalTy = OriginalExpr->getType();
2050     if (OriginalTy->isPointerType()) {
2051       if (IL->getValue() == 0) {
2052         Out << "null";
2053         return false;
2054       }
2055     }
2056     else if (OriginalTy->isObjCObjectPointerType()) {
2057       if (IL->getValue() == 0) {
2058         Out << "nil";
2059         return false;
2060       }
2061     }
2062 
2063     Out << IL->getValue();
2064     return false;
2065   }
2066 
2067   return false;
2068 }
2069 
2070 std::shared_ptr<PathDiagnosticPiece>
2071 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const BinaryOperator *BExpr,
2072                                   const bool tookTrue, BugReporterContext &BRC,
2073                                   BugReport &R, const ExplodedNode *N) {
2074   bool shouldInvert = false;
2075   Optional<bool> shouldPrune;
2076 
2077   SmallString<128> LhsString, RhsString;
2078   {
2079     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
2080     const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS,
2081                                        BRC, R, N, shouldPrune);
2082     const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS,
2083                                        BRC, R, N, shouldPrune);
2084 
2085     shouldInvert = !isVarLHS && isVarRHS;
2086   }
2087 
2088   BinaryOperator::Opcode Op = BExpr->getOpcode();
2089 
2090   if (BinaryOperator::isAssignmentOp(Op)) {
2091     // For assignment operators, all that we care about is that the LHS
2092     // evaluates to "true" or "false".
2093     return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
2094                                   BRC, R, N);
2095   }
2096 
2097   // For non-assignment operations, we require that we can understand
2098   // both the LHS and RHS.
2099   if (LhsString.empty() || RhsString.empty() ||
2100       !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp)
2101     return nullptr;
2102 
2103   // Should we invert the strings if the LHS is not a variable name?
2104   SmallString<256> buf;
2105   llvm::raw_svector_ostream Out(buf);
2106   Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
2107 
2108   // Do we need to invert the opcode?
2109   if (shouldInvert)
2110     switch (Op) {
2111       default: break;
2112       case BO_LT: Op = BO_GT; break;
2113       case BO_GT: Op = BO_LT; break;
2114       case BO_LE: Op = BO_GE; break;
2115       case BO_GE: Op = BO_LE; break;
2116     }
2117 
2118   if (!tookTrue)
2119     switch (Op) {
2120       case BO_EQ: Op = BO_NE; break;
2121       case BO_NE: Op = BO_EQ; break;
2122       case BO_LT: Op = BO_GE; break;
2123       case BO_GT: Op = BO_LE; break;
2124       case BO_LE: Op = BO_GT; break;
2125       case BO_GE: Op = BO_LT; break;
2126       default:
2127         return nullptr;
2128     }
2129 
2130   switch (Op) {
2131     case BO_EQ:
2132       Out << "equal to ";
2133       break;
2134     case BO_NE:
2135       Out << "not equal to ";
2136       break;
2137     default:
2138       Out << BinaryOperator::getOpcodeStr(Op) << ' ';
2139       break;
2140   }
2141 
2142   Out << (shouldInvert ? LhsString : RhsString);
2143   const LocationContext *LCtx = N->getLocationContext();
2144   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2145   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2146   if (shouldPrune.hasValue())
2147     event->setPrunable(shouldPrune.getValue());
2148   return event;
2149 }
2150 
2151 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitConditionVariable(
2152     StringRef LhsString, const Expr *CondVarExpr, const bool tookTrue,
2153     BugReporterContext &BRC, BugReport &report, const ExplodedNode *N) {
2154   // FIXME: If there's already a constraint tracker for this variable,
2155   // we shouldn't emit anything here (c.f. the double note in
2156   // test/Analysis/inlining/path-notes.c)
2157   SmallString<256> buf;
2158   llvm::raw_svector_ostream Out(buf);
2159   Out << "Assuming " << LhsString << " is ";
2160 
2161   QualType Ty = CondVarExpr->getType();
2162 
2163   if (Ty->isPointerType())
2164     Out << (tookTrue ? "not null" : "null");
2165   else if (Ty->isObjCObjectPointerType())
2166     Out << (tookTrue ? "not nil" : "nil");
2167   else if (Ty->isBooleanType())
2168     Out << (tookTrue ? "true" : "false");
2169   else if (Ty->isIntegralOrEnumerationType())
2170     Out << (tookTrue ? "non-zero" : "zero");
2171   else
2172     return nullptr;
2173 
2174   const LocationContext *LCtx = N->getLocationContext();
2175   PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
2176   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2177 
2178   if (const auto *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
2179     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
2180       const ProgramState *state = N->getState().get();
2181       if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2182         if (report.isInteresting(R))
2183           event->setPrunable(false);
2184       }
2185     }
2186   }
2187 
2188   return event;
2189 }
2190 
2191 std::shared_ptr<PathDiagnosticPiece>
2192 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const DeclRefExpr *DR,
2193                                   const bool tookTrue, BugReporterContext &BRC,
2194                                   BugReport &report, const ExplodedNode *N) {
2195   const auto *VD = dyn_cast<VarDecl>(DR->getDecl());
2196   if (!VD)
2197     return nullptr;
2198 
2199   SmallString<256> Buf;
2200   llvm::raw_svector_ostream Out(Buf);
2201 
2202   Out << "Assuming '" << VD->getDeclName() << "' is ";
2203 
2204   QualType VDTy = VD->getType();
2205 
2206   if (VDTy->isPointerType())
2207     Out << (tookTrue ? "non-null" : "null");
2208   else if (VDTy->isObjCObjectPointerType())
2209     Out << (tookTrue ? "non-nil" : "nil");
2210   else if (VDTy->isScalarType())
2211     Out << (tookTrue ? "not equal to 0" : "0");
2212   else
2213     return nullptr;
2214 
2215   const LocationContext *LCtx = N->getLocationContext();
2216   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2217   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2218 
2219   const ProgramState *state = N->getState().get();
2220   if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2221     if (report.isInteresting(R))
2222       event->setPrunable(false);
2223     else {
2224       SVal V = state->getSVal(R);
2225       if (report.isInteresting(V))
2226         event->setPrunable(false);
2227     }
2228   }
2229   return std::move(event);
2230 }
2231 
2232 const char *const ConditionBRVisitor::GenericTrueMessage =
2233     "Assuming the condition is true";
2234 const char *const ConditionBRVisitor::GenericFalseMessage =
2235     "Assuming the condition is false";
2236 
2237 bool ConditionBRVisitor::isPieceMessageGeneric(
2238     const PathDiagnosticPiece *Piece) {
2239   return Piece->getString() == GenericTrueMessage ||
2240          Piece->getString() == GenericFalseMessage;
2241 }
2242 
2243 void LikelyFalsePositiveSuppressionBRVisitor::finalizeVisitor(
2244     BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR) {
2245   // Here we suppress false positives coming from system headers. This list is
2246   // based on known issues.
2247   AnalyzerOptions &Options = BRC.getAnalyzerOptions();
2248   const Decl *D = N->getLocationContext()->getDecl();
2249 
2250   if (AnalysisDeclContext::isInStdNamespace(D)) {
2251     // Skip reports within the 'std' namespace. Although these can sometimes be
2252     // the user's fault, we currently don't report them very well, and
2253     // Note that this will not help for any other data structure libraries, like
2254     // TR1, Boost, or llvm/ADT.
2255     if (Options.ShouldSuppressFromCXXStandardLibrary) {
2256       BR.markInvalid(getTag(), nullptr);
2257       return;
2258     } else {
2259       // If the complete 'std' suppression is not enabled, suppress reports
2260       // from the 'std' namespace that are known to produce false positives.
2261 
2262       // The analyzer issues a false use-after-free when std::list::pop_front
2263       // or std::list::pop_back are called multiple times because we cannot
2264       // reason about the internal invariants of the data structure.
2265       if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2266         const CXXRecordDecl *CD = MD->getParent();
2267         if (CD->getName() == "list") {
2268           BR.markInvalid(getTag(), nullptr);
2269           return;
2270         }
2271       }
2272 
2273       // The analyzer issues a false positive when the constructor of
2274       // std::__independent_bits_engine from algorithms is used.
2275       if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) {
2276         const CXXRecordDecl *CD = MD->getParent();
2277         if (CD->getName() == "__independent_bits_engine") {
2278           BR.markInvalid(getTag(), nullptr);
2279           return;
2280         }
2281       }
2282 
2283       for (const LocationContext *LCtx = N->getLocationContext(); LCtx;
2284            LCtx = LCtx->getParent()) {
2285         const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
2286         if (!MD)
2287           continue;
2288 
2289         const CXXRecordDecl *CD = MD->getParent();
2290         // The analyzer issues a false positive on
2291         //   std::basic_string<uint8_t> v; v.push_back(1);
2292         // and
2293         //   std::u16string s; s += u'a';
2294         // because we cannot reason about the internal invariants of the
2295         // data structure.
2296         if (CD->getName() == "basic_string") {
2297           BR.markInvalid(getTag(), nullptr);
2298           return;
2299         }
2300 
2301         // The analyzer issues a false positive on
2302         //    std::shared_ptr<int> p(new int(1)); p = nullptr;
2303         // because it does not reason properly about temporary destructors.
2304         if (CD->getName() == "shared_ptr") {
2305           BR.markInvalid(getTag(), nullptr);
2306           return;
2307         }
2308       }
2309     }
2310   }
2311 
2312   // Skip reports within the sys/queue.h macros as we do not have the ability to
2313   // reason about data structure shapes.
2314   SourceManager &SM = BRC.getSourceManager();
2315   FullSourceLoc Loc = BR.getLocation(SM).asLocation();
2316   while (Loc.isMacroID()) {
2317     Loc = Loc.getSpellingLoc();
2318     if (SM.getFilename(Loc).endswith("sys/queue.h")) {
2319       BR.markInvalid(getTag(), nullptr);
2320       return;
2321     }
2322   }
2323 }
2324 
2325 std::shared_ptr<PathDiagnosticPiece>
2326 UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N,
2327                                  BugReporterContext &BRC, BugReport &BR) {
2328   ProgramStateRef State = N->getState();
2329   ProgramPoint ProgLoc = N->getLocation();
2330 
2331   // We are only interested in visiting CallEnter nodes.
2332   Optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>();
2333   if (!CEnter)
2334     return nullptr;
2335 
2336   // Check if one of the arguments is the region the visitor is tracking.
2337   CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager();
2338   CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
2339   unsigned Idx = 0;
2340   ArrayRef<ParmVarDecl *> parms = Call->parameters();
2341 
2342   for (const auto ParamDecl : parms) {
2343     const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
2344     ++Idx;
2345 
2346     // Are we tracking the argument or its subregion?
2347     if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts()))
2348       continue;
2349 
2350     // Check the function parameter type.
2351     assert(ParamDecl && "Formal parameter has no decl?");
2352     QualType T = ParamDecl->getType();
2353 
2354     if (!(T->isAnyPointerType() || T->isReferenceType())) {
2355       // Function can only change the value passed in by address.
2356       continue;
2357     }
2358 
2359     // If it is a const pointer value, the function does not intend to
2360     // change the value.
2361     if (T->getPointeeType().isConstQualified())
2362       continue;
2363 
2364     // Mark the call site (LocationContext) as interesting if the value of the
2365     // argument is undefined or '0'/'NULL'.
2366     SVal BoundVal = State->getSVal(R);
2367     if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
2368       BR.markInteresting(CEnter->getCalleeContext());
2369       return nullptr;
2370     }
2371   }
2372   return nullptr;
2373 }
2374 
2375 std::shared_ptr<PathDiagnosticPiece>
2376 CXXSelfAssignmentBRVisitor::VisitNode(const ExplodedNode *Succ,
2377                                       BugReporterContext &BRC, BugReport &) {
2378   if (Satisfied)
2379     return nullptr;
2380 
2381   const auto Edge = Succ->getLocation().getAs<BlockEdge>();
2382   if (!Edge.hasValue())
2383     return nullptr;
2384 
2385   auto Tag = Edge->getTag();
2386   if (!Tag)
2387     return nullptr;
2388 
2389   if (Tag->getTagDescription() != "cplusplus.SelfAssignment")
2390     return nullptr;
2391 
2392   Satisfied = true;
2393 
2394   const auto *Met =
2395       dyn_cast<CXXMethodDecl>(Succ->getCodeDecl().getAsFunction());
2396   assert(Met && "Not a C++ method.");
2397   assert((Met->isCopyAssignmentOperator() || Met->isMoveAssignmentOperator()) &&
2398          "Not a copy/move assignment operator.");
2399 
2400   const auto *LCtx = Edge->getLocationContext();
2401 
2402   const auto &State = Succ->getState();
2403   auto &SVB = State->getStateManager().getSValBuilder();
2404 
2405   const auto Param =
2406       State->getSVal(State->getRegion(Met->getParamDecl(0), LCtx));
2407   const auto This =
2408       State->getSVal(SVB.getCXXThis(Met, LCtx->getStackFrame()));
2409 
2410   auto L = PathDiagnosticLocation::create(Met, BRC.getSourceManager());
2411 
2412   if (!L.isValid() || !L.asLocation().isValid())
2413     return nullptr;
2414 
2415   SmallString<256> Buf;
2416   llvm::raw_svector_ostream Out(Buf);
2417 
2418   Out << "Assuming " << Met->getParamDecl(0)->getName() <<
2419     ((Param == This) ? " == " : " != ") << "*this";
2420 
2421   auto Piece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
2422   Piece->addRange(Met->getSourceRange());
2423 
2424   return std::move(Piece);
2425 }
2426 
2427 
2428 FalsePositiveRefutationBRVisitor::FalsePositiveRefutationBRVisitor()
2429     : Constraints(ConstraintRangeTy::Factory().getEmptyMap()) {}
2430 
2431 void FalsePositiveRefutationBRVisitor::finalizeVisitor(
2432     BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) {
2433   // Collect new constraints
2434   VisitNode(EndPathNode, BRC, BR);
2435 
2436   // Create a refutation manager
2437   llvm::SMTSolverRef RefutationSolver = llvm::CreateZ3Solver();
2438   ASTContext &Ctx = BRC.getASTContext();
2439 
2440   // Add constraints to the solver
2441   for (const auto &I : Constraints) {
2442     const SymbolRef Sym = I.first;
2443     auto RangeIt = I.second.begin();
2444 
2445     llvm::SMTExprRef Constraints = SMTConv::getRangeExpr(
2446         RefutationSolver, Ctx, Sym, RangeIt->From(), RangeIt->To(),
2447         /*InRange=*/true);
2448     while ((++RangeIt) != I.second.end()) {
2449       Constraints = RefutationSolver->mkOr(
2450           Constraints, SMTConv::getRangeExpr(RefutationSolver, Ctx, Sym,
2451                                              RangeIt->From(), RangeIt->To(),
2452                                              /*InRange=*/true));
2453     }
2454 
2455     RefutationSolver->addConstraint(Constraints);
2456   }
2457 
2458   // And check for satisfiability
2459   Optional<bool> isSat = RefutationSolver->check();
2460   if (!isSat.hasValue())
2461     return;
2462 
2463   if (!isSat.getValue())
2464     BR.markInvalid("Infeasible constraints", EndPathNode->getLocationContext());
2465 }
2466 
2467 std::shared_ptr<PathDiagnosticPiece>
2468 FalsePositiveRefutationBRVisitor::VisitNode(const ExplodedNode *N,
2469                                             BugReporterContext &,
2470                                             BugReport &) {
2471   // Collect new constraints
2472   const ConstraintRangeTy &NewCs = N->getState()->get<ConstraintRange>();
2473   ConstraintRangeTy::Factory &CF =
2474       N->getState()->get_context<ConstraintRange>();
2475 
2476   // Add constraints if we don't have them yet
2477   for (auto const &C : NewCs) {
2478     const SymbolRef &Sym = C.first;
2479     if (!Constraints.contains(Sym)) {
2480       Constraints = CF.add(Constraints, Sym, C.second);
2481     }
2482   }
2483 
2484   return nullptr;
2485 }
2486 
2487 int NoteTag::Kind = 0;
2488 
2489 void TagVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
2490   static int Tag = 0;
2491   ID.AddPointer(&Tag);
2492 }
2493 
2494 std::shared_ptr<PathDiagnosticPiece>
2495 TagVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
2496                       BugReport &R) {
2497   ProgramPoint PP = N->getLocation();
2498   const NoteTag *T = dyn_cast_or_null<NoteTag>(PP.getTag());
2499   if (!T)
2500     return nullptr;
2501 
2502   if (Optional<std::string> Msg = T->generateMessage(BRC, R)) {
2503     PathDiagnosticLocation Loc =
2504         PathDiagnosticLocation::create(PP, BRC.getSourceManager());
2505     return std::make_shared<PathDiagnosticEventPiece>(Loc, *Msg);
2506   }
2507 
2508   return nullptr;
2509 }
2510 
2511 void FalsePositiveRefutationBRVisitor::Profile(
2512     llvm::FoldingSetNodeID &ID) const {
2513   static int Tag = 0;
2514   ID.AddPointer(&Tag);
2515 }
2516