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