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