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