clang  10.0.0svn
BugReporter.cpp
Go to the documentation of this file.
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 
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"
25 #include "clang/Analysis/CFG.h"
29 #include "clang/Basic/LLVM.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 
72 using namespace clang;
73 using namespace ento;
74 using namespace llvm;
75 
76 #define DEBUG_TYPE "BugReporter"
77 
78 STATISTIC(MaxBugClassSize,
79  "The maximum number of bug reports in the same equivalence class");
80 STATISTIC(MaxValidBugClassSize,
81  "The maximum number of bug reports in the same equivalence class "
82  "where at least one report is valid (not suppressed)");
83 
84 BugReporterVisitor::~BugReporterVisitor() = default;
85 
86 void BugReporterContext::anchor() {}
87 
88 //===----------------------------------------------------------------------===//
89 // PathDiagnosticBuilder and its associated routines and helper objects.
90 //===----------------------------------------------------------------------===//
91 
92 namespace {
93 
94 /// A (CallPiece, node assiciated with its CallEnter) pair.
95 using CallWithEntry =
96  std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>;
97 using CallWithEntryStack = SmallVector<CallWithEntry, 6>;
98 
99 /// Map from each node to the diagnostic pieces visitors emit for them.
100 using 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.
105 using 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.
111 class 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 
123 public:
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 
133 public:
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.
200 class 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 
215 public:
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.
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 
241 private:
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 
251  generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S,
252  PathDiagnosticLocation &Start) const;
253 
255  generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst,
256  PathDiagnosticLocation &Start) const;
257 
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 
279 
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.
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 
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 
342 eventsDescribeSameCondition(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.
368 static 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()) {
382  removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
383  break;
385  removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
386  break;
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  }
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.
418 static 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()) {
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  }
444  auto &macro = cast<PathDiagnosticMacroPiece>(*piece);
445  if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting))
446  continue;
447  containsSomethingInteresting = true;
448  break;
449  }
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  }
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.
471 static 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.
482 static 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.
489 static void
490 adjustCallLocations(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.
523 static 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()))
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.
557 static 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()))
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 
574 PathDiagnosticLocation 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 
584 PathDiagnosticLocation 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 
613 static 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 
635 static PathDiagnosticLocation
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
718 void 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 
734 static void CompactMacroExpandedPieces(PathPieces &path,
735  const SourceManager& SM);
736 
737 PathDiagnosticPieceRef 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 
795 PathDiagnosticPieceRef 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 
806 PathDiagnosticPieceRef 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 =
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 =
848  }
849  }
850  return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
851  os.str());
852 }
853 
854 void 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 
998 static 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 
1011 static 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 
1017 static 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 
1027 static 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 
1041 static 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.
1075 static 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.
1102 static 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 
1109 constexpr llvm::StringLiteral StrEnteringLoop = "Entering loop body";
1110 constexpr llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
1111 constexpr llvm::StringLiteral StrLoopRangeEmpty =
1112  "Loop body skipped when range is empty";
1113 constexpr llvm::StringLiteral StrLoopCollectionEmpty =
1114  "Loop body skipped when collection is empty";
1115 
1116 static std::unique_ptr<FilesToLineNumsMap>
1117 findExecutedLines(const SourceManager &SM, const ExplodedNode *N);
1118 
1119 void 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,
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,
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 
1311 static std::unique_ptr<PathDiagnostic>
1312 generateDiagnosticForBasicReport(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 
1321 static std::unique_ptr<PathDiagnostic>
1322 generateEmptyDiagnosticForReport(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 
1332 static 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 
1353 static 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 
1394 static 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 
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.
1410 static 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();
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.
1491 static 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.
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)
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.
1612 static 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.
1667 static 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.
1677 static 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 
1730 static 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 
1753 static 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.
1935 static 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.
1952 static 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 
1966 PathDiagnosticConstruct::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 
1975 PathDiagnosticBuilder::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 
1983 std::unique_ptr<PathDiagnostic>
1984 PathDiagnosticBuilder::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,
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 
2100 void BugType::anchor() {}
2101 
2102 void BuiltinBug::anchor() {}
2103 
2104 //===----------------------------------------------------------------------===//
2105 // Methods for BugReport and subclasses.
2106 //===----------------------------------------------------------------------===//
2107 
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 
2125  Callbacks.clear();
2126 }
2127 
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 
2137 void 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 
2152 void 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 
2175 template <class T>
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 
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 
2215 void 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 
2228  bugreporter::TrackingKind TKind) {
2229  markInteresting(V.getAsRegion(), TKind);
2230  markInteresting(V.getAsSymbol(), TKind);
2231 }
2232 
2234  if (!LC)
2235  return;
2236  InterestingLocationContexts.insert(LC);
2237 }
2238 
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 
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 
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 
2292  return getInterestingnessKind(V).hasValue();
2293 }
2294 
2296  return getInterestingnessKind(sym).hasValue();
2297 }
2298 
2300  return getInterestingnessKind(R).hasValue();
2301 }
2302 
2304  if (!LC)
2305  return false;
2306  return InterestingLocationContexts.count(LC);
2307 }
2308 
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 
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 
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.
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))
2361 
2362  // For binary operators, return the location of the operator.
2363  if (const auto *B = dyn_cast<BinaryOperator>(S))
2365 
2367  return PathDiagnosticLocation::createEnd(S, SM, LC);
2368 
2369  if (S->getBeginLoc().isValid())
2370  return PathDiagnosticLocation(S, SM, LC);
2371 
2372  return PathDiagnosticLocation(
2374  }
2375 
2376  return PathDiagnosticLocation::createDeclEnd(ErrorNode->getLocationContext(),
2377  SM);
2378 }
2379 
2380 //===----------------------------------------------------------------------===//
2381 // Methods for BugReporter and subclasses.
2382 //===----------------------------------------------------------------------===//
2383 
2385  return Eng.getGraph();
2386 }
2387 
2389  return Eng.getStateManager();
2390 }
2391 
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 
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 
2419 namespace {
2420 
2421 /// A wrapper around an ExplodedGraph that contains a single path from the root
2422 /// to the error node.
2423 class BugPathInfo {
2424 public:
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.
2432 class 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 *>;
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 
2476 public:
2477  BugPathGetter(const ExplodedGraph *OriginalGraph,
2479 
2480  BugPathInfo *getNextBugPath();
2481 };
2482 
2483 } // namespace
2484 
2485 BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph,
2486  ArrayRef<PathSensitiveBugReport *> &bugReports) {
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 
2549 BugPathInfo *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(), OrigN->isSink());
2570 
2571  // Link up the new node with the previous node.
2572  if (Succ)
2573  Succ->addPredecessor(NewN, *GNew);
2574  else
2575  CurrentBugPath.ErrorNode = NewN;
2576 
2577  Succ = NewN;
2578 
2579  // Are we at the final node?
2580  if (OrigN->pred_empty()) {
2581  GNew->addRoot(NewN);
2582  break;
2583  }
2584 
2585  // Find the next predeccessor node. We choose the node that is marked
2586  // with the lowest BFS number.
2587  OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2588  PriorityCompare<false>(PriorityMap));
2589  }
2590 
2591  CurrentBugPath.BugPath = std::move(GNew);
2592 
2593  return &CurrentBugPath;
2594 }
2595 
2596 /// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic
2597 /// object and collapses PathDiagosticPieces that are expanded by macros.
2598 static void CompactMacroExpandedPieces(PathPieces &path,
2599  const SourceManager& SM) {
2600  using MacroStackTy = std::vector<
2601  std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>;
2602 
2603  using PiecesTy = std::vector<PathDiagnosticPieceRef>;
2604 
2605  MacroStackTy MacroStack;
2606  PiecesTy Pieces;
2607 
2608  for (PathPieces::const_iterator I = path.begin(), E = path.end();
2609  I != E; ++I) {
2610  const auto &piece = *I;
2611 
2612  // Recursively compact calls.
2613  if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
2614  CompactMacroExpandedPieces(call->path, SM);
2615  }
2616 
2617  // Get the location of the PathDiagnosticPiece.
2618  const FullSourceLoc Loc = piece->getLocation().asLocation();
2619 
2620  // Determine the instantiation location, which is the location we group
2621  // related PathDiagnosticPieces.
2622  SourceLocation InstantiationLoc = Loc.isMacroID() ?
2623  SM.getExpansionLoc(Loc) :
2624  SourceLocation();
2625 
2626  if (Loc.isFileID()) {
2627  MacroStack.clear();
2628  Pieces.push_back(piece);
2629  continue;
2630  }
2631 
2632  assert(Loc.isMacroID());
2633 
2634  // Is the PathDiagnosticPiece within the same macro group?
2635  if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
2636  MacroStack.back().first->subPieces.push_back(piece);
2637  continue;
2638  }
2639 
2640  // We aren't in the same group. Are we descending into a new macro
2641  // or are part of an old one?
2642  std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
2643 
2644  SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2645  SM.getExpansionLoc(Loc) :
2646  SourceLocation();
2647 
2648  // Walk the entire macro stack.
2649  while (!MacroStack.empty()) {
2650  if (InstantiationLoc == MacroStack.back().second) {
2651  MacroGroup = MacroStack.back().first;
2652  break;
2653  }
2654 
2655  if (ParentInstantiationLoc == MacroStack.back().second) {
2656  MacroGroup = MacroStack.back().first;
2657  break;
2658  }
2659 
2660  MacroStack.pop_back();
2661  }
2662 
2663  if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
2664  // Create a new macro group and add it to the stack.
2665  auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
2666  PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
2667 
2668  if (MacroGroup)
2669  MacroGroup->subPieces.push_back(NewGroup);
2670  else {
2671  assert(InstantiationLoc.isFileID());
2672  Pieces.push_back(NewGroup);
2673  }
2674 
2675  MacroGroup = NewGroup;
2676  MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
2677  }
2678 
2679  // Finally, add the PathDiagnosticPiece to the group.
2680  MacroGroup->subPieces.push_back(piece);
2681  }
2682 
2683  // Now take the pieces and construct a new PathDiagnostic.
2684  path.clear();
2685 
2686  path.insert(path.end(), Pieces.begin(), Pieces.end());
2687 }
2688 
2689 /// Generate notes from all visitors.
2690 /// Notes associated with {@code ErrorNode} are generated using
2691 /// {@code getEndPath}, and the rest are generated with {@code VisitNode}.
2692 static std::unique_ptr<VisitorsDiagnosticsTy>
2693 generateVisitorsDiagnostics(PathSensitiveBugReport *R,
2694  const ExplodedNode *ErrorNode,
2695  BugReporterContext &BRC) {
2696  std::unique_ptr<VisitorsDiagnosticsTy> Notes =
2697  std::make_unique<VisitorsDiagnosticsTy>();
2699 
2700  // Run visitors on all nodes starting from the node *before* the last one.
2701  // The last node is reserved for notes generated with {@code getEndPath}.
2702  const ExplodedNode *NextNode = ErrorNode->getFirstPred();
2703  while (NextNode) {
2704 
2705  // At each iteration, move all visitors from report to visitor list. This is
2706  // important, because the Profile() functions of the visitors make sure that
2707  // a visitor isn't added multiple times for the same node, but it's fine
2708  // to add the a visitor with Profile() for different nodes (e.g. tracking
2709  // a region at different points of the symbolic execution).
2710  for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors())
2711  visitors.push_back(std::move(Visitor));
2712 
2713  R->clearVisitors();
2714 
2715  const ExplodedNode *Pred = NextNode->getFirstPred();
2716  if (!Pred) {
2717  PathDiagnosticPieceRef LastPiece;
2718  for (auto &V : visitors) {
2719  V->finalizeVisitor(BRC, ErrorNode, *R);
2720 
2721  if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) {
2722  assert(!LastPiece &&
2723  "There can only be one final piece in a diagnostic.");
2724  assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event &&
2725  "The final piece must contain a message!");
2726  LastPiece = std::move(Piece);
2727  (*Notes)[ErrorNode].push_back(LastPiece);
2728  }
2729  }
2730  break;
2731  }
2732 
2733  for (auto &V : visitors) {
2734  auto P = V->VisitNode(NextNode, BRC, *R);
2735  if (P)
2736  (*Notes)[NextNode].push_back(std::move(P));
2737  }
2738 
2739  if (!R->isValid())
2740  break;
2741 
2742  NextNode = Pred;
2743  }
2744 
2745  return Notes;
2746 }
2747 
2748 Optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(
2750  PathSensitiveBugReporter &Reporter) {
2751 
2752  BugPathGetter BugGraph(&Reporter.getGraph(), bugReports);
2753 
2754  while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) {
2755  // Find the BugReport with the original location.
2756  PathSensitiveBugReport *R = BugPath->Report;
2757  assert(R && "No original report found for sliced graph.");
2758  assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
2759  const ExplodedNode *ErrorNode = BugPath->ErrorNode;
2760 
2761  // Register refutation visitors first, if they mark the bug invalid no
2762  // further analysis is required
2763  R->addVisitor(std::make_unique<LikelyFalsePositiveSuppressionBRVisitor>());
2764 
2765  // Register additional node visitors.
2766  R->addVisitor(std::make_unique<NilReceiverBRVisitor>());
2767  R->addVisitor(std::make_unique<ConditionBRVisitor>());
2768  R->addVisitor(std::make_unique<TagVisitor>());
2769 
2770  BugReporterContext BRC(Reporter);
2771 
2772  // Run all visitors on a given graph, once.
2773  std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes =
2774  generateVisitorsDiagnostics(R, ErrorNode, BRC);
2775 
2776  if (R->isValid()) {
2777  if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
2778  // If crosscheck is enabled, remove all visitors, add the refutation
2779  // visitor and check again
2780  R->clearVisitors();
2781  R->addVisitor(std::make_unique<FalsePositiveRefutationBRVisitor>());
2782 
2783  // We don't overrite the notes inserted by other visitors because the
2784  // refutation manager does not add any new note to the path
2785  generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC);
2786  }
2787 
2788  // Check if the bug is still valid
2789  if (R->isValid())
2790  return PathDiagnosticBuilder(
2791  std::move(BRC), std::move(BugPath->BugPath), BugPath->Report,
2792  BugPath->ErrorNode, std::move(visitorNotes));
2793  }
2794  }
2795 
2796  return {};
2797 }
2798 
2799 std::unique_ptr<DiagnosticForConsumerMapTy>
2802  ArrayRef<PathSensitiveBugReport *> &bugReports) {
2803  assert(!bugReports.empty());
2804 
2805  auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
2806 
2808  PathDiagnosticBuilder::findValidReport(bugReports, *this);
2809 
2810  if (PDB) {
2811  for (PathDiagnosticConsumer *PC : consumers) {
2812  if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) {
2813  (*Out)[PC] = std::move(PD);
2814  }
2815  }
2816  }
2817 
2818  return Out;
2819 }
2820 
2821 void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
2822  bool ValidSourceLoc = R->getLocation().isValid();
2823  assert(ValidSourceLoc);
2824  // If we mess up in a release build, we'd still prefer to just drop the bug
2825  // instead of trying to go on.
2826  if (!ValidSourceLoc)
2827  return;
2828 
2829  // Compute the bug report's hash to determine its equivalence class.
2830  llvm::FoldingSetNodeID ID;
2831  R->Profile(ID);
2832 
2833  // Lookup the equivance class. If there isn't one, create it.
2834  void *InsertPos;
2835  BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
2836 
2837  if (!EQ) {
2838  EQ = new BugReportEquivClass(std::move(R));
2839  EQClasses.InsertNode(EQ, InsertPos);
2840  EQClassesVector.push_back(EQ);
2841  } else
2842  EQ->AddReport(std::move(R));
2843 }
2844 
2845 void PathSensitiveBugReporter::emitReport(std::unique_ptr<BugReport> R) {
2846  if (auto PR = dyn_cast<PathSensitiveBugReport>(R.get()))
2847  if (const ExplodedNode *E = PR->getErrorNode()) {
2848  // An error node must either be a sink or have a tag, otherwise
2849  // it could get reclaimed before the path diagnostic is created.
2850  assert((E->isSink() || E->getLocation().getTag()) &&
2851  "Error node must either be a sink or have a tag");
2852 
2853  const AnalysisDeclContext *DeclCtx =
2854  E->getLocationContext()->getAnalysisDeclContext();
2855  // The source of autosynthesized body can be handcrafted AST or a model
2856  // file. The locations from handcrafted ASTs have no valid source
2857  // locations and have to be discarded. Locations from model files should
2858  // be preserved for processing and reporting.
2859  if (DeclCtx->isBodyAutosynthesized() &&
2861  return;
2862  }
2863 
2864  BugReporter::emitReport(std::move(R));
2865 }
2866 
2867 //===----------------------------------------------------------------------===//
2868 // Emitting reports in equivalence classes.
2869 //===----------------------------------------------------------------------===//
2870 
2871 namespace {
2872 
2873 struct FRIEC_WLItem {
2874  const ExplodedNode *N;
2876 
2877  FRIEC_WLItem(const ExplodedNode *n)
2878  : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2879 };
2880 
2881 } // namespace
2882 
2883 BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass(
2884  BugReportEquivClass &EQ, SmallVectorImpl<BugReport *> &bugReports) {
2885  // If we don't need to suppress any of the nodes because they are
2886  // post-dominated by a sink, simply add all the nodes in the equivalence class
2887  // to 'Nodes'. Any of the reports will serve as a "representative" report.
2888  assert(EQ.getReports().size() > 0);
2889  const BugType& BT = EQ.getReports()[0]->getBugType();
2890  if (!BT.isSuppressOnSink()) {
2891  BugReport *R = EQ.getReports()[0].get();
2892  for (auto &J : EQ.getReports()) {
2893  if (auto *PR = dyn_cast<PathSensitiveBugReport>(J.get())) {
2894  R = PR;
2895  bugReports.push_back(PR);
2896  }
2897  }
2898  return R;
2899  }
2900 
2901  // For bug reports that should be suppressed when all paths are post-dominated
2902  // by a sink node, iterate through the reports in the equivalence class
2903  // until we find one that isn't post-dominated (if one exists). We use a
2904  // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2905  // this as a recursive function, but we don't want to risk blowing out the
2906  // stack for very long paths.
2907  BugReport *exampleReport = nullptr;
2908 
2909  for (const auto &I: EQ.getReports()) {
2910  auto *R = dyn_cast<PathSensitiveBugReport>(I.get());
2911  if (!R)
2912  continue;
2913 
2914  const ExplodedNode *errorNode = R->getErrorNode();
2915  if (errorNode->isSink()) {
2916  llvm_unreachable(
2917  "BugType::isSuppressSink() should not be 'true' for sink end nodes");
2918  }
2919  // No successors? By definition this nodes isn't post-dominated by a sink.
2920  if (errorNode->succ_empty()) {
2921  bugReports.push_back(R);
2922  if (!exampleReport)
2923  exampleReport = R;
2924  continue;
2925  }
2926 
2927  // See if we are in a no-return CFG block. If so, treat this similarly
2928  // to being post-dominated by a sink. This works better when the analysis
2929  // is incomplete and we have never reached the no-return function call(s)
2930  // that we'd inevitably bump into on this path.
2931  if (const CFGBlock *ErrorB = errorNode->getCFGBlock())
2932  if (ErrorB->isInevitablySinking())
2933  continue;
2934 
2935  // At this point we know that 'N' is not a sink and it has at least one
2936  // successor. Use a DFS worklist to find a non-sink end-of-path node.
2937  using WLItem = FRIEC_WLItem;
2938  using DFSWorkList = SmallVector<WLItem, 10>;
2939 
2940  llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2941 
2942  DFSWorkList WL;
2943  WL.push_back(errorNode);
2944  Visited[errorNode] = 1;
2945 
2946  while (!WL.empty()) {
2947  WLItem &WI = WL.back();
2948  assert(!WI.N->succ_empty());
2949 
2950  for (; WI.I != WI.E; ++WI.I) {
2951  const ExplodedNode *Succ = *WI.I;
2952  // End-of-path node?
2953  if (Succ->succ_empty()) {
2954  // If we found an end-of-path node that is not a sink.
2955  if (!Succ->isSink()) {
2956  bugReports.push_back(R);
2957  if (!exampleReport)
2958  exampleReport = R;
2959  WL.clear();
2960  break;
2961  }
2962  // Found a sink? Continue on to the next successor.
2963  continue;
2964  }
2965  // Mark the successor as visited. If it hasn't been explored,
2966  // enqueue it to the DFS worklist.
2967  unsigned &mark = Visited[Succ];
2968  if (!mark) {
2969  mark = 1;
2970  WL.push_back(Succ);
2971  break;
2972  }
2973  }
2974 
2975  // The worklist may have been cleared at this point. First
2976  // check if it is empty before checking the last item.
2977  if (!WL.empty() && &WL.back() == &WI)
2978  WL.pop_back();
2979  }
2980  }
2981 
2982  // ExampleReport will be NULL if all the nodes in the equivalence class
2983  // were post-dominated by sinks.
2984  return exampleReport;
2985 }
2986 
2987 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
2988  SmallVector<BugReport*, 10> bugReports;
2989  BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
2990  if (!report)
2991  return;
2992 
2993  ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
2994  std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
2995  generateDiagnosticForConsumerMap(report, Consumers, bugReports);
2996 
2997  for (auto &P : *Diagnostics) {
2998  PathDiagnosticConsumer *Consumer = P.first;
2999  std::unique_ptr<PathDiagnostic> &PD = P.second;
3000 
3001  // If the path is empty, generate a single step path with the location
3002  // of the issue.
3003  if (PD->path.empty()) {
3004  PathDiagnosticLocation L = report->getLocation();
3005  auto piece = std::make_unique<PathDiagnosticEventPiece>(
3006  L, report->getDescription());
3007  for (SourceRange Range : report->getRanges())
3008  piece->addRange(Range);
3009  PD->setEndOfPath(std::move(piece));
3010  }
3011 
3012  PathPieces &Pieces = PD->getMutablePieces();
3013  if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) {
3014  // For path diagnostic consumers that don't support extra notes,
3015  // we may optionally convert those to path notes.
3016  for (auto I = report->getNotes().rbegin(),
3017  E = report->getNotes().rend(); I != E; ++I) {
3018  PathDiagnosticNotePiece *Piece = I->get();
3019  auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
3020  Piece->getLocation(), Piece->getString());
3021  for (const auto &R: Piece->getRanges())
3022  ConvertedPiece->addRange(R);
3023 
3024  Pieces.push_front(std::move(ConvertedPiece));
3025  }
3026  } else {
3027  for (auto I = report->getNotes().rbegin(),
3028  E = report->getNotes().rend(); I != E; ++I)
3029  Pieces.push_front(*I);
3030  }
3031 
3032  for (const auto &I : report->getFixits())
3033  Pieces.back()->addFixit(I);
3034 
3036  Consumer->HandlePathDiagnostic(std::move(PD));
3037  }
3038 }
3039 
3040 /// Insert all lines participating in the function signature \p Signature
3041 /// into \p ExecutedLines.
3042 static void populateExecutedLinesWithFunctionSignature(
3043  const Decl *Signature, const SourceManager &SM,
3044  FilesToLineNumsMap &ExecutedLines) {
3045  SourceRange SignatureSourceRange;
3046  const Stmt* Body = Signature->getBody();
3047  if (const auto FD = dyn_cast<FunctionDecl>(Signature)) {
3048  SignatureSourceRange = FD->getSourceRange();
3049  } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) {
3050  SignatureSourceRange = OD->getSourceRange();
3051  } else {
3052  return;
3053  }
3054  SourceLocation Start = SignatureSourceRange.getBegin();
3055  SourceLocation End = Body ? Body->getSourceRange().getBegin()
3056  : SignatureSourceRange.getEnd();
3057  if (!Start.isValid() || !End.isValid())
3058  return;
3059  unsigned StartLine = SM.getExpansionLineNumber(Start);
3060  unsigned EndLine = SM.getExpansionLineNumber(End);
3061 
3062  FileID FID = SM.getFileID(SM.getExpansionLoc(Start));
3063  for (unsigned Line = StartLine; Line <= EndLine; Line++)
3064  ExecutedLines[FID].insert(Line);
3065 }
3066 
3067 static void populateExecutedLinesWithStmt(
3068  const Stmt *S, const SourceManager &SM,
3069  FilesToLineNumsMap &ExecutedLines) {
3070  SourceLocation Loc = S->getSourceRange().getBegin();
3071  if (!Loc.isValid())
3072  return;
3073  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
3074  FileID FID = SM.getFileID(ExpansionLoc);
3075  unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
3076  ExecutedLines[FID].insert(LineNo);
3077 }
3078 
3079 /// \return all executed lines including function signatures on the path
3080 /// starting from \p N.
3081 static std::unique_ptr<FilesToLineNumsMap>
3082 findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
3083  auto ExecutedLines = std::make_unique<FilesToLineNumsMap>();
3084 
3085  while (N) {
3086  if (N->getFirstPred() == nullptr) {
3087  // First node: show signature of the entrance point.
3088  const Decl *D = N->getLocationContext()->getDecl();
3089  populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3090  } else if (auto CE = N->getLocationAs<CallEnter>()) {
3091  // Inlined function: show signature.
3092  const Decl* D = CE->getCalleeContext()->getDecl();
3093  populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
3094  } else if (const Stmt *S = N->getStmtForDiagnostics()) {
3095  populateExecutedLinesWithStmt(S, SM, *ExecutedLines);
3096 
3097  // Show extra context for some parent kinds.
3098  const Stmt *P = N->getParentMap().getParent(S);
3099 
3100  // The path exploration can die before the node with the associated
3101  // return statement is generated, but we do want to show the whole
3102  // return.
3103  if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
3104  populateExecutedLinesWithStmt(RS, SM, *ExecutedLines);
3105  P = N->getParentMap().getParent(RS);
3106  }
3107 
3108  if (P && (isa<SwitchCase>(P) || isa<LabelStmt>(P)))
3109  populateExecutedLinesWithStmt(P, SM, *ExecutedLines);
3110  }
3111 
3112  N = N->getFirstPred();
3113  }
3114  return ExecutedLines;
3115 }
3116 
3117 std::unique_ptr<DiagnosticForConsumerMapTy>
3119  BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3120  ArrayRef<BugReport *> bugReports) {
3121  auto *basicReport = cast<BasicBugReport>(exampleReport);
3122  auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
3123  for (auto *Consumer : consumers)
3124  (*Out)[Consumer] = generateDiagnosticForBasicReport(basicReport);
3125  return Out;
3126 }
3127 
3128 static PathDiagnosticCallPiece *
3129 getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
3130  const SourceManager &SMgr) {
3131  SourceLocation CallLoc = CP->callEnter.asLocation();
3132 
3133  // If the call is within a macro, don't do anything (for now).
3134  if (CallLoc.isMacroID())
3135  return nullptr;
3136 
3137  assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
3138  "The call piece should not be in a header file.");
3139 
3140  // Check if CP represents a path through a function outside of the main file.
3141  if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
3142  return CP;
3143 
3144  const PathPieces &Path = CP->path;
3145  if (Path.empty())
3146  return nullptr;
3147 
3148  // Check if the last piece in the callee path is a call to a function outside
3149  // of the main file.
3150  if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
3151  return getFirstStackedCallToHeaderFile(CPInner, SMgr);
3152 
3153  // Otherwise, the last piece is in the main file.
3154  return nullptr;
3155 }
3156 
3157 static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) {
3158  if (PD.path.empty())
3159  return;
3160 
3161  PathDiagnosticPiece *LastP = PD.path.back().get();
3162  assert(LastP);
3163  const SourceManager &SMgr = LastP->getLocation().getManager();
3164 
3165  // We only need to check if the report ends inside headers, if the last piece
3166  // is a call piece.
3167  if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
3168  CP = getFirstStackedCallToHeaderFile(CP, SMgr);
3169  if (CP) {
3170  // Mark the piece.
3171  CP->setAsLastInMainSourceFile();
3172 
3173  // Update the path diagnostic message.
3174  const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
3175  if (ND) {
3176  SmallString<200> buf;
3177  llvm::raw_svector_ostream os(buf);
3178  os << " (within a call to '" << ND->getDeclName() << "')";
3179  PD.appendToDesc(os.str());
3180  }
3181 
3182  // Reset the report containing declaration and location.
3183  PD.setDeclWithIssue(CP->getCaller());
3184  PD.setLocation(CP->getLocation());
3185 
3186  return;
3187  }
3188  }
3189 }
3190 
3191 
3192 
3193 std::unique_ptr<DiagnosticForConsumerMapTy>
3194 PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
3195  BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3196  ArrayRef<BugReport *> bugReports) {
3197  std::vector<BasicBugReport *> BasicBugReports;
3198  std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;
3199  if (isa<BasicBugReport>(exampleReport))
3200  return BugReporter::generateDiagnosticForConsumerMap(exampleReport,
3201  consumers, bugReports);
3202 
3203  // Generate the full path sensitive diagnostic, using the generation scheme
3204  // specified by the PathDiagnosticConsumer. Note that we have to generate
3205  // path diagnostics even for consumers which do not support paths, because
3206  // the BugReporterVisitors may mark this bug as a false positive.
3207  assert(!bugReports.empty());
3208  MaxBugClassSize.updateMax(bugReports.size());
3209 
3210  // Avoid copying the whole array because there may be a lot of reports.
3211  ArrayRef<PathSensitiveBugReport *> convertedArrayOfReports(
3212  reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()),
3213  reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end()));
3214  std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics(
3215  consumers, convertedArrayOfReports);
3216 
3217  if (Out->empty())
3218  return Out;
3219 
3220  MaxValidBugClassSize.updateMax(bugReports.size());
3221 
3222  // Examine the report and see if the last piece is in a header. Reset the
3223  // report location to the last piece in the main source file.
3224  const AnalyzerOptions &Opts = getAnalyzerOptions();
3225  for (auto const &P : *Out)
3226  if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
3227  resetDiagnosticLocationToMainFile(*P.second);
3228 
3229  return Out;
3230 }
3231 
3232 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3233  const CheckerBase *Checker, StringRef Name,
3234  StringRef Category, StringRef Str,
3235  PathDiagnosticLocation Loc,
3236  ArrayRef<SourceRange> Ranges,
3237  ArrayRef<FixItHint> Fixits) {
3238  EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str,
3239  Loc, Ranges, Fixits);
3240 }
3241 
3242 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3243  CheckerNameRef CheckName,
3244  StringRef name, StringRef category,
3245  StringRef str, PathDiagnosticLocation Loc,
3246  ArrayRef<SourceRange> Ranges,
3247  ArrayRef<FixItHint> Fixits) {
3248  // 'BT' is owned by BugReporter.
3249  BugType *BT = getBugTypeForName(CheckName, name, category);
3250  auto R = std::make_unique<BasicBugReport>(*BT, str, Loc);
3251  R->setDeclWithIssue(DeclWithIssue);
3252  for (const auto &SR : Ranges)
3253  R->addRange(SR);
3254  for (const auto &FH : Fixits)
3255  R->addFixItHint(FH);
3256  emitReport(std::move(R));
3257 }
3258 
3259 BugType *BugReporter::getBugTypeForName(CheckerNameRef CheckName,
3260  StringRef name, StringRef category) {
3261  SmallString<136> fullDesc;
3262  llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3263  << ":" << category;
3264  BugType *&BT = StrBugTypes[fullDesc];
3265  if (!BT)
3266  BT = new BugType(CheckName, name, category);
3267  return BT;
3268 }
Indicates that the tracked object is a CF object.
static void removeEdgesToDefaultInitializers(PathPieces &Pieces)
Remove edges in and out of C++ default initializer expressions.
bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const
Returns true if the spelling locations for both SourceLocations are part of the same file buffer...
ProgramStateManager & getStateManager() const
getStateManager - Return the state manager used by the analysis engine.
static void removeRedundantMsgs(PathPieces &path)
An optimization pass over PathPieces that removes redundant diagnostics generated by both ConditionBR...
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
succ_iterator succ_begin()
Definition: CFG.h:956
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:986
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
Stmt - This represents one statement.
Definition: Stmt.h:66
std::unique_ptr< DiagnosticForConsumerMapTy > generatePathDiagnostics(ArrayRef< PathDiagnosticConsumer *> consumers, ArrayRef< PathSensitiveBugReport *> &bugReports)
bugReports A set of bug reports within a single equivalence class
An instance of this object exists for each enum constant that is defined.
Definition: Decl.h:2827
Defines the SourceManager interface.
const CFGBlock * getSrc() const
Definition: ProgramPoint.h:511
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
Represents a point when we begin processing an inlined call.
Definition: ProgramPoint.h:630
StringRef P
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term)
static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD)
Populate executes lines with lines containing at least one diagnostics.
const Stmt * getLoopTarget() const
Definition: CFG.h:1068
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:134
virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex)
Produces the message of the following form: &#39;Msg via Nth parameter&#39;.
void Profile(llvm::FoldingSetNodeID &ID) const
Represents a program point just before an implicit call event.
Definition: ProgramPoint.h:583
static void adjustCallLocations(PathPieces &Pieces, PathDiagnosticLocation *LastCallLocation=nullptr)
Recursively scan through a path and make sure that all call pieces have valid locations.
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
const ProgramStateRef & getState() const
static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS, const SourceManager &SM)
Create a location for the end of the compound statement.
unsigned succ_size() const
Definition: CFG.h:974
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:171
SymbolRef getAsLocSymbol(bool IncludeBaseRegions=false) const
If this SVal is a location and wraps a symbol, return that SymbolRef.
Definition: SVals.cpp:85
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
RangeSelector range(RangeSelector Begin, RangeSelector End)
Selects from the start of Begin and to the end of End.
static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond)
FileID getFileID() const
Defines the Objective-C statement AST node classes.
static PathDiagnosticLocation createDeclEnd(const LocationContext *LC, const SourceManager &SM)
Constructs a location for the end of the enclosing declaration body.
Defines the clang::Expr interface and subclasses for C++ expressions.
static PathDiagnosticLocation createSingleLocation(const PathDiagnosticLocation &PDL)
Convert the given location into a single kind location.
static void removePiecesWithInvalidLocations(PathPieces &Pieces)
Remove all pieces with invalid locations as these cannot be serialized.
BoundNodesTreeBuilder Nodes
static const Stmt * getStmtBeforeCond(const ParentMap &PM, const Stmt *Term, const ExplodedNode *N)
Symbolic value.
Definition: SymExpr.h:29
static PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC, bool allowNestedContexts=false)
static Optional< size_t > getLengthOnSingleLine(const SourceManager &SM, SourceRange Range)
Returns the number of bytes in the given (character-based) SourceRange.
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
const SymExpr * SymbolRef
Definition: SymExpr.h:110
void markInteresting(SymbolRef sym, bugreporter::TrackingKind TKind=bugreporter::TrackingKind::Thorough)
Marks a symbol as interesting.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:274
AnalysisDeclContext contains the context data for the function or method under analysis.
static void removeIdenticalEvents(PathPieces &path)
std::unique_ptr< ExplodedGraph > trim(ArrayRef< const NodeTy *> Nodes, InterExplodedGraphMap *ForwardMap=nullptr, InterExplodedGraphMap *InverseMap=nullptr) const
Creates a trimmed version of the graph that only contains paths leading to the given nodes...
int Category
Definition: Format.cpp:1752
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=None, ArrayRef< FixItHint > Fixits=None)
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
void addPredecessor(ExplodedNode *V, ExplodedGraph &G)
addPredeccessor - Adds a predecessor to the current node, and in tandem add this node as a successor ...
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y)
Return true if X is contained by Y.
static void insertToInterestingnessMap(llvm::DenseMap< T, bugreporter::TrackingKind > &InterestingnessMap, T Val, bugreporter::TrackingKind TKind)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
static bool isJumpToFalseBranch(const BlockEdge *BE)
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
const LocationContext * getLocationContext() const
std::string getMessage(const ExplodedNode *N) override
Search the call expression for the symbol Sym and dispatch the &#39;getMessageForX()&#39; methods to construc...
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2965
Represents a point after we ran remove dead bindings AFTER processing the given statement.
Definition: ProgramPoint.h:489
bool isInteresting(SymbolRef sym) const
static PathDiagnosticLocation createMemberLoc(const MemberExpr *ME, const SourceManager &SM)
For member expressions, return the location of the &#39;.
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:127
NodeId Parent
Definition: ASTDiff.cpp:191
const ExplodedNode *const * const_succ_iterator
const Stmt * getCallSite() const
static SourceLocation getValidSourceLocation(const Stmt *S, LocationOrAnalysisDeclContext LAC, bool UseEndOfStatement=false)
Construct a source location that corresponds to either the beginning or the end of the given statemen...
Represents a single basic block in a source-level CFG.
Definition: CFG.h:576
bool isValid() const
Represents a point when we finish the call exit sequence (for inlined call).
Definition: ProgramPoint.h:688
bool isBodyAutosynthesizedFromModelFile() const
Checks if the body of the Decl is generated by the BodyFarm from a model file.
STATISTIC(MaxBugClassSize, "The maximum number of bug reports in the same equivalence class")
This represents one expression.
Definition: Expr.h:108
Stmt * getTerminatorCondition(bool StripParens=true)
Definition: CFG.cpp:5822
SourceLocation End
pred_iterator pred_end()
const AnnotatedLine * Line
#define V(N, I)
Definition: ASTContext.h:2913
std::vector< std::string > SilencedCheckersAndPackages
Vector of checker/package names which will not emit warnings.
unsigned getLineNumber(bool *Invalid=nullptr) const
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:558
static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C, PathPieces &Path)
Drop the very first edge in a path, which should be a function entry edge.
static std::unique_ptr< PathDiagnostic > generateEmptyDiagnosticForReport(const PathSensitiveBugReport *R, const SourceManager &SM)
void FlushReports()
Generate and flush diagnostics for all bug reports.
const CFGBlock * getDst() const
Definition: ProgramPoint.h:515
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:2610
bool isBodyAutosynthesized() const
Checks if the body of the Decl is generated by the BodyFarm.
SourceLocation getEnd() const
std::map< FileID, std::set< unsigned > > FilesToLineNumsMap
File IDs mapped to sets of line numbers.
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
static void removePopUpNotes(PathPieces &Path)
Same logic as above to remove extra pieces.
const SourceManager & SM
Definition: Format.cpp:1609
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:111
void emitReport(std::unique_ptr< BugReport > R) override
Add the given report to the set of reports tracked by BugReporter.
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:992
constexpr llvm::StringLiteral StrLoopCollectionEmpty
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
static PathDiagnosticEventPiece * eventsDescribeSameCondition(PathDiagnosticEventPiece *X, PathDiagnosticEventPiece *Y)
static void simplifySimpleBranches(PathPieces &pieces)
Move edges from a branch condition to a branch target when the condition is simple.
constexpr llvm::StringLiteral StrLoopRangeEmpty
Encodes a location in the source.
virtual std::unique_ptr< DiagnosticForConsumerMapTy > generateDiagnosticForConsumerMap(BugReport *exampleReport, ArrayRef< PathDiagnosticConsumer *> consumers, ArrayRef< BugReport *> bugReports)
Generate the diagnostics for the given bug report.
Stmt * getLabel()
Definition: CFG.h:1070
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:140
T castAs() const
Convert to the specified ProgramPoint type, asserting that this ProgramPoint is of the desired type...
Definition: ProgramPoint.h:140
const MemRegion * getAsRegion() const
Definition: SVals.cpp:151
static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL)
static void removeContextCycles(PathPieces &Path, const SourceManager &SM)
Eliminate two-edge cycles created by addContextEdges().
static void addContextEdges(PathPieces &pieces, const LocationContext *LC)
Adds synthetic edges from top-level statements to their subexpressions.
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:377
static void removePunyEdges(PathPieces &path, const SourceManager &SM, const ParentMap &PM)
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:75
static PathDiagnosticLocation createEnd(const Stmt *S, const SourceManager &SM, const LocationOrAnalysisDeclContext LAC)
Create a location for the end of the statement.
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
static const Stmt * getEnclosingParent(const Stmt *S, const ParentMap &PM)
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code...
static bool hasImplicitBody(const Decl *D)
Returns true if the given decl has been implicitly given a body, either by the analyzer or by the com...
Optional< bugreporter::TrackingKind > getInterestingnessKind(SymbolRef sym) const
ast_type_traits::DynTypedNode Node
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
const llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
constexpr llvm::StringLiteral StrLoopBodyZero
void clearVisitors()
Remove all visitors attached to this bug report.
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
static void addEdgeToPath(PathPieces &path, PathDiagnosticLocation &PrevLoc, PathDiagnosticLocation NewLoc)
Adds a sanitized control-flow diagnostic edge to a path.
const StackFrameContext * getCalleeContext() const
Definition: ProgramPoint.h:695
void Profile(llvm::FoldingSetNodeID &hash) const override
Reports are uniqued to ensure that we do not emit multiple diagnostics for each bug.
StmtClass getStmtClass() const
Definition: Stmt.h:1087
static std::unique_ptr< PathDiagnostic > generateDiagnosticForBasicReport(const BasicBugReport *R)
Stmt * getTerminatorStmt()
Definition: CFG.h:1051
static const Stmt * getTerminatorCondition(const CFGBlock *B)
A customized wrapper for CFGBlock::getTerminatorCondition() which returns the element for ObjCForColl...
const Decl * getDecl() const
llvm::DenseMap< const ExplodedNode *, const ExplodedNode * > InterExplodedGraphMap
bool isMacroID() const
constexpr llvm::StringLiteral StrEnteringLoop
static bool isLoop(const Stmt *Term)
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Iterator for iterating over Stmt * arrays that contain only T *.
Definition: Stmt.h:1042
static std::unique_ptr< FilesToLineNumsMap > findExecutedLines(const SourceManager &SM, const ExplodedNode *N)
const LocationContext * getLocationContext() const
Definition: ProgramPoint.h:179
void Profile(llvm::FoldingSetNodeID &hash) const override
Profile to identify equivalent bug reports for error report coalescing.
const ExplodedGraph & getGraph() const
getGraph - Get the exploded graph created by the analysis engine for the analyzed method or function...
const StackFrameContext * getStackFrame() const
CharSourceRange getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
Stores options for the analyzer from the command line.
SourceManager & getSourceManager()
Definition: ASTContext.h:675
static std::shared_ptr< PathDiagnosticCallPiece > construct(const CallExitEnd &CE, const SourceManager &SM)
static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path, OptimizedCallsSet &OCS)
static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO, const SourceManager &SM)
Create the location for the operator of the binary expression.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:14579
static bool removeUnneededCalls(const PathDiagnosticConstruct &C, PathPieces &pieces, const PathSensitiveBugReport *R, bool IsInteresting=false)
Recursively scan through a path and prune out calls and macros pieces that aren&#39;t needed...
Defines the clang::SourceLocation class and associated facilities.
const ParentMap & getParentMap() const
pred_iterator pred_begin()
static bool isContainedByStmt(const ParentMap &PM, const Stmt *S, const Stmt *SubS)
static const Stmt * getStmtParent(const Stmt *S, const ParentMap &PM)
void addVisitor(std::unique_ptr< BugReporterVisitor > visitor)
Add custom or predefined bug report visitors to this report.
const MemRegion * getBaseRegion() const
Definition: MemRegion.cpp:1160
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:262
A SourceLocation and its associated SourceManager.
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:945
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:248
Optional< T > getAs() const
Convert to the specified ProgramPoint type, returning None if this ProgramPoint is not of the desired...
Definition: ProgramPoint.h:151
static void CompactMacroExpandedPieces(PathPieces &path, const SourceManager &SM)
CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic object and collapses PathDi...
const Decl * getDeclWithIssue() const override
The smallest declaration that contains the bug location.
SourceLocation getBegin() const
This class handles loading and caching of source files into memory.
SmallVector< std::unique_ptr< BugReporterVisitor >, 8 > VisitorList
Definition: BugReporter.h:291
bool EQ(InterpState &S, CodePtr OpPC)
Definition: Interp.h:219
CFGBlock & getExit()
Definition: CFG.h:1318