BugReporter.cpp revision 8484b37a2b7720c016d27a672343b1c67bd2e731
161f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
261f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//
361f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//                     The LLVM Compiler Infrastructure
461f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//
561f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek// This file is distributed under the University of Illinois Open Source
661f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek// License. See LICENSE.TXT for details.
761f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//
861f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//===----------------------------------------------------------------------===//
961f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//
1061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//  This file defines BugReporter, a utility class for generating
116c07bdba93b095b66e2c8c82dd5ed458fa8285eaTed Kremenek//  PathDiagnostics.
1261f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//
1361f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek//===----------------------------------------------------------------------===//
1461f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
15cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose#define DEBUG_TYPE "BugReporter"
16cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose
179b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
1861f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek#include "clang/AST/ASTContext.h"
19c35fb7d67d515659ad2325b4f6ec97c9fe64fb63Benjamin Kramer#include "clang/AST/DeclObjC.h"
2061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek#include "clang/AST/Expr.h"
2100605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek#include "clang/AST/ParentMap.h"
2216f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Analysis/CFG.h"
2461f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek#include "clang/Analysis/ProgramPoint.h"
2555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h"
2655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
279b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
2855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
29331b0ac44b9eb0ffcba66b4f3f3f9adb27c2434fTed Kremenek#include "llvm/ADT/DenseMap.h"
30802e02463b880f53a6e645bde78cc412481ce9e0Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/OwningPtr.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/STLExtras.h"
3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/SmallString.h"
34cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose#include "llvm/ADT/Statistic.h"
3555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
3610aa55410bbecbb658ce14a5f801d50cf06d12dfTed Kremenek#include <queue>
3761f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
3861f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenekusing namespace clang;
399ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekusing namespace ento;
4061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
41cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan RoseSTATISTIC(MaxBugClassSize,
42cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose          "The maximum number of bug reports in the same equivalence class");
43cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan RoseSTATISTIC(MaxValidBugClassSize,
44cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose          "The maximum number of bug reports in the same equivalence class "
45cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose          "where at least one report is valid (not suppressed)");
46cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose
478966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed KremenekBugReporterVisitor::~BugReporterVisitor() {}
481b431023814196f87515a540ebcb9e9f1a9176a1Ted Kremenek
4999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid BugReporterContext::anchor() {}
5099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
51cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek//===----------------------------------------------------------------------===//
523106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// Helper routines for walking the ExplodedGraph and fetching statements.
53cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek//===----------------------------------------------------------------------===//
5461f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
559c378f705405d37f49795d5e915989de774fe11fTed Kremenekstatic const Stmt *GetPreviousStmt(const ExplodedNode *N) {
560f8579274a010f360a371b53101859d9d6052314Anna Zaks  for (N = N->getFirstPred(); N; N = N->getFirstPred())
570f8579274a010f360a371b53101859d9d6052314Anna Zaks    if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
58b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek      return S;
591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek  return 0;
61b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek}
62b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek
635f85e17df3f5b0a8021443f2b590daecfb2cbd17Ted Kremenekstatic inline const Stmt*
649c378f705405d37f49795d5e915989de774fe11fTed KremenekGetCurrentOrPreviousStmt(const ExplodedNode *N) {
650f8579274a010f360a371b53101859d9d6052314Anna Zaks  if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
66b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek    return S;
671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek  return GetPreviousStmt(N);
69b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek}
701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek//===----------------------------------------------------------------------===//
72c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek// Diagnostic cleanup.
73c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek//===----------------------------------------------------------------------===//
74c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
75b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenekstatic PathDiagnosticEventPiece *
76b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted KremenekeventsDescribeSameCondition(PathDiagnosticEventPiece *X,
77b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek                            PathDiagnosticEventPiece *Y) {
78b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  // Prefer diagnostics that come from ConditionBRVisitor over
79b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  // those that came from TrackConstraintBRVisitor.
80b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  const void *tagPreferred = ConditionBRVisitor::getTag();
81b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  const void *tagLesser = TrackConstraintBRVisitor::getTag();
82b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
83b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  if (X->getLocation() != Y->getLocation())
84b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    return 0;
85b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
86b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
87b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    return X;
88b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
89b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
90b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    return Y;
91b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
92b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  return 0;
93b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek}
94b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
953800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek/// An optimization pass over PathPieces that removes redundant diagnostics
963800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek/// generated by both ConditionBRVisitor and TrackConstraintBRVisitor.  Both
973800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek/// BugReporterVisitors use different methods to generate diagnostics, with
983800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek/// one capable of emitting diagnostics in some cases but not in others.  This
993800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek/// can lead to redundant diagnostic pieces at the same point in a path.
1003800165e56107df7430520aa98afdf7065db2dd2Ted Kremenekstatic void removeRedundantMsgs(PathPieces &path) {
101b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  unsigned N = path.size();
102b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  if (N < 2)
103b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    return;
1043800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek  // NOTE: this loop intentionally is not using an iterator.  Instead, we
1053800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek  // are streaming the path and modifying it in place.  This is done by
1063800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek  // grabbing the front, processing it, and if we decide to keep it append
1073800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek  // it to the end of the path.  The entire path is processed in this way.
108b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  for (unsigned i = 0; i < N; ++i) {
109b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    IntrusiveRefCntPtr<PathDiagnosticPiece> piece(path.front());
110b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    path.pop_front();
111b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
112b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    switch (piece->getKind()) {
113b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek      case clang::ento::PathDiagnosticPiece::Call:
1143800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek        removeRedundantMsgs(cast<PathDiagnosticCallPiece>(piece)->path);
115b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        break;
116b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek      case clang::ento::PathDiagnosticPiece::Macro:
1173800165e56107df7430520aa98afdf7065db2dd2Ted Kremenek        removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(piece)->subPieces);
118b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        break;
119b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek      case clang::ento::PathDiagnosticPiece::ControlFlow:
120b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        break;
121b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek      case clang::ento::PathDiagnosticPiece::Event: {
122b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        if (i == N-1)
123b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          break;
124b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
125b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        if (PathDiagnosticEventPiece *nextEvent =
126ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek            dyn_cast<PathDiagnosticEventPiece>(path.front().getPtr())) {
127b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          PathDiagnosticEventPiece *event =
128b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek            cast<PathDiagnosticEventPiece>(piece);
129b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          // Check to see if we should keep one of the two pieces.  If we
130b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          // come up with a preference, record which piece to keep, and consume
131b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          // another piece from the path.
132b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          if (PathDiagnosticEventPiece *pieceToKeep =
133b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek              eventsDescribeSameCondition(event, nextEvent)) {
134b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek            piece = pieceToKeep;
135b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek            path.pop_front();
136b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek            ++i;
137b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek          }
138b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        }
139b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek        break;
140b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek      }
141b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    }
142b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    path.push_back(piece);
143b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek  }
144b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek}
145b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
146d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek/// A map from PathDiagnosticPiece to the LocationContext of the inlined
147d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek/// function call it represents.
148bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenektypedef llvm::DenseMap<const PathPieces *, const LocationContext *>
149d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        LocationContextMap;
150d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek
151c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek/// Recursively scan through a path and prune out calls and macros pieces
152c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek/// that aren't needed.  Return true if afterwards the path contains
153afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose/// "interesting stuff" which means it shouldn't be pruned from the parent path.
154d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenekstatic bool removeUnneededCalls(PathPieces &pieces, BugReport *R,
155d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek                                LocationContextMap &LCM) {
156c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek  bool containsSomethingInteresting = false;
157c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek  const unsigned N = pieces.size();
158c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
159c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek  for (unsigned i = 0 ; i < N ; ++i) {
160c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    // Remove the front piece from the path.  If it is still something we
161c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    // want to keep once we are done, we will push it back on the end.
162c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
163c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    pieces.pop_front();
164c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
165afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    // Throw away pieces with invalid locations. Note that we can't throw away
166afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    // calls just yet because they might have something interesting inside them.
167afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    // If so, their locations will be adjusted as necessary later.
168a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek    if (piece->getKind() != PathDiagnosticPiece::Call &&
169a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek        piece->getLocation().asLocation().isInvalid())
170a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek      continue;
171a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek
172725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek    switch (piece->getKind()) {
173725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      case PathDiagnosticPiece::Call: {
174725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
17580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // Check if the location context is interesting.
176bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek        assert(LCM.count(&call->path));
177bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek        if (R->isInteresting(LCM[&call->path])) {
17880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          containsSomethingInteresting = true;
17980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          break;
18080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        }
181368f3b070e8cb657a65bfa443d60256676d269e7Jordan Rose
182d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        if (!removeUnneededCalls(call->path, R, LCM))
183368f3b070e8cb657a65bfa443d60256676d269e7Jordan Rose          continue;
184a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek
185725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        containsSomethingInteresting = true;
186725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        break;
187725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      }
188725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      case PathDiagnosticPiece::Macro: {
189725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
190d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        if (!removeUnneededCalls(macro->subPieces, R, LCM))
191725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek          continue;
192c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek        containsSomethingInteresting = true;
193725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        break;
194725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      }
195725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      case PathDiagnosticPiece::Event: {
196725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
197a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek
198725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        // We never throw away an event, but we do throw it away wholesale
199725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        // as part of a path if we throw the entire path away.
20022505ef15e32db31a4f834a387cf73a913bc8f66Ted Kremenek        containsSomethingInteresting |= !event->isPrunable();
201725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        break;
202725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      }
203725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek      case PathDiagnosticPiece::ControlFlow:
204725167443808efdc39a99f4eb132a0ae64ac5118Ted Kremenek        break;
205c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    }
206c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
207c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek    pieces.push_back(piece);
208c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek  }
209c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
210c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek  return containsSomethingInteresting;
211c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek}
212c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek
213afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose/// Recursively scan through a path and make sure that all call pieces have
214afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose/// valid locations. Note that all other pieces with invalid locations should
215afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose/// have already been pruned out.
216afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rosestatic void adjustCallLocations(PathPieces &Pieces,
217afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose                                PathDiagnosticLocation *LastCallLocation = 0) {
218afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose  for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E; ++I) {
219ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek    PathDiagnosticCallPiece *Call = dyn_cast<PathDiagnosticCallPiece>(*I);
220afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
221afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    if (!Call) {
222ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek      assert((*I)->getLocation().asLocation().isValid());
223afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose      continue;
224afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    }
225afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
226afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    if (LastCallLocation) {
227187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose      if (!Call->callEnter.asLocation().isValid() ||
228187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose          Call->getCaller()->isImplicit())
229afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose        Call->callEnter = *LastCallLocation;
230187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose      if (!Call->callReturn.asLocation().isValid() ||
231187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose          Call->getCaller()->isImplicit())
232afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose        Call->callReturn = *LastCallLocation;
233afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    }
234afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
235afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    // Recursively clean out the subclass.  Keep this call around if
236afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    // it contains any informative diagnostics.
237afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    PathDiagnosticLocation *ThisCallLocation;
238187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose    if (Call->callEnterWithin.asLocation().isValid() &&
239187f8bd88bfc92cf3fea62b7d8db5f92edce410aJordan Rose        !Call->getCallee()->isImplicit())
240afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose      ThisCallLocation = &Call->callEnterWithin;
241afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    else
242afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose      ThisCallLocation = &Call->callEnter;
243afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
244afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    assert(ThisCallLocation && "Outermost call has an invalid location");
245afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose    adjustCallLocations(Call->path, ThisCallLocation);
246afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose  }
247afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose}
248afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
249c89f4b05721f53cfbaf32fc0c4919a4616e68440Ted Kremenek//===----------------------------------------------------------------------===//
2503106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// PathDiagnosticBuilder and its associated routines and helper objects.
251b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek//===----------------------------------------------------------------------===//
252b479dadea93b93d3f0a335c3518b703f140b3f50Ted Kremenek
253babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremeneknamespace {
254ba5fb5a955c896815c439289fc51c03cf0635129Kovarththanan Rajaratnamclass NodeMapClosure : public BugReport::NodeResolver {
255c9963132736782d0c9178c744b3e2307cfb98a08Jordan Rose  InterExplodedGraphMap &M;
2567dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenekpublic:
257c9963132736782d0c9178c744b3e2307cfb98a08Jordan Rose  NodeMapClosure(InterExplodedGraphMap &m) : M(m) {}
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2599c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
260c9963132736782d0c9178c744b3e2307cfb98a08Jordan Rose    return M.lookup(N);
2617dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek  }
2627dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek};
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264ba5fb5a955c896815c439289fc51c03cf0635129Kovarththanan Rajaratnamclass PathDiagnosticBuilder : public BugReporterContext {
2657dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek  BugReport *R;
266ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  PathDiagnosticConsumer *PDC;
2677dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek  NodeMapClosure NMC;
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
26959950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek  const LocationContext *LC;
27059950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek
2718966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek  PathDiagnosticBuilder(GRBugReporter &br,
272c9963132736782d0c9178c744b3e2307cfb98a08Jordan Rose                        BugReport *r, InterExplodedGraphMap &Backmap,
273ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie                        PathDiagnosticConsumer *pdc)
2748966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek    : BugReporterContext(br),
27559950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek      R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
27659950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek  {}
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2789c378f705405d37f49795d5e915989de774fe11fTed Kremenek  PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2809c378f705405d37f49795d5e915989de774fe11fTed Kremenek  PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
2819c378f705405d37f49795d5e915989de774fe11fTed Kremenek                                            const ExplodedNode *N);
2821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2838e6431adab313e283a992698f6fc7afe62420999Anna Zaks  BugReport *getBugReport() { return R; }
2848e6431adab313e283a992698f6fc7afe62420999Anna Zaks
285212f6d3b5fb3fa55ba1e40671cfc336430abc8ddTom Care  Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
28659950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek
28759950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek  ParentMap& getParentMap() { return LC->getParentMap(); }
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289c3f83ad7adaff2623986aa1c3e57833babd03d50Ted Kremenek  const Stmt *getParent(const Stmt *S) {
290c3f83ad7adaff2623986aa1c3e57833babd03d50Ted Kremenek    return getParentMap().getParent(S);
291c3f83ad7adaff2623986aa1c3e57833babd03d50Ted Kremenek  }
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2938966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek  virtual NodeMapClosure& getNodeResolver() { return NMC; }
2947297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor
295d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek  PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
297ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
298ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie    return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
2997dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek  }
3007dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek
301babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek  bool supportsLogicalOpControlFlow() const {
302babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek    return PDC ? PDC->supportsLogicalOpControlFlow() : true;
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
304babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek};
305babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek} // end anonymous namespace
306babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek
30700605e0f011ed99225a22026344c5fc1b3212cd7Ted KremenekPathDiagnosticLocation
3089c378f705405d37f49795d5e915989de774fe11fTed KremenekPathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
3090f8579274a010f360a371b53101859d9d6052314Anna Zaks  if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N))
31059950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek    return PathDiagnosticLocation(S, getSourceManager(), LC);
31100605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek
3120cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks  return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
3130cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks                                               getSourceManager());
314082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek}
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31600605e0f011ed99225a22026344c5fc1b3212cd7Ted KremenekPathDiagnosticLocation
3179c378f705405d37f49795d5e915989de774fe11fTed KremenekPathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
3189c378f705405d37f49795d5e915989de774fe11fTed Kremenek                                          const ExplodedNode *N) {
319babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek
320143ca222583a4a355fdc89af852deef287499300Ted Kremenek  // Slow, but probably doesn't matter.
321b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek  if (os.str().empty())
322b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek    os << ' ';
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32400605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek  const PathDiagnosticLocation &Loc = ExecutionContinues(N);
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32600605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek  if (Loc.asStmt())
327b697b100f2354ac84b7a0785cab9481e8bfdcf23Ted Kremenek    os << "Execution continues on line "
328642116259e8df6286063a17361c20e95b5017a0aChandler Carruth       << getSourceManager().getExpansionLineNumber(Loc.asLocation())
3298966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek       << '.';
3304f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek  else {
3314f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    os << "Execution jumps to the end of the ";
3324f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    const Decl *D = N->getLocationContext()->getDecl();
3334f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    if (isa<ObjCMethodDecl>(D))
3344f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek      os << "method";
3354f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    else if (isa<FunctionDecl>(D))
3364f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek      os << "function";
3374f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    else {
3384f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek      assert(isa<BlockDecl>(D));
3394f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek      os << "anonymous block";
3404f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    }
3414f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek    os << '.';
3424f1db537722e8d46146cd95f5962a6dcf6e44e54Ted Kremenek  }
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
344082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek  return Loc;
345143ca222583a4a355fdc89af852deef287499300Ted Kremenek}
346143ca222583a4a355fdc89af852deef287499300Ted Kremenek
347ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenekstatic bool IsNested(const Stmt *S, ParentMap &PM) {
348ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek  if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
349ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek    return true;
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
351ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek  const Stmt *Parent = PM.getParentIgnoreParens(S);
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
353ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek  if (Parent)
354ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek    switch (Parent->getStmtClass()) {
355ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek      case Stmt::ForStmtClass:
356ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek      case Stmt::DoStmtClass:
357ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek      case Stmt::WhileStmtClass:
358ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek        return true;
359ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek      default:
360ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek        break;
361ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek    }
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
364ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek}
365ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek
366d8c938b0f6b7a7156181be575239e4c6e15a2adbTed KremenekPathDiagnosticLocation
367d8c938b0f6b7a7156181be575239e4c6e15a2adbTed KremenekPathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
3689c378f705405d37f49795d5e915989de774fe11fTed Kremenek  assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ParentMap &P = getParentMap();
3708966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek  SourceManager &SMgr = getSourceManager();
371e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek
372ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek  while (IsNested(S, P)) {
3738c8b0ad9601d6ccf3d7b2a3f77a896ef4fb4e6e9Ted Kremenek    const Stmt *Parent = P.getParentIgnoreParens(S);
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
375af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek    if (!Parent)
376af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      break;
3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
378af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek    switch (Parent->getStmtClass()) {
3795fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek      case Stmt::BinaryOperatorClass: {
3805fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek        const BinaryOperator *B = cast<BinaryOperator>(Parent);
3815fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek        if (B->isLogicalOp())
382220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
3835fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek        break;
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
385af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::CompoundStmtClass:
386af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::StmtExprClass:
387220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks        return PathDiagnosticLocation(S, SMgr, LC);
3881d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek      case Stmt::ChooseExprClass:
3891d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        // Similar to '?' if we are referring to condition, just have the edge
3901d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        // point to the entire choose expression.
3911d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        if (cast<ChooseExpr>(Parent)->getCond() == S)
392220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(Parent, SMgr, LC);
3931d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        else
394220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
39556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      case Stmt::BinaryConditionalOperatorClass:
3961d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek      case Stmt::ConditionalOperatorClass:
3971d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        // For '?', if we are referring to condition, just have the edge point
3981d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        // to the entire '?' expression.
39956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
400220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(Parent, SMgr, LC);
4011d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek        else
402220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
403af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::DoStmtClass:
404220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
405af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::ForStmtClass:
406af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        if (cast<ForStmt>(Parent)->getBody() == S)
407220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        break;
409af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::IfStmtClass:
410af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        if (cast<IfStmt>(Parent)->getCond() != S)
411220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
4128bd4d037d837e7922c3661d6158229da58a03887Ted Kremenek        break;
413af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::ObjCForCollectionStmtClass:
414af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
415220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
416af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        break;
417af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      case Stmt::WhileStmtClass:
418af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        if (cast<WhileStmt>(Parent)->getCond() != S)
419220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(S, SMgr, LC);
420af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        break;
421af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek      default:
422af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek        break;
423af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek    }
424af3e3d54e990b385e5a653d2994d7d41427a13b8Ted Kremenek
425d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek    S = Parent;
426d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek  }
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
428d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek  assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
429e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek
430e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  // Special case: DeclStmts can appear in for statement declarations, in which
431e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  //  case the ForStmt is the context.
432e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  if (isa<DeclStmt>(S)) {
433e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    if (const Stmt *Parent = P.getParent(S)) {
434e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek      switch (Parent->getStmtClass()) {
435e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek        case Stmt::ForStmtClass:
436e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek        case Stmt::ObjCForCollectionStmtClass:
437220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks          return PathDiagnosticLocation(Parent, SMgr, LC);
438e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek        default:
439e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek          break;
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
442e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  }
443e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  else if (isa<BinaryOperator>(S)) {
444e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    // Special case: the binary operator represents the initialization
445e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    // code in a for statement (this can happen when the variable being
446e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    // initialized is an old variable.
447e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    if (const ForStmt *FS =
448e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek          dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
449e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek      if (FS->getInit() == S)
450220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks        return PathDiagnosticLocation(FS, SMgr, LC);
451e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek    }
452e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek  }
453e88a170da9219ef6a62d2182560c4de2ebffbd59Ted Kremenek
454220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks  return PathDiagnosticLocation(S, SMgr, LC);
455d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek}
456d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek
457cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek//===----------------------------------------------------------------------===//
458d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose// "Visitors only" path diagnostic generation algorithm.
459d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose//===----------------------------------------------------------------------===//
460d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rosestatic bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
461d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose                                               PathDiagnosticBuilder &PDB,
462d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose                                               const ExplodedNode *N,
463d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose                                      ArrayRef<BugReporterVisitor *> visitors) {
464d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  // All path generation skips the very first node (the error node).
465d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  // This is because there is special handling for the end-of-path note.
466d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  N = N->getFirstPred();
467d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  if (!N)
468d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose    return true;
469d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose
470d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  BugReport *R = PDB.getBugReport();
471d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  while (const ExplodedNode *Pred = N->getFirstPred()) {
472d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose    for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
473d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose                                                  E = visitors.end();
474d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose         I != E; ++I) {
475d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose      // Visit all the node pairs, but throw the path pieces away.
476d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose      PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
477d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose      delete Piece;
478d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose    }
479d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose
480d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose    N = Pred;
481d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  }
482d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose
483d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  return R->isValid();
484d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose}
485d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose
486d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose//===----------------------------------------------------------------------===//
4873106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// "Minimal" path diagnostic generation algorithm.
488cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek//===----------------------------------------------------------------------===//
48956a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zakstypedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
49056a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zakstypedef SmallVector<StackDiagPair, 6> StackDiagVector;
49156a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks
492368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaksstatic void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
49356a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks                                         StackDiagVector &CallStack) {
494368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks  // If the piece contains a special message, add it to all the call
495368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks  // pieces on the active stack.
496368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks  if (PathDiagnosticEventPiece *ep =
497368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        dyn_cast<PathDiagnosticEventPiece>(P)) {
498368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks
49956a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks    if (ep->hasCallStackHint())
50056a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks      for (StackDiagVector::iterator I = CallStack.begin(),
50156a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks                                     E = CallStack.end(); I != E; ++I) {
50256a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks        PathDiagnosticCallPiece *CP = I->first;
50356a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks        const ExplodedNode *N = I->second;
5048fe4525680ce72e90cee3e58b5654e3ae955447fNAKAMURA Takumi        std::string stackMsg = ep->getCallStackMessage(N);
50556a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks
506368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        // The last message on the path to final bug is the most important
507368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        // one. Since we traverse the path backwards, do not add the message
508368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        // if one has been previously added.
50956a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks        if  (!CP->hasCallStackMessage())
51056a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks          CP->setCallStackMessage(stackMsg);
51156a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks      }
512368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks  }
513368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks}
514cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek
51577d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenekstatic void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
51614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
5178347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rosestatic bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
5183106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek                                          PathDiagnosticBuilder &PDB,
5193bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                          const ExplodedNode *N,
520d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek                                          LocationContextMap &LCM,
5213bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                      ArrayRef<BugReporterVisitor *> visitors) {
5228966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek
5233106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  SourceManager& SMgr = PDB.getSourceManager();
52459950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek  const LocationContext *LC = PDB.LC;
5259c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const ExplodedNode *NextNode = N->pred_empty()
5263106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek                                        ? NULL : *(N->pred_begin());
527368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks
52856a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks  StackDiagVector CallStack;
529368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks
5303106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  while (NextNode) {
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    N = NextNode;
53259950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek    PDB.LC = N->getLocationContext();
5330f8579274a010f360a371b53101859d9d6052314Anna Zaks    NextNode = N->getFirstPred();
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53561f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek    ProgramPoint P = N->getLocation();
53680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
53780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks    do {
5387a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
53980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PathDiagnosticCallPiece *C =
54080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PathDiagnosticCallPiece::construct(N, *CE, SMgr);
541d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        // Record the mapping from call piece to LocationContext.
542bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek        LCM[&C->path] = CE->getCalleeContext();
54380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PD.getActivePath().push_front(C);
54480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PD.pushActivePath(&C->path);
54580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        CallStack.push_back(StackDiagPair(C, N));
54680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        break;
5479373937945e1e075dfa08169eaccc1ad0b31f699Anna Zaks      }
548183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose
5497a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
55080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // Flush all locations, and pop the active path.
55180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        bool VisitedEntireCall = PD.isWithinCall();
55280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PD.popActivePath();
55380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
55480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // Either we just added a bunch of stuff to the top-level path, or
55580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // we have a previous CallExitEnd.  If the former, it means that the
55680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // path terminated within a function call.  We must then take the
55780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // current contents of the active path and place it within
55880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // a new PathDiagnosticCallPiece.
55980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PathDiagnosticCallPiece *C;
56080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        if (VisitedEntireCall) {
56180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
56280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        } else {
56380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          const Decl *Caller = CE->getLocationContext()->getDecl();
56480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
565d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek          // Record the mapping from call piece to LocationContext.
566bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek          LCM[&C->path] = CE->getCalleeContext();
56780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        }
56880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
56980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        C->setCallee(*CE, SMgr);
57080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        if (!CallStack.empty()) {
57180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          assert(CallStack.back().first == C);
57280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          CallStack.pop_back();
57380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        }
57480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        break;
575368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks      }
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5777a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
57880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        const CFGBlock *Src = BE->getSrc();
57980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        const CFGBlock *Dst = BE->getDst();
58080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        const Stmt *T = Src->getTerminator();
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        if (!T)
58380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          break;
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        PathDiagnosticLocation Start =
58680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PathDiagnosticLocation::createBegin(T, SMgr,
58780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                N->getLocationContext());
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        switch (T->getStmtClass()) {
59061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek        default:
59161f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          break;
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59361f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek        case Stmt::GotoStmtClass:
5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        case Stmt::IndirectGotoStmtClass: {
5950f8579274a010f360a371b53101859d9d6052314Anna Zaks          const Stmt *S = PathDiagnosticLocation::getNextStmt(N);
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59761f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          if (!S)
59880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            break;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
600297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          std::string sbuf;
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          llvm::raw_string_ostream os(sbuf);
602d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek          const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60400605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek          os << "Control jumps to line "
60580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              << End.asLocation().getExpansionLineNumber();
60680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
60780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              Start, End, os.str()));
60861f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          break;
60961f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek        }
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        case Stmt::SwitchStmtClass: {
61261f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          // Figure out what case arm we took.
613297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          std::string sbuf;
614297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          llvm::raw_string_ostream os(sbuf);
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6169c378f705405d37f49795d5e915989de774fe11fTed Kremenek          if (const Stmt *S = Dst->getLabel()) {
617220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks            PathDiagnosticLocation End(S, SMgr, LC);
6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6195a429954aedd8a830a70fcea2cb51561673e4fc3Ted Kremenek            switch (S->getStmtClass()) {
62080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            default:
62180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              os << "No cases match in the switch statement. "
62280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              "Control jumps to line "
62380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              << End.asLocation().getExpansionLineNumber();
62480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              break;
62580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            case Stmt::DefaultStmtClass:
62680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              os << "Control jumps to the 'default' case at line "
62780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              << End.asLocation().getExpansionLineNumber();
62880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              break;
62980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
63080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            case Stmt::CaseStmtClass: {
63180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              os << "Control jumps to 'case ";
63280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              const CaseStmt *Case = cast<CaseStmt>(S);
63380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
63480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
63580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              // Determine if it is an enum.
63680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              bool GetRawInt = true;
63780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
63880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
63980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                // FIXME: Maybe this should be an assertion.  Are there cases
64080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                // were it is not an EnumConstantDecl?
64180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                const EnumConstantDecl *D =
64203509aea098772644bf4662dc1c88634818ceeccZhongxing Xu                    dyn_cast<EnumConstantDecl>(DR->getDecl());
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                if (D) {
64580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  GetRawInt = false;
64680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  os << *D;
6475a429954aedd8a830a70fcea2cb51561673e4fc3Ted Kremenek                }
64880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              }
6499ec64d6e2fe575b297e1eaa5051efc2983373e25Eli Friedman
65080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              if (GetRawInt)
65180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
6529ec64d6e2fe575b297e1eaa5051efc2983373e25Eli Friedman
65380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              os << ":'  at line "
65480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  << End.asLocation().getExpansionLineNumber();
65580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              break;
65661f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek            }
65780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            }
65880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
65980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, os.str()));
66061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          }
661567839273dfc395a459cbe77d5f8d128b9ae85bfTed Kremenek          else {
662c3517ebb48a51e146badc08bc3684520c41da6c8Ted Kremenek            os << "'Default' branch taken. ";
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
66480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
66580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, os.str()));
666567839273dfc395a459cbe77d5f8d128b9ae85bfTed Kremenek          }
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66861f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek          break;
66961f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek        }
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6712673c9ff76ebdb72b7f846e2ff1e0f4256372e19Ted Kremenek        case Stmt::BreakStmtClass:
6722673c9ff76ebdb72b7f846e2ff1e0f4256372e19Ted Kremenek        case Stmt::ContinueStmtClass: {
673297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          std::string sbuf;
674297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          llvm::raw_string_ostream os(sbuf);
67500605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek          PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
67680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
67780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              Start, End, os.str()));
6782673c9ff76ebdb72b7f846e2ff1e0f4256372e19Ted Kremenek          break;
6792673c9ff76ebdb72b7f846e2ff1e0f4256372e19Ted Kremenek        }
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // Determine control-flow for ternary '?'.
68256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        case Stmt::BinaryConditionalOperatorClass:
683706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek        case Stmt::ConditionalOperatorClass: {
684297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          std::string sbuf;
685297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek          llvm::raw_string_ostream os(sbuf);
6861d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek          os << "'?' condition is ";
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
688706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek          if (*(Src->succ_begin()+1) == Dst)
689082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek            os << "false";
690706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek          else
691082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek            os << "true";
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69300605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek          PathDiagnosticLocation End = PDB.ExecutionContinues(N);
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6951d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek          if (const Stmt *S = End.asStmt())
6961d9a23ac34fee297e28361653a72c519cb04a854Ted Kremenek            End = PDB.getEnclosingStmtLocation(S);
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks          PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
69980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              Start, End, os.str()));
700babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          break;
701babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek        }
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        // Determine control-flow for short-circuited '&&' and '||'.
704babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek        case Stmt::BinaryOperatorClass: {
705babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          if (!PDB.supportsLogicalOpControlFlow())
706babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek            break;
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70803509aea098772644bf4662dc1c88634818ceeccZhongxing Xu          const BinaryOperator *B = cast<BinaryOperator>(T);
709babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          std::string sbuf;
710babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          llvm::raw_string_ostream os(sbuf);
711babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          os << "Left side of '";
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          if (B->getOpcode() == BO_LAnd) {
714f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            os << "&&" << "' is ";
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
716f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            if (*(Src->succ_begin()+1) == Dst) {
717f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              os << "false";
718220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks              PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
7190cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks              PathDiagnosticLocation Start =
72080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  PathDiagnosticLocation::createOperatorLoc(B, SMgr);
72180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
72280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  Start, End, os.str()));
7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            }
724f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            else {
725f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              os << "true";
726220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks              PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
727f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              PathDiagnosticLocation End = PDB.ExecutionContinues(N);
72880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
72980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  Start, End, os.str()));
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            }
731babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          }
732babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          else {
7332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall            assert(B->getOpcode() == BO_LOr);
734f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            os << "||" << "' is ";
7351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
736f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            if (*(Src->succ_begin()+1) == Dst) {
737f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              os << "false";
738220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks              PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
739f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              PathDiagnosticLocation End = PDB.ExecutionContinues(N);
74080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
74180de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  Start, End, os.str()));
742f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            }
743f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            else {
744f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek              os << "true";
745220ac8c175cb1bf9b18d82eefe036995d7a2164dAnna Zaks              PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
7460cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks              PathDiagnosticLocation Start =
74780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  PathDiagnosticLocation::createOperatorLoc(B, SMgr);
74880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks              PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
74980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                  Start, End, os.str()));
750f5ab8e6eb4b1c14cb3a0f69aad526192794c95ceTed Kremenek            }
751babdd7b56d02e2b6924a2c93b061d6a48bb5f0caTed Kremenek          }
7521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
753706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek          break;
754706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek        }
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        case Stmt::DoStmtClass:  {
757706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek          if (*(Src->succ_begin()) == Dst) {
758297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek            std::string sbuf;
759297308eda369de400e6ea7057dffeadd9f92e385Ted Kremenek            llvm::raw_string_ostream os(sbuf);
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
761c3517ebb48a51e146badc08bc3684520c41da6c8Ted Kremenek            os << "Loop condition is true. ";
762d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
764d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek            if (const Stmt *S = End.asStmt())
765d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek              End = PDB.getEnclosingStmtLocation(S);
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
76880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, os.str()));
769082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek          }
770082cb8d7bd9bdb3fe58e8e1a2897c79c4ebcc3a7Ted Kremenek          else {
77100605e0f011ed99225a22026344c5fc1b3212cd7Ted Kremenek            PathDiagnosticLocation End = PDB.ExecutionContinues(N);
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
773d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek            if (const Stmt *S = End.asStmt())
774d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek              End = PDB.getEnclosingStmtLocation(S);
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
77780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, "Loop condition is false.  Exiting loop"));
7783106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          }
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7803106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          break;
7813106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        }
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7833106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        case Stmt::WhileStmtClass:
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        case Stmt::ForStmtClass: {
7853106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          if (*(Src->succ_begin()+1) == Dst) {
7863106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            std::string sbuf;
7873106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            llvm::raw_string_ostream os(sbuf);
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7893106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            os << "Loop condition is false. ";
7903106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
7913106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            if (const Stmt *S = End.asStmt())
7923106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek              End = PDB.getEnclosingStmtLocation(S);
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
79580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, os.str()));
7963106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          }
7973106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          else {
7983106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            PathDiagnosticLocation End = PDB.ExecutionContinues(N);
7993106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            if (const Stmt *S = End.asStmt())
8003106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek              End = PDB.getEnclosingStmtLocation(S);
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80280de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
80380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, "Loop condition is true.  Entering loop body"));
8043106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          }
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8063106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          break;
8073106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        }
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8093106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        case Stmt::IfStmtClass: {
8103106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          PathDiagnosticLocation End = PDB.ExecutionContinues(N);
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8123106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          if (const Stmt *S = End.asStmt())
8133106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek            End = PDB.getEnclosingStmtLocation(S);
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8153106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          if (*(Src->succ_begin()+1) == Dst)
81680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
81780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, "Taking false branch"));
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          else
81980de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks            PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
82080de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks                Start, End, "Taking true branch"));
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8223106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          break;
8233106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        }
82480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks        }
8253106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      }
82680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks    } while(0);
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
828dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek    if (NextNode) {
8298e6431adab313e283a992698f6fc7afe62420999Anna Zaks      // Add diagnostic pieces from custom visitors.
8308e6431adab313e283a992698f6fc7afe62420999Anna Zaks      BugReport *R = PDB.getBugReport();
8313bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose      for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
8323bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                                    E = visitors.end();
8333bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose           I != E; ++I) {
834368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
8352042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek          PD.getActivePath().push_front(p);
836368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks          updateStackPiecesWithMessage(p, CallStack);
837368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        }
838dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek      }
8398966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek    }
8403106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  }
8411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8428347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose  if (!PDB.getBugReport()->isValid())
8438347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose    return false;
8448347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose
84514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  // After constructing the full PathDiagnostic, do a pass over it to compact
84614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  // PathDiagnosticPieces that occur within a macro.
84777d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek  CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
8488347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose  return true;
8493106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
8503106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
8513106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
8525fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek// "Extensive" PathDiagnostic generation.
8535fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek//===----------------------------------------------------------------------===//
8545fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek
8555fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenekstatic bool IsControlFlowExpr(const Stmt *S) {
8565fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek  const Expr *E = dyn_cast<Expr>(S);
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8585fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek  if (!E)
8595fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek    return false;
8601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  E = E->IgnoreParenCasts();
8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (isa<AbstractConditionalOperator>(E))
8645fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek    return true;
8651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8665fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek  if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
8675fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek    if (B->isLogicalOp())
8685fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek      return true;
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
8715fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek}
8725fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek
87314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremeneknamespace {
874ba5fb5a955c896815c439289fc51c03cf0635129Kovarththanan Rajaratnamclass ContextLocation : public PathDiagnosticLocation {
8758f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  bool IsDead;
8768f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenekpublic:
8778f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
8788f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek    : PathDiagnosticLocation(L), IsDead(isdead) {}
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void markDead() { IsDead = true; }
8818f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  bool isDead() const { return IsDead; }
8828f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek};
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
884e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenekstatic PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
885e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek                                              const LocationContext *LC,
886e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek                                              bool firstCharOnly = false) {
887e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  if (const Stmt *S = L.asStmt()) {
888e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek    const Stmt *Original = S;
889e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek    while (1) {
890e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      // Adjust the location for some expressions that are best referenced
891e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      // by one of their subexpressions.
892e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      switch (S->getStmtClass()) {
893e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        default:
894e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          break;
895e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::ParenExprClass:
896e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::GenericSelectionExprClass:
897e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          S = cast<Expr>(S)->IgnoreParens();
898e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          firstCharOnly = true;
899e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          continue;
900e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::BinaryConditionalOperatorClass:
901e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::ConditionalOperatorClass:
902e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          S = cast<AbstractConditionalOperator>(S)->getCond();
903e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          firstCharOnly = true;
904e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          continue;
905e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::ChooseExprClass:
906e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          S = cast<ChooseExpr>(S)->getCond();
907e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          firstCharOnly = true;
908e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          continue;
909e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek        case Stmt::BinaryOperatorClass:
910e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          S = cast<BinaryOperator>(S)->getLHS();
911e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          firstCharOnly = true;
912e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek          continue;
913e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      }
914e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek
915e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      break;
916e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek    }
917e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek
918e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek    if (S != Original)
919e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      L = PathDiagnosticLocation(S, L.getManager(), LC);
920e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  }
921e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek
922e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  if (firstCharOnly)
923e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek    L  = PathDiagnosticLocation::createSingleLocation(L);
924e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek
925e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  return L;
926e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek}
927e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek
928ba5fb5a955c896815c439289fc51c03cf0635129Kovarththanan Rajaratnamclass EdgeBuilder {
9298f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  std::vector<ContextLocation> CLocs;
9308f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  typedef std::vector<ContextLocation>::iterator iterator;
93114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  PathDiagnostic &PD;
93214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  PathDiagnosticBuilder &PDB;
93314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  PathDiagnosticLocation PrevLoc;
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9358f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  bool IsConsumedExpr(const PathDiagnosticLocation &L);
9361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  bool containsLocation(const PathDiagnosticLocation &Container,
93814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek                        const PathDiagnosticLocation &Containee);
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
94014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
94414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  void popLocation() {
9458f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek    if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
9465c7168cbe8a0eabd05e0e12e469090d2cabd27c0Ted Kremenek      // For contexts, we only one the first character as the range.
947e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek      rawAddEdge(cleanUpLocation(CLocs.back(), PDB.LC, true));
9485c7168cbe8a0eabd05e0e12e469090d2cabd27c0Ted Kremenek    }
94914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    CLocs.pop_back();
95014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenekpublic:
95314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
95414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    : PD(pd), PDB(pdb) {
9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
956a301a6773db085575ac51e3c966858180390c25bTed Kremenek      // If the PathDiagnostic already has pieces, add the enclosing statement
957a301a6773db085575ac51e3c966858180390c25bTed Kremenek      // of the first piece as a context as well.
958802e02463b880f53a6e645bde78cc412481ce9e0Ted Kremenek      if (!PD.path.empty()) {
959ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek        PrevLoc = (*PD.path.begin())->getLocation();
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek        if (const Stmt *S = PrevLoc.asStmt())
962e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek          addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
96314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      }
96414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
96514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
96614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  ~EdgeBuilder() {
96714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    while (!CLocs.empty()) popLocation();
9680cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks
969a301a6773db085575ac51e3c966858180390c25bTed Kremenek    // Finally, add an initial edge from the start location of the first
970a301a6773db085575ac51e3c966858180390c25bTed Kremenek    // statement (if it doesn't already exist).
9710cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks    PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
97259950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek                                                       PDB.LC,
9730cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks                                                       PDB.getSourceManager());
9740cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks    if (L.isValid())
9750cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks      rawAddEdge(L);
97614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
97714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
9785de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek  void flushLocations() {
9795de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek    while (!CLocs.empty())
9805de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek      popLocation();
9815de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek    PrevLoc = PathDiagnosticLocation();
9825de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek  }
9835de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek
9843ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose  void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false,
9853ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose               bool IsPostJump = false);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9878bd4d037d837e7922c3661d6158229da58a03887Ted Kremenek  void rawAddEdge(PathDiagnosticLocation NewLoc);
9881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  void addContext(const Stmt *S);
990183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose  void addContext(const PathDiagnosticLocation &L);
991e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  void addExtendedContext(const Stmt *S);
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump};
99314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek} // end anonymous namespace
99414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
99514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
99614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted KremenekPathDiagnosticLocation
99714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted KremenekEdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
99814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (const Stmt *S = L.asStmt()) {
99914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (IsControlFlowExpr(S))
100014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      return L;
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return PDB.getEnclosingStmtLocation(S);
100314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  return L;
100614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
100714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
100814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenekbool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
100914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek                                   const PathDiagnosticLocation &Containee) {
101014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
101114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (Container == Containee)
101214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return true;
10131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (Container.asDecl())
101514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return true;
10161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (const Stmt *S = Containee.asStmt())
101814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (const Stmt *ContainerS = Container.asStmt()) {
101914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      while (S) {
102014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek        if (S == ContainerS)
102114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek          return true;
102214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek        S = PDB.getParent(S);
102314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      }
102414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      return false;
102514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    }
102614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
102714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  // Less accurate: compare using source ranges.
102814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  SourceRange ContainerR = Container.asRange();
102914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  SourceRange ContaineeR = Containee.asRange();
10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  SourceManager &SM = PDB.getSourceManager();
1032402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1033402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1034402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1035402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1038642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1039642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1040642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
104214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  assert(ContainerBegLine <= ContainerEndLine);
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ContaineeBegLine <= ContaineeEndLine);
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
104514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  return (ContainerBegLine <= ContaineeBegLine &&
104614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek          ContainerEndLine >= ContaineeEndLine &&
104714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek          (ContainerBegLine != ContaineeBegLine ||
1048a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth           SM.getExpansionColumnNumber(ContainerRBeg) <=
1049a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth           SM.getExpansionColumnNumber(ContaineeRBeg)) &&
105014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek          (ContainerEndLine != ContaineeEndLine ||
1051a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth           SM.getExpansionColumnNumber(ContainerREnd) >=
10526488dc31153be6f98b404c7860be6c66bb4ec917Ted Kremenek           SM.getExpansionColumnNumber(ContaineeREnd)));
105314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
105414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
105514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenekvoid EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
105614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (!PrevLoc.isValid()) {
105714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    PrevLoc = NewLoc;
105814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return;
105914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1061e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc, PDB.LC);
1062e19229be18725bd856410b478c0e63d81ab8e4f5Ted Kremenek  const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc, PDB.LC);
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1064a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek  if (PrevLocClean.asLocation().isInvalid()) {
1065a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek    PrevLoc = NewLoc;
1066a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek    return;
1067a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek  }
1068a43df9539644bf1c258e12710cd69d79b0b078cdTed Kremenek
10698c8b0ad9601d6ccf3d7b2a3f77a896ef4fb4e6e9Ted Kremenek  if (NewLocClean.asLocation() == PrevLocClean.asLocation())
107014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return;
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  // FIXME: Ignore intra-macro edges for now.
1073402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  if (NewLocClean.asLocation().getExpansionLoc() ==
1074402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth      PrevLocClean.asLocation().getExpansionLoc())
107514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return;
107614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
10772042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek  PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
10788c8b0ad9601d6ccf3d7b2a3f77a896ef4fb4e6e9Ted Kremenek  PrevLoc = NewLoc;
107914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
108014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
10813ea09a802f973c2726b2a489ae08a4bded93410bJordan Rosevoid EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd,
10823ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose                          bool IsPostJump) {
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1084a301a6773db085575ac51e3c966858180390c25bTed Kremenek  if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1085a301a6773db085575ac51e3c966858180390c25bTed Kremenek    return;
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
108814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
108914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  while (!CLocs.empty()) {
10908f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek    ContextLocation &TopContextLoc = CLocs.back();
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    // Is the top location context the same as the one for the new location?
109314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (TopContextLoc == CLoc) {
10948f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek      if (alwaysAdd) {
10953ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose        if (IsConsumedExpr(TopContextLoc))
10963ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose          TopContextLoc.markDead();
10978f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek
109814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek        rawAddEdge(NewLoc);
10998f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek      }
110014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
11013ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose      if (IsPostJump)
11023ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose        TopContextLoc.markDead();
110314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      return;
110414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    }
110514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
110614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (containsLocation(TopContextLoc, CLoc)) {
11078f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek      if (alwaysAdd) {
110814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek        rawAddEdge(NewLoc);
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11103ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose        if (IsConsumedExpr(CLoc)) {
11113ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose          CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/true));
11128f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek          return;
11138f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek        }
11148f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek      }
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11163ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose      CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/IsPostJump));
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return;
111814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    }
111914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
112014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    // Context does not contain the location.  Flush it.
112114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    popLocation();
112214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11245c7168cbe8a0eabd05e0e12e469090d2cabd27c0Ted Kremenek  // If we reach here, there is no enclosing context.  Just add the edge.
11255c7168cbe8a0eabd05e0e12e469090d2cabd27c0Ted Kremenek  rawAddEdge(NewLoc);
112614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
112714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
11288f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenekbool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
11298f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
11308f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek    return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11328f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek  return false;
11338f9b1b3865cd5814a8c4c768a34d56df6d6c93beTed Kremenek}
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1135e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenekvoid EdgeBuilder::addExtendedContext(const Stmt *S) {
1136e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  if (!S)
1137e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek    return;
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const Stmt *Parent = PDB.getParent(S);
1140e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  while (Parent) {
1141e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek    if (isa<CompoundStmt>(Parent))
1142e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek      Parent = PDB.getParent(Parent);
1143e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek    else
1144e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek      break;
1145e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  }
1146e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek
1147e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  if (Parent) {
1148e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek    switch (Parent->getStmtClass()) {
1149e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek      case Stmt::DoStmtClass:
1150e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek      case Stmt::ObjCAtSynchronizedStmtClass:
1151e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek        addContext(Parent);
1152e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek      default:
1153e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek        break;
1154e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek    }
1155e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  }
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1157e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek  addContext(S);
1158e1baed38437e08fee7ab372ba2579ce22da354c2Ted Kremenek}
11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenekvoid EdgeBuilder::addContext(const Stmt *S) {
116114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  if (!S)
116214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    return;
116314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
116459950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek  PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
1165183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose  addContext(L);
1166183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose}
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1168183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rosevoid EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
116914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  while (!CLocs.empty()) {
117014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    const PathDiagnosticLocation &TopContextLoc = CLocs.back();
117114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
117214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    // Is the top location context the same as the one for the new location?
117314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (TopContextLoc == L)
117414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      return;
117514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
117614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    if (containsLocation(TopContextLoc, L)) {
117714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      CLocs.push_back(L);
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return;
117914856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    }
118014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
118114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    // Context does not contain the location.  Flush it.
118214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    popLocation();
118314856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
118414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
118514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  CLocs.push_back(L);
118614856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
118714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
118811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// Cone-of-influence: support the reverse propagation of "interesting" symbols
118911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// and values by tracing interesting calculations backwards through evaluated
119011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// expressions along a path.  This is probably overly complicated, but the idea
119111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// is that if an expression computed an "interesting" value, the child
119211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// expressions are are also likely to be "interesting" as well (which then
119311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// propagates to the values they in turn compute).  This reverse propagation
119411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// is needed to track interesting correlations across function call boundaries,
119511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// where formal arguments bind to actual arguments, etc.  This is also needed
119611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// because the constraint solver sometimes simplifies certain symbolic values
119711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// into constants when appropriate, and this complicates reasoning about
119811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek// interesting values.
119911abcecc8c919673237cf37384290a1ef1943976Ted Kremenektypedef llvm::DenseSet<const Expr *> InterestingExprs;
120011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
120111abcecc8c919673237cf37384290a1ef1943976Ted Kremenekstatic void reversePropagateIntererstingSymbols(BugReport &R,
120211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                                InterestingExprs &IE,
120311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                                const ProgramState *State,
120411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                                const Expr *Ex,
120511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                                const LocationContext *LCtx) {
120611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  SVal V = State->getSVal(Ex, LCtx);
120711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  if (!(R.isInteresting(V) || IE.count(Ex)))
120811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    return;
120911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
121011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  switch (Ex->getStmtClass()) {
121111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    default:
121211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      if (!isa<CastExpr>(Ex))
121311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        break;
121411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      // Fall through.
121511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    case Stmt::BinaryOperatorClass:
121611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    case Stmt::UnaryOperatorClass: {
121711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      for (Stmt::const_child_iterator CI = Ex->child_begin(),
121811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek            CE = Ex->child_end();
121911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek            CI != CE; ++CI) {
122011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
122111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          IE.insert(child);
122211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          SVal ChildV = State->getSVal(child, LCtx);
122311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          R.markInteresting(ChildV);
122411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        }
122511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        break;
122611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      }
122711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    }
122811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  }
122911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
123011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  R.markInteresting(V);
123111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek}
123211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
123311abcecc8c919673237cf37384290a1ef1943976Ted Kremenekstatic void reversePropagateInterestingSymbols(BugReport &R,
123411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               InterestingExprs &IE,
123511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               const ProgramState *State,
123611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               const LocationContext *CalleeCtx,
123711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               const LocationContext *CallerCtx)
123811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek{
1239852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose  // FIXME: Handle non-CallExpr-based CallEvents.
124011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
124111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  const Stmt *CallSite = Callee->getCallSite();
1242852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose  if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
124311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
124411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      FunctionDecl::param_const_iterator PI = FD->param_begin(),
124511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                         PE = FD->param_end();
124611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
124711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      for (; AI != AE && PI != PE; ++AI, ++PI) {
124811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        if (const Expr *ArgE = *AI) {
124911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          if (const ParmVarDecl *PD = *PI) {
125011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek            Loc LV = State->getLValue(PD, CalleeCtx);
125111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek            if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
125211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek              IE.insert(ArgE);
125311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          }
125411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        }
125511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      }
125611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek    }
125711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  }
125811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek}
12598185674528423e2504a1fae35c28c24104846510Ted Kremenek
1260b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek//===----------------------------------------------------------------------===//
1261b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek// Functions for determining if a loop was executed 0 times.
1262b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek//===----------------------------------------------------------------------===//
1263b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
1264b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenekstatic bool isLoop(const Stmt *Term) {
12658185674528423e2504a1fae35c28c24104846510Ted Kremenek  switch (Term->getStmtClass()) {
12668185674528423e2504a1fae35c28c24104846510Ted Kremenek    case Stmt::ForStmtClass:
12678185674528423e2504a1fae35c28c24104846510Ted Kremenek    case Stmt::WhileStmtClass:
1268c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek    case Stmt::ObjCForCollectionStmtClass:
1269b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek      return true;
12708185674528423e2504a1fae35c28c24104846510Ted Kremenek    default:
12718185674528423e2504a1fae35c28c24104846510Ted Kremenek      // Note that we intentionally do not include do..while here.
12728185674528423e2504a1fae35c28c24104846510Ted Kremenek      return false;
12738185674528423e2504a1fae35c28c24104846510Ted Kremenek  }
1274b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek}
12758185674528423e2504a1fae35c28c24104846510Ted Kremenek
1276b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenekstatic bool isJumpToFalseBranch(const BlockEdge *BE) {
12778185674528423e2504a1fae35c28c24104846510Ted Kremenek  const CFGBlock *Src = BE->getSrc();
12788185674528423e2504a1fae35c28c24104846510Ted Kremenek  assert(Src->succ_size() == 2);
12798185674528423e2504a1fae35c28c24104846510Ted Kremenek  return (*(Src->succ_begin()+1) == BE->getDst());
12808185674528423e2504a1fae35c28c24104846510Ted Kremenek}
12818185674528423e2504a1fae35c28c24104846510Ted Kremenek
1282b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek/// Return true if the terminator is a loop and the destination is the
1283b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek/// false branch.
1284b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenekstatic bool isLoopJumpPastBody(const Stmt *Term, const BlockEdge *BE) {
1285b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek  if (!isLoop(Term))
1286b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek    return false;
1287b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1288b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek  // Did we take the false branch?
1289b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek  return isJumpToFalseBranch(BE);
1290b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek}
1291b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1292b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenekstatic bool isContainedByStmt(ParentMap &PM, const Stmt *S, const Stmt *SubS) {
1293b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  while (SubS) {
1294b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    if (SubS == S)
1295b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      return true;
1296b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    SubS = PM.getParent(SubS);
1297b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  }
1298b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  return false;
1299b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek}
1300b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
1301b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenekstatic const Stmt *getStmtBeforeCond(ParentMap &PM, const Stmt *Term,
1302b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                                     const ExplodedNode *N) {
1303b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  while (N) {
1304b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1305b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    if (SP) {
1306b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      const Stmt *S = SP->getStmt();
1307b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      if (!isContainedByStmt(PM, Term, S))
1308b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek        return S;
1309b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    }
13100f8579274a010f360a371b53101859d9d6052314Anna Zaks    N = N->getFirstPred();
1311b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  }
1312b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  return 0;
1313b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek}
1314b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
1315b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenekstatic bool isInLoopBody(ParentMap &PM, const Stmt *S, const Stmt *Term) {
1316b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  const Stmt *LoopBody = 0;
1317b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  switch (Term->getStmtClass()) {
1318b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    case Stmt::ForStmtClass: {
1319b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      const ForStmt *FS = cast<ForStmt>(Term);
1320b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      if (isContainedByStmt(PM, FS->getInc(), S))
1321b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek        return true;
1322b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      LoopBody = FS->getBody();
1323b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      break;
1324b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    }
1325c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek    case Stmt::ObjCForCollectionStmtClass: {
1326c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek      const ObjCForCollectionStmt *FC = cast<ObjCForCollectionStmt>(Term);
1327c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek      LoopBody = FC->getBody();
1328c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek      break;
1329c5b9c8bc6d77175f6d41d898511b1e7b1e2f86f8Ted Kremenek    }
1330b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    case Stmt::WhileStmtClass:
1331b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      LoopBody = cast<WhileStmt>(Term)->getBody();
1332b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      break;
1333b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek    default:
1334b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek      return false;
1335b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  }
1336b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek  return isContainedByStmt(PM, LoopBody, S);
1337b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek}
1338b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
1339b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek//===----------------------------------------------------------------------===//
1340b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek// Top-level logic for generating extensive path diagnostics.
1341b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek//===----------------------------------------------------------------------===//
1342b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
13438347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rosestatic bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
134414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek                                            PathDiagnosticBuilder &PDB,
13453bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                            const ExplodedNode *N,
1346d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek                                            LocationContextMap &LCM,
13473bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                      ArrayRef<BugReporterVisitor *> visitors) {
134814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  EdgeBuilder EB(PD, PDB);
13490cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks  const SourceManager& SM = PDB.getSourceManager();
135056a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks  StackDiagVector CallStack;
135111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek  InterestingExprs IE;
135214856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
13539c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
135414856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  while (NextNode) {
135514856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    N = NextNode;
13560f8579274a010f360a371b53101859d9d6052314Anna Zaks    NextNode = N->getFirstPred();
135714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek    ProgramPoint P = N->getLocation();
135814856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
1359dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek    do {
13607a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
136111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        if (const Expr *Ex = PS->getStmtAs<Expr>())
136211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
136311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                              N->getState().getPtr(), Ex,
136411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                              N->getLocationContext());
136511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek      }
136611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
13677a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1368183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        const Stmt *S = CE->getCalleeContext()->getCallSite();
1369183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1370852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose            reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1371852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose                                                N->getState().getPtr(), Ex,
1372852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose                                                N->getLocationContext());
1373852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose        }
1374183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose
1375183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        PathDiagnosticCallPiece *C =
1376183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose          PathDiagnosticCallPiece::construct(N, *CE, SM);
1377bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek        LCM[&C->path] = CE->getCalleeContext();
1378183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose
13793ea09a802f973c2726b2a489ae08a4bded93410bJordan Rose        EB.addEdge(C->callReturn, /*AlwaysAdd=*/true, /*IsPostJump=*/true);
1380183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        EB.flushLocations();
1381183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose
1382183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        PD.getActivePath().push_front(C);
1383183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        PD.pushActivePath(&C->path);
1384183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        CallStack.push_back(StackDiagPair(C, N));
13855de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek        break;
13865de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek      }
13874ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek
13882042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek      // Pop the call hierarchy if we are done walking the contents
13892042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek      // of a function call.
13907a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1391097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek        // Add an edge to the start of the function.
1392097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek        const Decl *D = CE->getCalleeContext()->getDecl();
1393097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek        PathDiagnosticLocation pos =
1394097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek          PathDiagnosticLocation::createBegin(D, SM);
1395097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek        EB.addEdge(pos);
1396097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek
1397097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek        // Flush all locations, and pop the active path.
1398183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        bool VisitedEntireCall = PD.isWithinCall();
13994ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek        EB.flushLocations();
14002042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        PD.popActivePath();
14014ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek        PDB.LC = N->getLocationContext();
1402097ebb3d8ce55d1f78a3f1e7a0978dbde5ee2898Ted Kremenek
1403183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        // Either we just added a bunch of stuff to the top-level path, or
1404183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        // we have a previous CallExitEnd.  If the former, it means that the
14052042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        // path terminated within a function call.  We must then take the
14062042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        // current contents of the active path and place it within
14072042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        // a new PathDiagnosticCallPiece.
1408183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        PathDiagnosticCallPiece *C;
1409183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        if (VisitedEntireCall) {
1410183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose          C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1411183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        } else {
1412183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose          const Decl *Caller = CE->getLocationContext()->getDecl();
14139373937945e1e075dfa08169eaccc1ad0b31f699Anna Zaks          C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1414bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek          LCM[&C->path] = CE->getCalleeContext();
14159373937945e1e075dfa08169eaccc1ad0b31f699Anna Zaks        }
1416852aa0d2c5d2d1faf2d77b5aa3c0848068a342c5Jordan Rose
1417183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        C->setCallee(*CE, SM);
1418183ba8e19d49ab1ae25d3cdd0a19591369c5ab9fJordan Rose        EB.addContext(C->getLocation());
1419368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks
1420368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        if (!CallStack.empty()) {
142156a938ff85a444eb3d30d2634d92ce5b1f6fae56Anna Zaks          assert(CallStack.back().first == C);
1422368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks          CallStack.pop_back();
1423368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        }
14242042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        break;
14252042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek      }
14264ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek
14274ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek      // Note that is important that we update the LocationContext
14284ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek      // after looking at CallExits.  CallExit basically adds an
14294ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek      // edge in the *caller*, so we don't want to update the LocationContext
14304ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek      // too soon.
14314ba86bc53bb280ba46a08459eda7d283d513b61fTed Kremenek      PDB.LC = N->getLocationContext();
14325de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek
1433dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek      // Block edges.
14347a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
143511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        // Does this represent entering a call?  If so, look at propagating
143611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        // interesting symbols across call boundaries.
143711abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        if (NextNode) {
143811abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          const LocationContext *CallerCtx = NextNode->getLocationContext();
143911abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          const LocationContext *CalleeCtx = PDB.LC;
144011abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          if (CallerCtx != CalleeCtx) {
144111abcecc8c919673237cf37384290a1ef1943976Ted Kremenek            reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
144211abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               N->getState().getPtr(),
144311abcecc8c919673237cf37384290a1ef1943976Ted Kremenek                                               CalleeCtx, CallerCtx);
144411abcecc8c919673237cf37384290a1ef1943976Ted Kremenek          }
144511abcecc8c919673237cf37384290a1ef1943976Ted Kremenek        }
144611abcecc8c919673237cf37384290a1ef1943976Ted Kremenek
1447dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek        // Are we jumping to the head of a loop?  Add a special diagnostic.
1448f57a2aa02c0578c5bd834fec0d44c16ad9908620Ted Kremenek        if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
144959950d3aa54ca5066b1fb08a8c79ebfe10e0919bTed Kremenek          PathDiagnosticLocation L(Loop, SM, PDB.LC);
1450ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek          const CompoundStmt *CS = NULL;
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1452f57a2aa02c0578c5bd834fec0d44c16ad9908620Ted Kremenek          if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1453f57a2aa02c0578c5bd834fec0d44c16ad9908620Ted Kremenek            CS = dyn_cast<CompoundStmt>(FS->getBody());
1454f57a2aa02c0578c5bd834fec0d44c16ad9908620Ted Kremenek          else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1455f57a2aa02c0578c5bd834fec0d44c16ad9908620Ted Kremenek            CS = dyn_cast<CompoundStmt>(WS->getBody());
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1457dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek          PathDiagnosticEventPiece *p =
1458dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek            new PathDiagnosticEventPiece(L,
145907c015cbcee31f72e2d320ba2c713c046bed42faTed Kremenek                                        "Looping back to the head of the loop");
14602dd17abf11ae64339fa6bfaa57d76e13a5fbe5b8Ted Kremenek          p->setPrunable(true);
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1462dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek          EB.addEdge(p->getLocation(), true);
14632042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek          PD.getActivePath().push_front(p);
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1465ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek          if (CS) {
14660cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks            PathDiagnosticLocation BL =
14670cd59482abd8aec9ed1eaad11f5fe9c1e42639f6Anna Zaks              PathDiagnosticLocation::createEndBrace(CS, SM);
146807c015cbcee31f72e2d320ba2c713c046bed42faTed Kremenek            EB.addEdge(BL);
1469dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek          }
14708bd4d037d837e7922c3661d6158229da58a03887Ted Kremenek        }
14718185674528423e2504a1fae35c28c24104846510Ted Kremenek
1472b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek        const CFGBlock *BSrc = BE->getSrc();
1473b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek        ParentMap &PM = PDB.getParentMap();
1474b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek
1475b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek        if (const Stmt *Term = BSrc->getTerminator()) {
14768185674528423e2504a1fae35c28c24104846510Ted Kremenek          // Are we jumping past the loop body without ever executing the
14778185674528423e2504a1fae35c28c24104846510Ted Kremenek          // loop (because the condition was false)?
14787a95de68c093991047ed8d339479ccad51b88663David Blaikie          if (isLoopJumpPastBody(Term, &*BE) &&
1479b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek              !isInLoopBody(PM,
1480b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                            getStmtBeforeCond(PM,
1481b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                                              BSrc->getTerminatorCondition(),
1482b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                                              N),
1483b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                            Term)) {
14848185674528423e2504a1fae35c28c24104846510Ted Kremenek            PathDiagnosticLocation L(Term, SM, PDB.LC);
14858185674528423e2504a1fae35c28c24104846510Ted Kremenek            PathDiagnosticEventPiece *PE =
1486b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek                new PathDiagnosticEventPiece(L, "Loop body executed 0 times");
14878185674528423e2504a1fae35c28c24104846510Ted Kremenek            PE->setPrunable(true);
14888185674528423e2504a1fae35c28c24104846510Ted Kremenek
14898185674528423e2504a1fae35c28c24104846510Ted Kremenek            EB.addEdge(PE->getLocation(), true);
14908185674528423e2504a1fae35c28c24104846510Ted Kremenek            PD.getActivePath().push_front(PE);
14918185674528423e2504a1fae35c28c24104846510Ted Kremenek          }
14928185674528423e2504a1fae35c28c24104846510Ted Kremenek
1493b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek          // In any case, add the terminator as the current statement
1494b04a2387ac23adfa063de03844cb16c0d77fb405Ted Kremenek          // context for control edges.
1495ddb7babf448822e0f8da07fdf9446cf515d04ad5Ted Kremenek          EB.addContext(Term);
14968185674528423e2504a1fae35c28c24104846510Ted Kremenek        }
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1498dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek        break;
14998bd4d037d837e7922c3661d6158229da58a03887Ted Kremenek      }
150014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
15017a95de68c093991047ed8d339479ccad51b88663David Blaikie      if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
1502b07805485c603be3d8011f72611465324c9e664bDavid Blaikie        Optional<CFGElement> First = BE->getFirstElement();
1503b07805485c603be3d8011f72611465324c9e664bDavid Blaikie        if (Optional<CFGStmt> S = First ? First->getAs<CFGStmt>() : None) {
1504b07805485c603be3d8011f72611465324c9e664bDavid Blaikie          const Stmt *stmt = S->getStmt();
15053c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek          if (IsControlFlowExpr(stmt)) {
1506b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu            // Add the proper context for '&&', '||', and '?'.
15073c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek            EB.addContext(stmt);
1508b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu          }
1509b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu          else
15103c0349e87cdbd7316d06d2411d86ee1086e717a5Ted Kremenek            EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
1511dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek        }
1512b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu
1513dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek        break;
1514dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek      }
15155de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek
15165de4fdb8de700f95b0b863a9e5a4a508de17a034Ted Kremenek
1517dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek    } while (0);
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1519dd986cc9989f665370cef0917ba8ba3b4871e3e6Ted Kremenek    if (!NextNode)
152014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek      continue;
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15228e6431adab313e283a992698f6fc7afe62420999Anna Zaks    // Add pieces from custom visitors.
15238e6431adab313e283a992698f6fc7afe62420999Anna Zaks    BugReport *R = PDB.getBugReport();
15243bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose    for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
15253bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose                                                  E = visitors.end();
15263bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose         I != E; ++I) {
15278e6431adab313e283a992698f6fc7afe62420999Anna Zaks      if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
15288966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek        const PathDiagnosticLocation &Loc = p->getLocation();
15298966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek        EB.addEdge(Loc, true);
15302042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek        PD.getActivePath().push_front(p);
1531368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks        updateStackPiecesWithMessage(p, CallStack);
1532368a0d565f078666ca5bfb7fe08d04648688e4bcAnna Zaks
15338966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek        if (const Stmt *S = Loc.asStmt())
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
15358966bc1c8ce271c09936c0eaf6c841aef4a0af1bTed Kremenek      }
15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
153714856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek  }
15388347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose
15398347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose  return PDB.getBugReport()->isValid();
154014856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek}
154114856d7b7a2be05ae9a0fd5b2073631994c2ba43Ted Kremenek
1542af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek/// \brief Adds a sanitized control-flow diagnostic edge to a path.
1543af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenekstatic void addEdgeToPath(PathPieces &path,
1544af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                          PathDiagnosticLocation &PrevLoc,
1545af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                          PathDiagnosticLocation NewLoc,
1546af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                          const LocationContext *LC) {
1547bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (!NewLoc.isValid())
1548af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    return;
1549af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1550bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  SourceLocation NewLocL = NewLoc.asLocation();
1551bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (NewLocL.isInvalid() || NewLocL.isMacroID())
1552af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    return;
1553af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1554bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (!PrevLoc.isValid()) {
1555af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    PrevLoc = NewLoc;
1556af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    return;
1557af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  }
1558af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1559af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  // FIXME: ignore intra-macro edges for now.
1560bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (NewLoc.asLocation().getExpansionLoc() ==
1561bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      PrevLoc.asLocation().getExpansionLoc())
1562af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    return;
1563af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1564bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  path.push_front(new PathDiagnosticControlFlowPiece(NewLoc,
1565bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek                                                     PrevLoc));
1566af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  PrevLoc = NewLoc;
1567af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek}
1568af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1569af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenekstatic bool
1570af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted KremenekGenerateAlternateExtensivePathDiagnostic(PathDiagnostic& PD,
1571af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                         PathDiagnosticBuilder &PDB,
1572af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                         const ExplodedNode *N,
1573af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                         LocationContextMap &LCM,
1574af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                      ArrayRef<BugReporterVisitor *> visitors) {
1575af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1576af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  BugReport *report = PDB.getBugReport();
1577af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  const SourceManager& SM = PDB.getSourceManager();
1578af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  StackDiagVector CallStack;
1579af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  InterestingExprs IE;
1580af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1581af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  // Record the last location for a given visited stack frame.
1582af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  llvm::DenseMap<const StackFrameContext *, PathDiagnosticLocation>
1583af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    PrevLocMap;
1584af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1585af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  const ExplodedNode *NextNode = N->getFirstPred();
1586af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  while (NextNode) {
1587af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    N = NextNode;
1588af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    NextNode = N->getFirstPred();
1589af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    ProgramPoint P = N->getLocation();
1590af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1591af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    do {
1592be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // Have we encountered an entrance to a call?  It may be
1593be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // the case that we have not encountered a matching
1594be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // call exit before this point.  This means that the path
1595be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // terminated within the call itself.
1596be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1597be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        // Add an edge to the start of the function.
1598be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        const StackFrameContext *CalleeLC = CE->getCalleeContext();
1599be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PathDiagnosticLocation &PrevLocCallee = PrevLocMap[CalleeLC];
1600be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        const Decl *D = CalleeLC->getDecl();
1601be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        addEdgeToPath(PD.getActivePath(), PrevLocCallee,
1602be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                      PathDiagnosticLocation::createBegin(D, SM),
1603be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                      CalleeLC);
1604cd389d8cc9abeffb1416b70dd58148e66e5d822bTed Kremenek
1605be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        // Did we visit an entire call?
1606be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        bool VisitedEntireCall = PD.isWithinCall();
1607be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PD.popActivePath();
1608be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1609be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PathDiagnosticCallPiece *C;
1610be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        if (VisitedEntireCall) {
1611be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          PathDiagnosticPiece *P = PD.getActivePath().front().getPtr();
1612be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          C = cast<PathDiagnosticCallPiece>(P);
1613be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        } else {
1614be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          const Decl *Caller = CE->getLocationContext()->getDecl();
1615be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1616be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1617be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // Since we just transferred the path over to the call piece,
1618be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // reset the mapping from active to location context.
1619be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          assert(PD.getActivePath().size() == 1 &&
1620be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                 PD.getActivePath().front() == C);
1621be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          LCM[&PD.getActivePath()] = 0;
1622be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1623be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // Record the location context mapping for the path within
1624be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // the call.
1625be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          assert(LCM[&C->path] == 0 ||
1626be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                 LCM[&C->path] == CE->getCalleeContext());
1627be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          LCM[&C->path] = CE->getCalleeContext();
1628be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1629be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // If this is the first item in the active path, record
1630be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // the new mapping from active path to location context.
1631be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          const LocationContext *&NewLC = LCM[&PD.getActivePath()];
1632be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          if (!NewLC) {
1633be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek            NewLC = N->getLocationContext();
1634be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          }
1635be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          PDB.LC = NewLC;
1636be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1637be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // Update the previous location in the active path
1638be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          // since we just created the call piece lazily.
1639be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          PrevLocMap[PDB.LC->getCurrentStackFrame()] = C->getLocation();
1640be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        }
1641be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        C->setCallee(*CE, SM);
1642be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1643be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        if (!CallStack.empty()) {
1644be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          assert(CallStack.back().first == C);
1645be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          CallStack.pop_back();
1646be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        }
1647af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        break;
1648af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      }
1649af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1650be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // Query the location context here and the previous location
1651be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // as processing CallEnter may change the active path.
1652be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      PDB.LC = N->getLocationContext();
1653be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1654be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // Get the previous location for the current active
1655be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // location context.  All edges will be based on this
1656be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // location, and it will be updated in place.
1657be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      PathDiagnosticLocation &PrevLoc =
1658be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PrevLocMap[PDB.LC->getCurrentStackFrame()];
1659be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1660be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // Record the mapping from the active path to the location
1661be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      // context.
1662be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      assert(!LCM[&PD.getActivePath()] ||
1663be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek             LCM[&PD.getActivePath()] == PDB.LC);
1664be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      LCM[&PD.getActivePath()] = PDB.LC;
1665be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1666af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      // Have we encountered an exit from a function call?
1667af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1668af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        const Stmt *S = CE->getCalleeContext()->getCallSite();
1669af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Propagate the interesting symbols accordingly.
1670af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1671af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1672af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                              N->getState().getPtr(), Ex,
1673af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                              N->getLocationContext());
1674af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        }
1675af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1676af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // We are descending into a call (backwards).  Construct
1677af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // a new call piece to contain the path pieces for that call.
1678af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        PathDiagnosticCallPiece *C =
1679af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          PathDiagnosticCallPiece::construct(N, *CE, SM);
1680af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1681af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Record the location context for this call piece.
1682bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek        LCM[&C->path] = CE->getCalleeContext();
1683af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1684af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Add the edge to the return site.
1685be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        addEdgeToPath(PD.getActivePath(), PrevLoc, C->callReturn, PDB.LC);
1686be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PD.getActivePath().push_front(C);
1687af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1688af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Make the contents of the call the active path for now.
1689af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        PD.pushActivePath(&C->path);
1690af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        CallStack.push_back(StackDiagPair(C, N));
1691af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        break;
1692af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      }
1693af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1694be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
1695be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        // For expressions, make sure we propagate the
1696be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        // interesting symbols correctly.
1697be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        if (const Expr *Ex = PS->getStmtAs<Expr>())
1698be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1699be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                                              N->getState().getPtr(), Ex,
1700be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                                              N->getLocationContext());
1701af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1702be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        PathDiagnosticLocation L =
1703be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          PathDiagnosticLocation(PS->getStmt(), SM, PDB.LC);
1704be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1705af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        break;
1706af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      }
1707af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1708af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      // Block edges.
1709af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
1710af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Does this represent entering a call?  If so, look at propagating
1711af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // interesting symbols across call boundaries.
1712af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        if (NextNode) {
1713af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          const LocationContext *CallerCtx = NextNode->getLocationContext();
1714af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          const LocationContext *CalleeCtx = PDB.LC;
1715af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          if (CallerCtx != CalleeCtx) {
1716af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek            reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1717af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                               N->getState().getPtr(),
1718af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                               CalleeCtx, CallerCtx);
1719af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          }
1720af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        }
1721af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1722af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        // Are we jumping to the head of a loop?  Add a special diagnostic.
1723af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1724af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          PathDiagnosticLocation L(Loop, SM, PDB.LC);
1725af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1726af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          PathDiagnosticEventPiece *p =
1727af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek            new PathDiagnosticEventPiece(L, "Looping back to the head "
1728af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek                                            "of the loop");
1729af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          p->setPrunable(true);
1730af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1731be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek          addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1732cd389d8cc9abeffb1416b70dd58148e66e5d822bTed Kremenek          PD.getActivePath().push_front(p);
1733af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        }
1734b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1735af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        const CFGBlock *BSrc = BE->getSrc();
1736af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        ParentMap &PM = PDB.getParentMap();
1737af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1738af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        if (const Stmt *Term = BSrc->getTerminator()) {
1739af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          // Are we jumping past the loop body without ever executing the
1740af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          // loop (because the condition was false)?
1741b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek          if (isLoop(Term)) {
1742b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            const Stmt *TermCond = BSrc->getTerminatorCondition();
1743b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            bool IsInLoopBody =
1744b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              isInLoopBody(PM, getStmtBeforeCond(PM, TermCond, N), Term);
1745b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1746b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            const char *str = 0;
1747b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1748b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            if (isJumpToFalseBranch(&*BE)) {
1749b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              if (!IsInLoopBody) {
1750b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek                str = "Loop body executed 0 times";
1751b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              }
1752b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            }
1753b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            else {
1754b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              str = "Entering loop body";
1755b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            }
1756b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek
1757b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            if (str) {
1758d0f5faf319550b0504b2f8f822d06a6b0279285bTed Kremenek              PathDiagnosticLocation L(TermCond, SM, PDB.LC);
1759b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              PathDiagnosticEventPiece *PE =
1760b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek                new PathDiagnosticEventPiece(L, str);
1761b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              PE->setPrunable(true);
1762b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              addEdgeToPath(PD.getActivePath(), PrevLoc,
1763be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek                            PE->getLocation(), PDB.LC);
1764b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek              PD.getActivePath().push_front(PE);
1765b097a57f58672a825c99fdfb668b04e921e363b9Ted Kremenek            }
1766af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek          }
1767bc0fd8129626ff4e485388311b081e76d0f96795Ted Kremenek          else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) ||
1768bc0fd8129626ff4e485388311b081e76d0f96795Ted Kremenek                   isa<GotoStmt>(Term)) {
1769bc0fd8129626ff4e485388311b081e76d0f96795Ted Kremenek            PathDiagnosticLocation L(Term, SM, PDB.LC);
1770be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek            addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1771bc0fd8129626ff4e485388311b081e76d0f96795Ted Kremenek          }
1772af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        }
1773af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        break;
1774af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      }
1775af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    } while (0);
1776af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1777af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    if (!NextNode)
1778af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      continue;
1779af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1780be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek    // Since the active path may have been updated prior
1781be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek    // to this point, query the active location context now.
1782be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek    PathDiagnosticLocation &PrevLoc =
1783be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek      PrevLocMap[PDB.LC->getCurrentStackFrame()];
1784be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek
1785af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    // Add pieces from custom visitors.
1786af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1787af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek         E = visitors.end();
1788af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek         I != E; ++I) {
1789cd389d8cc9abeffb1416b70dd58148e66e5d822bTed Kremenek      if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *report)) {
1790be0b207c4916f823497d31cbf5083efb4e374163Ted Kremenek        addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1791cd389d8cc9abeffb1416b70dd58148e66e5d822bTed Kremenek        PD.getActivePath().push_front(p);
1792af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        updateStackPiecesWithMessage(p, CallStack);
1793af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      }
1794af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    }
1795af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  }
1796af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1797af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  return report->isValid();
1798af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek}
1799af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
1800f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenekconst Stmt *getLocStmt(PathDiagnosticLocation L) {
1801bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (!L.isValid())
1802bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    return 0;
1803f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek  return L.asStmt();
1804f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek}
1805f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek
1806f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenekconst Stmt *getStmtParent(const Stmt *S, ParentMap &PM) {
1807bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  if (!S)
1808bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    return 0;
1809e644ed5308ab22e4bcb5f821fe7ea9dae324a0a8Ted Kremenek  return PM.getParentIgnoreParens(S);
1810bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek}
1811bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
181215676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenekstatic bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
181315676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek  switch (S->getStmtClass()) {
181403194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek    case Stmt::BinaryOperatorClass: {
181503194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek      const BinaryOperator *BO = cast<BinaryOperator>(S);
181603194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek      if (!BO->isLogicalOp())
181703194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek        return false;
181803194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek      return BO->getLHS() == Cond || BO->getRHS() == Cond;
181903194fb1bbf2b2d17ff7e3d61ddb9d73e9297fdcTed Kremenek    }
1820a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek    case Stmt::IfStmtClass:
1821a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek      return cast<IfStmt>(S)->getCond() == Cond;
182215676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::ForStmtClass:
182315676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<ForStmt>(S)->getCond() == Cond;
182415676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::WhileStmtClass:
182515676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<WhileStmt>(S)->getCond() == Cond;
182615676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::DoStmtClass:
182715676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<DoStmt>(S)->getCond() == Cond;
182815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::ChooseExprClass:
182915676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<ChooseExpr>(S)->getCond() == Cond;
183015676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::IndirectGotoStmtClass:
183115676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
183215676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::SwitchStmtClass:
183315676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<SwitchStmt>(S)->getCond() == Cond;
183415676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::BinaryConditionalOperatorClass:
183515676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
18368484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek    case Stmt::ConditionalOperatorClass: {
18378484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek      const ConditionalOperator *CO = cast<ConditionalOperator>(S);
18388484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek      return CO->getCond() == Cond ||
18398484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek             CO->getLHS() == Cond ||
18408484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek             CO->getRHS() == Cond;
18418484b37a2b7720c016d27a672343b1c67bd2e731Ted Kremenek    }
184215676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    case Stmt::ObjCForCollectionStmtClass:
184315676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
184415676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    default:
184515676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      return false;
184615676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek  }
184715676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek}
184815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek
184996b8134337883908fcc45484486fe200d6b3e32fTed Kremenekstatic bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1850e2f7337958f21802e98777f441fe20ef7ba2adffTed Kremenek  const ForStmt *FS = dyn_cast<ForStmt>(FL);
185196b8134337883908fcc45484486fe200d6b3e32fTed Kremenek  if (!FS)
1852e2f7337958f21802e98777f441fe20ef7ba2adffTed Kremenek    return false;
185396b8134337883908fcc45484486fe200d6b3e32fTed Kremenek  return FS->getInc() == S || FS->getInit() == S;
1854e2f7337958f21802e98777f441fe20ef7ba2adffTed Kremenek}
1855e2f7337958f21802e98777f441fe20ef7ba2adffTed Kremenek
185615676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenektypedef llvm::DenseSet<const PathDiagnosticCallPiece *>
185715676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek        OptimizedCallsSet;
185815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek
185915676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenekstatic bool optimizeEdges(PathPieces &path, SourceManager &SM,
186015676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek                          OptimizedCallsSet &OCS,
1861bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek                          LocationContextMap &LCM) {
1862bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  bool hasChanges = false;
1863bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  const LocationContext *LC = LCM[&path];
1864bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  assert(LC);
1865f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek  bool isFirst = true;
1866bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1867481da5554d03271b0d87b695449963f7728c5895Ted Kremenek  for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
186815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    bool wasFirst = isFirst;
186915676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    isFirst = false;
1870bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1871bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // Optimize subpaths.
1872bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    if (PathDiagnosticCallPiece *CallI = dyn_cast<PathDiagnosticCallPiece>(*I)){
1873481da5554d03271b0d87b695449963f7728c5895Ted Kremenek      // Record the fact that a call has been optimized so we only do the
1874481da5554d03271b0d87b695449963f7728c5895Ted Kremenek      // effort once.
187515676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      if (!OCS.count(CallI)) {
1876a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek        while (optimizeEdges(CallI->path, SM, OCS, LCM)) {}
187715676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek        OCS.insert(CallI);
187815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      }
1879481da5554d03271b0d87b695449963f7728c5895Ted Kremenek      ++I;
1880bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      continue;
1881bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    }
1882bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1883bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // Pattern match the current piece and its successor.
1884bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    PathDiagnosticControlFlowPiece *PieceI =
1885bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      dyn_cast<PathDiagnosticControlFlowPiece>(*I);
1886bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1887481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    if (!PieceI) {
1888481da5554d03271b0d87b695449963f7728c5895Ted Kremenek      ++I;
1889bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      continue;
1890481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    }
1891bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1892f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    ParentMap &PM = LC->getParentMap();
1893f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
1894f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *s1End   = getLocStmt(PieceI->getEndLocation());
1895f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *level1 = getStmtParent(s1Start, PM);
1896f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *level2 = getStmtParent(s1End, PM);
1897f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek
189815676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    if (wasFirst) {
1899f4bbb1d8c2aa8b6630110827361ee0655e731548Ted Kremenek      wasFirst = false;
1900f4bbb1d8c2aa8b6630110827361ee0655e731548Ted Kremenek
1901a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek      // If the first edge (in isolation) is just a transition from
1902a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek      // an expression to a parent expression then eliminate that edge.
1903a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek      if (level1 && level2 && level2 == PM.getParent(level1)) {
1904f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek        path.erase(I);
1905f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek        // Since we are erasing the current edge at the start of the
1906f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek        // path, just return now so we start analyzing the start of the path
1907f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek        // again.
1908f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek        return true;
1909f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek      }
191096b8134337883908fcc45484486fe200d6b3e32fTed Kremenek
191196b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      // If the first edge (in isolation) is a transition from the
191296b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      // initialization or increment in a for loop then remove it.
191396b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      if (level1 && isIncrementOrInitInForLoop(s1Start, level1)) {
191496b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        path.erase(I);
191596b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        return true;
191696b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      }
1917f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    }
1918f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek
191915676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    PathPieces::iterator NextI = I; ++NextI;
192015676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    if (NextI == E)
192115676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek      break;
192215676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek
1923bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    PathDiagnosticControlFlowPiece *PieceNextI =
1924bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      dyn_cast<PathDiagnosticControlFlowPiece>(*NextI);
1925bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1926481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    if (!PieceNextI) {
1927481da5554d03271b0d87b695449963f7728c5895Ted Kremenek      ++I;
1928bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      continue;
1929481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    }
1930bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1931f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
1932f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *s2End   = getLocStmt(PieceNextI->getEndLocation());
1933f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *level3 = getStmtParent(s2Start, PM);
1934f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    const Stmt *level4 = getStmtParent(s2End, PM);
1935bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1936bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // Rule I.
1937bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    //
1938bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // If we have two consecutive control edges whose end/begin locations
1939481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    // are at the same level (e.g. statements or top-level expressions within
1940481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    // a compound statement, or siblings share a single ancestor expression),
1941481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    // then merge them if they have no interesting intermediate event.
1942bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    //
1943bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // For example:
1944bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    //
1945bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
1946481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    // parent is '1'.  Here 'x.y.z' represents the hierarchy of statements.
1947bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    //
1948bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // NOTE: this will be limited later in cases where we add barriers
1949bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    // to prevent this optimization.
1950f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    //
1951481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
1952bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      PieceI->setEndLocation(PieceNextI->getEndLocation());
1953bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      path.erase(NextI);
1954bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      hasChanges = true;
1955bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      continue;
1956bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek    }
1957b9e13d555fc9f3e5515e2b1fa6f720e6f10bb076Ted Kremenek
1958b9e13d555fc9f3e5515e2b1fa6f720e6f10bb076Ted Kremenek    // Rule II.
1959b9e13d555fc9f3e5515e2b1fa6f720e6f10bb076Ted Kremenek    //
1960a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek    // Eliminate edges between subexpressions and parent expressions
1961a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek    // when the subexpression is consumed.
1962f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    //
1963f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    // NOTE: this will be limited later in cases where we add barriers
1964f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    // to prevent this optimization.
1965f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    //
196696b8134337883908fcc45484486fe200d6b3e32fTed Kremenek    if (s1End && s1End == s2Start && level2) {
196796b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      if (isIncrementOrInitInForLoop(s1End, level2) ||
196896b8134337883908fcc45484486fe200d6b3e32fTed Kremenek          (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End)) &&
196996b8134337883908fcc45484486fe200d6b3e32fTed Kremenek            !isConditionForTerminator(level2, s1End)))
197096b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      {
197196b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        PieceI->setEndLocation(PieceNextI->getEndLocation());
197296b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        path.erase(NextI);
197396b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        hasChanges = true;
197496b8134337883908fcc45484486fe200d6b3e32fTed Kremenek        continue;
197596b8134337883908fcc45484486fe200d6b3e32fTed Kremenek      }
1976f468fa16e37fc8fa6a915fe36aee8f0434709789Ted Kremenek    }
1977481da5554d03271b0d87b695449963f7728c5895Ted Kremenek
1978481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    // No changes at this index?  Move to the next one.
1979481da5554d03271b0d87b695449963f7728c5895Ted Kremenek    ++I;
1980bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  }
1981bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
1982bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  // No changes.
1983bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek  return hasChanges;
1984bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek}
1985bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
19865fb5dfb6646464db3cd6d54a6332375c8fe36b75Ted Kremenek//===----------------------------------------------------------------------===//
19873106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// Methods for BugType and subclasses.
19883106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
1989404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios KyrtzidisBugType::~BugType() { }
1990404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis
19913106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenekvoid BugType::FlushReports(BugReporter &BR) {}
19923106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
199399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid BuiltinBug::anchor() {}
199499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
19953106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
19963106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// Methods for BugReport and subclasses.
19973106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
1998e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks
199999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid BugReport::NodeResolver::anchor() {}
200099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
20018e6431adab313e283a992698f6fc7afe62420999Anna Zaksvoid BugReport::addVisitor(BugReporterVisitor* visitor) {
20028e6431adab313e283a992698f6fc7afe62420999Anna Zaks  if (!visitor)
20038e6431adab313e283a992698f6fc7afe62420999Anna Zaks    return;
20048e6431adab313e283a992698f6fc7afe62420999Anna Zaks
20058e6431adab313e283a992698f6fc7afe62420999Anna Zaks  llvm::FoldingSetNodeID ID;
20068e6431adab313e283a992698f6fc7afe62420999Anna Zaks  visitor->Profile(ID);
20078e6431adab313e283a992698f6fc7afe62420999Anna Zaks  void *InsertPos;
20088e6431adab313e283a992698f6fc7afe62420999Anna Zaks
20098e6431adab313e283a992698f6fc7afe62420999Anna Zaks  if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
20108e6431adab313e283a992698f6fc7afe62420999Anna Zaks    delete visitor;
20118e6431adab313e283a992698f6fc7afe62420999Anna Zaks    return;
20128e6431adab313e283a992698f6fc7afe62420999Anna Zaks  }
20138e6431adab313e283a992698f6fc7afe62420999Anna Zaks
20148e6431adab313e283a992698f6fc7afe62420999Anna Zaks  CallbacksSet.InsertNode(visitor, InsertPos);
20153bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose  Callbacks.push_back(visitor);
20163bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose  ++ConfigurationChangeToken;
20178e6431adab313e283a992698f6fc7afe62420999Anna Zaks}
20188e6431adab313e283a992698f6fc7afe62420999Anna Zaks
20198e6431adab313e283a992698f6fc7afe62420999Anna ZaksBugReport::~BugReport() {
20208e6431adab313e283a992698f6fc7afe62420999Anna Zaks  for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
2021dc757b049796949e4b11646445a6598f0bdabd7aAnna Zaks    delete *I;
20228e6431adab313e283a992698f6fc7afe62420999Anna Zaks  }
2023c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  while (!interestingSymbols.empty()) {
2024c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    popInterestingSymbolsAndRegions();
2025c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
20268e6431adab313e283a992698f6fc7afe62420999Anna Zaks}
2027e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks
202807189521a15d9c088216b943649cb9fe231cbb57Ted Kremenekconst Decl *BugReport::getDeclWithIssue() const {
202907189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  if (DeclWithIssue)
203007189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek    return DeclWithIssue;
203107189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek
203207189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  const ExplodedNode *N = getErrorNode();
203307189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  if (!N)
203407189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek    return 0;
203507189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek
203607189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  const LocationContext *LC = N->getLocationContext();
203707189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  return LC->getCurrentStackFrame()->getDecl();
203807189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek}
203907189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek
2040e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaksvoid BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2041e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks  hash.AddPointer(&BT);
2042e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks  hash.AddString(Description);
204397bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks  PathDiagnosticLocation UL = getUniqueingLocation();
204497bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks  if (UL.isValid()) {
204597bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks    UL.Profile(hash);
2046ca8e36eb637e232475ef31c3f22d5da907390917Anna Zaks  } else if (Location.isValid()) {
2047590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks    Location.Profile(hash);
2048590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks  } else {
2049590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks    assert(ErrorNode);
2050590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks    hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
2051590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks  }
2052e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks
2053e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks  for (SmallVectorImpl<SourceRange>::const_iterator I =
2054e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks      Ranges.begin(), E = Ranges.end(); I != E; ++I) {
2055e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    const SourceRange range = *I;
2056e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    if (!range.isValid())
2057e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks      continue;
2058e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    hash.AddInteger(range.getBegin().getRawEncoding());
2059e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    hash.AddInteger(range.getEnd().getRawEncoding());
2060e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks  }
2061e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks}
20623106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
206376aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenekvoid BugReport::markInteresting(SymbolRef sym) {
206476aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (!sym)
206576aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek    return;
20663bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose
20673bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose  // If the symbol wasn't already in our set, note a configuration change.
2068c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  if (getInterestingSymbols().insert(sym).second)
20693bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose    ++ConfigurationChangeToken;
20708ec588e2ac57311604cf80608c7d4b3fb3b022f7Jordy Rose
20718ec588e2ac57311604cf80608c7d4b3fb3b022f7Jordy Rose  if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
2072c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    getInterestingRegions().insert(meta->getRegion());
207376aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
207476aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
207576aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenekvoid BugReport::markInteresting(const MemRegion *R) {
207676aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (!R)
207776aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek    return;
20783bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose
20793bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose  // If the base region wasn't already in our set, note a configuration change.
208076aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  R = R->getBaseRegion();
2081c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  if (getInterestingRegions().insert(R).second)
20823bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose    ++ConfigurationChangeToken;
20838ec588e2ac57311604cf80608c7d4b3fb3b022f7Jordy Rose
208476aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2085c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    getInterestingSymbols().insert(SR->getSymbol());
208676aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
208776aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
208876aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenekvoid BugReport::markInteresting(SVal V) {
208976aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  markInteresting(V.getAsRegion());
209076aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  markInteresting(V.getAsSymbol());
209176aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
209276aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
209380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaksvoid BugReport::markInteresting(const LocationContext *LC) {
209480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks  if (!LC)
209580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks    return;
209680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks  InterestingLocationContexts.insert(LC);
209780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks}
209880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
2099c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekbool BugReport::isInteresting(SVal V) {
210076aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
210176aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
210276aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
2103c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekbool BugReport::isInteresting(SymbolRef sym) {
210476aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (!sym)
210576aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek    return false;
21068ec588e2ac57311604cf80608c7d4b3fb3b022f7Jordy Rose  // We don't currently consider metadata symbols to be interesting
21078ec588e2ac57311604cf80608c7d4b3fb3b022f7Jordy Rose  // even if we know their region is interesting. Is that correct behavior?
2108c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  return getInterestingSymbols().count(sym);
210976aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
211076aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
2111c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekbool BugReport::isInteresting(const MemRegion *R) {
211276aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (!R)
211376aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek    return false;
211476aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  R = R->getBaseRegion();
2115c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  bool b = getInterestingRegions().count(R);
211676aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (b)
211776aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek    return true;
211876aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2119c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    return getInterestingSymbols().count(SR->getSymbol());
212076aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek  return false;
212176aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek}
2122c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
212380de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaksbool BugReport::isInteresting(const LocationContext *LC) {
212480de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks  if (!LC)
212580de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks    return false;
212680de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks  return InterestingLocationContexts.count(LC);
212780de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks}
212880de487e03dd0f44e4572e2122ebc1aa6a3961f5Anna Zaks
2129c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekvoid BugReport::lazyInitializeInterestingSets() {
2130c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  if (interestingSymbols.empty()) {
2131c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    interestingSymbols.push_back(new Symbols());
2132c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    interestingRegions.push_back(new Regions());
2133c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
2134c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
2135c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2136c4bac8e376b98d633bb00ee5f510d5e58449753cTed KremenekBugReport::Symbols &BugReport::getInterestingSymbols() {
2137c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  lazyInitializeInterestingSets();
2138c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  return *interestingSymbols.back();
2139c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
2140c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2141c4bac8e376b98d633bb00ee5f510d5e58449753cTed KremenekBugReport::Regions &BugReport::getInterestingRegions() {
2142c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  lazyInitializeInterestingSets();
2143c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  return *interestingRegions.back();
2144c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
2145c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2146c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekvoid BugReport::pushInterestingSymbolsAndRegions() {
2147c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
2148c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  interestingRegions.push_back(new Regions(getInterestingRegions()));
2149c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
2150c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2151c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekvoid BugReport::popInterestingSymbolsAndRegions() {
2152c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  delete interestingSymbols.back();
2153c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  interestingSymbols.pop_back();
2154c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  delete interestingRegions.back();
2155c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  interestingRegions.pop_back();
2156c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
215776aadc346c3a4c363238a1e1232f324c3355d9e0Ted Kremenek
21589c378f705405d37f49795d5e915989de774fe11fTed Kremenekconst Stmt *BugReport::getStmt() const {
2159e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks  if (!ErrorNode)
2160e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    return 0;
2161e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks
2162212f6d3b5fb3fa55ba1e40671cfc336430abc8ddTom Care  ProgramPoint ProgP = ErrorNode->getLocation();
21635f85e17df3f5b0a8021443f2b590daecfb2cbd17Ted Kremenek  const Stmt *S = NULL;
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21657a95de68c093991047ed8d339479ccad51b88663David Blaikie  if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2166fafd3834754d2093e0ad7a1c005860fd527ecb7fZhongxing Xu    CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
216750d5bc4e3b53eb8bb4ebf198f1213348a3fa0f38Zhongxing Xu    if (BE->getBlock() == &Exit)
2168212f6d3b5fb3fa55ba1e40671cfc336430abc8ddTom Care      S = GetPreviousStmt(ErrorNode);
21693106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  }
21705f85e17df3f5b0a8021443f2b590daecfb2cbd17Ted Kremenek  if (!S)
21710f8579274a010f360a371b53101859d9d6052314Anna Zaks    S = PathDiagnosticLocation::getStmt(ErrorNode);
21721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return S;
21743106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
21753106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
2176640ccf071076e684713cc3c3276bb51982bff607Argyrios Kyrtzidisstd::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
2177e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna ZaksBugReport::getRanges() {
2178e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    // If no custom ranges, add the range of the statement corresponding to
2179e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    // the error node.
2180e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    if (Ranges.empty()) {
2181e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks      if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
2182e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks        addRange(E->getSourceRange());
2183e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks      else
2184e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks        return std::make_pair(ranges_iterator(), ranges_iterator());
2185e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    }
2186e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks
218714924267950e75c6c1f6fcea39fa507b7168bc39Anna Zaks    // User-specified absence of range info.
218814924267950e75c6c1f6fcea39fa507b7168bc39Anna Zaks    if (Ranges.size() == 1 && !Ranges.begin()->isValid())
218914924267950e75c6c1f6fcea39fa507b7168bc39Anna Zaks      return std::make_pair(ranges_iterator(), ranges_iterator());
219014924267950e75c6c1f6fcea39fa507b7168bc39Anna Zaks
2191e172e8b9e7fc67d7d03589af7e92fe777afcf33aAnna Zaks    return std::make_pair(Ranges.begin(), Ranges.end());
21923106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
21933106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
2194590dd8e0959d8df5621827768987c4792b74fc06Anna ZaksPathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
2195b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks  if (ErrorNode) {
2196590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks    assert(!Location.isValid() &&
2197b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks     "Either Location or ErrorNode should be specified but not both.");
21980f8579274a010f360a371b53101859d9d6052314Anna Zaks    return PathDiagnosticLocation::createEndOfPath(ErrorNode, SM);
2199b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks  } else {
2200b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks    assert(Location.isValid());
2201b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks    return Location;
2202b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks  }
2203b7530a4ca9a7ef62350682bbb374a06de6fdaa9fAnna Zaks
2204590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks  return PathDiagnosticLocation();
22053106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
22063106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22073106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
22083106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// Methods for BugReporter and subclasses.
22093106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
22103106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22114a5f724538cbc275370c9504e8169ce92503256cBenjamin KramerBugReportEquivClass::~BugReportEquivClass() { }
2212a2f4ec0df645fc249d2945beef9653f03b175417Zhongxing XuGRBugReporter::~GRBugReporter() { }
22133106198188ac6eb2753f1764d5c28376b0b76351Ted KremenekBugReporterData::~BugReporterData() {}
22143106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
221538b02b912e1a55c912f603c4369431264d36a381Zhongxing XuExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
22163106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
221718c66fdc3c4008d335885695fe36fb5353c5f672Ted KremenekProgramStateManager&
22183106198188ac6eb2753f1764d5c28376b0b76351Ted KremenekGRBugReporter::getStateManager() { return Eng.getStateManager(); }
22193106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22203b030a28cda2b953758507769c1d436bec5ec45eAnna ZaksBugReporter::~BugReporter() {
22213b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks  FlushReports();
22223b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks
22233b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks  // Free the bug reports we are tracking.
22243b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks  typedef std::vector<BugReportEquivClass *> ContTy;
22253b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks  for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
22263b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks       I != E; ++I) {
22273b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks    delete *I;
22283b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks  }
22293b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks}
22303106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22313106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenekvoid BugReporter::FlushReports() {
22323106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  if (BugTypes.isEmpty())
22333106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    return;
22343106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22353106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  // First flush the warnings for each BugType.  This may end up creating new
2236404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // warnings and new BugTypes.
2237404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // FIXME: Only NSErrorChecker needs BugType's FlushReports.
2238404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // Turn NSErrorChecker into a proper checker and remove this.
22395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<const BugType*, 16> bugTypes;
22403106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2241404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    bugTypes.push_back(*I);
22425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVector<const BugType*, 16>::iterator
2243404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis         I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
22443106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    const_cast<BugType*>(*I)->FlushReports(*this);
22453106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
2246d015f4febe85d3e3340172d70042840c51bbd836Anna Zaks  // We need to flush reports in deterministic order to ensure the order
2247d015f4febe85d3e3340172d70042840c51bbd836Anna Zaks  // of the reports is consistent between runs.
22480eb6c37dd4e4ad8fa2363003dea270f9fd6c2969Anna Zaks  typedef std::vector<BugReportEquivClass *> ContVecTy;
22490eb6c37dd4e4ad8fa2363003dea270f9fd6c2969Anna Zaks  for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
22500eb6c37dd4e4ad8fa2363003dea270f9fd6c2969Anna Zaks       EI != EE; ++EI){
22510eb6c37dd4e4ad8fa2363003dea270f9fd6c2969Anna Zaks    BugReportEquivClass& EQ = **EI;
2252404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    FlushReport(EQ);
22533106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  }
22543106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
2255404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // BugReporter owns and deletes only BugTypes created implicitly through
2256404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // EmitBasicReport.
2257404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // FIXME: There are leaks from checkers that assume that the BugTypes they
2258404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // create will be destroyed by the BugReporter.
2259404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  for (llvm::StringMap<BugType*>::iterator
2260404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis         I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
2261404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    delete I->second;
2262404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis
22633106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  // Remove all references to the BugType objects.
22643baf672378f105602d2b12f03f00277ae1936fe9Ted Kremenek  BugTypes = F.getEmptySet();
22653106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
22663106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22673106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
22683106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek// PathDiagnostics generation.
22693106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek//===----------------------------------------------------------------------===//
22703106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
22711efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rosenamespace {
22721efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose/// A wrapper around a report graph, which contains only a single path, and its
22731efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose/// node maps.
22741efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Roseclass ReportGraph {
22751efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rosepublic:
22761efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  InterExplodedGraphMap BackMap;
2277228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  OwningPtr<ExplodedGraph> Graph;
2278228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  const ExplodedNode *ErrorNode;
22791efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  size_t Index;
22801efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose};
22811efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose
22821efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose/// A wrapper around a trimmed graph and its node maps.
22831efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Roseclass TrimmedGraph {
22841efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  InterExplodedGraphMap InverseMap;
22852110350909701fcd6b55c636e24a675f0a905feaJordan Rose
22862110350909701fcd6b55c636e24a675f0a905feaJordan Rose  typedef llvm::DenseMap<const ExplodedNode *, unsigned> PriorityMapTy;
22872110350909701fcd6b55c636e24a675f0a905feaJordan Rose  PriorityMapTy PriorityMap;
22882110350909701fcd6b55c636e24a675f0a905feaJordan Rose
2289228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  typedef std::pair<const ExplodedNode *, size_t> NodeIndexPair;
2290228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  SmallVector<NodeIndexPair, 32> ReportNodes;
2291228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
22921efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  OwningPtr<ExplodedGraph> G;
2293228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2294228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  /// A helper class for sorting ExplodedNodes by priority.
2295228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  template <bool Descending>
2296228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  class PriorityCompare {
2297228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    const PriorityMapTy &PriorityMap;
2298228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2299228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  public:
2300228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2301228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2302228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2303228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2304228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2305228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      PriorityMapTy::const_iterator E = PriorityMap.end();
2306228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2307228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      if (LI == E)
2308228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose        return Descending;
2309228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      if (RI == E)
2310228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose        return !Descending;
2311228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2312228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      return Descending ? LI->second > RI->second
2313228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose                        : LI->second < RI->second;
2314228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    }
2315228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2316228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    bool operator()(const NodeIndexPair &LHS, const NodeIndexPair &RHS) const {
2317228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      return (*this)(LHS.first, RHS.first);
2318228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    }
2319228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  };
2320228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
23211efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rosepublic:
23221efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  TrimmedGraph(const ExplodedGraph *OriginalGraph,
23232110350909701fcd6b55c636e24a675f0a905feaJordan Rose               ArrayRef<const ExplodedNode *> Nodes);
23241efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose
2325228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  bool popNextReportGraph(ReportGraph &GraphWrapper);
23261efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose};
23271efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose}
23281efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose
23292110350909701fcd6b55c636e24a675f0a905feaJordan RoseTrimmedGraph::TrimmedGraph(const ExplodedGraph *OriginalGraph,
23302110350909701fcd6b55c636e24a675f0a905feaJordan Rose                           ArrayRef<const ExplodedNode *> Nodes) {
23312110350909701fcd6b55c636e24a675f0a905feaJordan Rose  // The trimmed graph is created in the body of the constructor to ensure
23322110350909701fcd6b55c636e24a675f0a905feaJordan Rose  // that the DenseMaps have been initialized already.
2333228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  InterExplodedGraphMap ForwardMap;
23340f3a34fb7fea37ebfbcba8b400ccb697b9559b49Jordan Rose  G.reset(OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap));
23351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23363106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  // Find the (first) error node in the trimmed graph.  We just need to consult
23371efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  // the node map which maps from nodes in the original graph to nodes
23383106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  // in the new graph.
2339228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
23403106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
23411efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  for (unsigned i = 0, count = Nodes.size(); i < count; ++i) {
2342228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    if (const ExplodedNode *NewNode = ForwardMap.lookup(Nodes[i])) {
2343228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      ReportNodes.push_back(std::make_pair(NewNode, i));
2344228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      RemainingNodes.insert(NewNode);
23453106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    }
234640406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  }
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2348228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2349228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2350228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  // Perform a forward BFS to find all the shortest paths.
2351228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  std::queue<const ExplodedNode *> WS;
2352228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose
2353228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  assert(G->num_roots() == 1);
2354228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  WS.push(*G->roots_begin());
2355228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  unsigned Priority = 0;
23563106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
23573106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  while (!WS.empty()) {
2358228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    const ExplodedNode *Node = WS.front();
23593106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    WS.pop();
23601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23612110350909701fcd6b55c636e24a675f0a905feaJordan Rose    PriorityMapTy::iterator PriorityEntry;
23622110350909701fcd6b55c636e24a675f0a905feaJordan Rose    bool IsNew;
23632110350909701fcd6b55c636e24a675f0a905feaJordan Rose    llvm::tie(PriorityEntry, IsNew) =
23642110350909701fcd6b55c636e24a675f0a905feaJordan Rose      PriorityMap.insert(std::make_pair(Node, Priority));
2365228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    ++Priority;
23661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23672110350909701fcd6b55c636e24a675f0a905feaJordan Rose    if (!IsNew) {
23682110350909701fcd6b55c636e24a675f0a905feaJordan Rose      assert(PriorityEntry->second <= Priority);
23692110350909701fcd6b55c636e24a675f0a905feaJordan Rose      continue;
23702110350909701fcd6b55c636e24a675f0a905feaJordan Rose    }
23711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2372228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    if (RemainingNodes.erase(Node))
2373228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      if (RemainingNodes.empty())
2374228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose        break;
2375a5f80b2ea6d30c5055c067530d63bb0dcaf937d0Jordan Rose
2376228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    for (ExplodedNode::const_pred_iterator I = Node->succ_begin(),
2377228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose                                           E = Node->succ_end();
23782110350909701fcd6b55c636e24a675f0a905feaJordan Rose         I != E; ++I)
2379228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      WS.push(*I);
2380239b6e47d282bd66c8b559ac47b8b42b34da619eJordan Rose  }
23812110350909701fcd6b55c636e24a675f0a905feaJordan Rose
2382228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  // Sort the error paths from longest to shortest.
2383228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  std::sort(ReportNodes.begin(), ReportNodes.end(),
2384228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose            PriorityCompare<true>(PriorityMap));
2385228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose}
23862110350909701fcd6b55c636e24a675f0a905feaJordan Rose
2387228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rosebool TrimmedGraph::popNextReportGraph(ReportGraph &GraphWrapper) {
2388228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  if (ReportNodes.empty())
2389228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    return false;
23902110350909701fcd6b55c636e24a675f0a905feaJordan Rose
2391228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  const ExplodedNode *OrigN;
2392228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  llvm::tie(OrigN, GraphWrapper.Index) = ReportNodes.pop_back_val();
2393228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  assert(PriorityMap.find(OrigN) != PriorityMap.end() &&
2394228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose         "error node not accessible from root");
23952110350909701fcd6b55c636e24a675f0a905feaJordan Rose
23962110350909701fcd6b55c636e24a675f0a905feaJordan Rose  // Create a new graph with a single path.  This is the graph
23972110350909701fcd6b55c636e24a675f0a905feaJordan Rose  // that will be returned to the caller.
23982110350909701fcd6b55c636e24a675f0a905feaJordan Rose  ExplodedGraph *GNew = new ExplodedGraph();
23992110350909701fcd6b55c636e24a675f0a905feaJordan Rose  GraphWrapper.Graph.reset(GNew);
2400228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  GraphWrapper.BackMap.clear();
24011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2402228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  // Now walk from the error node up the BFS path, always taking the
2403228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  // predeccessor with the lowest number.
2404228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  ExplodedNode *Succ = 0;
2405228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  while (true) {
24063106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Create the equivalent node in the new graph with the same state
24073106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // and location.
24083e5ebf1a05603e08f2d0b2b2a5fa9406fe4cfb22Jordan Rose    ExplodedNode *NewN = GNew->getNode(OrigN->getLocation(), OrigN->getState(),
24093e5ebf1a05603e08f2d0b2b2a5fa9406fe4cfb22Jordan Rose                                       OrigN->isSink());
24101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24113106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Store the mapping to the original node.
2412228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    InterExplodedGraphMap::const_iterator IMitr = InverseMap.find(OrigN);
24133106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    assert(IMitr != InverseMap.end() && "No mapping to original node.");
24141efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose    GraphWrapper.BackMap[NewN] = IMitr->second;
24151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24163106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Link up the new node with the previous node.
2417228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    if (Succ)
2418228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      Succ->addPredecessor(NewN, *GNew);
2419228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    else
2420228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      GraphWrapper.ErrorNode = NewN;
24211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2422228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    Succ = NewN;
24231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24243106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Are we at the final node?
2425228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    if (OrigN->pred_empty()) {
2426228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      GNew->addRoot(NewN);
24273106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      break;
24283106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    }
24291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2430228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    // Find the next predeccessor node.  We choose the node that is marked
24312110350909701fcd6b55c636e24a675f0a905feaJordan Rose    // with the lowest BFS number.
2432228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2433228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose                          PriorityCompare<false>(PriorityMap));
24342110350909701fcd6b55c636e24a675f0a905feaJordan Rose  }
24352110350909701fcd6b55c636e24a675f0a905feaJordan Rose
2436228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  return true;
24372110350909701fcd6b55c636e24a675f0a905feaJordan Rose}
24382110350909701fcd6b55c636e24a675f0a905feaJordan Rose
24392110350909701fcd6b55c636e24a675f0a905feaJordan Rose
24403106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
24413106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek///  and collapses PathDiagosticPieces that are expanded by macros.
244277d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenekstatic void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
24432042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek  typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
24442042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek                                SourceLocation> > MacroStackTy;
24451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2446c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith  typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
24473106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek          PiecesTy;
24481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24493106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  MacroStackTy MacroStack;
24503106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  PiecesTy Pieces;
24511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2452ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek  for (PathPieces::const_iterator I = path.begin(), E = path.end();
24532042fc1f36d471f437023e8899f0c4fadded2341Ted Kremenek       I!=E; ++I) {
245477d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek
2455ed866e73bab7733f5226f84c52edefe23d694b2fTed Kremenek    PathDiagnosticPiece *piece = I->getPtr();
245677d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek
245777d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    // Recursively compact calls.
245877d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
245977d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek      CompactPathDiagnostic(call->path, SM);
246077d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    }
246177d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek
24623106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Get the location of the PathDiagnosticPiece.
246377d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    const FullSourceLoc Loc = piece->getLocation().asLocation();
24641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24653106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Determine the instantiation location, which is the location we group
24663106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // related PathDiagnosticPieces.
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SourceLocation InstantiationLoc = Loc.isMacroID() ?
2468402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth                                      SM.getExpansionLoc(Loc) :
24693106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek                                      SourceLocation();
24701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24713106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    if (Loc.isFileID()) {
24723106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      MacroStack.clear();
247377d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek      Pieces.push_back(piece);
24743106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      continue;
24753106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    }
2476706e3cf2fa7523b5497c52c11a2b6dad2a1ce2acTed Kremenek
24773106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    assert(Loc.isMacroID());
24781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24793106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Is the PathDiagnosticPiece within the same macro group?
24803106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
248177d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek      MacroStack.back().first->subPieces.push_back(piece);
24823106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      continue;
24833106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    }
2484d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek
24853106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // We aren't in the same group.  Are we descending into a new macro
24863106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // or are part of an old one?
2487c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith    IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
2488d8c938b0f6b7a7156181be575239e4c6e15a2adbTed Kremenek
24893106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2490402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth                                          SM.getExpansionLoc(Loc) :
24913106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek                                          SourceLocation();
24921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24933106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Walk the entire macro stack.
24943106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    while (!MacroStack.empty()) {
24953106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      if (InstantiationLoc == MacroStack.back().second) {
24963106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        MacroGroup = MacroStack.back().first;
24973106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        break;
24983106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      }
24991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25003106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      if (ParentInstantiationLoc == MacroStack.back().second) {
25013106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        MacroGroup = MacroStack.back().first;
25023106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        break;
250361f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek      }
25041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25053106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      MacroStack.pop_back();
25066837faa083bebad39aa342f84c2b450fb6410eafTed Kremenek    }
25071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25083106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
25093106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      // Create a new macro group and add it to the stack.
2510590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks      PathDiagnosticMacroPiece *NewGroup =
2511590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks        new PathDiagnosticMacroPiece(
251277d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek          PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
25135a429954aedd8a830a70fcea2cb51561673e4fc3Ted Kremenek
25143106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      if (MacroGroup)
2515802e02463b880f53a6e645bde78cc412481ce9e0Ted Kremenek        MacroGroup->subPieces.push_back(NewGroup);
25163106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      else {
25173106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        assert(InstantiationLoc.isFileID());
25183106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek        Pieces.push_back(NewGroup);
25193106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      }
25201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25213106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      MacroGroup = NewGroup;
25223106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek      MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
25237dc8664a54f4ede40a5f4adee3f5081a59d7ee1cTed Kremenek    }
25243106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek
25253106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek    // Finally, add the PathDiagnosticPiece to the group.
252677d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    MacroGroup->subPieces.push_back(piece);
25273106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  }
25281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25293106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek  // Now take the pieces and construct a new PathDiagnostic.
253077d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek  path.clear();
25311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
253277d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek  for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
253377d09441e59d3bced6c3d55505eb3a67a784fe02Ted Kremenek    path.push_back(*I);
253461f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek}
253561f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
25368347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rosebool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
2537c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                           PathDiagnosticConsumer &PC,
2538c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                           ArrayRef<BugReport *> &bugReports) {
253940406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  assert(!bugReports.empty());
25408347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose
25419a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose  bool HasValid = false;
2542228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  bool HasInvalid = false;
25432f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose  SmallVector<const ExplodedNode *, 32> errorNodes;
2544c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
25459a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose                                      E = bugReports.end(); I != E; ++I) {
25469a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose    if ((*I)->isValid()) {
25479a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose      HasValid = true;
25489a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose      errorNodes.push_back((*I)->getErrorNode());
25499a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose    } else {
2550228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      // Keep the errorNodes list in sync with the bugReports list.
2551228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose      HasInvalid = true;
25529a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose      errorNodes.push_back(0);
25539a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose    }
255440406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  }
25551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25569a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose  // If all the reports have been marked invalid by a previous path generation,
25579a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose  // we're done.
25589a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose  if (!HasValid)
25599a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose    return false;
25609a9fe4068eed2fc72ec985e5ae393fb79a8fb9adJordan Rose
25612f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose  typedef PathDiagnosticConsumer::PathGenerationScheme PathGenerationScheme;
25622f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose  PathGenerationScheme ActiveScheme = PC.getGenerationScheme();
25632f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
2564af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  if (ActiveScheme == PathDiagnosticConsumer::Extensive) {
2565af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    AnalyzerOptions &options = getEngine().getAnalysisManager().options;
256615676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek    if (options.getBooleanOption("path-diagnostics-alternate", false)) {
2567af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      ActiveScheme = PathDiagnosticConsumer::AlternateExtensive;
2568af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek    }
2569af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek  }
2570af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek
25711efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose  TrimmedGraph TrimG(&getGraph(), errorNodes);
2572228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  ReportGraph ErrorGraph;
25731efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose
2574228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  while (TrimG.popNextReportGraph(ErrorGraph)) {
25752f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // Find the BugReport with the original location.
25761efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose    assert(ErrorGraph.Index < bugReports.size());
25771efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose    BugReport *R = bugReports[ErrorGraph.Index];
25782f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    assert(R && "No original report found for sliced graph.");
25792f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
25802f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
25812f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // Start building the path diagnostic...
25821efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose    PathDiagnosticBuilder PDB(*this, R, ErrorGraph.BackMap, &PC);
25831efffab67364f5afcc25f5f5f77e0f7ba5d41055Jordan Rose    const ExplodedNode *N = ErrorGraph.ErrorNode;
25842f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
25852f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // Register additional node visitors.
25862f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    R->addVisitor(new NilReceiverBRVisitor());
25872f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    R->addVisitor(new ConditionBRVisitor());
25882f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    R->addVisitor(new LikelyFalsePositiveSuppressionBRVisitor());
25892f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
25902f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    BugReport::VisitorList visitors;
25912f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    unsigned origReportConfigToken, finalReportConfigToken;
2592d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek    LocationContextMap LCM;
25932f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
25942f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // While generating diagnostics, it's possible the visitors will decide
25952f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // new symbols and regions are interesting, or add other visitors based on
25962f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // the information they find. If they do, we need to regenerate the path
25972f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // based on our new report configuration.
25982f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    do {
25992f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Get a clean copy of all the visitors.
26002f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      for (BugReport::visitor_iterator I = R->visitor_begin(),
26012f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose                                       E = R->visitor_end(); I != E; ++I)
26022f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        visitors.push_back((*I)->clone());
26032f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
26042f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Clear out the active path from any previous work.
26052f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      PD.resetPath();
26062f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      origReportConfigToken = R->getConfigurationChangeToken();
26072f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
26082f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Generate the very last diagnostic piece - the piece is visible before
26092f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // the trace is expanded.
26102f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      PathDiagnosticPiece *LastPiece = 0;
26112f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
26122f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose          I != E; ++I) {
26132f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
26142f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose          assert (!LastPiece &&
26152f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose              "There can only be one final piece in a diagnostic.");
26162f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose          LastPiece = Piece;
26172f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        }
26183bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose      }
261986ff12c8a8a356ca284ca7687749216fbfd74519Anna Zaks
26202f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      if (ActiveScheme != PathDiagnosticConsumer::None) {
26212f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        if (!LastPiece)
26222f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose          LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
26232f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        assert(LastPiece);
2624d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose        PD.setEndOfPath(LastPiece);
26258347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose      }
26262f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
2627d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek      // Make sure we get a clean location context map so we don't
2628d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek      // hold onto old mappings.
2629d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek      LCM.clear();
2630d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek
26312f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      switch (ActiveScheme) {
2632af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek      case PathDiagnosticConsumer::AlternateExtensive:
2633af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        GenerateAlternateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
2634af2836593979d4973bec5bd21f10eb6cc0d0f3e3Ted Kremenek        break;
26352f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      case PathDiagnosticConsumer::Extensive:
2636d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        GenerateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
26372f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        break;
26382f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      case PathDiagnosticConsumer::Minimal:
2639d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        GenerateMinimalPathDiagnostic(PD, PDB, N, LCM, visitors);
26402f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        break;
26412f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      case PathDiagnosticConsumer::None:
26422f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors);
26432f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        break;
2644d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose      }
26453bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose
26462f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Clean up the visitors we used.
26472f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      llvm::DeleteContainerPointers(visitors);
26483bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose
26492f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Did anything change while generating this path?
26502f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      finalReportConfigToken = R->getConfigurationChangeToken();
26512f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    } while (finalReportConfigToken != origReportConfigToken);
26523bc75ca0a636efdc93471c9b6bad43085a22bf3aJordy Rose
2653228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose    if (!R->isValid())
26542f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      continue;
2655b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek
26562f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // Finally, prune the diagnostic path of uninteresting stuff.
26572f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    if (!PD.path.empty()) {
26582f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      // Remove messages that are basically the same.
26592f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      removeRedundantMsgs(PD.getMutablePieces());
26602f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
26612f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      if (R->shouldPrunePath() &&
26622f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose          getEngine().getAnalysisManager().options.shouldPrunePaths()) {
2663d306a530fca74e40916121f5583e0545e470b3c4Ted Kremenek        bool stillHasNotes = removeUnneededCalls(PD.getMutablePieces(), R, LCM);
26642f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        assert(stillHasNotes);
26652f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose        (void)stillHasNotes;
26662f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      }
26672f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose
26682f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose      adjustCallLocations(PD.getMutablePieces());
2669bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek
2670bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      if (ActiveScheme == PathDiagnosticConsumer::AlternateExtensive) {
267115676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek        OptimizedCallsSet OCS;
2672a399f776ee29e099da33eaf7f9d585b4edc4b61dTed Kremenek        while (optimizeEdges(PD.getMutablePieces(), getSourceManager(),
267315676bec6fa9ad9466d93c163f2d1b8a3f559b3aTed Kremenek                             OCS, LCM)) {}
2674bb521b8f14ca29ee4e17ae1f9877586ef0bf8378Ted Kremenek      }
2675b85cce094887ab5cf1c47acfe306e2fb1d3cfbb1Ted Kremenek    }
2676afa7cae15b117c4b75794c6c32424953d94b4359Jordan Rose
26772f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    // We found a report and didn't suppress it.
26782f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose    return true;
2679ed7948b55fa4b2505f240cc5287137f451172b4cTed Kremenek  }
26808347d3d45e6f128bba19821f0d2f54cadd4d49bbJordan Rose
26812f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose  // We suppressed all the reports in this equivalence class.
2682228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  assert(!HasInvalid && "Inconsistent suppression");
2683228094a28f81ddba94427239dea5c6e59ff6aabcJordan Rose  (void)HasInvalid;
26842f13eb116e62161c5e4d198f7831f226e5cea9daJordan Rose  return false;
26853106198188ac6eb2753f1764d5c28376b0b76351Ted Kremenek}
26861aa44c7c9198796085b77c6bdd7c90030845a688Ted Kremenek
2687cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenekvoid BugReporter::Register(BugType *BT) {
26883baf672378f105602d2b12f03f00277ae1936fe9Ted Kremenek  BugTypes = F.add(BugTypes, BT);
268976d90c8bf83d071fd1bdbffa6d9f2af87c6bb7b5Ted Kremenek}
269076d90c8bf83d071fd1bdbffa6d9f2af87c6bb7b5Ted Kremenek
2691785950e59424dca7ce0081bebf13c0acd2c4fff6Jordan Rosevoid BugReporter::emitReport(BugReport* R) {
2692cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  // Compute the bug report's hash to determine its equivalence class.
2693cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  llvm::FoldingSetNodeID ID;
2694cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  R->Profile(ID);
26951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Lookup the equivance class.  If there isn't one, create it.
2697cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  BugType& BT = R->getBugType();
2698cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  Register(&BT);
2699cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  void *InsertPos;
2700404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
27011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2702cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  if (!EQ) {
2703cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek    EQ = new BugReportEquivClass(R);
2704404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    EQClasses.InsertNode(EQ, InsertPos);
27053b030a28cda2b953758507769c1d436bec5ec45eAnna Zaks    EQClassesVector.push_back(EQ);
2706cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  }
2707cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  else
2708cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek    EQ->AddReport(R);
270961f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek}
271061f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek
271106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
271206c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek//===----------------------------------------------------------------------===//
271306c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek// Emitting reports in equivalence classes.
271406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek//===----------------------------------------------------------------------===//
271506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
271606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremeneknamespace {
2717ba5fb5a955c896815c439289fc51c03cf0635129Kovarththanan Rajaratnamstruct FRIEC_WLItem {
271806c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  const ExplodedNode *N;
271906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  ExplodedNode::const_succ_iterator I, E;
272006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
272106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  FRIEC_WLItem(const ExplodedNode *n)
272206c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  : N(n), I(N->succ_begin()), E(N->succ_end()) {}
272306c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek};
272406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek}
272506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
272661f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenekstatic BugReport *
272761f52bd3c524268e25b48a1ed3730aedd6cc8374Ted KremenekFindReportInEquivalenceClass(BugReportEquivClass& EQ,
27285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                             SmallVectorImpl<BugReport*> &bugReports) {
272961f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
273006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
273106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  assert(I != E);
27324a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer  BugType& BT = I->getBugType();
273361f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
273440406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  // If we don't need to suppress any of the nodes because they are
273540406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  // post-dominated by a sink, simply add all the nodes in the equivalence class
273640406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  // to 'Nodes'.  Any of the reports will serve as a "representative" report.
273761f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek  if (!BT.isSuppressOnSink()) {
27384a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer    BugReport *R = I;
273961f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek    for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
27409c378f705405d37f49795d5e915989de774fe11fTed Kremenek      const ExplodedNode *N = I->getErrorNode();
274161f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      if (N) {
27424a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer        R = I;
274340406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek        bugReports.push_back(R);
274461f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      }
274561f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek    }
274606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    return R;
274761f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek  }
274861f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
274906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // For bug reports that should be suppressed when all paths are post-dominated
275006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // by a sink node, iterate through the reports in the equivalence class
275106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // until we find one that isn't post-dominated (if one exists).  We use a
275206c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // DFS traversal of the ExplodedGraph to find a non-sink node.  We could write
275306c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // this as a recursive function, but we don't want to risk blowing out the
275406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  // stack for very long paths.
275540406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  BugReport *exampleReport = 0;
275661f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
275706c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  for (; I != E; ++I) {
27584a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer    const ExplodedNode *errorNode = I->getErrorNode();
275906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
276040406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek    if (!errorNode)
276106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek      continue;
276240406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek    if (errorNode->isSink()) {
2763b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable(
276406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek           "BugType::isSuppressSink() should not be 'true' for sink end nodes");
276506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    }
276661f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek    // No successors?  By definition this nodes isn't post-dominated by a sink.
276740406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek    if (errorNode->succ_empty()) {
27684a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer      bugReports.push_back(I);
276940406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek      if (!exampleReport)
27704a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer        exampleReport = I;
277161f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      continue;
277261f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek    }
277361f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
277406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    // At this point we know that 'N' is not a sink and it has at least one
277506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    // successor.  Use a DFS worklist to find a non-sink end-of-path node.
277606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    typedef FRIEC_WLItem WLItem;
27775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    typedef SmallVector<WLItem, 10> DFSWorkList;
277806c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
277906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
278006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    DFSWorkList WL;
278140406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek    WL.push_back(errorNode);
278240406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek    Visited[errorNode] = 1;
278306c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
278406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    while (!WL.empty()) {
278506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek      WLItem &WI = WL.back();
278606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek      assert(!WI.N->succ_empty());
278706c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
278806c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek      for (; WI.I != WI.E; ++WI.I) {
278906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        const ExplodedNode *Succ = *WI.I;
279006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        // End-of-path node?
279106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        if (Succ->succ_empty()) {
279261f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek          // If we found an end-of-path node that is not a sink.
279361f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek          if (!Succ->isSink()) {
27944a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer            bugReports.push_back(I);
279540406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek            if (!exampleReport)
27964a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer              exampleReport = I;
279761f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek            WL.clear();
279861f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek            break;
279961f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek          }
280006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek          // Found a sink?  Continue on to the next successor.
280106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek          continue;
280206c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        }
280306c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        // Mark the successor as visited.  If it hasn't been explored,
280406c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        // enqueue it to the DFS worklist.
280506c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        unsigned &mark = Visited[Succ];
280606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        if (!mark) {
280706c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek          mark = 1;
280806c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek          WL.push_back(Succ);
280906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek          break;
281006c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        }
281106c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek      }
281261f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek
281361f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      // The worklist may have been cleared at this point.  First
281461f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      // check if it is empty before checking the last item.
281561f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek      if (!WL.empty() && &WL.back() == &WI)
281606c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek        WL.pop_back();
281706c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek    }
281806c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek  }
281906c9cb4d1a29e708f51bce9f7ee5acbe1c3761c3Ted Kremenek
282061f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek  // ExampleReport will be NULL if all the nodes in the equivalence class
282161f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek  // were post-dominated by sinks.
282240406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  return exampleReport;
282361f52bd3c524268e25b48a1ed3730aedd6cc8374Ted Kremenek}
2824e0a58073b76fc016325a35152533b8468df2bf4aTed Kremenek
2825cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenekvoid BugReporter::FlushReport(BugReportEquivClass& EQ) {
28265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<BugReport*, 10> bugReports;
282740406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
2828c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  if (exampleReport) {
2829c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2830c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2831c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                                 E=C.end(); I != E; ++I) {
2832c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      FlushReport(exampleReport, **I, bugReports);
2833c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    }
2834c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
2835c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek}
2836c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2837c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekvoid BugReporter::FlushReport(BugReport *exampleReport,
2838c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                              PathDiagnosticConsumer &PD,
2839c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                              ArrayRef<BugReport*> bugReports) {
28401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2841cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  // FIXME: Make sure we use the 'R' for the path that was actually used.
28421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Probably doesn't make a difference in practice.
284340406fe63df2b932d6e9fd021f77f097f9d33afbTed Kremenek  BugType& BT = exampleReport->getBugType();
28441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28456f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<PathDiagnostic>
284607189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek    D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
284707189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek                         exampleReport->getBugType().getName(),
28483a46f5fd1709f6df03bbb8b0abf84052dc0f39ffJordan Rose                         exampleReport->getDescription(),
28493a46f5fd1709f6df03bbb8b0abf84052dc0f39ffJordan Rose                         exampleReport->getShortDescription(/*Fallback=*/false),
285097bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks                         BT.getCategory(),
285197bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks                         exampleReport->getUniqueingLocation(),
285297bfb558f69c09b01a5c1510f08dc91eb62329a7Anna Zaks                         exampleReport->getUniqueingDecl()));
2853d49967f8764135ae65658e354b6d38e3637c9de3Ted Kremenek
2854cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose  MaxBugClassSize = std::max(bugReports.size(),
2855cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose                             static_cast<size_t>(MaxBugClassSize));
2856cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose
2857c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // Generate the full path diagnostic, using the generation scheme
2858d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  // specified by the PathDiagnosticConsumer. Note that we have to generate
2859d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  // path diagnostics even for consumers which do not support paths, because
2860d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  // the BugReporterVisitors may mark this bug as a false positive.
2861d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose  if (!bugReports.empty())
2862d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose    if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2863d632d6fc606f0be438c3b6fe5c43f1b3f5db98b1Jordan Rose      return;
28643148eb4a75f70f2636075c364d03104223f004d3Ted Kremenek
2865cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose  MaxValidBugClassSize = std::max(bugReports.size(),
2866cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose                                  static_cast<size_t>(MaxValidBugClassSize));
2867cc08ca9b3cd2b715a699bcc772ce2e83a502915aJordan Rose
2868c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // If the path is empty, generate a single step path with the location
2869c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // of the issue.
2870802e02463b880f53a6e645bde78cc412481ce9e0Ted Kremenek  if (D->path.empty()) {
2871c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2872c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    PathDiagnosticPiece *piece =
2873c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2874c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    BugReport::ranges_iterator Beg, End;
2875c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    llvm::tie(Beg, End) = exampleReport->getRanges();
287607189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek    for ( ; Beg != End; ++Beg)
287707189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek      piece->addRange(*Beg);
28783a46f5fd1709f6df03bbb8b0abf84052dc0f39ffJordan Rose    D->setEndOfPath(piece);
28793148eb4a75f70f2636075c364d03104223f004d3Ted Kremenek  }
28801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2881c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // Get the meta data.
2882c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
2883c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2884c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                                e = Meta.end(); i != e; ++i) {
2885c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    D->addMeta(*i);
2886c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
2887c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
2888c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  PD.HandlePathDiagnostic(D.take());
288961f3e058056ab519d249aa67e3d52b0ead57c63eTed Kremenek}
289057202071e477530e9348bc76671ee369b2399b92Ted Kremenek
289107189521a15d9c088216b943649cb9fe231cbb57Ted Kremenekvoid BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
289207189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek                                  StringRef name,
28935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef category,
2894590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks                                  StringRef str, PathDiagnosticLocation Loc,
28958c036c7f77d69f96df49219ed0bdbade200d52ebTed Kremenek                                  SourceRange* RBeg, unsigned NumRanges) {
28961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2897404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  // 'BT' is owned by BugReporter.
2898404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  BugType *BT = getBugTypeForName(name, category);
2899590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks  BugReport *R = new BugReport(*BT, str, Loc);
290007189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek  R->setDeclWithIssue(DeclWithIssue);
2901cf118d41f7930a18dce97416ef7834a62642f587Ted Kremenek  for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2902785950e59424dca7ce0081bebf13c0acd2c4fff6Jordan Rose  emitReport(R);
2903cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek}
2904404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis
29055f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerBugType *BugReporter::getBugTypeForName(StringRef name,
29065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                        StringRef category) {
2907f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<136> fullDesc;
2908404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2909404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  llvm::StringMapEntry<BugType *> &
2910404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis      entry = StrBugTypes.GetOrCreateValue(fullDesc);
2911404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  BugType *BT = entry.getValue();
2912404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  if (!BT) {
2913404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    BT = new BugType(name, category);
2914404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis    entry.setValue(BT);
2915404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  }
2916404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis  return BT;
2917404fc3ad6bd844bf8ce70cbf9974ab297704a122Argyrios Kyrtzidis}
2918