BugReporter.cpp revision 360784
1//===- BugReporter.cpp - Generate PathDiagnostics for bugs ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file defines BugReporter, a utility class for generating
10//  PathDiagnostics.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclBase.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/ParentMap.h"
21#include "clang/AST/Stmt.h"
22#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtObjC.h"
24#include "clang/Analysis/AnalysisDeclContext.h"
25#include "clang/Analysis/CFG.h"
26#include "clang/Analysis/CFGStmtMap.h"
27#include "clang/Analysis/PathDiagnostic.h"
28#include "clang/Analysis/ProgramPoint.h"
29#include "clang/Basic/LLVM.h"
30#include "clang/Basic/SourceLocation.h"
31#include "clang/Basic/SourceManager.h"
32#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
33#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
34#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
35#include "clang/StaticAnalyzer/Core/Checker.h"
36#include "clang/StaticAnalyzer/Core/CheckerManager.h"
37#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
38#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
39#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
40#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
41#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
42#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
43#include "llvm/ADT/ArrayRef.h"
44#include "llvm/ADT/DenseMap.h"
45#include "llvm/ADT/DenseSet.h"
46#include "llvm/ADT/FoldingSet.h"
47#include "llvm/ADT/None.h"
48#include "llvm/ADT/Optional.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/SmallPtrSet.h"
51#include "llvm/ADT/SmallString.h"
52#include "llvm/ADT/SmallVector.h"
53#include "llvm/ADT/Statistic.h"
54#include "llvm/ADT/StringRef.h"
55#include "llvm/ADT/iterator_range.h"
56#include "llvm/Support/Casting.h"
57#include "llvm/Support/Compiler.h"
58#include "llvm/Support/ErrorHandling.h"
59#include "llvm/Support/MemoryBuffer.h"
60#include "llvm/Support/raw_ostream.h"
61#include <algorithm>
62#include <cassert>
63#include <cstddef>
64#include <iterator>
65#include <memory>
66#include <queue>
67#include <string>
68#include <tuple>
69#include <utility>
70#include <vector>
71
72using namespace clang;
73using namespace ento;
74using namespace llvm;
75
76#define DEBUG_TYPE "BugReporter"
77
78STATISTIC(MaxBugClassSize,
79          "The maximum number of bug reports in the same equivalence class");
80STATISTIC(MaxValidBugClassSize,
81          "The maximum number of bug reports in the same equivalence class "
82          "where at least one report is valid (not suppressed)");
83
84BugReporterVisitor::~BugReporterVisitor() = default;
85
86void BugReporterContext::anchor() {}
87
88//===----------------------------------------------------------------------===//
89// PathDiagnosticBuilder and its associated routines and helper objects.
90//===----------------------------------------------------------------------===//
91
92namespace {
93
94/// A (CallPiece, node assiciated with its CallEnter) pair.
95using CallWithEntry =
96    std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>;
97using CallWithEntryStack = SmallVector<CallWithEntry, 6>;
98
99/// Map from each node to the diagnostic pieces visitors emit for them.
100using VisitorsDiagnosticsTy =
101    llvm::DenseMap<const ExplodedNode *, std::vector<PathDiagnosticPieceRef>>;
102
103/// A map from PathDiagnosticPiece to the LocationContext of the inlined
104/// function call it represents.
105using LocationContextMap =
106    llvm::DenseMap<const PathPieces *, const LocationContext *>;
107
108/// A helper class that contains everything needed to construct a
109/// PathDiagnostic object. It does no much more then providing convenient
110/// getters and some well placed asserts for extra security.
111class PathDiagnosticConstruct {
112  /// The consumer we're constructing the bug report for.
113  const PathDiagnosticConsumer *Consumer;
114  /// Our current position in the bug path, which is owned by
115  /// PathDiagnosticBuilder.
116  const ExplodedNode *CurrentNode;
117  /// A mapping from parts of the bug path (for example, a function call, which
118  /// would span backwards from a CallExit to a CallEnter with the nodes in
119  /// between them) with the location contexts it is associated with.
120  LocationContextMap LCM;
121  const SourceManager &SM;
122
123public:
124  /// We keep stack of calls to functions as we're ascending the bug path.
125  /// TODO: PathDiagnostic has a stack doing the same thing, shouldn't we use
126  /// that instead?
127  CallWithEntryStack CallStack;
128  /// The bug report we're constructing. For ease of use, this field is kept
129  /// public, though some "shortcut" getters are provided for commonly used
130  /// methods of PathDiagnostic.
131  std::unique_ptr<PathDiagnostic> PD;
132
133public:
134  PathDiagnosticConstruct(const PathDiagnosticConsumer *PDC,
135                          const ExplodedNode *ErrorNode,
136                          const PathSensitiveBugReport *R);
137
138  /// \returns the location context associated with the current position in the
139  /// bug path.
140  const LocationContext *getCurrLocationContext() const {
141    assert(CurrentNode && "Already reached the root!");
142    return CurrentNode->getLocationContext();
143  }
144
145  /// Same as getCurrLocationContext (they should always return the same
146  /// location context), but works after reaching the root of the bug path as
147  /// well.
148  const LocationContext *getLocationContextForActivePath() const {
149    return LCM.find(&PD->getActivePath())->getSecond();
150  }
151
152  const ExplodedNode *getCurrentNode() const { return CurrentNode; }
153
154  /// Steps the current node to its predecessor.
155  /// \returns whether we reached the root of the bug path.
156  bool ascendToPrevNode() {
157    CurrentNode = CurrentNode->getFirstPred();
158    return static_cast<bool>(CurrentNode);
159  }
160
161  const ParentMap &getParentMap() const {
162    return getCurrLocationContext()->getParentMap();
163  }
164
165  const SourceManager &getSourceManager() const { return SM; }
166
167  const Stmt *getParent(const Stmt *S) const {
168    return getParentMap().getParent(S);
169  }
170
171  void updateLocCtxMap(const PathPieces *Path, const LocationContext *LC) {
172    assert(Path && LC);
173    LCM[Path] = LC;
174  }
175
176  const LocationContext *getLocationContextFor(const PathPieces *Path) const {
177    assert(LCM.count(Path) &&
178           "Failed to find the context associated with these pieces!");
179    return LCM.find(Path)->getSecond();
180  }
181
182  bool isInLocCtxMap(const PathPieces *Path) const { return LCM.count(Path); }
183
184  PathPieces &getActivePath() { return PD->getActivePath(); }
185  PathPieces &getMutablePieces() { return PD->getMutablePieces(); }
186
187  bool shouldAddPathEdges() const { return Consumer->shouldAddPathEdges(); }
188  bool shouldGenerateDiagnostics() const {
189    return Consumer->shouldGenerateDiagnostics();
190  }
191  bool supportsLogicalOpControlFlow() const {
192    return Consumer->supportsLogicalOpControlFlow();
193  }
194};
195
196/// Contains every contextual information needed for constructing a
197/// PathDiagnostic object for a given bug report. This class and its fields are
198/// immutable, and passes a BugReportConstruct object around during the
199/// construction.
200class PathDiagnosticBuilder : public BugReporterContext {
201  /// A linear path from the error node to the root.
202  std::unique_ptr<const ExplodedGraph> BugPath;
203  /// The bug report we're describing. Visitors create their diagnostics with
204  /// them being the last entities being able to modify it (for example,
205  /// changing interestingness here would cause inconsistencies as to how this
206  /// file and visitors construct diagnostics), hence its const.
207  const PathSensitiveBugReport *R;
208  /// The leaf of the bug path. This isn't the same as the bug reports error
209  /// node, which refers to the *original* graph, not the bug path.
210  const ExplodedNode *const ErrorNode;
211  /// The diagnostic pieces visitors emitted, which is expected to be collected
212  /// by the time this builder is constructed.
213  std::unique_ptr<const VisitorsDiagnosticsTy> VisitorsDiagnostics;
214
215public:
216  /// Find a non-invalidated report for a given equivalence class,  and returns
217  /// a PathDiagnosticBuilder able to construct bug reports for different
218  /// consumers. Returns None if no valid report is found.
219  static Optional<PathDiagnosticBuilder>
220  findValidReport(ArrayRef<PathSensitiveBugReport *> &bugReports,
221                  PathSensitiveBugReporter &Reporter);
222
223  PathDiagnosticBuilder(
224      BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
225      PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
226      std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics);
227
228  /// This function is responsible for generating diagnostic pieces that are
229  /// *not* provided by bug report visitors.
230  /// These diagnostics may differ depending on the consumer's settings,
231  /// and are therefore constructed separately for each consumer.
232  ///
233  /// There are two path diagnostics generation modes: with adding edges (used
234  /// for plists) and without  (used for HTML and text). When edges are added,
235  /// the path is modified to insert artificially generated edges.
236  /// Otherwise, more detailed diagnostics is emitted for block edges,
237  /// explaining the transitions in words.
238  std::unique_ptr<PathDiagnostic>
239  generate(const PathDiagnosticConsumer *PDC) const;
240
241private:
242  void updateStackPiecesWithMessage(PathDiagnosticPieceRef P,
243                                    const CallWithEntryStack &CallStack) const;
244  void generatePathDiagnosticsForNode(PathDiagnosticConstruct &C,
245                                      PathDiagnosticLocation &PrevLoc) const;
246
247  void generateMinimalDiagForBlockEdge(PathDiagnosticConstruct &C,
248                                       BlockEdge BE) const;
249
250  PathDiagnosticPieceRef
251  generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S,
252                        PathDiagnosticLocation &Start) const;
253
254  PathDiagnosticPieceRef
255  generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst,
256                          PathDiagnosticLocation &Start) const;
257
258  PathDiagnosticPieceRef
259  generateDiagForBinaryOP(const PathDiagnosticConstruct &C, const Stmt *T,
260                          const CFGBlock *Src, const CFGBlock *DstC) const;
261
262  PathDiagnosticLocation
263  ExecutionContinues(const PathDiagnosticConstruct &C) const;
264
265  PathDiagnosticLocation
266  ExecutionContinues(llvm::raw_string_ostream &os,
267                     const PathDiagnosticConstruct &C) const;
268
269  const PathSensitiveBugReport *getBugReport() const { return R; }
270};
271
272} // namespace
273
274//===----------------------------------------------------------------------===//
275// Base implementation of stack hint generators.
276//===----------------------------------------------------------------------===//
277
278StackHintGenerator::~StackHintGenerator() = default;
279
280std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
281  if (!N)
282    return getMessageForSymbolNotFound();
283
284  ProgramPoint P = N->getLocation();
285  CallExitEnd CExit = P.castAs<CallExitEnd>();
286
287  // FIXME: Use CallEvent to abstract this over all calls.
288  const Stmt *CallSite = CExit.getCalleeContext()->getCallSite();
289  const auto *CE = dyn_cast_or_null<CallExpr>(CallSite);
290  if (!CE)
291    return {};
292
293  // Check if one of the parameters are set to the interesting symbol.
294  unsigned ArgIndex = 0;
295  for (CallExpr::const_arg_iterator I = CE->arg_begin(),
296                                    E = CE->arg_end(); I != E; ++I, ++ArgIndex){
297    SVal SV = N->getSVal(*I);
298
299    // Check if the variable corresponding to the symbol is passed by value.
300    SymbolRef AS = SV.getAsLocSymbol();
301    if (AS == Sym) {
302      return getMessageForArg(*I, ArgIndex);
303    }
304
305    // Check if the parameter is a pointer to the symbol.
306    if (Optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) {
307      // Do not attempt to dereference void*.
308      if ((*I)->getType()->isVoidPointerType())
309        continue;
310      SVal PSV = N->getState()->getSVal(Reg->getRegion());
311      SymbolRef AS = PSV.getAsLocSymbol();
312      if (AS == Sym) {
313        return getMessageForArg(*I, ArgIndex);
314      }
315    }
316  }
317
318  // Check if we are returning the interesting symbol.
319  SVal SV = N->getSVal(CE);
320  SymbolRef RetSym = SV.getAsLocSymbol();
321  if (RetSym == Sym) {
322    return getMessageForReturn(CE);
323  }
324
325  return getMessageForSymbolNotFound();
326}
327
328std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE,
329                                                          unsigned ArgIndex) {
330  // Printed parameters start at 1, not 0.
331  ++ArgIndex;
332
333  return (llvm::Twine(Msg) + " via " + std::to_string(ArgIndex) +
334          llvm::getOrdinalSuffix(ArgIndex) + " parameter").str();
335}
336
337//===----------------------------------------------------------------------===//
338// Diagnostic cleanup.
339//===----------------------------------------------------------------------===//
340
341static PathDiagnosticEventPiece *
342eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
343                            PathDiagnosticEventPiece *Y) {
344  // Prefer diagnostics that come from ConditionBRVisitor over
345  // those that came from TrackConstraintBRVisitor,
346  // unless the one from ConditionBRVisitor is
347  // its generic fallback diagnostic.
348  const void *tagPreferred = ConditionBRVisitor::getTag();
349  const void *tagLesser = TrackConstraintBRVisitor::getTag();
350
351  if (X->getLocation() != Y->getLocation())
352    return nullptr;
353
354  if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
355    return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X;
356
357  if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
358    return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y;
359
360  return nullptr;
361}
362
363/// An optimization pass over PathPieces that removes redundant diagnostics
364/// generated by both ConditionBRVisitor and TrackConstraintBRVisitor.  Both
365/// BugReporterVisitors use different methods to generate diagnostics, with
366/// one capable of emitting diagnostics in some cases but not in others.  This
367/// can lead to redundant diagnostic pieces at the same point in a path.
368static void removeRedundantMsgs(PathPieces &path) {
369  unsigned N = path.size();
370  if (N < 2)
371    return;
372  // NOTE: this loop intentionally is not using an iterator.  Instead, we
373  // are streaming the path and modifying it in place.  This is done by
374  // grabbing the front, processing it, and if we decide to keep it append
375  // it to the end of the path.  The entire path is processed in this way.
376  for (unsigned i = 0; i < N; ++i) {
377    auto piece = std::move(path.front());
378    path.pop_front();
379
380    switch (piece->getKind()) {
381      case PathDiagnosticPiece::Call:
382        removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
383        break;
384      case PathDiagnosticPiece::Macro:
385        removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
386        break;
387      case PathDiagnosticPiece::Event: {
388        if (i == N-1)
389          break;
390
391        if (auto *nextEvent =
392            dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
393          auto *event = cast<PathDiagnosticEventPiece>(piece.get());
394          // Check to see if we should keep one of the two pieces.  If we
395          // come up with a preference, record which piece to keep, and consume
396          // another piece from the path.
397          if (auto *pieceToKeep =
398                  eventsDescribeSameCondition(event, nextEvent)) {
399            piece = std::move(pieceToKeep == event ? piece : path.front());
400            path.pop_front();
401            ++i;
402          }
403        }
404        break;
405      }
406      case PathDiagnosticPiece::ControlFlow:
407      case PathDiagnosticPiece::Note:
408      case PathDiagnosticPiece::PopUp:
409        break;
410    }
411    path.push_back(std::move(piece));
412  }
413}
414
415/// Recursively scan through a path and prune out calls and macros pieces
416/// that aren't needed.  Return true if afterwards the path contains
417/// "interesting stuff" which means it shouldn't be pruned from the parent path.
418static bool removeUnneededCalls(const PathDiagnosticConstruct &C,
419                                PathPieces &pieces,
420                                const PathSensitiveBugReport *R,
421                                bool IsInteresting = false) {
422  bool containsSomethingInteresting = IsInteresting;
423  const unsigned N = pieces.size();
424
425  for (unsigned i = 0 ; i < N ; ++i) {
426    // Remove the front piece from the path.  If it is still something we
427    // want to keep once we are done, we will push it back on the end.
428    auto piece = std::move(pieces.front());
429    pieces.pop_front();
430
431    switch (piece->getKind()) {
432      case PathDiagnosticPiece::Call: {
433        auto &call = cast<PathDiagnosticCallPiece>(*piece);
434        // Check if the location context is interesting.
435        if (!removeUnneededCalls(
436                C, call.path, R,
437                R->isInteresting(C.getLocationContextFor(&call.path))))
438          continue;
439
440        containsSomethingInteresting = true;
441        break;
442      }
443      case PathDiagnosticPiece::Macro: {
444        auto &macro = cast<PathDiagnosticMacroPiece>(*piece);
445        if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting))
446          continue;
447        containsSomethingInteresting = true;
448        break;
449      }
450      case PathDiagnosticPiece::Event: {
451        auto &event = cast<PathDiagnosticEventPiece>(*piece);
452
453        // We never throw away an event, but we do throw it away wholesale
454        // as part of a path if we throw the entire path away.
455        containsSomethingInteresting |= !event.isPrunable();
456        break;
457      }
458      case PathDiagnosticPiece::ControlFlow:
459      case PathDiagnosticPiece::Note:
460      case PathDiagnosticPiece::PopUp:
461        break;
462    }
463
464    pieces.push_back(std::move(piece));
465  }
466
467  return containsSomethingInteresting;
468}
469
470/// Same logic as above to remove extra pieces.
471static void removePopUpNotes(PathPieces &Path) {
472  for (unsigned int i = 0; i < Path.size(); ++i) {
473    auto Piece = std::move(Path.front());
474    Path.pop_front();
475    if (!isa<PathDiagnosticPopUpPiece>(*Piece))
476      Path.push_back(std::move(Piece));
477  }
478}
479
480/// Returns true if the given decl has been implicitly given a body, either by
481/// the analyzer or by the compiler proper.
482static bool hasImplicitBody(const Decl *D) {
483  assert(D);
484  return D->isImplicit() || !D->hasBody();
485}
486
487/// Recursively scan through a path and make sure that all call pieces have
488/// valid locations.
489static void
490adjustCallLocations(PathPieces &Pieces,
491                    PathDiagnosticLocation *LastCallLocation = nullptr) {
492  for (const auto &I : Pieces) {
493    auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get());
494
495    if (!Call)
496      continue;
497
498    if (LastCallLocation) {
499      bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
500      if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
501        Call->callEnter = *LastCallLocation;
502      if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
503        Call->callReturn = *LastCallLocation;
504    }
505
506    // Recursively clean out the subclass.  Keep this call around if
507    // it contains any informative diagnostics.
508    PathDiagnosticLocation *ThisCallLocation;
509    if (Call->callEnterWithin.asLocation().isValid() &&
510        !hasImplicitBody(Call->getCallee()))
511      ThisCallLocation = &Call->callEnterWithin;
512    else
513      ThisCallLocation = &Call->callEnter;
514
515    assert(ThisCallLocation && "Outermost call has an invalid location");
516    adjustCallLocations(Call->path, ThisCallLocation);
517  }
518}
519
520/// Remove edges in and out of C++ default initializer expressions. These are
521/// for fields that have in-class initializers, as opposed to being initialized
522/// explicitly in a constructor or braced list.
523static void removeEdgesToDefaultInitializers(PathPieces &Pieces) {
524  for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
525    if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
526      removeEdgesToDefaultInitializers(C->path);
527
528    if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
529      removeEdgesToDefaultInitializers(M->subPieces);
530
531    if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) {
532      const Stmt *Start = CF->getStartLocation().asStmt();
533      const Stmt *End = CF->getEndLocation().asStmt();
534      if (Start && isa<CXXDefaultInitExpr>(Start)) {
535        I = Pieces.erase(I);
536        continue;
537      } else if (End && isa<CXXDefaultInitExpr>(End)) {
538        PathPieces::iterator Next = std::next(I);
539        if (Next != E) {
540          if (auto *NextCF =
541                  dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) {
542            NextCF->setStartLocation(CF->getStartLocation());
543          }
544        }
545        I = Pieces.erase(I);
546        continue;
547      }
548    }
549
550    I++;
551  }
552}
553
554/// Remove all pieces with invalid locations as these cannot be serialized.
555/// We might have pieces with invalid locations as a result of inlining Body
556/// Farm generated functions.
557static void removePiecesWithInvalidLocations(PathPieces &Pieces) {
558  for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
559    if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
560      removePiecesWithInvalidLocations(C->path);
561
562    if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
563      removePiecesWithInvalidLocations(M->subPieces);
564
565    if (!(*I)->getLocation().isValid() ||
566        !(*I)->getLocation().asLocation().isValid()) {
567      I = Pieces.erase(I);
568      continue;
569    }
570    I++;
571  }
572}
573
574PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
575    const PathDiagnosticConstruct &C) const {
576  if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
577    return PathDiagnosticLocation(S, getSourceManager(),
578                                  C.getCurrLocationContext());
579
580  return PathDiagnosticLocation::createDeclEnd(C.getCurrLocationContext(),
581                                               getSourceManager());
582}
583
584PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
585    llvm::raw_string_ostream &os, const PathDiagnosticConstruct &C) const {
586  // Slow, but probably doesn't matter.
587  if (os.str().empty())
588    os << ' ';
589
590  const PathDiagnosticLocation &Loc = ExecutionContinues(C);
591
592  if (Loc.asStmt())
593    os << "Execution continues on line "
594       << getSourceManager().getExpansionLineNumber(Loc.asLocation())
595       << '.';
596  else {
597    os << "Execution jumps to the end of the ";
598    const Decl *D = C.getCurrLocationContext()->getDecl();
599    if (isa<ObjCMethodDecl>(D))
600      os << "method";
601    else if (isa<FunctionDecl>(D))
602      os << "function";
603    else {
604      assert(isa<BlockDecl>(D));
605      os << "anonymous block";
606    }
607    os << '.';
608  }
609
610  return Loc;
611}
612
613static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
614  if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
615    return PM.getParentIgnoreParens(S);
616
617  const Stmt *Parent = PM.getParentIgnoreParens(S);
618  if (!Parent)
619    return nullptr;
620
621  switch (Parent->getStmtClass()) {
622  case Stmt::ForStmtClass:
623  case Stmt::DoStmtClass:
624  case Stmt::WhileStmtClass:
625  case Stmt::ObjCForCollectionStmtClass:
626  case Stmt::CXXForRangeStmtClass:
627    return Parent;
628  default:
629    break;
630  }
631
632  return nullptr;
633}
634
635static PathDiagnosticLocation
636getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC,
637                         bool allowNestedContexts = false) {
638  if (!S)
639    return {};
640
641  const SourceManager &SMgr = LC->getDecl()->getASTContext().getSourceManager();
642
643  while (const Stmt *Parent = getEnclosingParent(S, LC->getParentMap())) {
644    switch (Parent->getStmtClass()) {
645      case Stmt::BinaryOperatorClass: {
646        const auto *B = cast<BinaryOperator>(Parent);
647        if (B->isLogicalOp())
648          return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
649        break;
650      }
651      case Stmt::CompoundStmtClass:
652      case Stmt::StmtExprClass:
653        return PathDiagnosticLocation(S, SMgr, LC);
654      case Stmt::ChooseExprClass:
655        // Similar to '?' if we are referring to condition, just have the edge
656        // point to the entire choose expression.
657        if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
658          return PathDiagnosticLocation(Parent, SMgr, LC);
659        else
660          return PathDiagnosticLocation(S, SMgr, LC);
661      case Stmt::BinaryConditionalOperatorClass:
662      case Stmt::ConditionalOperatorClass:
663        // For '?', if we are referring to condition, just have the edge point
664        // to the entire '?' expression.
665        if (allowNestedContexts ||
666            cast<AbstractConditionalOperator>(Parent)->getCond() == S)
667          return PathDiagnosticLocation(Parent, SMgr, LC);
668        else
669          return PathDiagnosticLocation(S, SMgr, LC);
670      case Stmt::CXXForRangeStmtClass:
671        if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
672          return PathDiagnosticLocation(S, SMgr, LC);
673        break;
674      case Stmt::DoStmtClass:
675          return PathDiagnosticLocation(S, SMgr, LC);
676      case Stmt::ForStmtClass:
677        if (cast<ForStmt>(Parent)->getBody() == S)
678          return PathDiagnosticLocation(S, SMgr, LC);
679        break;
680      case Stmt::IfStmtClass:
681        if (cast<IfStmt>(Parent)->getCond() != S)
682          return PathDiagnosticLocation(S, SMgr, LC);
683        break;
684      case Stmt::ObjCForCollectionStmtClass:
685        if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
686          return PathDiagnosticLocation(S, SMgr, LC);
687        break;
688      case Stmt::WhileStmtClass:
689        if (cast<WhileStmt>(Parent)->getCond() != S)
690          return PathDiagnosticLocation(S, SMgr, LC);
691        break;
692      default:
693        break;
694    }
695
696    S = Parent;
697  }
698
699  assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
700
701  return PathDiagnosticLocation(S, SMgr, LC);
702}
703
704//===----------------------------------------------------------------------===//
705// "Minimal" path diagnostic generation algorithm.
706//===----------------------------------------------------------------------===//
707
708/// If the piece contains a special message, add it to all the call pieces on
709/// the active stack. For example, my_malloc allocated memory, so MallocChecker
710/// will construct an event at the call to malloc(), and add a stack hint that
711/// an allocated memory was returned. We'll use this hint to construct a message
712/// when returning from the call to my_malloc
713///
714///   void *my_malloc() { return malloc(sizeof(int)); }
715///   void fishy() {
716///     void *ptr = my_malloc(); // returned allocated memory
717///   } // leak
718void PathDiagnosticBuilder::updateStackPiecesWithMessage(
719    PathDiagnosticPieceRef P, const CallWithEntryStack &CallStack) const {
720  if (R->hasCallStackHint(P))
721    for (const auto &I : CallStack) {
722      PathDiagnosticCallPiece *CP = I.first;
723      const ExplodedNode *N = I.second;
724      std::string stackMsg = R->getCallStackMessage(P, N);
725
726      // The last message on the path to final bug is the most important
727      // one. Since we traverse the path backwards, do not add the message
728      // if one has been previously added.
729      if (!CP->hasCallStackMessage())
730        CP->setCallStackMessage(stackMsg);
731    }
732}
733
734static void CompactMacroExpandedPieces(PathPieces &path,
735                                       const SourceManager& SM);
736
737PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP(
738    const PathDiagnosticConstruct &C, const CFGBlock *Dst,
739    PathDiagnosticLocation &Start) const {
740
741  const SourceManager &SM = getSourceManager();
742  // Figure out what case arm we took.
743  std::string sbuf;
744  llvm::raw_string_ostream os(sbuf);
745  PathDiagnosticLocation End;
746
747  if (const Stmt *S = Dst->getLabel()) {
748    End = PathDiagnosticLocation(S, SM, C.getCurrLocationContext());
749
750    switch (S->getStmtClass()) {
751    default:
752      os << "No cases match in the switch statement. "
753        "Control jumps to line "
754        << End.asLocation().getExpansionLineNumber();
755      break;
756    case Stmt::DefaultStmtClass:
757      os << "Control jumps to the 'default' case at line "
758        << End.asLocation().getExpansionLineNumber();
759      break;
760
761    case Stmt::CaseStmtClass: {
762      os << "Control jumps to 'case ";
763      const auto *Case = cast<CaseStmt>(S);
764      const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
765
766      // Determine if it is an enum.
767      bool GetRawInt = true;
768
769      if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) {
770        // FIXME: Maybe this should be an assertion.  Are there cases
771        // were it is not an EnumConstantDecl?
772        const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl());
773
774        if (D) {
775          GetRawInt = false;
776          os << *D;
777        }
778      }
779
780      if (GetRawInt)
781        os << LHS->EvaluateKnownConstInt(getASTContext());
782
783      os << ":'  at line " << End.asLocation().getExpansionLineNumber();
784      break;
785    }
786    }
787  } else {
788    os << "'Default' branch taken. ";
789    End = ExecutionContinues(os, C);
790  }
791  return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
792                                                       os.str());
793}
794
795PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
796    const PathDiagnosticConstruct &C, const Stmt *S,
797    PathDiagnosticLocation &Start) const {
798  std::string sbuf;
799  llvm::raw_string_ostream os(sbuf);
800  const PathDiagnosticLocation &End =
801      getEnclosingStmtLocation(S, C.getCurrLocationContext());
802  os << "Control jumps to line " << End.asLocation().getExpansionLineNumber();
803  return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str());
804}
805
806PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
807    const PathDiagnosticConstruct &C, const Stmt *T, const CFGBlock *Src,
808    const CFGBlock *Dst) const {
809
810  const SourceManager &SM = getSourceManager();
811
812  const auto *B = cast<BinaryOperator>(T);
813  std::string sbuf;
814  llvm::raw_string_ostream os(sbuf);
815  os << "Left side of '";
816  PathDiagnosticLocation Start, End;
817
818  if (B->getOpcode() == BO_LAnd) {
819    os << "&&"
820      << "' is ";
821
822    if (*(Src->succ_begin() + 1) == Dst) {
823      os << "false";
824      End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
825      Start =
826        PathDiagnosticLocation::createOperatorLoc(B, SM);
827    } else {
828      os << "true";
829      Start =
830          PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
831      End = ExecutionContinues(C);
832    }
833  } else {
834    assert(B->getOpcode() == BO_LOr);
835    os << "||"
836      << "' is ";
837
838    if (*(Src->succ_begin() + 1) == Dst) {
839      os << "false";
840      Start =
841          PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
842      End = ExecutionContinues(C);
843    } else {
844      os << "true";
845      End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
846      Start =
847        PathDiagnosticLocation::createOperatorLoc(B, SM);
848    }
849  }
850  return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
851                                                         os.str());
852}
853
854void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
855    PathDiagnosticConstruct &C, BlockEdge BE) const {
856  const SourceManager &SM = getSourceManager();
857  const LocationContext *LC = C.getCurrLocationContext();
858  const CFGBlock *Src = BE.getSrc();
859  const CFGBlock *Dst = BE.getDst();
860  const Stmt *T = Src->getTerminatorStmt();
861  if (!T)
862    return;
863
864  auto Start = PathDiagnosticLocation::createBegin(T, SM, LC);
865  switch (T->getStmtClass()) {
866  default:
867    break;
868
869  case Stmt::GotoStmtClass:
870  case Stmt::IndirectGotoStmtClass: {
871    if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
872      C.getActivePath().push_front(generateDiagForGotoOP(C, S, Start));
873    break;
874  }
875
876  case Stmt::SwitchStmtClass: {
877    C.getActivePath().push_front(generateDiagForSwitchOP(C, Dst, Start));
878    break;
879  }
880
881  case Stmt::BreakStmtClass:
882  case Stmt::ContinueStmtClass: {
883    std::string sbuf;
884    llvm::raw_string_ostream os(sbuf);
885    PathDiagnosticLocation End = ExecutionContinues(os, C);
886    C.getActivePath().push_front(
887        std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
888    break;
889  }
890
891  // Determine control-flow for ternary '?'.
892  case Stmt::BinaryConditionalOperatorClass:
893  case Stmt::ConditionalOperatorClass: {
894    std::string sbuf;
895    llvm::raw_string_ostream os(sbuf);
896    os << "'?' condition is ";
897
898    if (*(Src->succ_begin() + 1) == Dst)
899      os << "false";
900    else
901      os << "true";
902
903    PathDiagnosticLocation End = ExecutionContinues(C);
904
905    if (const Stmt *S = End.asStmt())
906      End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
907
908    C.getActivePath().push_front(
909        std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
910    break;
911  }
912
913  // Determine control-flow for short-circuited '&&' and '||'.
914  case Stmt::BinaryOperatorClass: {
915    if (!C.supportsLogicalOpControlFlow())
916      break;
917
918    C.getActivePath().push_front(generateDiagForBinaryOP(C, T, Src, Dst));
919    break;
920  }
921
922  case Stmt::DoStmtClass:
923    if (*(Src->succ_begin()) == Dst) {
924      std::string sbuf;
925      llvm::raw_string_ostream os(sbuf);
926
927      os << "Loop condition is true. ";
928      PathDiagnosticLocation End = ExecutionContinues(os, C);
929
930      if (const Stmt *S = End.asStmt())
931        End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
932
933      C.getActivePath().push_front(
934          std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
935                                                           os.str()));
936    } else {
937      PathDiagnosticLocation End = ExecutionContinues(C);
938
939      if (const Stmt *S = End.asStmt())
940        End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
941
942      C.getActivePath().push_front(
943          std::make_shared<PathDiagnosticControlFlowPiece>(
944              Start, End, "Loop condition is false.  Exiting loop"));
945    }
946    break;
947
948  case Stmt::WhileStmtClass:
949  case Stmt::ForStmtClass:
950    if (*(Src->succ_begin() + 1) == Dst) {
951      std::string sbuf;
952      llvm::raw_string_ostream os(sbuf);
953
954      os << "Loop condition is false. ";
955      PathDiagnosticLocation End = ExecutionContinues(os, C);
956      if (const Stmt *S = End.asStmt())
957        End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
958
959      C.getActivePath().push_front(
960          std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
961                                                           os.str()));
962    } else {
963      PathDiagnosticLocation End = ExecutionContinues(C);
964      if (const Stmt *S = End.asStmt())
965        End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
966
967      C.getActivePath().push_front(
968          std::make_shared<PathDiagnosticControlFlowPiece>(
969              Start, End, "Loop condition is true.  Entering loop body"));
970    }
971
972    break;
973
974  case Stmt::IfStmtClass: {
975    PathDiagnosticLocation End = ExecutionContinues(C);
976
977    if (const Stmt *S = End.asStmt())
978      End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
979
980    if (*(Src->succ_begin() + 1) == Dst)
981      C.getActivePath().push_front(
982          std::make_shared<PathDiagnosticControlFlowPiece>(
983              Start, End, "Taking false branch"));
984    else
985      C.getActivePath().push_front(
986          std::make_shared<PathDiagnosticControlFlowPiece>(
987              Start, End, "Taking true branch"));
988
989    break;
990  }
991  }
992}
993
994//===----------------------------------------------------------------------===//
995// Functions for determining if a loop was executed 0 times.
996//===----------------------------------------------------------------------===//
997
998static bool isLoop(const Stmt *Term) {
999  switch (Term->getStmtClass()) {
1000    case Stmt::ForStmtClass:
1001    case Stmt::WhileStmtClass:
1002    case Stmt::ObjCForCollectionStmtClass:
1003    case Stmt::CXXForRangeStmtClass:
1004      return true;
1005    default:
1006      // Note that we intentionally do not include do..while here.
1007      return false;
1008  }
1009}
1010
1011static bool isJumpToFalseBranch(const BlockEdge *BE) {
1012  const CFGBlock *Src = BE->getSrc();
1013  assert(Src->succ_size() == 2);
1014  return (*(Src->succ_begin()+1) == BE->getDst());
1015}
1016
1017static bool isContainedByStmt(const ParentMap &PM, const Stmt *S,
1018                              const Stmt *SubS) {
1019  while (SubS) {
1020    if (SubS == S)
1021      return true;
1022    SubS = PM.getParent(SubS);
1023  }
1024  return false;
1025}
1026
1027static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term,
1028                                     const ExplodedNode *N) {
1029  while (N) {
1030    Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1031    if (SP) {
1032      const Stmt *S = SP->getStmt();
1033      if (!isContainedByStmt(PM, Term, S))
1034        return S;
1035    }
1036    N = N->getFirstPred();
1037  }
1038  return nullptr;
1039}
1040
1041static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) {
1042  const Stmt *LoopBody = nullptr;
1043  switch (Term->getStmtClass()) {
1044    case Stmt::CXXForRangeStmtClass: {
1045      const auto *FR = cast<CXXForRangeStmt>(Term);
1046      if (isContainedByStmt(PM, FR->getInc(), S))
1047        return true;
1048      if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
1049        return true;
1050      LoopBody = FR->getBody();
1051      break;
1052    }
1053    case Stmt::ForStmtClass: {
1054      const auto *FS = cast<ForStmt>(Term);
1055      if (isContainedByStmt(PM, FS->getInc(), S))
1056        return true;
1057      LoopBody = FS->getBody();
1058      break;
1059    }
1060    case Stmt::ObjCForCollectionStmtClass: {
1061      const auto *FC = cast<ObjCForCollectionStmt>(Term);
1062      LoopBody = FC->getBody();
1063      break;
1064    }
1065    case Stmt::WhileStmtClass:
1066      LoopBody = cast<WhileStmt>(Term)->getBody();
1067      break;
1068    default:
1069      return false;
1070  }
1071  return isContainedByStmt(PM, LoopBody, S);
1072}
1073
1074/// Adds a sanitized control-flow diagnostic edge to a path.
1075static void addEdgeToPath(PathPieces &path,
1076                          PathDiagnosticLocation &PrevLoc,
1077                          PathDiagnosticLocation NewLoc) {
1078  if (!NewLoc.isValid())
1079    return;
1080
1081  SourceLocation NewLocL = NewLoc.asLocation();
1082  if (NewLocL.isInvalid())
1083    return;
1084
1085  if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
1086    PrevLoc = NewLoc;
1087    return;
1088  }
1089
1090  // Ignore self-edges, which occur when there are multiple nodes at the same
1091  // statement.
1092  if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
1093    return;
1094
1095  path.push_front(
1096      std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc));
1097  PrevLoc = NewLoc;
1098}
1099
1100/// A customized wrapper for CFGBlock::getTerminatorCondition()
1101/// which returns the element for ObjCForCollectionStmts.
1102static const Stmt *getTerminatorCondition(const CFGBlock *B) {
1103  const Stmt *S = B->getTerminatorCondition();
1104  if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S))
1105    return FS->getElement();
1106  return S;
1107}
1108
1109constexpr llvm::StringLiteral StrEnteringLoop = "Entering loop body";
1110constexpr llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
1111constexpr llvm::StringLiteral StrLoopRangeEmpty =
1112    "Loop body skipped when range is empty";
1113constexpr llvm::StringLiteral StrLoopCollectionEmpty =
1114    "Loop body skipped when collection is empty";
1115
1116static std::unique_ptr<FilesToLineNumsMap>
1117findExecutedLines(const SourceManager &SM, const ExplodedNode *N);
1118
1119void PathDiagnosticBuilder::generatePathDiagnosticsForNode(
1120    PathDiagnosticConstruct &C, PathDiagnosticLocation &PrevLoc) const {
1121  ProgramPoint P = C.getCurrentNode()->getLocation();
1122  const SourceManager &SM = getSourceManager();
1123
1124  // Have we encountered an entrance to a call?  It may be
1125  // the case that we have not encountered a matching
1126  // call exit before this point.  This means that the path
1127  // terminated within the call itself.
1128  if (auto CE = P.getAs<CallEnter>()) {
1129
1130    if (C.shouldAddPathEdges()) {
1131      // Add an edge to the start of the function.
1132      const StackFrameContext *CalleeLC = CE->getCalleeContext();
1133      const Decl *D = CalleeLC->getDecl();
1134      // Add the edge only when the callee has body. We jump to the beginning
1135      // of the *declaration*, however we expect it to be followed by the
1136      // body. This isn't the case for autosynthesized property accessors in
1137      // Objective-C. No need for a similar extra check for CallExit points
1138      // because the exit edge comes from a statement (i.e. return),
1139      // not from declaration.
1140      if (D->hasBody())
1141        addEdgeToPath(C.getActivePath(), PrevLoc,
1142                      PathDiagnosticLocation::createBegin(D, SM));
1143    }
1144
1145    // Did we visit an entire call?
1146    bool VisitedEntireCall = C.PD->isWithinCall();
1147    C.PD->popActivePath();
1148
1149    PathDiagnosticCallPiece *Call;
1150    if (VisitedEntireCall) {
1151      Call = cast<PathDiagnosticCallPiece>(C.getActivePath().front().get());
1152    } else {
1153      // The path terminated within a nested location context, create a new
1154      // call piece to encapsulate the rest of the path pieces.
1155      const Decl *Caller = CE->getLocationContext()->getDecl();
1156      Call = PathDiagnosticCallPiece::construct(C.getActivePath(), Caller);
1157      assert(C.getActivePath().size() == 1 &&
1158             C.getActivePath().front().get() == Call);
1159
1160      // Since we just transferred the path over to the call piece, reset the
1161      // mapping of the active path to the current location context.
1162      assert(C.isInLocCtxMap(&C.getActivePath()) &&
1163             "When we ascend to a previously unvisited call, the active path's "
1164             "address shouldn't change, but rather should be compacted into "
1165             "a single CallEvent!");
1166      C.updateLocCtxMap(&C.getActivePath(), C.getCurrLocationContext());
1167
1168      // Record the location context mapping for the path within the call.
1169      assert(!C.isInLocCtxMap(&Call->path) &&
1170             "When we ascend to a previously unvisited call, this must be the "
1171             "first time we encounter the caller context!");
1172      C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1173    }
1174    Call->setCallee(*CE, SM);
1175
1176    // Update the previous location in the active path.
1177    PrevLoc = Call->getLocation();
1178
1179    if (!C.CallStack.empty()) {
1180      assert(C.CallStack.back().first == Call);
1181      C.CallStack.pop_back();
1182    }
1183    return;
1184  }
1185
1186  assert(C.getCurrLocationContext() == C.getLocationContextForActivePath() &&
1187         "The current position in the bug path is out of sync with the "
1188         "location context associated with the active path!");
1189
1190  // Have we encountered an exit from a function call?
1191  if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1192
1193    // We are descending into a call (backwards).  Construct
1194    // a new call piece to contain the path pieces for that call.
1195    auto Call = PathDiagnosticCallPiece::construct(*CE, SM);
1196    // Record the mapping from call piece to LocationContext.
1197    assert(!C.isInLocCtxMap(&Call->path) &&
1198           "We just entered a call, this must've been the first time we "
1199           "encounter its context!");
1200    C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1201
1202    if (C.shouldAddPathEdges()) {
1203      // Add the edge to the return site.
1204      addEdgeToPath(C.getActivePath(), PrevLoc, Call->callReturn);
1205      PrevLoc.invalidate();
1206    }
1207
1208    auto *P = Call.get();
1209    C.getActivePath().push_front(std::move(Call));
1210
1211    // Make the contents of the call the active path for now.
1212    C.PD->pushActivePath(&P->path);
1213    C.CallStack.push_back(CallWithEntry(P, C.getCurrentNode()));
1214    return;
1215  }
1216
1217  if (auto PS = P.getAs<PostStmt>()) {
1218    if (!C.shouldAddPathEdges())
1219      return;
1220
1221    // Add an edge.  If this is an ObjCForCollectionStmt do
1222    // not add an edge here as it appears in the CFG both
1223    // as a terminator and as a terminator condition.
1224    if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
1225      PathDiagnosticLocation L =
1226          PathDiagnosticLocation(PS->getStmt(), SM, C.getCurrLocationContext());
1227      addEdgeToPath(C.getActivePath(), PrevLoc, L);
1228    }
1229
1230  } else if (auto BE = P.getAs<BlockEdge>()) {
1231
1232    if (!C.shouldAddPathEdges()) {
1233      generateMinimalDiagForBlockEdge(C, *BE);
1234      return;
1235    }
1236
1237    // Are we jumping to the head of a loop?  Add a special diagnostic.
1238    if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1239      PathDiagnosticLocation L(Loop, SM, C.getCurrLocationContext());
1240      const Stmt *Body = nullptr;
1241
1242      if (const auto *FS = dyn_cast<ForStmt>(Loop))
1243        Body = FS->getBody();
1244      else if (const auto *WS = dyn_cast<WhileStmt>(Loop))
1245        Body = WS->getBody();
1246      else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) {
1247        Body = OFS->getBody();
1248      } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) {
1249        Body = FRS->getBody();
1250      }
1251      // do-while statements are explicitly excluded here
1252
1253      auto p = std::make_shared<PathDiagnosticEventPiece>(
1254          L, "Looping back to the head "
1255          "of the loop");
1256      p->setPrunable(true);
1257
1258      addEdgeToPath(C.getActivePath(), PrevLoc, p->getLocation());
1259      C.getActivePath().push_front(std::move(p));
1260
1261      if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
1262        addEdgeToPath(C.getActivePath(), PrevLoc,
1263                      PathDiagnosticLocation::createEndBrace(CS, SM));
1264      }
1265    }
1266
1267    const CFGBlock *BSrc = BE->getSrc();
1268    const ParentMap &PM = C.getParentMap();
1269
1270    if (const Stmt *Term = BSrc->getTerminatorStmt()) {
1271      // Are we jumping past the loop body without ever executing the
1272      // loop (because the condition was false)?
1273      if (isLoop(Term)) {
1274        const Stmt *TermCond = getTerminatorCondition(BSrc);
1275        bool IsInLoopBody = isInLoopBody(
1276            PM, getStmtBeforeCond(PM, TermCond, C.getCurrentNode()), Term);
1277
1278        StringRef str;
1279
1280        if (isJumpToFalseBranch(&*BE)) {
1281          if (!IsInLoopBody) {
1282            if (isa<ObjCForCollectionStmt>(Term)) {
1283              str = StrLoopCollectionEmpty;
1284            } else if (isa<CXXForRangeStmt>(Term)) {
1285              str = StrLoopRangeEmpty;
1286            } else {
1287              str = StrLoopBodyZero;
1288            }
1289          }
1290        } else {
1291          str = StrEnteringLoop;
1292        }
1293
1294        if (!str.empty()) {
1295          PathDiagnosticLocation L(TermCond ? TermCond : Term, SM,
1296                                   C.getCurrLocationContext());
1297          auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
1298          PE->setPrunable(true);
1299          addEdgeToPath(C.getActivePath(), PrevLoc, PE->getLocation());
1300          C.getActivePath().push_front(std::move(PE));
1301        }
1302      } else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) ||
1303          isa<GotoStmt>(Term)) {
1304        PathDiagnosticLocation L(Term, SM, C.getCurrLocationContext());
1305        addEdgeToPath(C.getActivePath(), PrevLoc, L);
1306      }
1307    }
1308  }
1309}
1310
1311static std::unique_ptr<PathDiagnostic>
1312generateDiagnosticForBasicReport(const BasicBugReport *R) {
1313  const BugType &BT = R->getBugType();
1314  return std::make_unique<PathDiagnostic>(
1315      BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(),
1316      R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1317      BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(),
1318      std::make_unique<FilesToLineNumsMap>());
1319}
1320
1321static std::unique_ptr<PathDiagnostic>
1322generateEmptyDiagnosticForReport(const PathSensitiveBugReport *R,
1323                                 const SourceManager &SM) {
1324  const BugType &BT = R->getBugType();
1325  return std::make_unique<PathDiagnostic>(
1326      BT.getCheckerName(), R->getDeclWithIssue(), BT.getDescription(),
1327      R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1328      BT.getCategory(), R->getUniqueingLocation(), R->getUniqueingDecl(),
1329      findExecutedLines(SM, R->getErrorNode()));
1330}
1331
1332static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
1333  if (!S)
1334    return nullptr;
1335
1336  while (true) {
1337    S = PM.getParentIgnoreParens(S);
1338
1339    if (!S)
1340      break;
1341
1342    if (isa<FullExpr>(S) ||
1343        isa<CXXBindTemporaryExpr>(S) ||
1344        isa<SubstNonTypeTemplateParmExpr>(S))
1345      continue;
1346
1347    break;
1348  }
1349
1350  return S;
1351}
1352
1353static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
1354  switch (S->getStmtClass()) {
1355    case Stmt::BinaryOperatorClass: {
1356      const auto *BO = cast<BinaryOperator>(S);
1357      if (!BO->isLogicalOp())
1358        return false;
1359      return BO->getLHS() == Cond || BO->getRHS() == Cond;
1360    }
1361    case Stmt::IfStmtClass:
1362      return cast<IfStmt>(S)->getCond() == Cond;
1363    case Stmt::ForStmtClass:
1364      return cast<ForStmt>(S)->getCond() == Cond;
1365    case Stmt::WhileStmtClass:
1366      return cast<WhileStmt>(S)->getCond() == Cond;
1367    case Stmt::DoStmtClass:
1368      return cast<DoStmt>(S)->getCond() == Cond;
1369    case Stmt::ChooseExprClass:
1370      return cast<ChooseExpr>(S)->getCond() == Cond;
1371    case Stmt::IndirectGotoStmtClass:
1372      return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
1373    case Stmt::SwitchStmtClass:
1374      return cast<SwitchStmt>(S)->getCond() == Cond;
1375    case Stmt::BinaryConditionalOperatorClass:
1376      return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
1377    case Stmt::ConditionalOperatorClass: {
1378      const auto *CO = cast<ConditionalOperator>(S);
1379      return CO->getCond() == Cond ||
1380             CO->getLHS() == Cond ||
1381             CO->getRHS() == Cond;
1382    }
1383    case Stmt::ObjCForCollectionStmtClass:
1384      return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
1385    case Stmt::CXXForRangeStmtClass: {
1386      const auto *FRS = cast<CXXForRangeStmt>(S);
1387      return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
1388    }
1389    default:
1390      return false;
1391  }
1392}
1393
1394static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1395  if (const auto *FS = dyn_cast<ForStmt>(FL))
1396    return FS->getInc() == S || FS->getInit() == S;
1397  if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL))
1398    return FRS->getInc() == S || FRS->getRangeStmt() == S ||
1399           FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
1400  return false;
1401}
1402
1403using OptimizedCallsSet = llvm::DenseSet<const PathDiagnosticCallPiece *>;
1404
1405/// Adds synthetic edges from top-level statements to their subexpressions.
1406///
1407/// This avoids a "swoosh" effect, where an edge from a top-level statement A
1408/// points to a sub-expression B.1 that's not at the start of B. In these cases,
1409/// we'd like to see an edge from A to B, then another one from B to B.1.
1410static void addContextEdges(PathPieces &pieces, const LocationContext *LC) {
1411  const ParentMap &PM = LC->getParentMap();
1412  PathPieces::iterator Prev = pieces.end();
1413  for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
1414       Prev = I, ++I) {
1415    auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1416
1417    if (!Piece)
1418      continue;
1419
1420    PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
1421    SmallVector<PathDiagnosticLocation, 4> SrcContexts;
1422
1423    PathDiagnosticLocation NextSrcContext = SrcLoc;
1424    const Stmt *InnerStmt = nullptr;
1425    while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
1426      SrcContexts.push_back(NextSrcContext);
1427      InnerStmt = NextSrcContext.asStmt();
1428      NextSrcContext = getEnclosingStmtLocation(InnerStmt, LC,
1429                                                /*allowNested=*/true);
1430    }
1431
1432    // Repeatedly split the edge as necessary.
1433    // This is important for nested logical expressions (||, &&, ?:) where we
1434    // want to show all the levels of context.
1435    while (true) {
1436      const Stmt *Dst = Piece->getEndLocation().getStmtOrNull();
1437
1438      // We are looking at an edge. Is the destination within a larger
1439      // expression?
1440      PathDiagnosticLocation DstContext =
1441          getEnclosingStmtLocation(Dst, LC, /*allowNested=*/true);
1442      if (!DstContext.isValid() || DstContext.asStmt() == Dst)
1443        break;
1444
1445      // If the source is in the same context, we're already good.
1446      if (llvm::find(SrcContexts, DstContext) != SrcContexts.end())
1447        break;
1448
1449      // Update the subexpression node to point to the context edge.
1450      Piece->setStartLocation(DstContext);
1451
1452      // Try to extend the previous edge if it's at the same level as the source
1453      // context.
1454      if (Prev != E) {
1455        auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get());
1456
1457        if (PrevPiece) {
1458          if (const Stmt *PrevSrc =
1459                  PrevPiece->getStartLocation().getStmtOrNull()) {
1460            const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
1461            if (PrevSrcParent ==
1462                getStmtParent(DstContext.getStmtOrNull(), PM)) {
1463              PrevPiece->setEndLocation(DstContext);
1464              break;
1465            }
1466          }
1467        }
1468      }
1469
1470      // Otherwise, split the current edge into a context edge and a
1471      // subexpression edge. Note that the context statement may itself have
1472      // context.
1473      auto P =
1474          std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext);
1475      Piece = P.get();
1476      I = pieces.insert(I, std::move(P));
1477    }
1478  }
1479}
1480
1481/// Move edges from a branch condition to a branch target
1482///        when the condition is simple.
1483///
1484/// This restructures some of the work of addContextEdges.  That function
1485/// creates edges this may destroy, but they work together to create a more
1486/// aesthetically set of edges around branches.  After the call to
1487/// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
1488/// the branch to the branch condition, and (3) an edge from the branch
1489/// condition to the branch target.  We keep (1), but may wish to remove (2)
1490/// and move the source of (3) to the branch if the branch condition is simple.
1491static void simplifySimpleBranches(PathPieces &pieces) {
1492  for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
1493    const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1494
1495    if (!PieceI)
1496      continue;
1497
1498    const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1499    const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1500
1501    if (!s1Start || !s1End)
1502      continue;
1503
1504    PathPieces::iterator NextI = I; ++NextI;
1505    if (NextI == E)
1506      break;
1507
1508    PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
1509
1510    while (true) {
1511      if (NextI == E)
1512        break;
1513
1514      const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1515      if (EV) {
1516        StringRef S = EV->getString();
1517        if (S == StrEnteringLoop || S == StrLoopBodyZero ||
1518            S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) {
1519          ++NextI;
1520          continue;
1521        }
1522        break;
1523      }
1524
1525      PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1526      break;
1527    }
1528
1529    if (!PieceNextI)
1530      continue;
1531
1532    const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1533    const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1534
1535    if (!s2Start || !s2End || s1End != s2Start)
1536      continue;
1537
1538    // We only perform this transformation for specific branch kinds.
1539    // We don't want to do this for do..while, for example.
1540    if (!(isa<ForStmt>(s1Start) || isa<WhileStmt>(s1Start) ||
1541          isa<IfStmt>(s1Start) || isa<ObjCForCollectionStmt>(s1Start) ||
1542          isa<CXXForRangeStmt>(s1Start)))
1543      continue;
1544
1545    // Is s1End the branch condition?
1546    if (!isConditionForTerminator(s1Start, s1End))
1547      continue;
1548
1549    // Perform the hoisting by eliminating (2) and changing the start
1550    // location of (3).
1551    PieceNextI->setStartLocation(PieceI->getStartLocation());
1552    I = pieces.erase(I);
1553  }
1554}
1555
1556/// Returns the number of bytes in the given (character-based) SourceRange.
1557///
1558/// If the locations in the range are not on the same line, returns None.
1559///
1560/// Note that this does not do a precise user-visible character or column count.
1561static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1562                                              SourceRange Range) {
1563  SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
1564                             SM.getExpansionRange(Range.getEnd()).getEnd());
1565
1566  FileID FID = SM.getFileID(ExpansionRange.getBegin());
1567  if (FID != SM.getFileID(ExpansionRange.getEnd()))
1568    return None;
1569
1570  bool Invalid;
1571  const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid);
1572  if (Invalid)
1573    return None;
1574
1575  unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
1576  unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
1577  StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
1578
1579  // We're searching the raw bytes of the buffer here, which might include
1580  // escaped newlines and such. That's okay; we're trying to decide whether the
1581  // SourceRange is covering a large or small amount of space in the user's
1582  // editor.
1583  if (Snippet.find_first_of("\r\n") != StringRef::npos)
1584    return None;
1585
1586  // This isn't Unicode-aware, but it doesn't need to be.
1587  return Snippet.size();
1588}
1589
1590/// \sa getLengthOnSingleLine(SourceManager, SourceRange)
1591static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1592                                              const Stmt *S) {
1593  return getLengthOnSingleLine(SM, S->getSourceRange());
1594}
1595
1596/// Eliminate two-edge cycles created by addContextEdges().
1597///
1598/// Once all the context edges are in place, there are plenty of cases where
1599/// there's a single edge from a top-level statement to a subexpression,
1600/// followed by a single path note, and then a reverse edge to get back out to
1601/// the top level. If the statement is simple enough, the subexpression edges
1602/// just add noise and make it harder to understand what's going on.
1603///
1604/// This function only removes edges in pairs, because removing only one edge
1605/// might leave other edges dangling.
1606///
1607/// This will not remove edges in more complicated situations:
1608/// - if there is more than one "hop" leading to or from a subexpression.
1609/// - if there is an inlined call between the edges instead of a single event.
1610/// - if the whole statement is large enough that having subexpression arrows
1611///   might be helpful.
1612static void removeContextCycles(PathPieces &Path, const SourceManager &SM) {
1613  for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
1614    // Pattern match the current piece and its successor.
1615    const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1616
1617    if (!PieceI) {
1618      ++I;
1619      continue;
1620    }
1621
1622    const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1623    const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1624
1625    PathPieces::iterator NextI = I; ++NextI;
1626    if (NextI == E)
1627      break;
1628
1629    const auto *PieceNextI =
1630        dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1631
1632    if (!PieceNextI) {
1633      if (isa<PathDiagnosticEventPiece>(NextI->get())) {
1634        ++NextI;
1635        if (NextI == E)
1636          break;
1637        PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1638      }
1639
1640      if (!PieceNextI) {
1641        ++I;
1642        continue;
1643      }
1644    }
1645
1646    const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1647    const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1648
1649    if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
1650      const size_t MAX_SHORT_LINE_LENGTH = 80;
1651      Optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
1652      if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
1653        Optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
1654        if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
1655          Path.erase(I);
1656          I = Path.erase(NextI);
1657          continue;
1658        }
1659      }
1660    }
1661
1662    ++I;
1663  }
1664}
1665
1666/// Return true if X is contained by Y.
1667static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) {
1668  while (X) {
1669    if (X == Y)
1670      return true;
1671    X = PM.getParent(X);
1672  }
1673  return false;
1674}
1675
1676// Remove short edges on the same line less than 3 columns in difference.
1677static void removePunyEdges(PathPieces &path, const SourceManager &SM,
1678                            const ParentMap &PM) {
1679  bool erased = false;
1680
1681  for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
1682       erased ? I : ++I) {
1683    erased = false;
1684
1685    const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1686
1687    if (!PieceI)
1688      continue;
1689
1690    const Stmt *start = PieceI->getStartLocation().getStmtOrNull();
1691    const Stmt *end   = PieceI->getEndLocation().getStmtOrNull();
1692
1693    if (!start || !end)
1694      continue;
1695
1696    const Stmt *endParent = PM.getParent(end);
1697    if (!endParent)
1698      continue;
1699
1700    if (isConditionForTerminator(end, endParent))
1701      continue;
1702
1703    SourceLocation FirstLoc = start->getBeginLoc();
1704    SourceLocation SecondLoc = end->getBeginLoc();
1705
1706    if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
1707      continue;
1708    if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
1709      std::swap(SecondLoc, FirstLoc);
1710
1711    SourceRange EdgeRange(FirstLoc, SecondLoc);
1712    Optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
1713
1714    // If the statements are on different lines, continue.
1715    if (!ByteWidth)
1716      continue;
1717
1718    const size_t MAX_PUNY_EDGE_LENGTH = 2;
1719    if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
1720      // FIXME: There are enough /bytes/ between the endpoints of the edge, but
1721      // there might not be enough /columns/. A proper user-visible column count
1722      // is probably too expensive, though.
1723      I = path.erase(I);
1724      erased = true;
1725      continue;
1726    }
1727  }
1728}
1729
1730static void removeIdenticalEvents(PathPieces &path) {
1731  for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
1732    const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get());
1733
1734    if (!PieceI)
1735      continue;
1736
1737    PathPieces::iterator NextI = I; ++NextI;
1738    if (NextI == E)
1739      return;
1740
1741    const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1742
1743    if (!PieceNextI)
1744      continue;
1745
1746    // Erase the second piece if it has the same exact message text.
1747    if (PieceI->getString() == PieceNextI->getString()) {
1748      path.erase(NextI);
1749    }
1750  }
1751}
1752
1753static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path,
1754                          OptimizedCallsSet &OCS) {
1755  bool hasChanges = false;
1756  const LocationContext *LC = C.getLocationContextFor(&path);
1757  assert(LC);
1758  const ParentMap &PM = LC->getParentMap();
1759  const SourceManager &SM = C.getSourceManager();
1760
1761  for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
1762    // Optimize subpaths.
1763    if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) {
1764      // Record the fact that a call has been optimized so we only do the
1765      // effort once.
1766      if (!OCS.count(CallI)) {
1767        while (optimizeEdges(C, CallI->path, OCS)) {
1768        }
1769        OCS.insert(CallI);
1770      }
1771      ++I;
1772      continue;
1773    }
1774
1775    // Pattern match the current piece and its successor.
1776    auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1777
1778    if (!PieceI) {
1779      ++I;
1780      continue;
1781    }
1782
1783    const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1784    const Stmt *s1End   = PieceI->getEndLocation().getStmtOrNull();
1785    const Stmt *level1 = getStmtParent(s1Start, PM);
1786    const Stmt *level2 = getStmtParent(s1End, PM);
1787
1788    PathPieces::iterator NextI = I; ++NextI;
1789    if (NextI == E)
1790      break;
1791
1792    const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1793
1794    if (!PieceNextI) {
1795      ++I;
1796      continue;
1797    }
1798
1799    const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1800    const Stmt *s2End   = PieceNextI->getEndLocation().getStmtOrNull();
1801    const Stmt *level3 = getStmtParent(s2Start, PM);
1802    const Stmt *level4 = getStmtParent(s2End, PM);
1803
1804    // Rule I.
1805    //
1806    // If we have two consecutive control edges whose end/begin locations
1807    // are at the same level (e.g. statements or top-level expressions within
1808    // a compound statement, or siblings share a single ancestor expression),
1809    // then merge them if they have no interesting intermediate event.
1810    //
1811    // For example:
1812    //
1813    // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
1814    // parent is '1'.  Here 'x.y.z' represents the hierarchy of statements.
1815    //
1816    // NOTE: this will be limited later in cases where we add barriers
1817    // to prevent this optimization.
1818    if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
1819      PieceI->setEndLocation(PieceNextI->getEndLocation());
1820      path.erase(NextI);
1821      hasChanges = true;
1822      continue;
1823    }
1824
1825    // Rule II.
1826    //
1827    // Eliminate edges between subexpressions and parent expressions
1828    // when the subexpression is consumed.
1829    //
1830    // NOTE: this will be limited later in cases where we add barriers
1831    // to prevent this optimization.
1832    if (s1End && s1End == s2Start && level2) {
1833      bool removeEdge = false;
1834      // Remove edges into the increment or initialization of a
1835      // loop that have no interleaving event.  This means that
1836      // they aren't interesting.
1837      if (isIncrementOrInitInForLoop(s1End, level2))
1838        removeEdge = true;
1839      // Next only consider edges that are not anchored on
1840      // the condition of a terminator.  This are intermediate edges
1841      // that we might want to trim.
1842      else if (!isConditionForTerminator(level2, s1End)) {
1843        // Trim edges on expressions that are consumed by
1844        // the parent expression.
1845        if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
1846          removeEdge = true;
1847        }
1848        // Trim edges where a lexical containment doesn't exist.
1849        // For example:
1850        //
1851        //  X -> Y -> Z
1852        //
1853        // If 'Z' lexically contains Y (it is an ancestor) and
1854        // 'X' does not lexically contain Y (it is a descendant OR
1855        // it has no lexical relationship at all) then trim.
1856        //
1857        // This can eliminate edges where we dive into a subexpression
1858        // and then pop back out, etc.
1859        else if (s1Start && s2End &&
1860                 lexicalContains(PM, s2Start, s2End) &&
1861                 !lexicalContains(PM, s1End, s1Start)) {
1862          removeEdge = true;
1863        }
1864        // Trim edges from a subexpression back to the top level if the
1865        // subexpression is on a different line.
1866        //
1867        // A.1 -> A -> B
1868        // becomes
1869        // A.1 -> B
1870        //
1871        // These edges just look ugly and don't usually add anything.
1872        else if (s1Start && s2End &&
1873                 lexicalContains(PM, s1Start, s1End)) {
1874          SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
1875                                PieceI->getStartLocation().asLocation());
1876          if (!getLengthOnSingleLine(SM, EdgeRange).hasValue())
1877            removeEdge = true;
1878        }
1879      }
1880
1881      if (removeEdge) {
1882        PieceI->setEndLocation(PieceNextI->getEndLocation());
1883        path.erase(NextI);
1884        hasChanges = true;
1885        continue;
1886      }
1887    }
1888
1889    // Optimize edges for ObjC fast-enumeration loops.
1890    //
1891    // (X -> collection) -> (collection -> element)
1892    //
1893    // becomes:
1894    //
1895    // (X -> element)
1896    if (s1End == s2Start) {
1897      const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3);
1898      if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
1899          s2End == FS->getElement()) {
1900        PieceI->setEndLocation(PieceNextI->getEndLocation());
1901        path.erase(NextI);
1902        hasChanges = true;
1903        continue;
1904      }
1905    }
1906
1907    // No changes at this index?  Move to the next one.
1908    ++I;
1909  }
1910
1911  if (!hasChanges) {
1912    // Adjust edges into subexpressions to make them more uniform
1913    // and aesthetically pleasing.
1914    addContextEdges(path, LC);
1915    // Remove "cyclical" edges that include one or more context edges.
1916    removeContextCycles(path, SM);
1917    // Hoist edges originating from branch conditions to branches
1918    // for simple branches.
1919    simplifySimpleBranches(path);
1920    // Remove any puny edges left over after primary optimization pass.
1921    removePunyEdges(path, SM, PM);
1922    // Remove identical events.
1923    removeIdenticalEvents(path);
1924  }
1925
1926  return hasChanges;
1927}
1928
1929/// Drop the very first edge in a path, which should be a function entry edge.
1930///
1931/// If the first edge is not a function entry edge (say, because the first
1932/// statement had an invalid source location), this function does nothing.
1933// FIXME: We should just generate invalid edges anyway and have the optimizer
1934// deal with them.
1935static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C,
1936                                  PathPieces &Path) {
1937  const auto *FirstEdge =
1938      dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
1939  if (!FirstEdge)
1940    return;
1941
1942  const Decl *D = C.getLocationContextFor(&Path)->getDecl();
1943  PathDiagnosticLocation EntryLoc =
1944      PathDiagnosticLocation::createBegin(D, C.getSourceManager());
1945  if (FirstEdge->getStartLocation() != EntryLoc)
1946    return;
1947
1948  Path.pop_front();
1949}
1950
1951/// Populate executes lines with lines containing at least one diagnostics.
1952static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD) {
1953
1954  PathPieces path = PD.path.flatten(/*ShouldFlattenMacros=*/true);
1955  FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines();
1956
1957  for (const auto &P : path) {
1958    FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc();
1959    FileID FID = Loc.getFileID();
1960    unsigned LineNo = Loc.getLineNumber();
1961    assert(FID.isValid());
1962    ExecutedLines[FID].insert(LineNo);
1963  }
1964}
1965
1966PathDiagnosticConstruct::PathDiagnosticConstruct(
1967    const PathDiagnosticConsumer *PDC, const ExplodedNode *ErrorNode,
1968    const PathSensitiveBugReport *R)
1969    : Consumer(PDC), CurrentNode(ErrorNode),
1970      SM(CurrentNode->getCodeDecl().getASTContext().getSourceManager()),
1971      PD(generateEmptyDiagnosticForReport(R, getSourceManager())) {
1972  LCM[&PD->getActivePath()] = ErrorNode->getLocationContext();
1973}
1974
1975PathDiagnosticBuilder::PathDiagnosticBuilder(
1976    BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
1977    PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
1978    std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics)
1979    : BugReporterContext(BRC), BugPath(std::move(BugPath)), R(r),
1980      ErrorNode(ErrorNode),
1981      VisitorsDiagnostics(std::move(VisitorsDiagnostics)) {}
1982
1983std::unique_ptr<PathDiagnostic>
1984PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
1985  PathDiagnosticConstruct Construct(PDC, ErrorNode, R);
1986
1987  const SourceManager &SM = getSourceManager();
1988  const AnalyzerOptions &Opts = getAnalyzerOptions();
1989  StringRef ErrorTag = ErrorNode->getLocation().getTag()->getTagDescription();
1990
1991  // See whether we need to silence the checker/package.
1992  // FIXME: This will not work if the report was emitted with an incorrect tag.
1993  for (const std::string &CheckerOrPackage : Opts.SilencedCheckersAndPackages) {
1994    if (ErrorTag.startswith(CheckerOrPackage))
1995      return nullptr;
1996  }
1997
1998  if (!PDC->shouldGenerateDiagnostics())
1999    return generateEmptyDiagnosticForReport(R, getSourceManager());
2000
2001  // Construct the final (warning) event for the bug report.
2002  auto EndNotes = VisitorsDiagnostics->find(ErrorNode);
2003  PathDiagnosticPieceRef LastPiece;
2004  if (EndNotes != VisitorsDiagnostics->end()) {
2005    assert(!EndNotes->second.empty());
2006    LastPiece = EndNotes->second[0];
2007  } else {
2008    LastPiece = BugReporterVisitor::getDefaultEndPath(*this, ErrorNode,
2009                                                      *getBugReport());
2010  }
2011  Construct.PD->setEndOfPath(LastPiece);
2012
2013  PathDiagnosticLocation PrevLoc = Construct.PD->getLocation();
2014  // From the error node to the root, ascend the bug path and construct the bug
2015  // report.
2016  while (Construct.ascendToPrevNode()) {
2017    generatePathDiagnosticsForNode(Construct, PrevLoc);
2018
2019    auto VisitorNotes = VisitorsDiagnostics->find(Construct.getCurrentNode());
2020    if (VisitorNotes == VisitorsDiagnostics->end())
2021      continue;
2022
2023    // This is a workaround due to inability to put shared PathDiagnosticPiece
2024    // into a FoldingSet.
2025    std::set<llvm::FoldingSetNodeID> DeduplicationSet;
2026
2027    // Add pieces from custom visitors.
2028    for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) {
2029      llvm::FoldingSetNodeID ID;
2030      Note->Profile(ID);
2031      if (!DeduplicationSet.insert(ID).second)
2032        continue;
2033
2034      if (PDC->shouldAddPathEdges())
2035        addEdgeToPath(Construct.getActivePath(), PrevLoc, Note->getLocation());
2036      updateStackPiecesWithMessage(Note, Construct.CallStack);
2037      Construct.getActivePath().push_front(Note);
2038    }
2039  }
2040
2041  if (PDC->shouldAddPathEdges()) {
2042    // Add an edge to the start of the function.
2043    // We'll prune it out later, but it helps make diagnostics more uniform.
2044    const StackFrameContext *CalleeLC =
2045        Construct.getLocationContextForActivePath()->getStackFrame();
2046    const Decl *D = CalleeLC->getDecl();
2047    addEdgeToPath(Construct.getActivePath(), PrevLoc,
2048                  PathDiagnosticLocation::createBegin(D, SM));
2049  }
2050
2051
2052  // Finally, prune the diagnostic path of uninteresting stuff.
2053  if (!Construct.PD->path.empty()) {
2054    if (R->shouldPrunePath() && Opts.ShouldPrunePaths) {
2055      bool stillHasNotes =
2056          removeUnneededCalls(Construct, Construct.getMutablePieces(), R);
2057      assert(stillHasNotes);
2058      (void)stillHasNotes;
2059    }
2060
2061    // Remove pop-up notes if needed.
2062    if (!Opts.ShouldAddPopUpNotes)
2063      removePopUpNotes(Construct.getMutablePieces());
2064
2065    // Redirect all call pieces to have valid locations.
2066    adjustCallLocations(Construct.getMutablePieces());
2067    removePiecesWithInvalidLocations(Construct.getMutablePieces());
2068
2069    if (PDC->shouldAddPathEdges()) {
2070
2071      // Reduce the number of edges from a very conservative set
2072      // to an aesthetically pleasing subset that conveys the
2073      // necessary information.
2074      OptimizedCallsSet OCS;
2075      while (optimizeEdges(Construct, Construct.getMutablePieces(), OCS)) {
2076      }
2077
2078      // Drop the very first function-entry edge. It's not really necessary
2079      // for top-level functions.
2080      dropFunctionEntryEdge(Construct, Construct.getMutablePieces());
2081    }
2082
2083    // Remove messages that are basically the same, and edges that may not
2084    // make sense.
2085    // We have to do this after edge optimization in the Extensive mode.
2086    removeRedundantMsgs(Construct.getMutablePieces());
2087    removeEdgesToDefaultInitializers(Construct.getMutablePieces());
2088  }
2089
2090  if (Opts.ShouldDisplayMacroExpansions)
2091    CompactMacroExpandedPieces(Construct.getMutablePieces(), SM);
2092
2093  return std::move(Construct.PD);
2094}
2095
2096//===----------------------------------------------------------------------===//
2097// Methods for BugType and subclasses.
2098//===----------------------------------------------------------------------===//
2099
2100void BugType::anchor() {}
2101
2102void BuiltinBug::anchor() {}
2103
2104//===----------------------------------------------------------------------===//
2105// Methods for BugReport and subclasses.
2106//===----------------------------------------------------------------------===//
2107
2108void PathSensitiveBugReport::addVisitor(
2109    std::unique_ptr<BugReporterVisitor> visitor) {
2110  if (!visitor)
2111    return;
2112
2113  llvm::FoldingSetNodeID ID;
2114  visitor->Profile(ID);
2115
2116  void *InsertPos = nullptr;
2117  if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
2118    return;
2119  }
2120
2121  Callbacks.push_back(std::move(visitor));
2122}
2123
2124void PathSensitiveBugReport::clearVisitors() {
2125  Callbacks.clear();
2126}
2127
2128const Decl *PathSensitiveBugReport::getDeclWithIssue() const {
2129  const ExplodedNode *N = getErrorNode();
2130  if (!N)
2131    return nullptr;
2132
2133  const LocationContext *LC = N->getLocationContext();
2134  return LC->getStackFrame()->getDecl();
2135}
2136
2137void BasicBugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2138  hash.AddInteger(static_cast<int>(getKind()));
2139  hash.AddPointer(&BT);
2140  hash.AddString(Description);
2141  assert(Location.isValid());
2142  Location.Profile(hash);
2143
2144  for (SourceRange range : Ranges) {
2145    if (!range.isValid())
2146      continue;
2147    hash.AddInteger(range.getBegin().getRawEncoding());
2148    hash.AddInteger(range.getEnd().getRawEncoding());
2149  }
2150}
2151
2152void PathSensitiveBugReport::Profile(llvm::FoldingSetNodeID &hash) const {
2153  hash.AddInteger(static_cast<int>(getKind()));
2154  hash.AddPointer(&BT);
2155  hash.AddString(Description);
2156  PathDiagnosticLocation UL = getUniqueingLocation();
2157  if (UL.isValid()) {
2158    UL.Profile(hash);
2159  } else {
2160    // TODO: The statement may be null if the report was emitted before any
2161    // statements were executed. In particular, some checkers by design
2162    // occasionally emit their reports in empty functions (that have no
2163    // statements in their body). Do we profile correctly in this case?
2164    hash.AddPointer(ErrorNode->getCurrentOrPreviousStmtForDiagnostics());
2165  }
2166
2167  for (SourceRange range : Ranges) {
2168    if (!range.isValid())
2169      continue;
2170    hash.AddInteger(range.getBegin().getRawEncoding());
2171    hash.AddInteger(range.getEnd().getRawEncoding());
2172  }
2173}
2174
2175template <class T>
2176static void insertToInterestingnessMap(
2177    llvm::DenseMap<T, bugreporter::TrackingKind> &InterestingnessMap, T Val,
2178    bugreporter::TrackingKind TKind) {
2179  auto Result = InterestingnessMap.insert({Val, TKind});
2180
2181  if (Result.second)
2182    return;
2183
2184  // Even if this symbol/region was already marked as interesting as a
2185  // condition, if we later mark it as interesting again but with
2186  // thorough tracking, overwrite it. Entities marked with thorough
2187  // interestiness are the most important (or most interesting, if you will),
2188  // and we wouldn't like to downplay their importance.
2189
2190  switch (TKind) {
2191    case bugreporter::TrackingKind::Thorough:
2192      Result.first->getSecond() = bugreporter::TrackingKind::Thorough;
2193      return;
2194    case bugreporter::TrackingKind::Condition:
2195      return;
2196  }
2197
2198  llvm_unreachable(
2199      "BugReport::markInteresting currently can only handle 2 different "
2200      "tracking kinds! Please define what tracking kind should this entitiy"
2201      "have, if it was already marked as interesting with a different kind!");
2202}
2203
2204void PathSensitiveBugReport::markInteresting(SymbolRef sym,
2205                                             bugreporter::TrackingKind TKind) {
2206  if (!sym)
2207    return;
2208
2209  insertToInterestingnessMap(InterestingSymbols, sym, TKind);
2210
2211  if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
2212    markInteresting(meta->getRegion(), TKind);
2213}
2214
2215void PathSensitiveBugReport::markInteresting(const MemRegion *R,
2216                                             bugreporter::TrackingKind TKind) {
2217  if (!R)
2218    return;
2219
2220  R = R->getBaseRegion();
2221  insertToInterestingnessMap(InterestingRegions, R, TKind);
2222
2223  if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2224    markInteresting(SR->getSymbol(), TKind);
2225}
2226
2227void PathSensitiveBugReport::markInteresting(SVal V,
2228                                             bugreporter::TrackingKind TKind) {
2229  markInteresting(V.getAsRegion(), TKind);
2230  markInteresting(V.getAsSymbol(), TKind);
2231}
2232
2233void PathSensitiveBugReport::markInteresting(const LocationContext *LC) {
2234  if (!LC)
2235    return;
2236  InterestingLocationContexts.insert(LC);
2237}
2238
2239Optional<bugreporter::TrackingKind>
2240PathSensitiveBugReport::getInterestingnessKind(SVal V) const {
2241  auto RKind = getInterestingnessKind(V.getAsRegion());
2242  auto SKind = getInterestingnessKind(V.getAsSymbol());
2243  if (!RKind)
2244    return SKind;
2245  if (!SKind)
2246    return RKind;
2247
2248  // If either is marked with throrough tracking, return that, we wouldn't like
2249  // to downplay a note's importance by 'only' mentioning it as a condition.
2250  switch(*RKind) {
2251    case bugreporter::TrackingKind::Thorough:
2252      return RKind;
2253    case bugreporter::TrackingKind::Condition:
2254      return SKind;
2255  }
2256
2257  llvm_unreachable(
2258      "BugReport::getInterestingnessKind currently can only handle 2 different "
2259      "tracking kinds! Please define what tracking kind should we return here "
2260      "when the kind of getAsRegion() and getAsSymbol() is different!");
2261  return None;
2262}
2263
2264Optional<bugreporter::TrackingKind>
2265PathSensitiveBugReport::getInterestingnessKind(SymbolRef sym) const {
2266  if (!sym)
2267    return None;
2268  // We don't currently consider metadata symbols to be interesting
2269  // even if we know their region is interesting. Is that correct behavior?
2270  auto It = InterestingSymbols.find(sym);
2271  if (It == InterestingSymbols.end())
2272    return None;
2273  return It->getSecond();
2274}
2275
2276Optional<bugreporter::TrackingKind>
2277PathSensitiveBugReport::getInterestingnessKind(const MemRegion *R) const {
2278  if (!R)
2279    return None;
2280
2281  R = R->getBaseRegion();
2282  auto It = InterestingRegions.find(R);
2283  if (It != InterestingRegions.end())
2284    return It->getSecond();
2285
2286  if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2287    return getInterestingnessKind(SR->getSymbol());
2288  return None;
2289}
2290
2291bool PathSensitiveBugReport::isInteresting(SVal V) const {
2292  return getInterestingnessKind(V).hasValue();
2293}
2294
2295bool PathSensitiveBugReport::isInteresting(SymbolRef sym) const {
2296  return getInterestingnessKind(sym).hasValue();
2297}
2298
2299bool PathSensitiveBugReport::isInteresting(const MemRegion *R) const {
2300  return getInterestingnessKind(R).hasValue();
2301}
2302
2303bool PathSensitiveBugReport::isInteresting(const LocationContext *LC)  const {
2304  if (!LC)
2305    return false;
2306  return InterestingLocationContexts.count(LC);
2307}
2308
2309const Stmt *PathSensitiveBugReport::getStmt() const {
2310  if (!ErrorNode)
2311    return nullptr;
2312
2313  ProgramPoint ProgP = ErrorNode->getLocation();
2314  const Stmt *S = nullptr;
2315
2316  if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2317    CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
2318    if (BE->getBlock() == &Exit)
2319      S = ErrorNode->getPreviousStmtForDiagnostics();
2320  }
2321  if (!S)
2322    S = ErrorNode->getStmtForDiagnostics();
2323
2324  return S;
2325}
2326
2327ArrayRef<SourceRange>
2328PathSensitiveBugReport::getRanges() const {
2329  // If no custom ranges, add the range of the statement corresponding to
2330  // the error node.
2331  if (Ranges.empty() && isa_and_nonnull<Expr>(getStmt()))
2332      return ErrorNodeRange;
2333
2334  return Ranges;
2335}
2336
2337PathDiagnosticLocation
2338PathSensitiveBugReport::getLocation() const {
2339  assert(ErrorNode && "Cannot create a location with a null node.");
2340  const Stmt *S = ErrorNode->getStmtForDiagnostics();
2341    ProgramPoint P = ErrorNode->getLocation();
2342  const LocationContext *LC = P.getLocationContext();
2343  SourceManager &SM =
2344      ErrorNode->getState()->getStateManager().getContext().getSourceManager();
2345
2346  if (!S) {
2347    // If this is an implicit call, return the implicit call point location.
2348    if (Optional<PreImplicitCall> PIE = P.getAs<PreImplicitCall>())
2349      return PathDiagnosticLocation(PIE->getLocation(), SM);
2350    if (auto FE = P.getAs<FunctionExitPoint>()) {
2351      if (const ReturnStmt *RS = FE->getStmt())
2352        return PathDiagnosticLocation::createBegin(RS, SM, LC);
2353    }
2354    S = ErrorNode->getNextStmtForDiagnostics();
2355  }
2356
2357  if (S) {
2358    // For member expressions, return the location of the '.' or '->'.
2359    if (const auto *ME = dyn_cast<MemberExpr>(S))
2360      return PathDiagnosticLocation::createMemberLoc(ME, SM);
2361
2362    // For binary operators, return the location of the operator.
2363    if (const auto *B = dyn_cast<BinaryOperator>(S))
2364      return PathDiagnosticLocation::createOperatorLoc(B, SM);
2365
2366    if (P.getAs<PostStmtPurgeDeadSymbols>())
2367      return PathDiagnosticLocation::createEnd(S, SM, LC);
2368
2369    if (S->getBeginLoc().isValid())
2370      return PathDiagnosticLocation(S, SM, LC);
2371
2372    return PathDiagnosticLocation(
2373        PathDiagnosticLocation::getValidSourceLocation(S, LC), SM);
2374  }
2375
2376  return PathDiagnosticLocation::createDeclEnd(ErrorNode->getLocationContext(),
2377                                               SM);
2378}
2379
2380//===----------------------------------------------------------------------===//
2381// Methods for BugReporter and subclasses.
2382//===----------------------------------------------------------------------===//
2383
2384const ExplodedGraph &PathSensitiveBugReporter::getGraph() const {
2385  return Eng.getGraph();
2386}
2387
2388ProgramStateManager &PathSensitiveBugReporter::getStateManager() const {
2389  return Eng.getStateManager();
2390}
2391
2392BugReporter::~BugReporter() {
2393  // Make sure reports are flushed.
2394  assert(StrBugTypes.empty() &&
2395         "Destroying BugReporter before diagnostics are emitted!");
2396
2397  // Free the bug reports we are tracking.
2398  for (const auto I : EQClassesVector)
2399    delete I;
2400}
2401
2402void BugReporter::FlushReports() {
2403  // We need to flush reports in deterministic order to ensure the order
2404  // of the reports is consistent between runs.
2405  for (const auto EQ : EQClassesVector)
2406    FlushReport(*EQ);
2407
2408  // BugReporter owns and deletes only BugTypes created implicitly through
2409  // EmitBasicReport.
2410  // FIXME: There are leaks from checkers that assume that the BugTypes they
2411  // create will be destroyed by the BugReporter.
2412  llvm::DeleteContainerSeconds(StrBugTypes);
2413}
2414
2415//===----------------------------------------------------------------------===//
2416// PathDiagnostics generation.
2417//===----------------------------------------------------------------------===//
2418
2419namespace {
2420
2421/// A wrapper around an ExplodedGraph that contains a single path from the root
2422/// to the error node.
2423class BugPathInfo {
2424public:
2425  std::unique_ptr<ExplodedGraph> BugPath;
2426  PathSensitiveBugReport *Report;
2427  const ExplodedNode *ErrorNode;
2428};
2429
2430/// A wrapper around an ExplodedGraph whose leafs are all error nodes. Can
2431/// conveniently retrieve bug paths from a single error node to the root.
2432class BugPathGetter {
2433  std::unique_ptr<ExplodedGraph> TrimmedGraph;
2434
2435  using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>;
2436
2437  /// Assign each node with its distance from the root.
2438  PriorityMapTy PriorityMap;
2439
2440  /// Since the getErrorNode() or BugReport refers to the original ExplodedGraph,
2441  /// we need to pair it to the error node of the constructed trimmed graph.
2442  using ReportNewNodePair =
2443      std::pair<PathSensitiveBugReport *, const ExplodedNode *>;
2444  SmallVector<ReportNewNodePair, 32> ReportNodes;
2445
2446  BugPathInfo CurrentBugPath;
2447
2448  /// A helper class for sorting ExplodedNodes by priority.
2449  template <bool Descending>
2450  class PriorityCompare {
2451    const PriorityMapTy &PriorityMap;
2452
2453  public:
2454    PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2455
2456    bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2457      PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2458      PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2459      PriorityMapTy::const_iterator E = PriorityMap.end();
2460
2461      if (LI == E)
2462        return Descending;
2463      if (RI == E)
2464        return !Descending;
2465
2466      return Descending ? LI->second > RI->second
2467                        : LI->second < RI->second;
2468    }
2469
2470    bool operator()(const ReportNewNodePair &LHS,
2471                    const ReportNewNodePair &RHS) const {
2472      return (*this)(LHS.second, RHS.second);
2473    }
2474  };
2475
2476public:
2477  BugPathGetter(const ExplodedGraph *OriginalGraph,
2478                ArrayRef<PathSensitiveBugReport *> &bugReports);
2479
2480  BugPathInfo *getNextBugPath();
2481};
2482
2483} // namespace
2484
2485BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph,
2486                             ArrayRef<PathSensitiveBugReport *> &bugReports) {
2487  SmallVector<const ExplodedNode *, 32> Nodes;
2488  for (const auto I : bugReports) {
2489    assert(I->isValid() &&
2490           "We only allow BugReporterVisitors and BugReporter itself to "
2491           "invalidate reports!");
2492    Nodes.emplace_back(I->getErrorNode());
2493  }
2494
2495  // The trimmed graph is created in the body of the constructor to ensure
2496  // that the DenseMaps have been initialized already.
2497  InterExplodedGraphMap ForwardMap;
2498  TrimmedGraph = OriginalGraph->trim(Nodes, &ForwardMap);
2499
2500  // Find the (first) error node in the trimmed graph.  We just need to consult
2501  // the node map which maps from nodes in the original graph to nodes
2502  // in the new graph.
2503  llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
2504
2505  for (PathSensitiveBugReport *Report : bugReports) {
2506    const ExplodedNode *NewNode = ForwardMap.lookup(Report->getErrorNode());
2507    assert(NewNode &&
2508           "Failed to construct a trimmed graph that contains this error "
2509           "node!");
2510    ReportNodes.emplace_back(Report, NewNode);
2511    RemainingNodes.insert(NewNode);
2512  }
2513
2514  assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2515
2516  // Perform a forward BFS to find all the shortest paths.
2517  std::queue<const ExplodedNode *> WS;
2518
2519  assert(TrimmedGraph->num_roots() == 1);
2520  WS.push(*TrimmedGraph->roots_begin());
2521  unsigned Priority = 0;
2522
2523  while (!WS.empty()) {
2524    const ExplodedNode *Node = WS.front();
2525    WS.pop();
2526
2527    PriorityMapTy::iterator PriorityEntry;
2528    bool IsNew;
2529    std::tie(PriorityEntry, IsNew) = PriorityMap.insert({Node, Priority});
2530    ++Priority;
2531
2532    if (!IsNew) {
2533      assert(PriorityEntry->second <= Priority);
2534      continue;
2535    }
2536
2537    if (RemainingNodes.erase(Node))
2538      if (RemainingNodes.empty())
2539        break;
2540
2541    for (const ExplodedNode *Succ : Node->succs())
2542      WS.push(Succ);
2543  }
2544
2545  // Sort the error paths from longest to shortest.
2546  llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap));
2547}
2548
2549BugPathInfo *BugPathGetter::getNextBugPath() {
2550  if (ReportNodes.empty())
2551    return nullptr;
2552
2553  const ExplodedNode *OrigN;
2554  std::tie(CurrentBugPath.Report, OrigN) = ReportNodes.pop_back_val();
2555  assert(PriorityMap.find(OrigN) != PriorityMap.end() &&
2556         "error node not accessible from root");
2557
2558  // Create a new graph with a single path. This is the graph that will be
2559  // returned to the caller.
2560  auto GNew = std::make_unique<ExplodedGraph>();
2561
2562  // Now walk from the error node up the BFS path, always taking the
2563  // predeccessor with the lowest number.
2564  ExplodedNode *Succ = nullptr;
2565  while (true) {
2566    // Create the equivalent node in the new graph with the same state
2567    // and location.
2568    ExplodedNode *NewN = GNew->createUncachedNode(
2569        OrigN->getLocation(), OrigN->getState(),
2570        OrigN->getID(), OrigN->isSink());
2571
2572    // Link up the new node with the previous node.
2573    if (Succ)
2574      Succ->addPredecessor(NewN, *GNew);
2575    else
2576      CurrentBugPath.ErrorNode = NewN;
2577
2578    Succ = NewN;
2579
2580    // Are we at the final node?
2581    if (OrigN->pred_empty()) {
2582      GNew->addRoot(NewN);
2583      break;
2584    }
2585
2586    // Find the next predeccessor node.  We choose the node that is marked
2587    // with the lowest BFS number.
2588    OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2589                              PriorityCompare<false>(PriorityMap));
2590  }
2591
2592  CurrentBugPath.BugPath = std::move(GNew);
2593
2594  return &CurrentBugPath;
2595}
2596
2597/// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic
2598/// object and collapses PathDiagosticPieces that are expanded by macros.
2599static void CompactMacroExpandedPieces(PathPieces &path,
2600                                       const SourceManager& SM) {
2601  using MacroStackTy = std::vector<
2602      std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>;
2603
2604  using PiecesTy = std::vector<PathDiagnosticPieceRef>;
2605
2606  MacroStackTy MacroStack;
2607  PiecesTy Pieces;
2608
2609  for (PathPieces::const_iterator I = path.begin(), E = path.end();
2610       I != E; ++I) {
2611    const auto &piece = *I;
2612
2613    // Recursively compact calls.
2614    if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
2615      CompactMacroExpandedPieces(call->path, SM);
2616    }
2617
2618    // Get the location of the PathDiagnosticPiece.
2619    const FullSourceLoc Loc = piece->getLocation().asLocation();
2620
2621    // Determine the instantiation location, which is the location we group
2622    // related PathDiagnosticPieces.
2623    SourceLocation InstantiationLoc = Loc.isMacroID() ?
2624                                      SM.getExpansionLoc(Loc) :
2625                                      SourceLocation();
2626
2627    if (Loc.isFileID()) {
2628      MacroStack.clear();
2629      Pieces.push_back(piece);
2630      continue;
2631    }
2632
2633    assert(Loc.isMacroID());
2634
2635    // Is the PathDiagnosticPiece within the same macro group?
2636    if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
2637      MacroStack.back().first->subPieces.push_back(piece);
2638      continue;
2639    }
2640
2641    // We aren't in the same group.  Are we descending into a new macro
2642    // or are part of an old one?
2643    std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
2644
2645    SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2646                                          SM.getExpansionLoc(Loc) :
2647                                          SourceLocation();
2648
2649    // Walk the entire macro stack.
2650    while (!MacroStack.empty()) {
2651      if (InstantiationLoc == MacroStack.back().second) {
2652        MacroGroup = MacroStack.back().first;
2653        break;
2654      }
2655
2656      if (ParentInstantiationLoc == MacroStack.back().second) {
2657        MacroGroup = MacroStack.back().first;
2658        break;
2659      }
2660
2661      MacroStack.pop_back();
2662    }
2663
2664    if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
2665      // Create a new macro group and add it to the stack.
2666      auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
2667          PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
2668
2669      if (MacroGroup)
2670        MacroGroup->subPieces.push_back(NewGroup);
2671      else {
2672        assert(InstantiationLoc.isFileID());
2673        Pieces.push_back(NewGroup);
2674      }
2675
2676      MacroGroup = NewGroup;
2677      MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
2678    }
2679
2680    // Finally, add the PathDiagnosticPiece to the group.
2681    MacroGroup->subPieces.push_back(piece);
2682  }
2683
2684  // Now take the pieces and construct a new PathDiagnostic.
2685  path.clear();
2686
2687  path.insert(path.end(), Pieces.begin(), Pieces.end());
2688}
2689
2690/// Generate notes from all visitors.
2691/// Notes associated with {@code ErrorNode} are generated using
2692/// {@code getEndPath}, and the rest are generated with {@code VisitNode}.
2693static std::unique_ptr<VisitorsDiagnosticsTy>
2694generateVisitorsDiagnostics(PathSensitiveBugReport *R,
2695                            const ExplodedNode *ErrorNode,
2696                            BugReporterContext &BRC) {
2697  std::unique_ptr<VisitorsDiagnosticsTy> Notes =
2698      std::make_unique<VisitorsDiagnosticsTy>();
2699  PathSensitiveBugReport::VisitorList visitors;
2700
2701  // Run visitors on all nodes starting from the node *before* the last one.
2702  // The last node is reserved for notes generated with {@code getEndPath}.
2703  const ExplodedNode *NextNode = ErrorNode->getFirstPred();
2704  while (NextNode) {
2705
2706    // At each iteration, move all visitors from report to visitor list. This is
2707    // important, because the Profile() functions of the visitors make sure that
2708    // a visitor isn't added multiple times for the same node, but it's fine
2709    // to add the a visitor with Profile() for different nodes (e.g. tracking
2710    // a region at different points of the symbolic execution).
2711    for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors())
2712      visitors.push_back(std::move(Visitor));
2713
2714    R->clearVisitors();
2715
2716    const ExplodedNode *Pred = NextNode->getFirstPred();
2717    if (!Pred) {
2718      PathDiagnosticPieceRef LastPiece;
2719      for (auto &V : visitors) {
2720        V->finalizeVisitor(BRC, ErrorNode, *R);
2721
2722        if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) {
2723          assert(!LastPiece &&
2724                 "There can only be one final piece in a diagnostic.");
2725          assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event &&
2726                 "The final piece must contain a message!");
2727          LastPiece = std::move(Piece);
2728          (*Notes)[ErrorNode].push_back(LastPiece);
2729        }
2730      }
2731      break;
2732    }
2733
2734    for (auto &V : visitors) {
2735      auto P = V->VisitNode(NextNode, BRC, *R);
2736      if (P)
2737        (*Notes)[NextNode].push_back(std::move(P));
2738    }
2739
2740    if (!R->isValid())
2741      break;
2742
2743    NextNode = Pred;
2744  }
2745
2746  return Notes;
2747}
2748
2749Optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(
2750    ArrayRef<PathSensitiveBugReport *> &bugReports,
2751    PathSensitiveBugReporter &Reporter) {
2752
2753  BugPathGetter BugGraph(&Reporter.getGraph(), bugReports);
2754
2755  while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) {
2756    // Find the BugReport with the original location.
2757    PathSensitiveBugReport *R = BugPath->Report;
2758    assert(R && "No original report found for sliced graph.");
2759    assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
2760    const ExplodedNode *ErrorNode = BugPath->ErrorNode;
2761
2762    // Register refutation visitors first, if they mark the bug invalid no
2763    // further analysis is required
2764    R->addVisitor(std::make_unique<LikelyFalsePositiveSuppressionBRVisitor>());
2765
2766    // Register additional node visitors.
2767    R->addVisitor(std::make_unique<NilReceiverBRVisitor>());
2768    R->addVisitor(std::make_unique<ConditionBRVisitor>());
2769    R->addVisitor(std::make_unique<TagVisitor>());
2770
2771    BugReporterContext BRC(Reporter);
2772
2773    // Run all visitors on a given graph, once.
2774    std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes =
2775        generateVisitorsDiagnostics(R, ErrorNode, BRC);
2776
2777    if (R->isValid()) {
2778      if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
2779        // If crosscheck is enabled, remove all visitors, add the refutation
2780        // visitor and check again
2781        R->clearVisitors();
2782        R->addVisitor(std::make_unique<FalsePositiveRefutationBRVisitor>());
2783
2784        // We don't overrite the notes inserted by other visitors because the
2785        // refutation manager does not add any new note to the path
2786        generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC);
2787      }
2788
2789      // Check if the bug is still valid
2790      if (R->isValid())
2791        return PathDiagnosticBuilder(
2792            std::move(BRC), std::move(BugPath->BugPath), BugPath->Report,
2793            BugPath->ErrorNode, std::move(visitorNotes));
2794    }
2795  }
2796
2797  return {};
2798}
2799
2800std::unique_ptr<DiagnosticForConsumerMapTy>
2801PathSensitiveBugReporter::generatePathDiagnostics(
2802    ArrayRef<PathDiagnosticConsumer *> consumers,
2803    ArrayRef<PathSensitiveBugReport *> &bugReports) {
2804  assert(!bugReports.empty());
2805
2806  auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
2807
2808  Optional<PathDiagnosticBuilder> PDB =
2809      PathDiagnosticBuilder::findValidReport(bugReports, *this);
2810
2811  if (PDB) {
2812    for (PathDiagnosticConsumer *PC : consumers) {
2813      if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) {
2814        (*Out)[PC] = std::move(PD);
2815      }
2816    }
2817  }
2818
2819  return Out;
2820}
2821
2822void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
2823  bool ValidSourceLoc = R->getLocation().isValid();
2824  assert(ValidSourceLoc);
2825  // If we mess up in a release build, we'd still prefer to just drop the bug
2826  // instead of trying to go on.
2827  if (!ValidSourceLoc)
2828    return;
2829
2830  // Compute the bug report's hash to determine its equivalence class.
2831  llvm::FoldingSetNodeID ID;
2832  R->Profile(ID);
2833
2834  // Lookup the equivance class.  If there isn't one, create it.
2835  void *InsertPos;
2836  BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
2837
2838  if (!EQ) {
2839    EQ = new BugReportEquivClass(std::move(R));
2840    EQClasses.InsertNode(EQ, InsertPos);
2841    EQClassesVector.push_back(EQ);
2842  } else
2843    EQ->AddReport(std::move(R));
2844}
2845
2846void PathSensitiveBugReporter::emitReport(std::unique_ptr<BugReport> R) {
2847  if (auto PR = dyn_cast<PathSensitiveBugReport>(R.get()))
2848    if (const ExplodedNode *E = PR->getErrorNode()) {
2849      // An error node must either be a sink or have a tag, otherwise
2850      // it could get reclaimed before the path diagnostic is created.
2851      assert((E->isSink() || E->getLocation().getTag()) &&
2852             "Error node must either be a sink or have a tag");
2853
2854      const AnalysisDeclContext *DeclCtx =
2855          E->getLocationContext()->getAnalysisDeclContext();
2856      // The source of autosynthesized body can be handcrafted AST or a model
2857      // file. The locations from handcrafted ASTs have no valid source
2858      // locations and have to be discarded. Locations from model files should
2859      // be preserved for processing and reporting.
2860      if (DeclCtx->isBodyAutosynthesized() &&
2861          !DeclCtx->isBodyAutosynthesizedFromModelFile())
2862        return;
2863    }
2864
2865  BugReporter::emitReport(std::move(R));
2866}
2867
2868//===----------------------------------------------------------------------===//
2869// Emitting reports in equivalence classes.
2870//===----------------------------------------------------------------------===//
2871
2872namespace {
2873
2874struct FRIEC_WLItem {
2875  const ExplodedNode *N;
2876  ExplodedNode::const_succ_iterator I, E;
2877
2878  FRIEC_WLItem(const ExplodedNode *n)
2879      : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2880};
2881
2882} // namespace
2883
2884BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass(
2885    BugReportEquivClass &EQ, SmallVectorImpl<BugReport *> &bugReports) {
2886  // If we don't need to suppress any of the nodes because they are
2887  // post-dominated by a sink, simply add all the nodes in the equivalence class
2888  // to 'Nodes'.  Any of the reports will serve as a "representative" report.
2889  assert(EQ.getReports().size() > 0);
2890  const BugType& BT = EQ.getReports()[0]->getBugType();
2891  if (!BT.isSuppressOnSink()) {
2892    BugReport *R = EQ.getReports()[0].get();
2893    for (auto &J : EQ.getReports()) {
2894      if (auto *PR = dyn_cast<PathSensitiveBugReport>(J.get())) {
2895        R = PR;
2896        bugReports.push_back(PR);
2897      }
2898    }
2899    return R;
2900  }
2901
2902  // For bug reports that should be suppressed when all paths are post-dominated
2903  // by a sink node, iterate through the reports in the equivalence class
2904  // until we find one that isn't post-dominated (if one exists).  We use a
2905  // DFS traversal of the ExplodedGraph to find a non-sink node.  We could write
2906  // this as a recursive function, but we don't want to risk blowing out the
2907  // stack for very long paths.
2908  BugReport *exampleReport = nullptr;
2909
2910  for (const auto &I: EQ.getReports()) {
2911    auto *R = dyn_cast<PathSensitiveBugReport>(I.get());
2912    if (!R)
2913      continue;
2914
2915    const ExplodedNode *errorNode = R->getErrorNode();
2916    if (errorNode->isSink()) {
2917      llvm_unreachable(
2918           "BugType::isSuppressSink() should not be 'true' for sink end nodes");
2919    }
2920    // No successors?  By definition this nodes isn't post-dominated by a sink.
2921    if (errorNode->succ_empty()) {
2922      bugReports.push_back(R);
2923      if (!exampleReport)
2924        exampleReport = R;
2925      continue;
2926    }
2927
2928    // See if we are in a no-return CFG block. If so, treat this similarly
2929    // to being post-dominated by a sink. This works better when the analysis
2930    // is incomplete and we have never reached the no-return function call(s)
2931    // that we'd inevitably bump into on this path.
2932    if (const CFGBlock *ErrorB = errorNode->getCFGBlock())
2933      if (ErrorB->isInevitablySinking())
2934        continue;
2935
2936    // At this point we know that 'N' is not a sink and it has at least one
2937    // successor.  Use a DFS worklist to find a non-sink end-of-path node.
2938    using WLItem = FRIEC_WLItem;
2939    using DFSWorkList = SmallVector<WLItem, 10>;
2940
2941    llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2942
2943    DFSWorkList WL;
2944    WL.push_back(errorNode);
2945    Visited[errorNode] = 1;
2946
2947    while (!WL.empty()) {
2948      WLItem &WI = WL.back();
2949      assert(!WI.N->succ_empty());
2950
2951      for (; WI.I != WI.E; ++WI.I) {
2952        const ExplodedNode *Succ = *WI.I;
2953        // End-of-path node?
2954        if (Succ->succ_empty()) {
2955          // If we found an end-of-path node that is not a sink.
2956          if (!Succ->isSink()) {
2957            bugReports.push_back(R);
2958            if (!exampleReport)
2959              exampleReport = R;
2960            WL.clear();
2961            break;
2962          }
2963          // Found a sink?  Continue on to the next successor.
2964          continue;
2965        }
2966        // Mark the successor as visited.  If it hasn't been explored,
2967        // enqueue it to the DFS worklist.
2968        unsigned &mark = Visited[Succ];
2969        if (!mark) {
2970          mark = 1;
2971          WL.push_back(Succ);
2972          break;
2973        }
2974      }
2975
2976      // The worklist may have been cleared at this point.  First
2977      // check if it is empty before checking the last item.
2978      if (!WL.empty() && &WL.back() == &WI)
2979        WL.pop_back();
2980    }
2981  }
2982
2983  // ExampleReport will be NULL if all the nodes in the equivalence class
2984  // were post-dominated by sinks.
2985  return exampleReport;
2986}
2987
2988void BugReporter::FlushReport(BugReportEquivClass& EQ) {
2989  SmallVector<BugReport*, 10> bugReports;
2990  BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
2991  if (!report)
2992    return;
2993
2994  ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
2995  std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
2996      generateDiagnosticForConsumerMap(report, Consumers, bugReports);
2997
2998  for (auto &P : *Diagnostics) {
2999    PathDiagnosticConsumer *Consumer = P.first;
3000    std::unique_ptr<PathDiagnostic> &PD = P.second;
3001
3002    // If the path is empty, generate a single step path with the location
3003    // of the issue.
3004    if (PD->path.empty()) {
3005      PathDiagnosticLocation L = report->getLocation();
3006      auto piece = std::make_unique<PathDiagnosticEventPiece>(
3007        L, report->getDescription());
3008      for (SourceRange Range : report->getRanges())
3009        piece->addRange(Range);
3010      PD->setEndOfPath(std::move(piece));
3011    }
3012
3013    PathPieces &Pieces = PD->getMutablePieces();
3014    if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) {
3015      // For path diagnostic consumers that don't support extra notes,
3016      // we may optionally convert those to path notes.
3017      for (auto I = report->getNotes().rbegin(),
3018           E = report->getNotes().rend(); I != E; ++I) {
3019        PathDiagnosticNotePiece *Piece = I->get();
3020        auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
3021          Piece->getLocation(), Piece->getString());
3022        for (const auto &R: Piece->getRanges())
3023          ConvertedPiece->addRange(R);
3024
3025        Pieces.push_front(std::move(ConvertedPiece));
3026      }
3027    } else {
3028      for (auto I = report->getNotes().rbegin(),
3029           E = report->getNotes().rend(); I != E; ++I)
3030        Pieces.push_front(*I);
3031    }
3032
3033    for (const auto &I : report->getFixits())
3034      Pieces.back()->addFixit(I);
3035
3036    updateExecutedLinesWithDiagnosticPieces(*PD);
3037    Consumer->HandlePathDiagnostic(std::move(PD));
3038  }
3039}
3040
3041/// Insert all lines participating in the function signature \p Signature
3042/// into \p ExecutedLines.
3043static void populateExecutedLinesWithFunctionSignature(
3044    const Decl *Signature, const SourceManager &SM,
3045    FilesToLineNumsMap &ExecutedLines) {
3046  SourceRange SignatureSourceRange;
3047  const Stmt* Body = Signature->getBody();
3048  if (const auto FD = dyn_cast<FunctionDecl>(Signature)) {
3049    SignatureSourceRange = FD->getSourceRange();
3050  } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) {
3051    SignatureSourceRange = OD->getSourceRange();
3052  } else {
3053    return;
3054  }
3055  SourceLocation Start = SignatureSourceRange.getBegin();
3056  SourceLocation End = Body ? Body->getSourceRange().getBegin()
3057    : SignatureSourceRange.getEnd();
3058  if (!Start.isValid() || !End.isValid())
3059    return;
3060  unsigned StartLine = SM.getExpansionLineNumber(Start);
3061  unsigned EndLine = SM.getExpansionLineNumber(End);
3062
3063  FileID FID = SM.getFileID(SM.getExpansionLoc(Start));
3064  for (unsigned Line = StartLine; Line <= EndLine; Line++)
3065    ExecutedLines[FID].insert(Line);
3066}
3067
3068static void populateExecutedLinesWithStmt(
3069    const Stmt *S, const SourceManager &SM,
3070    FilesToLineNumsMap &ExecutedLines) {
3071  SourceLocation Loc = S->getSourceRange().getBegin();
3072  if (!Loc.isValid())
3073    return;
3074  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
3075  FileID FID = SM.getFileID(ExpansionLoc);
3076  unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
3077  ExecutedLines[FID].insert(LineNo);
3078}
3079
3080/// \return all executed lines including function signatures on the path
3081/// starting from \p N.
3082static std::unique_ptr<FilesToLineNumsMap>
3083findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
3084  auto ExecutedLines = std::make_unique<FilesToLineNumsMap>();
3085
3086  while (N) {
3087    if (N->getFirstPred() == nullptr) {
3088      // First node: show signature of the entrance point.
3089      const Decl *D = N->getLocationContext()->getDecl();
3090      populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3091    } else if (auto CE = N->getLocationAs<CallEnter>()) {
3092      // Inlined function: show signature.
3093      const Decl* D = CE->getCalleeContext()->getDecl();
3094      populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3095    } else if (const Stmt *S = N->getStmtForDiagnostics()) {
3096      populateExecutedLinesWithStmt(S, SM, *ExecutedLines);
3097
3098      // Show extra context for some parent kinds.
3099      const Stmt *P = N->getParentMap().getParent(S);
3100
3101      // The path exploration can die before the node with the associated
3102      // return statement is generated, but we do want to show the whole
3103      // return.
3104      if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
3105        populateExecutedLinesWithStmt(RS, SM, *ExecutedLines);
3106        P = N->getParentMap().getParent(RS);
3107      }
3108
3109      if (P && (isa<SwitchCase>(P) || isa<LabelStmt>(P)))
3110        populateExecutedLinesWithStmt(P, SM, *ExecutedLines);
3111    }
3112
3113    N = N->getFirstPred();
3114  }
3115  return ExecutedLines;
3116}
3117
3118std::unique_ptr<DiagnosticForConsumerMapTy>
3119BugReporter::generateDiagnosticForConsumerMap(
3120    BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3121    ArrayRef<BugReport *> bugReports) {
3122  auto *basicReport = cast<BasicBugReport>(exampleReport);
3123  auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
3124  for (auto *Consumer : consumers)
3125    (*Out)[Consumer] = generateDiagnosticForBasicReport(basicReport);
3126  return Out;
3127}
3128
3129static PathDiagnosticCallPiece *
3130getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
3131                                const SourceManager &SMgr) {
3132  SourceLocation CallLoc = CP->callEnter.asLocation();
3133
3134  // If the call is within a macro, don't do anything (for now).
3135  if (CallLoc.isMacroID())
3136    return nullptr;
3137
3138  assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
3139         "The call piece should not be in a header file.");
3140
3141  // Check if CP represents a path through a function outside of the main file.
3142  if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
3143    return CP;
3144
3145  const PathPieces &Path = CP->path;
3146  if (Path.empty())
3147    return nullptr;
3148
3149  // Check if the last piece in the callee path is a call to a function outside
3150  // of the main file.
3151  if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
3152    return getFirstStackedCallToHeaderFile(CPInner, SMgr);
3153
3154  // Otherwise, the last piece is in the main file.
3155  return nullptr;
3156}
3157
3158static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) {
3159  if (PD.path.empty())
3160    return;
3161
3162  PathDiagnosticPiece *LastP = PD.path.back().get();
3163  assert(LastP);
3164  const SourceManager &SMgr = LastP->getLocation().getManager();
3165
3166  // We only need to check if the report ends inside headers, if the last piece
3167  // is a call piece.
3168  if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
3169    CP = getFirstStackedCallToHeaderFile(CP, SMgr);
3170    if (CP) {
3171      // Mark the piece.
3172       CP->setAsLastInMainSourceFile();
3173
3174      // Update the path diagnostic message.
3175      const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
3176      if (ND) {
3177        SmallString<200> buf;
3178        llvm::raw_svector_ostream os(buf);
3179        os << " (within a call to '" << ND->getDeclName() << "')";
3180        PD.appendToDesc(os.str());
3181      }
3182
3183      // Reset the report containing declaration and location.
3184      PD.setDeclWithIssue(CP->getCaller());
3185      PD.setLocation(CP->getLocation());
3186
3187      return;
3188    }
3189  }
3190}
3191
3192
3193
3194std::unique_ptr<DiagnosticForConsumerMapTy>
3195PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
3196    BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3197    ArrayRef<BugReport *> bugReports) {
3198  std::vector<BasicBugReport *> BasicBugReports;
3199  std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;
3200  if (isa<BasicBugReport>(exampleReport))
3201    return BugReporter::generateDiagnosticForConsumerMap(exampleReport,
3202                                                         consumers, bugReports);
3203
3204  // Generate the full path sensitive diagnostic, using the generation scheme
3205  // specified by the PathDiagnosticConsumer. Note that we have to generate
3206  // path diagnostics even for consumers which do not support paths, because
3207  // the BugReporterVisitors may mark this bug as a false positive.
3208  assert(!bugReports.empty());
3209  MaxBugClassSize.updateMax(bugReports.size());
3210
3211  // Avoid copying the whole array because there may be a lot of reports.
3212  ArrayRef<PathSensitiveBugReport *> convertedArrayOfReports(
3213      reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()),
3214      reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end()));
3215  std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics(
3216      consumers, convertedArrayOfReports);
3217
3218  if (Out->empty())
3219    return Out;
3220
3221  MaxValidBugClassSize.updateMax(bugReports.size());
3222
3223  // Examine the report and see if the last piece is in a header. Reset the
3224  // report location to the last piece in the main source file.
3225  const AnalyzerOptions &Opts = getAnalyzerOptions();
3226  for (auto const &P : *Out)
3227    if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
3228      resetDiagnosticLocationToMainFile(*P.second);
3229
3230  return Out;
3231}
3232
3233void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3234                                  const CheckerBase *Checker, StringRef Name,
3235                                  StringRef Category, StringRef Str,
3236                                  PathDiagnosticLocation Loc,
3237                                  ArrayRef<SourceRange> Ranges,
3238                                  ArrayRef<FixItHint> Fixits) {
3239  EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str,
3240                  Loc, Ranges, Fixits);
3241}
3242
3243void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3244                                  CheckerNameRef CheckName,
3245                                  StringRef name, StringRef category,
3246                                  StringRef str, PathDiagnosticLocation Loc,
3247                                  ArrayRef<SourceRange> Ranges,
3248                                  ArrayRef<FixItHint> Fixits) {
3249  // 'BT' is owned by BugReporter.
3250  BugType *BT = getBugTypeForName(CheckName, name, category);
3251  auto R = std::make_unique<BasicBugReport>(*BT, str, Loc);
3252  R->setDeclWithIssue(DeclWithIssue);
3253  for (const auto &SR : Ranges)
3254    R->addRange(SR);
3255  for (const auto &FH : Fixits)
3256    R->addFixItHint(FH);
3257  emitReport(std::move(R));
3258}
3259
3260BugType *BugReporter::getBugTypeForName(CheckerNameRef CheckName,
3261                                        StringRef name, StringRef category) {
3262  SmallString<136> fullDesc;
3263  llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3264                                      << ":" << category;
3265  BugType *&BT = StrBugTypes[fullDesc];
3266  if (!BT)
3267    BT = new BugType(CheckName, name, category);
3268  return BT;
3269}
3270