blob: 46acea78a8e430982ae2879dd52cccd2c26d5edf [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Jordan Rose852aa0d2012-07-10 22:07:52 +000051 else if (const CallEnter *CE = dyn_cast<CallEnter>(&P))
52 return CE->getCallExpr();
53 else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P))
54 return CEE->getCalleeContext()->getCallSite();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekb697b102009-02-23 22:44:26 +000056 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057}
58
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000065GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067}
68
Ted Kremenek9c378f72011-08-12 23:37:29 +000069static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000070 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000071 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000072 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000075}
76
Ted Kremenek9c378f72011-08-12 23:37:29 +000077static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 // Check if the statement is '?' or '&&'/'||'. These are "merges",
81 // not actual statement points.
82 switch (S->getStmtClass()) {
83 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000084 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000087 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000089 continue;
90 break;
91 }
92 default:
93 break;
94 }
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return 0;
99}
100
Ted Kremenek5f85e172009-07-22 22:35:28 +0000101static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return GetPreviousStmt(N);
107}
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek5f85e172009-07-22 22:35:28 +0000109static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000110GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000111 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return GetNextStmt(N);
115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000118// Diagnostic cleanup.
119//===----------------------------------------------------------------------===//
120
121/// Recursively scan through a path and prune out calls and macros pieces
122/// that aren't needed. Return true if afterwards the path contains
123/// "interesting stuff" which means it should be pruned from the parent path.
Anna Zaks80de4872012-08-29 21:22:37 +0000124bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000125 bool containsSomethingInteresting = false;
126 const unsigned N = pieces.size();
127
128 for (unsigned i = 0 ; i < N ; ++i) {
129 // Remove the front piece from the path. If it is still something we
130 // want to keep once we are done, we will push it back on the end.
131 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
132 pieces.pop_front();
133
Ted Kremenek72516742012-03-01 00:05:06 +0000134 switch (piece->getKind()) {
135 case PathDiagnosticPiece::Call: {
136 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000137 // Check if the location context is interesting.
138 assert(LocationContextMap.count(call));
139 if (R->isInteresting(LocationContextMap[call])) {
140 containsSomethingInteresting = true;
141 break;
142 }
Ted Kremenek72516742012-03-01 00:05:06 +0000143 // Recursively clean out the subclass. Keep this call around if
144 // it contains any informative diagnostics.
Anna Zaks80de4872012-08-29 21:22:37 +0000145 if (!RemoveUneededCalls(call->path, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000146 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000147 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000148 break;
149 }
150 case PathDiagnosticPiece::Macro: {
151 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000152 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000153 continue;
154 containsSomethingInteresting = true;
155 break;
156 }
157 case PathDiagnosticPiece::Event: {
158 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
159 // We never throw away an event, but we do throw it away wholesale
160 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000161 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000162 break;
163 }
164 case PathDiagnosticPiece::ControlFlow:
165 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000166 }
167
168 pieces.push_back(piece);
169 }
170
171 return containsSomethingInteresting;
172}
173
174//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000175// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000176//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000177
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000178typedef llvm::DenseMap<const ExplodedNode*,
179const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000180
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000181namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000182class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000183 NodeBackMap& M;
184public:
185 NodeMapClosure(NodeBackMap *m) : M(*m) {}
186 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek9c378f72011-08-12 23:37:29 +0000188 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000189 NodeBackMap::iterator I = M.find(N);
190 return I == M.end() ? 0 : I->second;
191 }
192};
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000194class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000195 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000196 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000197 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000198 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000199public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000200 const LocationContext *LC;
201
Ted Kremenek8966bc12009-05-06 21:39:49 +0000202 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000203 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000204 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000205 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000206 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
207 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremenek9c378f72011-08-12 23:37:29 +0000209 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek9c378f72011-08-12 23:37:29 +0000211 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
212 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anna Zaks8e6431a2011-08-18 22:37:56 +0000214 BugReport *getBugReport() { return R; }
215
Tom Care212f6d32010-09-16 03:50:38 +0000216 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000217
218 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000220 const Stmt *getParent(const Stmt *S) {
221 return getParentMap().getParent(S);
222 }
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenek8966bc12009-05-06 21:39:49 +0000224 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000225
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000226 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
David Blaikieef3643f2011-09-26 00:51:36 +0000228 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
229 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000230 }
231
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000232 bool supportsLogicalOpControlFlow() const {
233 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000234 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000235};
236} // end anonymous namespace
237
Ted Kremenek00605e02009-03-27 20:55:39 +0000238PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000239PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000240 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000241 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000242
Anna Zaks0cd59482011-09-16 19:18:30 +0000243 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
244 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000245}
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek00605e02009-03-27 20:55:39 +0000247PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000248PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
249 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000250
Ted Kremenek143ca222008-05-06 18:11:09 +0000251 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000252 if (os.str().empty())
253 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek00605e02009-03-27 20:55:39 +0000255 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek00605e02009-03-27 20:55:39 +0000257 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000258 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000259 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000260 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000261 else {
262 os << "Execution jumps to the end of the ";
263 const Decl *D = N->getLocationContext()->getDecl();
264 if (isa<ObjCMethodDecl>(D))
265 os << "method";
266 else if (isa<FunctionDecl>(D))
267 os << "function";
268 else {
269 assert(isa<BlockDecl>(D));
270 os << "anonymous block";
271 }
272 os << '.';
273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000275 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000276}
277
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000278static bool IsNested(const Stmt *S, ParentMap &PM) {
279 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
280 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000282 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000284 if (Parent)
285 switch (Parent->getStmtClass()) {
286 case Stmt::ForStmtClass:
287 case Stmt::DoStmtClass:
288 case Stmt::WhileStmtClass:
289 return true;
290 default:
291 break;
292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
294 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000295}
296
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000297PathDiagnosticLocation
298PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000299 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000300 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000301 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000302
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000303 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000304 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000306 if (!Parent)
307 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000309 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000310 case Stmt::BinaryOperatorClass: {
311 const BinaryOperator *B = cast<BinaryOperator>(Parent);
312 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000313 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000314 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000315 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000316 case Stmt::CompoundStmtClass:
317 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000318 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000319 case Stmt::ChooseExprClass:
320 // Similar to '?' if we are referring to condition, just have the edge
321 // point to the entire choose expression.
322 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000323 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000324 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000326 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000327 case Stmt::ConditionalOperatorClass:
328 // For '?', if we are referring to condition, just have the edge point
329 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000330 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000331 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000332 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000333 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000334 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000335 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::ForStmtClass:
337 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000339 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000340 case Stmt::IfStmtClass:
341 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000343 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000344 case Stmt::ObjCForCollectionStmtClass:
345 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000346 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000347 break;
348 case Stmt::WhileStmtClass:
349 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000350 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000351 break;
352 default:
353 break;
354 }
355
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000356 S = Parent;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000359 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000360
361 // Special case: DeclStmts can appear in for statement declarations, in which
362 // case the ForStmt is the context.
363 if (isa<DeclStmt>(S)) {
364 if (const Stmt *Parent = P.getParent(S)) {
365 switch (Parent->getStmtClass()) {
366 case Stmt::ForStmtClass:
367 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000368 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000369 default:
370 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000371 }
372 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000373 }
374 else if (isa<BinaryOperator>(S)) {
375 // Special case: the binary operator represents the initialization
376 // code in a for statement (this can happen when the variable being
377 // initialized is an old variable.
378 if (const ForStmt *FS =
379 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
380 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000381 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000382 }
383 }
384
Anna Zaks220ac8c2011-09-15 01:08:34 +0000385 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000386}
387
Ted Kremenekcf118d42009-02-04 23:49:09 +0000388//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000389// "Minimal" path diagnostic generation algorithm.
390//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000391typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
392typedef SmallVector<StackDiagPair, 6> StackDiagVector;
393
Anna Zaks368a0d52012-03-15 21:13:02 +0000394static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000395 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000396 // If the piece contains a special message, add it to all the call
397 // pieces on the active stack.
398 if (PathDiagnosticEventPiece *ep =
399 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000400
Anna Zaks56a938f2012-03-16 23:24:20 +0000401 if (ep->hasCallStackHint())
402 for (StackDiagVector::iterator I = CallStack.begin(),
403 E = CallStack.end(); I != E; ++I) {
404 PathDiagnosticCallPiece *CP = I->first;
405 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000406 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000407
Anna Zaks368a0d52012-03-15 21:13:02 +0000408 // The last message on the path to final bug is the most important
409 // one. Since we traverse the path backwards, do not add the message
410 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000411 if (!CP->hasCallStackMessage())
412 CP->setCallStackMessage(stackMsg);
413 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000414 }
415}
Ted Kremenek31061982009-03-31 23:00:32 +0000416
Ted Kremenek77d09442012-03-02 01:27:31 +0000417static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
420 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000421 const ExplodedNode *N,
422 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000425 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000426 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000427 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000428
Anna Zaks56a938f2012-03-16 23:24:20 +0000429 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000432 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000433 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000434 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek31061982009-03-31 23:00:32 +0000436 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000437
Anna Zaks80de4872012-08-29 21:22:37 +0000438 do {
439 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
440 PathDiagnosticCallPiece *C =
441 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
442 GRBugReporter& BR = PDB.getBugReporter();
443 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
444 PD.getActivePath().push_front(C);
445 PD.pushActivePath(&C->path);
446 CallStack.push_back(StackDiagPair(C, N));
447 break;
Anna Zaks93739372012-03-14 18:58:28 +0000448 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000449
Anna Zaks80de4872012-08-29 21:22:37 +0000450 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
451 // Flush all locations, and pop the active path.
452 bool VisitedEntireCall = PD.isWithinCall();
453 PD.popActivePath();
454
455 // Either we just added a bunch of stuff to the top-level path, or
456 // we have a previous CallExitEnd. If the former, it means that the
457 // path terminated within a function call. We must then take the
458 // current contents of the active path and place it within
459 // a new PathDiagnosticCallPiece.
460 PathDiagnosticCallPiece *C;
461 if (VisitedEntireCall) {
462 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
463 } else {
464 const Decl *Caller = CE->getLocationContext()->getDecl();
465 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
466 GRBugReporter& BR = PDB.getBugReporter();
467 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
468 }
469
470 C->setCallee(*CE, SMgr);
471 if (!CallStack.empty()) {
472 assert(CallStack.back().first == C);
473 CallStack.pop_back();
474 }
475 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anna Zaks80de4872012-08-29 21:22:37 +0000478 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
479 const CFGBlock *Src = BE->getSrc();
480 const CFGBlock *Dst = BE->getDst();
481 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anna Zaks80de4872012-08-29 21:22:37 +0000483 if (!T)
484 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Anna Zaks80de4872012-08-29 21:22:37 +0000486 PathDiagnosticLocation Start =
487 PathDiagnosticLocation::createBegin(T, SMgr,
488 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anna Zaks80de4872012-08-29 21:22:37 +0000490 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000491 default:
492 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000495 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000496 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek31061982009-03-31 23:00:32 +0000498 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000499 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000502 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000503 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000506 << End.asLocation().getExpansionLineNumber();
507 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
508 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000509 break;
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
512 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000513 // Figure out what case arm we took.
514 std::string sbuf;
515 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek9c378f72011-08-12 23:37:29 +0000517 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000518 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek31061982009-03-31 23:00:32 +0000520 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000521 default:
522 os << "No cases match in the switch statement. "
523 "Control jumps to line "
524 << End.asLocation().getExpansionLineNumber();
525 break;
526 case Stmt::DefaultStmtClass:
527 os << "Control jumps to the 'default' case at line "
528 << End.asLocation().getExpansionLineNumber();
529 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anna Zaks80de4872012-08-29 21:22:37 +0000531 case Stmt::CaseStmtClass: {
532 os << "Control jumps to 'case ";
533 const CaseStmt *Case = cast<CaseStmt>(S);
534 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Anna Zaks80de4872012-08-29 21:22:37 +0000536 // Determine if it is an enum.
537 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Anna Zaks80de4872012-08-29 21:22:37 +0000539 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
540 // FIXME: Maybe this should be an assertion. Are there cases
541 // were it is not an EnumConstantDecl?
542 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000543 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Anna Zaks80de4872012-08-29 21:22:37 +0000545 if (D) {
546 GetRawInt = false;
547 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000548 }
Ted Kremenek31061982009-03-31 23:00:32 +0000549 }
Anna Zaks80de4872012-08-29 21:22:37 +0000550
551 if (GetRawInt)
552 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
553
554 os << ":' at line "
555 << End.asLocation().getExpansionLineNumber();
556 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000557 }
Anna Zaks80de4872012-08-29 21:22:37 +0000558 }
559 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
560 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000561 }
562 else {
563 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000564 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000565 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
566 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Ted Kremenek31061982009-03-31 23:00:32 +0000569 break;
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Ted Kremenek31061982009-03-31 23:00:32 +0000572 case Stmt::BreakStmtClass:
573 case Stmt::ContinueStmtClass: {
574 std::string sbuf;
575 llvm::raw_string_ostream os(sbuf);
576 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000577 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
578 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000579 break;
580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Anna Zaks80de4872012-08-29 21:22:37 +0000582 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000583 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000584 case Stmt::ConditionalOperatorClass: {
585 std::string sbuf;
586 llvm::raw_string_ostream os(sbuf);
587 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Ted Kremenek31061982009-03-31 23:00:32 +0000589 if (*(Src->succ_begin()+1) == Dst)
590 os << "false";
591 else
592 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Ted Kremenek31061982009-03-31 23:00:32 +0000594 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Ted Kremenek31061982009-03-31 23:00:32 +0000596 if (const Stmt *S = End.asStmt())
597 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anna Zaks80de4872012-08-29 21:22:37 +0000599 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
600 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000601 break;
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anna Zaks80de4872012-08-29 21:22:37 +0000604 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000605 case Stmt::BinaryOperatorClass: {
606 if (!PDB.supportsLogicalOpControlFlow())
607 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000609 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000610 std::string sbuf;
611 llvm::raw_string_ostream os(sbuf);
612 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000613
John McCall2de56d12010-08-25 11:45:40 +0000614 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000615 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 if (*(Src->succ_begin()+1) == Dst) {
618 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000619 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000620 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000621 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
622 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
623 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000624 }
Ted Kremenek31061982009-03-31 23:00:32 +0000625 else {
626 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000627 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000628 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000629 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
630 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000631 }
Ted Kremenek31061982009-03-31 23:00:32 +0000632 }
633 else {
John McCall2de56d12010-08-25 11:45:40 +0000634 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000635 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek31061982009-03-31 23:00:32 +0000637 if (*(Src->succ_begin()+1) == Dst) {
638 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000639 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000640 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000641 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
642 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000643 }
644 else {
645 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000646 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000647 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000648 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
649 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
650 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000651 }
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek31061982009-03-31 23:00:32 +0000654 break;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
657 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000658 if (*(Src->succ_begin()) == Dst) {
659 std::string sbuf;
660 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek31061982009-03-31 23:00:32 +0000662 os << "Loop condition is true. ";
663 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek31061982009-03-31 23:00:32 +0000665 if (const Stmt *S = End.asStmt())
666 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anna Zaks80de4872012-08-29 21:22:37 +0000668 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
669 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000670 }
671 else {
672 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek31061982009-03-31 23:00:32 +0000674 if (const Stmt *S = End.asStmt())
675 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anna Zaks80de4872012-08-29 21:22:37 +0000677 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
678 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek31061982009-03-31 23:00:32 +0000681 break;
682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek31061982009-03-31 23:00:32 +0000684 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000685 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000686 if (*(Src->succ_begin()+1) == Dst) {
687 std::string sbuf;
688 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 os << "Loop condition is false. ";
691 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
692 if (const Stmt *S = End.asStmt())
693 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Anna Zaks80de4872012-08-29 21:22:37 +0000695 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
696 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000697 }
698 else {
699 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
700 if (const Stmt *S = End.asStmt())
701 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Anna Zaks80de4872012-08-29 21:22:37 +0000703 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
704 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 break;
708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 case Stmt::IfStmtClass: {
711 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek31061982009-03-31 23:00:32 +0000713 if (const Stmt *S = End.asStmt())
714 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek31061982009-03-31 23:00:32 +0000716 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000717 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
718 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000719 else
Anna Zaks80de4872012-08-29 21:22:37 +0000720 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
721 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 break;
724 }
Anna Zaks80de4872012-08-29 21:22:37 +0000725 }
Ted Kremenek31061982009-03-31 23:00:32 +0000726 }
Anna Zaks80de4872012-08-29 21:22:37 +0000727 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000729 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000730 // Add diagnostic pieces from custom visitors.
731 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000732 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
733 E = visitors.end();
734 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000735 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000736 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000737 updateStackPiecesWithMessage(p, CallStack);
738 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000739 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000740 }
Ted Kremenek31061982009-03-31 23:00:32 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek14856d72009-04-06 23:06:54 +0000743 // After constructing the full PathDiagnostic, do a pass over it to compact
744 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000745 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000746}
747
748//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000749// "Extensive" PathDiagnostic generation.
750//===----------------------------------------------------------------------===//
751
752static bool IsControlFlowExpr(const Stmt *S) {
753 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000755 if (!E)
756 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
758 E = E->IgnoreParenCasts();
759
John McCall56ca35d2011-02-17 10:25:35 +0000760 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000761 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000763 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
764 if (B->isLogicalOp())
765 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
767 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000768}
769
Ted Kremenek14856d72009-04-06 23:06:54 +0000770namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000771class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000772 bool IsDead;
773public:
774 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
775 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000776
777 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000778 bool isDead() const { return IsDead; }
779};
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000781class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000782 std::vector<ContextLocation> CLocs;
783 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000784 PathDiagnostic &PD;
785 PathDiagnosticBuilder &PDB;
786 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000788 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek14856d72009-04-06 23:06:54 +0000790 bool containsLocation(const PathDiagnosticLocation &Container,
791 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek14856d72009-04-06 23:06:54 +0000793 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek9650cf32009-05-11 21:42:34 +0000795 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
796 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000797 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000798 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000799 while (1) {
800 // Adjust the location for some expressions that are best referenced
801 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000802 switch (S->getStmtClass()) {
803 default:
804 break;
805 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000806 case Stmt::GenericSelectionExprClass:
807 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000808 firstCharOnly = true;
809 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000810 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000811 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000812 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000813 firstCharOnly = true;
814 continue;
815 case Stmt::ChooseExprClass:
816 S = cast<ChooseExpr>(S)->getCond();
817 firstCharOnly = true;
818 continue;
819 case Stmt::BinaryOperatorClass:
820 S = cast<BinaryOperator>(S)->getLHS();
821 firstCharOnly = true;
822 continue;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek9650cf32009-05-11 21:42:34 +0000825 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek9650cf32009-05-11 21:42:34 +0000828 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000829 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek9650cf32009-05-11 21:42:34 +0000832 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000833 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000834
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000835 return L;
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek14856d72009-04-06 23:06:54 +0000838 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000839 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000840 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000841 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000842 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000843 CLocs.pop_back();
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek14856d72009-04-06 23:06:54 +0000846public:
847 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
848 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremeneka301a672009-04-22 18:16:20 +0000850 // If the PathDiagnostic already has pieces, add the enclosing statement
851 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000852 if (!PD.path.empty()) {
853 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek14856d72009-04-06 23:06:54 +0000855 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000856 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000857 }
858 }
859
860 ~EdgeBuilder() {
861 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000862
Ted Kremeneka301a672009-04-22 18:16:20 +0000863 // Finally, add an initial edge from the start location of the first
864 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000865 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000866 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000867 PDB.getSourceManager());
868 if (L.isValid())
869 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000870 }
871
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000872 void flushLocations() {
873 while (!CLocs.empty())
874 popLocation();
875 PrevLoc = PathDiagnosticLocation();
876 }
877
Ted Kremenek14856d72009-04-06 23:06:54 +0000878 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000880 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Ted Kremenek14856d72009-04-06 23:06:54 +0000882 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000883 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000884 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000885};
Ted Kremenek14856d72009-04-06 23:06:54 +0000886} // end anonymous namespace
887
888
889PathDiagnosticLocation
890EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
891 if (const Stmt *S = L.asStmt()) {
892 if (IsControlFlowExpr(S))
893 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
895 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 return L;
899}
900
901bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
902 const PathDiagnosticLocation &Containee) {
903
904 if (Container == Containee)
905 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 if (Container.asDecl())
908 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 if (const Stmt *S = Containee.asStmt())
911 if (const Stmt *ContainerS = Container.asStmt()) {
912 while (S) {
913 if (S == ContainerS)
914 return true;
915 S = PDB.getParent(S);
916 }
917 return false;
918 }
919
920 // Less accurate: compare using source ranges.
921 SourceRange ContainerR = Container.asRange();
922 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremenek14856d72009-04-06 23:06:54 +0000924 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000925 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
926 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
927 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
928 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Chandler Carruth64211622011-07-25 21:09:52 +0000930 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
931 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
932 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
933 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000936 assert(ContaineeBegLine <= ContaineeEndLine);
937
Ted Kremenek14856d72009-04-06 23:06:54 +0000938 return (ContainerBegLine <= ContaineeBegLine &&
939 ContainerEndLine >= ContaineeEndLine &&
940 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000941 SM.getExpansionColumnNumber(ContainerRBeg) <=
942 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000943 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000944 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000945 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000946}
947
Ted Kremenek14856d72009-04-06 23:06:54 +0000948void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
949 if (!PrevLoc.isValid()) {
950 PrevLoc = NewLoc;
951 return;
952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000954 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
955 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000957 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000961 if (NewLocClean.asLocation().getExpansionLoc() ==
962 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000963 return;
964
Ted Kremenek2042fc12012-02-24 06:00:00 +0000965 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000966 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000967}
968
969void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Ted Kremeneka301a672009-04-22 18:16:20 +0000971 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
972 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek14856d72009-04-06 23:06:54 +0000974 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
975
976 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000977 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek14856d72009-04-06 23:06:54 +0000979 // Is the top location context the same as the one for the new location?
980 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000981 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000982 if (IsConsumedExpr(TopContextLoc) &&
983 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000984 TopContextLoc.markDead();
985
Ted Kremenek14856d72009-04-06 23:06:54 +0000986 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000987 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000988
989 return;
990 }
991
992 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000993 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000994 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000996 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000997 CLocs.push_back(ContextLocation(CLoc, true));
998 return;
999 }
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001003 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 }
1005
1006 // Context does not contain the location. Flush it.
1007 popLocation();
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001010 // If we reach here, there is no enclosing context. Just add the edge.
1011 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001012}
1013
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001014bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1015 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1016 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001018 return false;
1019}
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremeneke1baed32009-05-05 23:13:38 +00001021void EdgeBuilder::addExtendedContext(const Stmt *S) {
1022 if (!S)
1023 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
1025 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001026 while (Parent) {
1027 if (isa<CompoundStmt>(Parent))
1028 Parent = PDB.getParent(Parent);
1029 else
1030 break;
1031 }
1032
1033 if (Parent) {
1034 switch (Parent->getStmtClass()) {
1035 case Stmt::DoStmtClass:
1036 case Stmt::ObjCAtSynchronizedStmtClass:
1037 addContext(Parent);
1038 default:
1039 break;
1040 }
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremeneke1baed32009-05-05 23:13:38 +00001043 addContext(S);
1044}
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek14856d72009-04-06 23:06:54 +00001046void EdgeBuilder::addContext(const Stmt *S) {
1047 if (!S)
1048 return;
1049
Ted Kremenek59950d32012-02-24 07:12:52 +00001050 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001051 addContext(L);
1052}
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Jordan Rose183ba8e2012-07-26 20:04:05 +00001054void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 while (!CLocs.empty()) {
1056 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1057
1058 // Is the top location context the same as the one for the new location?
1059 if (TopContextLoc == L)
1060 return;
1061
1062 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001063 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001064 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001065 }
1066
1067 // Context does not contain the location. Flush it.
1068 popLocation();
1069 }
1070
1071 CLocs.push_back(L);
1072}
1073
Ted Kremenek11abcec2012-05-02 00:31:29 +00001074// Cone-of-influence: support the reverse propagation of "interesting" symbols
1075// and values by tracing interesting calculations backwards through evaluated
1076// expressions along a path. This is probably overly complicated, but the idea
1077// is that if an expression computed an "interesting" value, the child
1078// expressions are are also likely to be "interesting" as well (which then
1079// propagates to the values they in turn compute). This reverse propagation
1080// is needed to track interesting correlations across function call boundaries,
1081// where formal arguments bind to actual arguments, etc. This is also needed
1082// because the constraint solver sometimes simplifies certain symbolic values
1083// into constants when appropriate, and this complicates reasoning about
1084// interesting values.
1085typedef llvm::DenseSet<const Expr *> InterestingExprs;
1086
1087static void reversePropagateIntererstingSymbols(BugReport &R,
1088 InterestingExprs &IE,
1089 const ProgramState *State,
1090 const Expr *Ex,
1091 const LocationContext *LCtx) {
1092 SVal V = State->getSVal(Ex, LCtx);
1093 if (!(R.isInteresting(V) || IE.count(Ex)))
1094 return;
1095
1096 switch (Ex->getStmtClass()) {
1097 default:
1098 if (!isa<CastExpr>(Ex))
1099 break;
1100 // Fall through.
1101 case Stmt::BinaryOperatorClass:
1102 case Stmt::UnaryOperatorClass: {
1103 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1104 CE = Ex->child_end();
1105 CI != CE; ++CI) {
1106 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1107 IE.insert(child);
1108 SVal ChildV = State->getSVal(child, LCtx);
1109 R.markInteresting(ChildV);
1110 }
1111 break;
1112 }
1113 }
1114 }
1115
1116 R.markInteresting(V);
1117}
1118
1119static void reversePropagateInterestingSymbols(BugReport &R,
1120 InterestingExprs &IE,
1121 const ProgramState *State,
1122 const LocationContext *CalleeCtx,
1123 const LocationContext *CallerCtx)
1124{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001125 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001126 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1127 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001128 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001129 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1130 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1131 PE = FD->param_end();
1132 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1133 for (; AI != AE && PI != PE; ++AI, ++PI) {
1134 if (const Expr *ArgE = *AI) {
1135 if (const ParmVarDecl *PD = *PI) {
1136 Loc LV = State->getLValue(PD, CalleeCtx);
1137 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1138 IE.insert(ArgE);
1139 }
1140 }
1141 }
1142 }
1143 }
1144}
1145
Ted Kremenek14856d72009-04-06 23:06:54 +00001146static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1147 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001148 const ExplodedNode *N,
1149 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001150 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001151 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001152 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001153 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001154
Ted Kremenek9c378f72011-08-12 23:37:29 +00001155 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001156 while (NextNode) {
1157 N = NextNode;
1158 NextNode = GetPredecessorNode(N);
1159 ProgramPoint P = N->getLocation();
1160
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001162 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1163 if (const Expr *Ex = PS->getStmtAs<Expr>())
1164 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1165 N->getState().getPtr(), Ex,
1166 N->getLocationContext());
1167 }
1168
Anna Zaks0b3ade82012-04-20 21:59:08 +00001169 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001170 const Stmt *S = CE->getCalleeContext()->getCallSite();
1171 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001172 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1173 N->getState().getPtr(), Ex,
1174 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001175 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001176
1177 PathDiagnosticCallPiece *C =
1178 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001179 GRBugReporter& BR = PDB.getBugReporter();
1180 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001181
1182 EB.addEdge(C->callReturn, true);
1183 EB.flushLocations();
1184
1185 PD.getActivePath().push_front(C);
1186 PD.pushActivePath(&C->path);
1187 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001188 break;
1189 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001190
Ted Kremenek2042fc12012-02-24 06:00:00 +00001191 // Pop the call hierarchy if we are done walking the contents
1192 // of a function call.
1193 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001194 // Add an edge to the start of the function.
1195 const Decl *D = CE->getCalleeContext()->getDecl();
1196 PathDiagnosticLocation pos =
1197 PathDiagnosticLocation::createBegin(D, SM);
1198 EB.addEdge(pos);
1199
1200 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001201 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001202 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001203 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001204 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001205
Jordan Rose183ba8e2012-07-26 20:04:05 +00001206 // Either we just added a bunch of stuff to the top-level path, or
1207 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001208 // path terminated within a function call. We must then take the
1209 // current contents of the active path and place it within
1210 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001211 PathDiagnosticCallPiece *C;
1212 if (VisitedEntireCall) {
1213 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1214 } else {
1215 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001216 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001217 GRBugReporter& BR = PDB.getBugReporter();
1218 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001219 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001220
Jordan Rose183ba8e2012-07-26 20:04:05 +00001221 C->setCallee(*CE, SM);
1222 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001223
1224 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001225 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001226 CallStack.pop_back();
1227 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001228 break;
1229 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001230
1231 // Note that is important that we update the LocationContext
1232 // after looking at CallExits. CallExit basically adds an
1233 // edge in the *caller*, so we don't want to update the LocationContext
1234 // too soon.
1235 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001236
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001237 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001238 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1239 // Does this represent entering a call? If so, look at propagating
1240 // interesting symbols across call boundaries.
1241 if (NextNode) {
1242 const LocationContext *CallerCtx = NextNode->getLocationContext();
1243 const LocationContext *CalleeCtx = PDB.LC;
1244 if (CallerCtx != CalleeCtx) {
1245 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1246 N->getState().getPtr(),
1247 CalleeCtx, CallerCtx);
1248 }
1249 }
1250
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001251 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001252 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001253 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001254 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001256 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1257 CS = dyn_cast<CompoundStmt>(FS->getBody());
1258 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1259 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001261 PathDiagnosticEventPiece *p =
1262 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001263 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001264 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001266 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001267 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001269 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001270 PathDiagnosticLocation BL =
1271 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001272 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001273 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001274 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001275
1276 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001277 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001279 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001280 }
1281
Mike Stump1eb44332009-09-09 15:08:12 +00001282 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001283 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1284 const Stmt *stmt = S->getStmt();
1285 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001286 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001287 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001288 }
1289 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001290 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001291 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001292
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001293 break;
1294 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001295
1296
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001297 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001299 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001300 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Anna Zaks8e6431a2011-08-18 22:37:56 +00001302 // Add pieces from custom visitors.
1303 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001304 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1305 E = visitors.end();
1306 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001307 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001308 const PathDiagnosticLocation &Loc = p->getLocation();
1309 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001310 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001311 updateStackPiecesWithMessage(p, CallStack);
1312
Ted Kremenek8966bc12009-05-06 21:39:49 +00001313 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001314 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001317 }
1318}
1319
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001320//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001321// Methods for BugType and subclasses.
1322//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001323BugType::~BugType() { }
1324
Ted Kremenekcf118d42009-02-04 23:49:09 +00001325void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001326
David Blaikie99ba9e32011-12-20 02:48:34 +00001327void BuiltinBug::anchor() {}
1328
Ted Kremenekcf118d42009-02-04 23:49:09 +00001329//===----------------------------------------------------------------------===//
1330// Methods for BugReport and subclasses.
1331//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001332
David Blaikie99ba9e32011-12-20 02:48:34 +00001333void BugReport::NodeResolver::anchor() {}
1334
Anna Zaks8e6431a2011-08-18 22:37:56 +00001335void BugReport::addVisitor(BugReporterVisitor* visitor) {
1336 if (!visitor)
1337 return;
1338
1339 llvm::FoldingSetNodeID ID;
1340 visitor->Profile(ID);
1341 void *InsertPos;
1342
1343 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1344 delete visitor;
1345 return;
1346 }
1347
1348 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001349 Callbacks.push_back(visitor);
1350 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001351}
1352
1353BugReport::~BugReport() {
1354 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001355 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001356 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001357 while (!interestingSymbols.empty()) {
1358 popInterestingSymbolsAndRegions();
1359 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001360}
Anna Zakse172e8b2011-08-17 23:00:25 +00001361
Ted Kremenek07189522012-04-04 18:11:35 +00001362const Decl *BugReport::getDeclWithIssue() const {
1363 if (DeclWithIssue)
1364 return DeclWithIssue;
1365
1366 const ExplodedNode *N = getErrorNode();
1367 if (!N)
1368 return 0;
1369
1370 const LocationContext *LC = N->getLocationContext();
1371 return LC->getCurrentStackFrame()->getDecl();
1372}
1373
Anna Zakse172e8b2011-08-17 23:00:25 +00001374void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1375 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001376 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001377 if (UniqueingLocation.isValid()) {
1378 UniqueingLocation.Profile(hash);
1379 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001380 Location.Profile(hash);
1381 } else {
1382 assert(ErrorNode);
1383 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1384 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001385
1386 for (SmallVectorImpl<SourceRange>::const_iterator I =
1387 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1388 const SourceRange range = *I;
1389 if (!range.isValid())
1390 continue;
1391 hash.AddInteger(range.getBegin().getRawEncoding());
1392 hash.AddInteger(range.getEnd().getRawEncoding());
1393 }
1394}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001395
Ted Kremenek76aadc32012-03-09 01:13:14 +00001396void BugReport::markInteresting(SymbolRef sym) {
1397 if (!sym)
1398 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001399
1400 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001401 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001402 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001403
1404 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001405 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001406}
1407
1408void BugReport::markInteresting(const MemRegion *R) {
1409 if (!R)
1410 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001411
1412 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001413 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001414 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001415 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001416
Ted Kremenek76aadc32012-03-09 01:13:14 +00001417 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001418 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001419}
1420
1421void BugReport::markInteresting(SVal V) {
1422 markInteresting(V.getAsRegion());
1423 markInteresting(V.getAsSymbol());
1424}
1425
Anna Zaks80de4872012-08-29 21:22:37 +00001426void BugReport::markInteresting(const LocationContext *LC) {
1427 if (!LC)
1428 return;
1429 InterestingLocationContexts.insert(LC);
1430}
1431
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001432bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001433 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1434}
1435
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001436bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001437 if (!sym)
1438 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001439 // We don't currently consider metadata symbols to be interesting
1440 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001441 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001442}
1443
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001444bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001445 if (!R)
1446 return false;
1447 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001448 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001449 if (b)
1450 return true;
1451 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001452 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001453 return false;
1454}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001455
Anna Zaks80de4872012-08-29 21:22:37 +00001456bool BugReport::isInteresting(const LocationContext *LC) {
1457 if (!LC)
1458 return false;
1459 return InterestingLocationContexts.count(LC);
1460}
1461
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001462void BugReport::lazyInitializeInterestingSets() {
1463 if (interestingSymbols.empty()) {
1464 interestingSymbols.push_back(new Symbols());
1465 interestingRegions.push_back(new Regions());
1466 }
1467}
1468
1469BugReport::Symbols &BugReport::getInterestingSymbols() {
1470 lazyInitializeInterestingSets();
1471 return *interestingSymbols.back();
1472}
1473
1474BugReport::Regions &BugReport::getInterestingRegions() {
1475 lazyInitializeInterestingSets();
1476 return *interestingRegions.back();
1477}
1478
1479void BugReport::pushInterestingSymbolsAndRegions() {
1480 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1481 interestingRegions.push_back(new Regions(getInterestingRegions()));
1482}
1483
1484void BugReport::popInterestingSymbolsAndRegions() {
1485 delete interestingSymbols.back();
1486 interestingSymbols.pop_back();
1487 delete interestingRegions.back();
1488 interestingRegions.pop_back();
1489}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001490
Ted Kremenek9c378f72011-08-12 23:37:29 +00001491const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001492 if (!ErrorNode)
1493 return 0;
1494
Tom Care212f6d32010-09-16 03:50:38 +00001495 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001496 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek9c378f72011-08-12 23:37:29 +00001498 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001499 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001500 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001501 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001502 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001503 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001504 S = GetStmt(ProgP);
1505
1506 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001507}
1508
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001509std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001510BugReport::getRanges() {
1511 // If no custom ranges, add the range of the statement corresponding to
1512 // the error node.
1513 if (Ranges.empty()) {
1514 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1515 addRange(E->getSourceRange());
1516 else
1517 return std::make_pair(ranges_iterator(), ranges_iterator());
1518 }
1519
Anna Zaks14924262011-08-24 20:31:06 +00001520 // User-specified absence of range info.
1521 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1522 return std::make_pair(ranges_iterator(), ranges_iterator());
1523
Anna Zakse172e8b2011-08-17 23:00:25 +00001524 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001525}
1526
Anna Zaks590dd8e2011-09-20 21:38:35 +00001527PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001528 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001529 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001530 "Either Location or ErrorNode should be specified but not both.");
1531
Ted Kremenek9c378f72011-08-12 23:37:29 +00001532 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001533 const LocationContext *LC = ErrorNode->getLocationContext();
1534
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001535 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001536 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001537 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001538 // For binary operators, return the location of the operator.
1539 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001540 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001541
Anna Zaks590dd8e2011-09-20 21:38:35 +00001542 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001543 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001544 } else {
1545 assert(Location.isValid());
1546 return Location;
1547 }
1548
Anna Zaks590dd8e2011-09-20 21:38:35 +00001549 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001550}
1551
Ted Kremenekcf118d42009-02-04 23:49:09 +00001552//===----------------------------------------------------------------------===//
1553// Methods for BugReporter and subclasses.
1554//===----------------------------------------------------------------------===//
1555
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001556BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001557GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001558BugReporterData::~BugReporterData() {}
1559
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001560ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001561
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001562ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001563GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1564
Anna Zaks3b030a22011-08-19 01:57:09 +00001565BugReporter::~BugReporter() {
1566 FlushReports();
1567
1568 // Free the bug reports we are tracking.
1569 typedef std::vector<BugReportEquivClass *> ContTy;
1570 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1571 I != E; ++I) {
1572 delete *I;
1573 }
1574}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001575
1576void BugReporter::FlushReports() {
1577 if (BugTypes.isEmpty())
1578 return;
1579
1580 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001581 // warnings and new BugTypes.
1582 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1583 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001584 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001585 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001586 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001587 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001588 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001589 const_cast<BugType*>(*I)->FlushReports(*this);
1590
Anna Zaksd015f4f2012-08-02 23:41:05 +00001591 // We need to flush reports in deterministic order to ensure the order
1592 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001593 typedef std::vector<BugReportEquivClass *> ContVecTy;
1594 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1595 EI != EE; ++EI){
1596 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001597 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001598 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001599
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001600 // BugReporter owns and deletes only BugTypes created implicitly through
1601 // EmitBasicReport.
1602 // FIXME: There are leaks from checkers that assume that the BugTypes they
1603 // create will be destroyed by the BugReporter.
1604 for (llvm::StringMap<BugType*>::iterator
1605 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1606 delete I->second;
1607
Ted Kremenekcf118d42009-02-04 23:49:09 +00001608 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001609 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001610}
1611
1612//===----------------------------------------------------------------------===//
1613// PathDiagnostics generation.
1614//===----------------------------------------------------------------------===//
1615
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001616static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001617 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001618MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001619 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Ted Kremenekcf118d42009-02-04 23:49:09 +00001621 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001622 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001623 // error node unless there are two or more error nodes with the same minimum
1624 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001625 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001626 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001627
1628 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001629 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1630 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632 // Create owning pointers for GTrim and NMap just to ensure that they are
1633 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001634 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1635 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenekcf118d42009-02-04 23:49:09 +00001637 // Find the (first) error node in the trimmed graph. We just need to consult
1638 // the node map (NMap) which maps from nodes in the original graph to nodes
1639 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001640
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001641 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001642 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001643 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001644
Ted Kremenek40406fe2010-12-03 06:52:30 +00001645 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1646 const ExplodedNode *originalNode = nodes[nodeIndex];
1647 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001648 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001649 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001650 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek938332c2009-05-16 01:11:58 +00001653 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001654
1655 // Create a new (third!) graph with a single path. This is the graph
1656 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001657 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenek10aa5542009-03-12 23:41:59 +00001659 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001660 // to the root node, and then construct a new graph that contains only
1661 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001662 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001664 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001665 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001667 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001668 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001669 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001671 if (Visited.find(Node) != Visited.end())
1672 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001674 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001676 if (Node->pred_empty()) {
1677 Root = Node;
1678 break;
1679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001681 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001682 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001683 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001684 }
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Ted Kremenek938332c2009-05-16 01:11:58 +00001686 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Ted Kremenek10aa5542009-03-12 23:41:59 +00001688 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001689 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001691 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001692 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001694 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001695 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001696 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001697 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001699 // Create the equivalent node in the new graph with the same state
1700 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001701 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001703 // Store the mapping to the original node.
1704 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1705 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001706 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001708 // Link up the new node with the previous node.
1709 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001710 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001712 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001714 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001715 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001716 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001717 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001718 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001719 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001720 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001723 // Find the next successor node. We choose the node that is marked
1724 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001725 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1726 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001727 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001729 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001731 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001733 if (I == Visited.end())
1734 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001736 if (!N || I->second < MinVal) {
1737 N = *SI;
1738 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001739 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Ted Kremenek938332c2009-05-16 01:11:58 +00001742 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Ted Kremenek938332c2009-05-16 01:11:58 +00001745 assert(First);
1746
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001747 return std::make_pair(std::make_pair(GNew, BM),
1748 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001749}
1750
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001751/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1752/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001753static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001754 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1755 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001757 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001758 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001760 MacroStackTy MacroStack;
1761 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Ted Kremenek77d09442012-03-02 01:27:31 +00001763 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001764 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001765
1766 PathDiagnosticPiece *piece = I->getPtr();
1767
1768 // Recursively compact calls.
1769 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1770 CompactPathDiagnostic(call->path, SM);
1771 }
1772
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001773 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001774 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001776 // Determine the instantiation location, which is the location we group
1777 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001778 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001779 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001780 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001782 if (Loc.isFileID()) {
1783 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001784 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001785 continue;
1786 }
1787
1788 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001790 // Is the PathDiagnosticPiece within the same macro group?
1791 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001792 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001793 continue;
1794 }
1795
1796 // We aren't in the same group. Are we descending into a new macro
1797 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001798 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001799
1800 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001801 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001802 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001804 // Walk the entire macro stack.
1805 while (!MacroStack.empty()) {
1806 if (InstantiationLoc == MacroStack.back().second) {
1807 MacroGroup = MacroStack.back().first;
1808 break;
1809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001811 if (ParentInstantiationLoc == MacroStack.back().second) {
1812 MacroGroup = MacroStack.back().first;
1813 break;
1814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001816 MacroStack.pop_back();
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001819 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1820 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001821 PathDiagnosticMacroPiece *NewGroup =
1822 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001823 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001824
1825 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001826 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001827 else {
1828 assert(InstantiationLoc.isFileID());
1829 Pieces.push_back(NewGroup);
1830 }
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001832 MacroGroup = NewGroup;
1833 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1834 }
1835
1836 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001837 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001840 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001841 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Ted Kremenek77d09442012-03-02 01:27:31 +00001843 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1844 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001845}
1846
Ted Kremenek7dc86642009-03-31 20:22:36 +00001847void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001848 PathDiagnosticConsumer &PC,
1849 ArrayRef<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremenek40406fe2010-12-03 06:52:30 +00001851 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001852 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001853 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1854 E = bugReports.end(); I != E; ++I) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001855 errorNodes.push_back((*I)->getErrorNode());
1856 }
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001858 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001860 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001861 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001862 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Ted Kremenekcf118d42009-02-04 23:49:09 +00001864 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001865 assert(GPair.second.second < bugReports.size());
1866 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001867 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001869 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1870 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001871 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
1873 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001874 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Anna Zaks8e6431a2011-08-18 22:37:56 +00001876 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001877 R->addVisitor(new NilReceiverBRVisitor());
1878 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001880 BugReport::VisitorList visitors;
1881 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001882
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001883 // While generating diagnostics, it's possible the visitors will decide
1884 // new symbols and regions are interesting, or add other visitors based on
1885 // the information they find. If they do, we need to regenerate the path
1886 // based on our new report configuration.
1887 do {
1888 // Get a clean copy of all the visitors.
1889 for (BugReport::visitor_iterator I = R->visitor_begin(),
1890 E = R->visitor_end(); I != E; ++I)
1891 visitors.push_back((*I)->clone());
1892
1893 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001894 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001895 originalReportConfigToken = R->getConfigurationChangeToken();
1896
1897 // Generate the very last diagnostic piece - the piece is visible before
1898 // the trace is expanded.
1899 PathDiagnosticPiece *LastPiece = 0;
1900 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1901 I != E; ++I) {
1902 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1903 assert (!LastPiece &&
1904 "There can only be one final piece in a diagnostic.");
1905 LastPiece = Piece;
1906 }
1907 }
1908 if (!LastPiece)
1909 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1910 if (LastPiece)
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001911 PD.setEndOfPath(LastPiece);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001912 else
1913 return;
1914
1915 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001916 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001917 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001918 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001919 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001920 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001921 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001922 case PathDiagnosticConsumer::None:
1923 llvm_unreachable("PathDiagnosticConsumer::None should never appear here");
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001924 }
1925
1926 // Clean up the visitors we used.
1927 llvm::DeleteContainerPointers(visitors);
1928
1929 // Did anything change while generating this path?
1930 finalReportConfigToken = R->getConfigurationChangeToken();
1931 } while(finalReportConfigToken != originalReportConfigToken);
1932
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001933 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001934 if (R->shouldPrunePath()) {
Anna Zaks80de4872012-08-29 21:22:37 +00001935 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(), R);
Ted Kremeneked7948b2012-05-31 06:03:17 +00001936 assert(hasSomethingInteresting);
1937 (void) hasSomethingInteresting;
1938 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001939}
1940
Ted Kremenekcf118d42009-02-04 23:49:09 +00001941void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001942 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001943}
1944
Mike Stump1eb44332009-09-09 15:08:12 +00001945void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001946 // Compute the bug report's hash to determine its equivalence class.
1947 llvm::FoldingSetNodeID ID;
1948 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001949
1950 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001951 BugType& BT = R->getBugType();
1952 Register(&BT);
1953 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001954 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Ted Kremenekcf118d42009-02-04 23:49:09 +00001956 if (!EQ) {
1957 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001958 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001959 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001960 }
1961 else
1962 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001963}
1964
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001965
1966//===----------------------------------------------------------------------===//
1967// Emitting reports in equivalence classes.
1968//===----------------------------------------------------------------------===//
1969
1970namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001971struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001972 const ExplodedNode *N;
1973 ExplodedNode::const_succ_iterator I, E;
1974
1975 FRIEC_WLItem(const ExplodedNode *n)
1976 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1977};
1978}
1979
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001980static BugReport *
1981FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001982 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001983
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001984 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1985 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001986 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001987
Ted Kremenek40406fe2010-12-03 06:52:30 +00001988 // If we don't need to suppress any of the nodes because they are
1989 // post-dominated by a sink, simply add all the nodes in the equivalence class
1990 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001991 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001992 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001993 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001994 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001995 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001996 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001997 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001998 }
1999 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002000 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002001 }
2002
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002003 // For bug reports that should be suppressed when all paths are post-dominated
2004 // by a sink node, iterate through the reports in the equivalence class
2005 // until we find one that isn't post-dominated (if one exists). We use a
2006 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2007 // this as a recursive function, but we don't want to risk blowing out the
2008 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002009 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002010
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002011 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002012 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002013
Ted Kremenek40406fe2010-12-03 06:52:30 +00002014 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002015 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002016 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002017 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002018 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002019 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002020 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002021 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002022 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002023 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002024 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002025 continue;
2026 }
2027
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002028 // At this point we know that 'N' is not a sink and it has at least one
2029 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2030 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002031 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002032 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2033
2034 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002035 WL.push_back(errorNode);
2036 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002037
2038 while (!WL.empty()) {
2039 WLItem &WI = WL.back();
2040 assert(!WI.N->succ_empty());
2041
2042 for (; WI.I != WI.E; ++WI.I) {
2043 const ExplodedNode *Succ = *WI.I;
2044 // End-of-path node?
2045 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002046 // If we found an end-of-path node that is not a sink.
2047 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002048 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002049 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002050 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002051 WL.clear();
2052 break;
2053 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002054 // Found a sink? Continue on to the next successor.
2055 continue;
2056 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002057 // Mark the successor as visited. If it hasn't been explored,
2058 // enqueue it to the DFS worklist.
2059 unsigned &mark = Visited[Succ];
2060 if (!mark) {
2061 mark = 1;
2062 WL.push_back(Succ);
2063 break;
2064 }
2065 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002066
2067 // The worklist may have been cleared at this point. First
2068 // check if it is empty before checking the last item.
2069 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002070 WL.pop_back();
2071 }
2072 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002073
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002074 // ExampleReport will be NULL if all the nodes in the equivalence class
2075 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002076 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002077}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002078
Ted Kremenekcf118d42009-02-04 23:49:09 +00002079void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002080 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002081 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002082 if (exampleReport) {
2083 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2084 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2085 E=C.end(); I != E; ++I) {
2086 FlushReport(exampleReport, **I, bugReports);
2087 }
2088 }
2089}
2090
2091void BugReporter::FlushReport(BugReport *exampleReport,
2092 PathDiagnosticConsumer &PD,
2093 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Ted Kremenekcf118d42009-02-04 23:49:09 +00002095 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002096 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002097 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002099 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002100 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2101 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002102 exampleReport->getDescription(),
2103 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002104 BT.getCategory()));
2105
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002106 // Generate the full path diagnostic, using the generation scheme
2107 // specified by the PathDiagnosticConsumer.
2108 if (PD.getGenerationScheme() != PathDiagnosticConsumer::None) {
2109 if (!bugReports.empty())
2110 GeneratePathDiagnostic(*D.get(), PD, bugReports);
2111 }
2112
2113 // If the path is empty, generate a single step path with the location
2114 // of the issue.
2115 if (D->path.empty()) {
2116 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2117 PathDiagnosticPiece *piece =
2118 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2119 BugReport::ranges_iterator Beg, End;
2120 llvm::tie(Beg, End) = exampleReport->getRanges();
2121 for ( ; Beg != End; ++Beg)
2122 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002123 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002124 }
2125
Ted Kremenek072192b2008-04-30 23:47:44 +00002126 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002127 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002128 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2129 e = Meta.end(); i != e; ++i) {
2130 D->addMeta(*i);
2131 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002132
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002133 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002134}
Ted Kremenek57202072008-07-14 17:40:50 +00002135
Ted Kremenek07189522012-04-04 18:11:35 +00002136void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002137 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002138 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002139 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002140 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002142 // 'BT' is owned by BugReporter.
2143 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002144 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002145 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002146 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2147 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002148}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002149
Chris Lattner5f9e2722011-07-23 10:55:15 +00002150BugType *BugReporter::getBugTypeForName(StringRef name,
2151 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002152 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002153 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2154 llvm::StringMapEntry<BugType *> &
2155 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2156 BugType *BT = entry.getValue();
2157 if (!BT) {
2158 BT = new BugType(name, category);
2159 entry.setValue(BT);
2160 }
2161 return BT;
2162}