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