blob: 571baecd729dfcaf9261bb4d8b797f1c5ee9481b [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.
124static bool RemoveUneededCalls(PathPieces &pieces) {
125 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);
137 // Recursively clean out the subclass. Keep this call around if
138 // it contains any informative diagnostics.
139 if (!RemoveUneededCalls(call->path))
140 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000141 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000142 break;
143 }
144 case PathDiagnosticPiece::Macro: {
145 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
146 if (!RemoveUneededCalls(macro->subPieces))
147 continue;
148 containsSomethingInteresting = true;
149 break;
150 }
151 case PathDiagnosticPiece::Event: {
152 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
153 // We never throw away an event, but we do throw it away wholesale
154 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000155 if (event->isPrunable())
156 continue;
157 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000158 break;
159 }
160 case PathDiagnosticPiece::ControlFlow:
161 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000162 }
163
164 pieces.push_back(piece);
165 }
166
167 return containsSomethingInteresting;
168}
169
170//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000171// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000172//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000173
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000174typedef llvm::DenseMap<const ExplodedNode*,
175const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000176
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000177namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000178class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000179 NodeBackMap& M;
180public:
181 NodeMapClosure(NodeBackMap *m) : M(*m) {}
182 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek9c378f72011-08-12 23:37:29 +0000184 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000185 NodeBackMap::iterator I = M.find(N);
186 return I == M.end() ? 0 : I->second;
187 }
188};
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000190class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000191 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000192 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000193 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000194 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000195public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000196 const LocationContext *LC;
197
Ted Kremenek8966bc12009-05-06 21:39:49 +0000198 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000199 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000200 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000201 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000202 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
203 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek9c378f72011-08-12 23:37:29 +0000205 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek9c378f72011-08-12 23:37:29 +0000207 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
208 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anna Zaks8e6431a2011-08-18 22:37:56 +0000210 BugReport *getBugReport() { return R; }
211
Tom Care212f6d32010-09-16 03:50:38 +0000212 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000213
214 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000216 const Stmt *getParent(const Stmt *S) {
217 return getParentMap().getParent(S);
218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenek8966bc12009-05-06 21:39:49 +0000220 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000221
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000222 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
David Blaikieef3643f2011-09-26 00:51:36 +0000224 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
225 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000226 }
227
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000228 bool supportsLogicalOpControlFlow() const {
229 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000230 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000231};
232} // end anonymous namespace
233
Ted Kremenek00605e02009-03-27 20:55:39 +0000234PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000235PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000236 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000237 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000238
Anna Zaks0cd59482011-09-16 19:18:30 +0000239 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
240 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000241}
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek00605e02009-03-27 20:55:39 +0000243PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000244PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
245 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000246
Ted Kremenek143ca222008-05-06 18:11:09 +0000247 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000248 if (os.str().empty())
249 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek00605e02009-03-27 20:55:39 +0000251 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek00605e02009-03-27 20:55:39 +0000253 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000254 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000255 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000256 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000257 else {
258 os << "Execution jumps to the end of the ";
259 const Decl *D = N->getLocationContext()->getDecl();
260 if (isa<ObjCMethodDecl>(D))
261 os << "method";
262 else if (isa<FunctionDecl>(D))
263 os << "function";
264 else {
265 assert(isa<BlockDecl>(D));
266 os << "anonymous block";
267 }
268 os << '.';
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000271 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000272}
273
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000274static bool IsNested(const Stmt *S, ParentMap &PM) {
275 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
276 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000278 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000280 if (Parent)
281 switch (Parent->getStmtClass()) {
282 case Stmt::ForStmtClass:
283 case Stmt::DoStmtClass:
284 case Stmt::WhileStmtClass:
285 return true;
286 default:
287 break;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
290 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000291}
292
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000293PathDiagnosticLocation
294PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000295 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000296 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000297 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000298
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000299 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000300 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000302 if (!Parent)
303 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000305 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000306 case Stmt::BinaryOperatorClass: {
307 const BinaryOperator *B = cast<BinaryOperator>(Parent);
308 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000309 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000310 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000311 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000312 case Stmt::CompoundStmtClass:
313 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000314 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000315 case Stmt::ChooseExprClass:
316 // Similar to '?' if we are referring to condition, just have the edge
317 // point to the entire choose expression.
318 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000319 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000320 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000321 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000322 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000323 case Stmt::ConditionalOperatorClass:
324 // For '?', if we are referring to condition, just have the edge point
325 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000326 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000328 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000329 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000330 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000331 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::ForStmtClass:
333 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000335 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::IfStmtClass:
337 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000339 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000340 case Stmt::ObjCForCollectionStmtClass:
341 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000343 break;
344 case Stmt::WhileStmtClass:
345 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000346 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000347 break;
348 default:
349 break;
350 }
351
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000352 S = Parent;
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000355 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000356
357 // Special case: DeclStmts can appear in for statement declarations, in which
358 // case the ForStmt is the context.
359 if (isa<DeclStmt>(S)) {
360 if (const Stmt *Parent = P.getParent(S)) {
361 switch (Parent->getStmtClass()) {
362 case Stmt::ForStmtClass:
363 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000364 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000365 default:
366 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000367 }
368 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000369 }
370 else if (isa<BinaryOperator>(S)) {
371 // Special case: the binary operator represents the initialization
372 // code in a for statement (this can happen when the variable being
373 // initialized is an old variable.
374 if (const ForStmt *FS =
375 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
376 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000377 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000378 }
379 }
380
Anna Zaks220ac8c2011-09-15 01:08:34 +0000381 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000382}
383
Ted Kremenekcf118d42009-02-04 23:49:09 +0000384//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000385// "Minimal" path diagnostic generation algorithm.
386//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000387typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
388typedef SmallVector<StackDiagPair, 6> StackDiagVector;
389
Anna Zaks368a0d52012-03-15 21:13:02 +0000390static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000391 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000392 // If the piece contains a special message, add it to all the call
393 // pieces on the active stack.
394 if (PathDiagnosticEventPiece *ep =
395 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000396
Anna Zaks56a938f2012-03-16 23:24:20 +0000397 if (ep->hasCallStackHint())
398 for (StackDiagVector::iterator I = CallStack.begin(),
399 E = CallStack.end(); I != E; ++I) {
400 PathDiagnosticCallPiece *CP = I->first;
401 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000402 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000403
Anna Zaks368a0d52012-03-15 21:13:02 +0000404 // The last message on the path to final bug is the most important
405 // one. Since we traverse the path backwards, do not add the message
406 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000407 if (!CP->hasCallStackMessage())
408 CP->setCallStackMessage(stackMsg);
409 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000410 }
411}
Ted Kremenek31061982009-03-31 23:00:32 +0000412
Ted Kremenek77d09442012-03-02 01:27:31 +0000413static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
416 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000417 const ExplodedNode *N,
418 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000421 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000422 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000423 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000424
Anna Zaks56a938f2012-03-16 23:24:20 +0000425 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000429 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000430 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek31061982009-03-31 23:00:32 +0000432 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000433
Anna Zaks0b3ade82012-04-20 21:59:08 +0000434 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000435 PathDiagnosticCallPiece *C =
436 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
437 PD.getActivePath().push_front(C);
438 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +0000439 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek2042fc12012-02-24 06:00:00 +0000440 continue;
441 }
442
443 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +0000444 // Flush all locations, and pop the active path.
445 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000446 PD.popActivePath();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000447
448 // Either we just added a bunch of stuff to the top-level path, or
449 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +0000450 // path terminated within a function call. We must then take the
451 // current contents of the active path and place it within
452 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +0000453 PathDiagnosticCallPiece *C;
454 if (VisitedEntireCall) {
455 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
456 } else {
Anna Zaks93739372012-03-14 18:58:28 +0000457 const Decl *Caller = CE->getLocationContext()->getDecl();
458 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
459 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000460
Ted Kremenek2042fc12012-02-24 06:00:00 +0000461 C->setCallee(*CE, SMgr);
Anna Zaks368a0d52012-03-15 21:13:02 +0000462 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +0000463 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +0000464 CallStack.pop_back();
465 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000466 continue;
467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek9c378f72011-08-12 23:37:29 +0000469 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
470 const CFGBlock *Src = BE->getSrc();
471 const CFGBlock *Dst = BE->getDst();
472 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 if (!T)
475 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Anna Zaks590dd8e2011-09-20 21:38:35 +0000477 PathDiagnosticLocation Start =
478 PathDiagnosticLocation::createBegin(T, SMgr,
479 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek31061982009-03-31 23:00:32 +0000481 switch (T->getStmtClass()) {
482 default:
483 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000486 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000487 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 if (!S)
490 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek31061982009-03-31 23:00:32 +0000492 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000493 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000494 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000497 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000498 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000499 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000500 break;
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
503 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000504 // Figure out what case arm we took.
505 std::string sbuf;
506 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek9c378f72011-08-12 23:37:29 +0000508 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000509 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Ted Kremenek31061982009-03-31 23:00:32 +0000511 switch (S->getStmtClass()) {
512 default:
513 os << "No cases match in the switch statement. "
514 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000515 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000516 break;
517 case Stmt::DefaultStmtClass:
518 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000519 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000520 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Ted Kremenek31061982009-03-31 23:00:32 +0000522 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000524 const CaseStmt *Case = cast<CaseStmt>(S);
525 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
527 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000528 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek9c378f72011-08-12 23:37:29 +0000530 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000531 // FIXME: Maybe this should be an assertion. Are there cases
532 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000533 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000534 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 if (D) {
537 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000538 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000539 }
540 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000541
542 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000543 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000546 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000547 break;
548 }
549 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000550 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000551 os.str()));
552 }
553 else {
554 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000555 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000556 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000557 os.str()));
558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenek31061982009-03-31 23:00:32 +0000560 break;
561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek31061982009-03-31 23:00:32 +0000563 case Stmt::BreakStmtClass:
564 case Stmt::ContinueStmtClass: {
565 std::string sbuf;
566 llvm::raw_string_ostream os(sbuf);
567 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000568 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000569 os.str()));
570 break;
571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Ted Kremenek31061982009-03-31 23:00:32 +0000573 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000574 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000575 case Stmt::ConditionalOperatorClass: {
576 std::string sbuf;
577 llvm::raw_string_ostream os(sbuf);
578 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Ted Kremenek31061982009-03-31 23:00:32 +0000580 if (*(Src->succ_begin()+1) == Dst)
581 os << "false";
582 else
583 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Ted Kremenek31061982009-03-31 23:00:32 +0000587 if (const Stmt *S = End.asStmt())
588 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Ted Kremenek2042fc12012-02-24 06:00:00 +0000590 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000591 os.str()));
592 break;
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremenek31061982009-03-31 23:00:32 +0000595 // Determine control-flow for short-circuited '&&' and '||'.
596 case Stmt::BinaryOperatorClass: {
597 if (!PDB.supportsLogicalOpControlFlow())
598 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000600 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000601 std::string sbuf;
602 llvm::raw_string_ostream os(sbuf);
603 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000604
John McCall2de56d12010-08-25 11:45:40 +0000605 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000606 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 if (*(Src->succ_begin()+1) == Dst) {
609 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000610 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000611 PathDiagnosticLocation Start =
612 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000613 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000614 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000615 }
Ted Kremenek31061982009-03-31 23:00:32 +0000616 else {
617 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000618 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000619 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000620 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000621 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000622 }
Ted Kremenek31061982009-03-31 23:00:32 +0000623 }
624 else {
John McCall2de56d12010-08-25 11:45:40 +0000625 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000626 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek31061982009-03-31 23:00:32 +0000628 if (*(Src->succ_begin()+1) == Dst) {
629 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000630 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000631 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000632 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000633 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000634 }
635 else {
636 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000637 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000638 PathDiagnosticLocation Start =
639 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000640 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000641 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000642 }
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek31061982009-03-31 23:00:32 +0000645 break;
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
648 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000649 if (*(Src->succ_begin()) == Dst) {
650 std::string sbuf;
651 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 os << "Loop condition is true. ";
654 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 if (const Stmt *S = End.asStmt())
657 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek2042fc12012-02-24 06:00:00 +0000659 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000660 os.str()));
661 }
662 else {
663 PathDiagnosticLocation End = PDB.ExecutionContinues(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
Ted Kremenek2042fc12012-02-24 06:00:00 +0000668 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000669 "Loop condition is false. Exiting loop"));
670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremenek31061982009-03-31 23:00:32 +0000672 break;
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek31061982009-03-31 23:00:32 +0000675 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000676 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000677 if (*(Src->succ_begin()+1) == Dst) {
678 std::string sbuf;
679 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek31061982009-03-31 23:00:32 +0000681 os << "Loop condition is false. ";
682 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
683 if (const Stmt *S = End.asStmt())
684 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Ted Kremenek2042fc12012-02-24 06:00:00 +0000686 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000687 os.str()));
688 }
689 else {
690 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
691 if (const Stmt *S = End.asStmt())
692 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek2042fc12012-02-24 06:00:00 +0000694 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000695 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 break;
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek31061982009-03-31 23:00:32 +0000701 case Stmt::IfStmtClass: {
702 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek31061982009-03-31 23:00:32 +0000704 if (const Stmt *S = End.asStmt())
705 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000708 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000709 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000710 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000711 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000712 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 break;
715 }
716 }
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000719 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000720 // Add diagnostic pieces from custom visitors.
721 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000722 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
723 E = visitors.end();
724 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000725 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000726 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000727 updateStackPiecesWithMessage(p, CallStack);
728 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000729 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000730 }
Ted Kremenek31061982009-03-31 23:00:32 +0000731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek14856d72009-04-06 23:06:54 +0000733 // After constructing the full PathDiagnostic, do a pass over it to compact
734 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000735 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000736}
737
738//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000739// "Extensive" PathDiagnostic generation.
740//===----------------------------------------------------------------------===//
741
742static bool IsControlFlowExpr(const Stmt *S) {
743 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000745 if (!E)
746 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
748 E = E->IgnoreParenCasts();
749
John McCall56ca35d2011-02-17 10:25:35 +0000750 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000751 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000753 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
754 if (B->isLogicalOp())
755 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
757 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000758}
759
Ted Kremenek14856d72009-04-06 23:06:54 +0000760namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000761class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000762 bool IsDead;
763public:
764 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
765 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000766
767 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000768 bool isDead() const { return IsDead; }
769};
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000771class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000772 std::vector<ContextLocation> CLocs;
773 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000774 PathDiagnostic &PD;
775 PathDiagnosticBuilder &PDB;
776 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000778 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek14856d72009-04-06 23:06:54 +0000780 bool containsLocation(const PathDiagnosticLocation &Container,
781 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek14856d72009-04-06 23:06:54 +0000783 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek9650cf32009-05-11 21:42:34 +0000785 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
786 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000787 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000788 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000789 while (1) {
790 // Adjust the location for some expressions that are best referenced
791 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000792 switch (S->getStmtClass()) {
793 default:
794 break;
795 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000796 case Stmt::GenericSelectionExprClass:
797 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000798 firstCharOnly = true;
799 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000800 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000801 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000802 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000803 firstCharOnly = true;
804 continue;
805 case Stmt::ChooseExprClass:
806 S = cast<ChooseExpr>(S)->getCond();
807 firstCharOnly = true;
808 continue;
809 case Stmt::BinaryOperatorClass:
810 S = cast<BinaryOperator>(S)->getLHS();
811 firstCharOnly = true;
812 continue;
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek9650cf32009-05-11 21:42:34 +0000815 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenek9650cf32009-05-11 21:42:34 +0000818 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000819 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek9650cf32009-05-11 21:42:34 +0000822 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000823 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000824
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000825 return L;
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek14856d72009-04-06 23:06:54 +0000828 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000829 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000830 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000831 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000832 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000833 CLocs.pop_back();
834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek14856d72009-04-06 23:06:54 +0000836public:
837 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
838 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremeneka301a672009-04-22 18:16:20 +0000840 // If the PathDiagnostic already has pieces, add the enclosing statement
841 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000842 if (!PD.path.empty()) {
843 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000846 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000847 }
848 }
849
850 ~EdgeBuilder() {
851 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000852
Ted Kremeneka301a672009-04-22 18:16:20 +0000853 // Finally, add an initial edge from the start location of the first
854 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000855 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000856 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000857 PDB.getSourceManager());
858 if (L.isValid())
859 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000860 }
861
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000862 void flushLocations() {
863 while (!CLocs.empty())
864 popLocation();
865 PrevLoc = PathDiagnosticLocation();
866 }
867
Ted Kremenek14856d72009-04-06 23:06:54 +0000868 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000870 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek14856d72009-04-06 23:06:54 +0000872 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000873 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000874 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000875};
Ted Kremenek14856d72009-04-06 23:06:54 +0000876} // end anonymous namespace
877
878
879PathDiagnosticLocation
880EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
881 if (const Stmt *S = L.asStmt()) {
882 if (IsControlFlowExpr(S))
883 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
885 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek14856d72009-04-06 23:06:54 +0000888 return L;
889}
890
891bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
892 const PathDiagnosticLocation &Containee) {
893
894 if (Container == Containee)
895 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek14856d72009-04-06 23:06:54 +0000897 if (Container.asDecl())
898 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek14856d72009-04-06 23:06:54 +0000900 if (const Stmt *S = Containee.asStmt())
901 if (const Stmt *ContainerS = Container.asStmt()) {
902 while (S) {
903 if (S == ContainerS)
904 return true;
905 S = PDB.getParent(S);
906 }
907 return false;
908 }
909
910 // Less accurate: compare using source ranges.
911 SourceRange ContainerR = Container.asRange();
912 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek14856d72009-04-06 23:06:54 +0000914 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000915 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
916 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
917 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
918 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chandler Carruth64211622011-07-25 21:09:52 +0000920 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
921 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
922 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
923 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek14856d72009-04-06 23:06:54 +0000925 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000926 assert(ContaineeBegLine <= ContaineeEndLine);
927
Ted Kremenek14856d72009-04-06 23:06:54 +0000928 return (ContainerBegLine <= ContaineeBegLine &&
929 ContainerEndLine >= ContaineeEndLine &&
930 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000931 SM.getExpansionColumnNumber(ContainerRBeg) <=
932 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000933 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000934 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000935 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000936}
937
Ted Kremenek14856d72009-04-06 23:06:54 +0000938void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
939 if (!PrevLoc.isValid()) {
940 PrevLoc = NewLoc;
941 return;
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000944 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
945 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000947 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000951 if (NewLocClean.asLocation().getExpansionLoc() ==
952 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 return;
954
Ted Kremenek2042fc12012-02-24 06:00:00 +0000955 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000956 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000957}
958
959void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Ted Kremeneka301a672009-04-22 18:16:20 +0000961 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
962 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek14856d72009-04-06 23:06:54 +0000964 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
965
966 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000967 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Ted Kremenek14856d72009-04-06 23:06:54 +0000969 // Is the top location context the same as the one for the new location?
970 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000971 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000972 if (IsConsumedExpr(TopContextLoc) &&
973 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000974 TopContextLoc.markDead();
975
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000977 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000978
979 return;
980 }
981
982 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000983 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000986 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000987 CLocs.push_back(ContextLocation(CLoc, true));
988 return;
989 }
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek14856d72009-04-06 23:06:54 +0000992 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000993 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000994 }
995
996 // Context does not contain the location. Flush it.
997 popLocation();
998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001000 // If we reach here, there is no enclosing context. Just add the edge.
1001 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001002}
1003
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001004bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1005 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1006 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001008 return false;
1009}
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremeneke1baed32009-05-05 23:13:38 +00001011void EdgeBuilder::addExtendedContext(const Stmt *S) {
1012 if (!S)
1013 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
1015 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001016 while (Parent) {
1017 if (isa<CompoundStmt>(Parent))
1018 Parent = PDB.getParent(Parent);
1019 else
1020 break;
1021 }
1022
1023 if (Parent) {
1024 switch (Parent->getStmtClass()) {
1025 case Stmt::DoStmtClass:
1026 case Stmt::ObjCAtSynchronizedStmtClass:
1027 addContext(Parent);
1028 default:
1029 break;
1030 }
1031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremeneke1baed32009-05-05 23:13:38 +00001033 addContext(S);
1034}
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Ted Kremenek14856d72009-04-06 23:06:54 +00001036void EdgeBuilder::addContext(const Stmt *S) {
1037 if (!S)
1038 return;
1039
Ted Kremenek59950d32012-02-24 07:12:52 +00001040 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001041 addContext(L);
1042}
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Jordan Rose183ba8e2012-07-26 20:04:05 +00001044void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 while (!CLocs.empty()) {
1046 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1047
1048 // Is the top location context the same as the one for the new location?
1049 if (TopContextLoc == L)
1050 return;
1051
1052 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001053 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001054 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 }
1056
1057 // Context does not contain the location. Flush it.
1058 popLocation();
1059 }
1060
1061 CLocs.push_back(L);
1062}
1063
Ted Kremenek11abcec2012-05-02 00:31:29 +00001064// Cone-of-influence: support the reverse propagation of "interesting" symbols
1065// and values by tracing interesting calculations backwards through evaluated
1066// expressions along a path. This is probably overly complicated, but the idea
1067// is that if an expression computed an "interesting" value, the child
1068// expressions are are also likely to be "interesting" as well (which then
1069// propagates to the values they in turn compute). This reverse propagation
1070// is needed to track interesting correlations across function call boundaries,
1071// where formal arguments bind to actual arguments, etc. This is also needed
1072// because the constraint solver sometimes simplifies certain symbolic values
1073// into constants when appropriate, and this complicates reasoning about
1074// interesting values.
1075typedef llvm::DenseSet<const Expr *> InterestingExprs;
1076
1077static void reversePropagateIntererstingSymbols(BugReport &R,
1078 InterestingExprs &IE,
1079 const ProgramState *State,
1080 const Expr *Ex,
1081 const LocationContext *LCtx) {
1082 SVal V = State->getSVal(Ex, LCtx);
1083 if (!(R.isInteresting(V) || IE.count(Ex)))
1084 return;
1085
1086 switch (Ex->getStmtClass()) {
1087 default:
1088 if (!isa<CastExpr>(Ex))
1089 break;
1090 // Fall through.
1091 case Stmt::BinaryOperatorClass:
1092 case Stmt::UnaryOperatorClass: {
1093 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1094 CE = Ex->child_end();
1095 CI != CE; ++CI) {
1096 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1097 IE.insert(child);
1098 SVal ChildV = State->getSVal(child, LCtx);
1099 R.markInteresting(ChildV);
1100 }
1101 break;
1102 }
1103 }
1104 }
1105
1106 R.markInteresting(V);
1107}
1108
1109static void reversePropagateInterestingSymbols(BugReport &R,
1110 InterestingExprs &IE,
1111 const ProgramState *State,
1112 const LocationContext *CalleeCtx,
1113 const LocationContext *CallerCtx)
1114{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001115 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001116 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1117 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001118 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001119 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1120 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1121 PE = FD->param_end();
1122 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1123 for (; AI != AE && PI != PE; ++AI, ++PI) {
1124 if (const Expr *ArgE = *AI) {
1125 if (const ParmVarDecl *PD = *PI) {
1126 Loc LV = State->getLValue(PD, CalleeCtx);
1127 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1128 IE.insert(ArgE);
1129 }
1130 }
1131 }
1132 }
1133 }
1134}
1135
Ted Kremenek14856d72009-04-06 23:06:54 +00001136static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1137 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001138 const ExplodedNode *N,
1139 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001140 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001141 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001142 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001143 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001144
Ted Kremenek9c378f72011-08-12 23:37:29 +00001145 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001146 while (NextNode) {
1147 N = NextNode;
1148 NextNode = GetPredecessorNode(N);
1149 ProgramPoint P = N->getLocation();
1150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001152 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1153 if (const Expr *Ex = PS->getStmtAs<Expr>())
1154 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1155 N->getState().getPtr(), Ex,
1156 N->getLocationContext());
1157 }
1158
Anna Zaks0b3ade82012-04-20 21:59:08 +00001159 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001160 const Stmt *S = CE->getCalleeContext()->getCallSite();
1161 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001162 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1163 N->getState().getPtr(), Ex,
1164 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001165 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001166
1167 PathDiagnosticCallPiece *C =
1168 PathDiagnosticCallPiece::construct(N, *CE, SM);
1169
1170 EB.addEdge(C->callReturn, true);
1171 EB.flushLocations();
1172
1173 PD.getActivePath().push_front(C);
1174 PD.pushActivePath(&C->path);
1175 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001176 break;
1177 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001178
Ted Kremenek2042fc12012-02-24 06:00:00 +00001179 // Pop the call hierarchy if we are done walking the contents
1180 // of a function call.
1181 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001182 // Add an edge to the start of the function.
1183 const Decl *D = CE->getCalleeContext()->getDecl();
1184 PathDiagnosticLocation pos =
1185 PathDiagnosticLocation::createBegin(D, SM);
1186 EB.addEdge(pos);
1187
1188 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001189 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001190 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001191 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001192 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001193
Jordan Rose183ba8e2012-07-26 20:04:05 +00001194 // Either we just added a bunch of stuff to the top-level path, or
1195 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001196 // path terminated within a function call. We must then take the
1197 // current contents of the active path and place it within
1198 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001199 PathDiagnosticCallPiece *C;
1200 if (VisitedEntireCall) {
1201 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1202 } else {
1203 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001204 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1205 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001206
Jordan Rose183ba8e2012-07-26 20:04:05 +00001207 C->setCallee(*CE, SM);
1208 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001209
1210 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001211 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001212 CallStack.pop_back();
1213 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001214 break;
1215 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001216
1217 // Note that is important that we update the LocationContext
1218 // after looking at CallExits. CallExit basically adds an
1219 // edge in the *caller*, so we don't want to update the LocationContext
1220 // too soon.
1221 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001222
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001223 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001224 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1225 // Does this represent entering a call? If so, look at propagating
1226 // interesting symbols across call boundaries.
1227 if (NextNode) {
1228 const LocationContext *CallerCtx = NextNode->getLocationContext();
1229 const LocationContext *CalleeCtx = PDB.LC;
1230 if (CallerCtx != CalleeCtx) {
1231 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1232 N->getState().getPtr(),
1233 CalleeCtx, CallerCtx);
1234 }
1235 }
1236
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001237 const CFGBlock &Blk = *BE->getSrc();
1238 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001240 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001241 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001242 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001243 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001245 if (!Term) {
1246 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1247 CS = dyn_cast<CompoundStmt>(FS->getBody());
1248 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001249 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001252 PathDiagnosticEventPiece *p =
1253 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001254 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001255 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001257 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001258 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001260 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001261 PathDiagnosticLocation BL =
1262 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001263 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001264 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001267 if (Term)
1268 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001270 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001271 }
1272
Mike Stump1eb44332009-09-09 15:08:12 +00001273 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001274 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1275 const Stmt *stmt = S->getStmt();
1276 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001277 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001278 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001279 }
1280 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001281 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001282 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001283
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001284 break;
1285 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001286
1287
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001288 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001290 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001291 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Anna Zaks8e6431a2011-08-18 22:37:56 +00001293 // Add pieces from custom visitors.
1294 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001295 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1296 E = visitors.end();
1297 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001298 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001299 const PathDiagnosticLocation &Loc = p->getLocation();
1300 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001301 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001302 updateStackPiecesWithMessage(p, CallStack);
1303
Ted Kremenek8966bc12009-05-06 21:39:49 +00001304 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001305 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001308 }
1309}
1310
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001311//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001312// Methods for BugType and subclasses.
1313//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001314BugType::~BugType() { }
1315
Ted Kremenekcf118d42009-02-04 23:49:09 +00001316void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001317
David Blaikie99ba9e32011-12-20 02:48:34 +00001318void BuiltinBug::anchor() {}
1319
Ted Kremenekcf118d42009-02-04 23:49:09 +00001320//===----------------------------------------------------------------------===//
1321// Methods for BugReport and subclasses.
1322//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001323
David Blaikie99ba9e32011-12-20 02:48:34 +00001324void BugReport::NodeResolver::anchor() {}
1325
Anna Zaks8e6431a2011-08-18 22:37:56 +00001326void BugReport::addVisitor(BugReporterVisitor* visitor) {
1327 if (!visitor)
1328 return;
1329
1330 llvm::FoldingSetNodeID ID;
1331 visitor->Profile(ID);
1332 void *InsertPos;
1333
1334 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1335 delete visitor;
1336 return;
1337 }
1338
1339 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001340 Callbacks.push_back(visitor);
1341 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001342}
1343
1344BugReport::~BugReport() {
1345 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001346 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001347 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001348 while (!interestingSymbols.empty()) {
1349 popInterestingSymbolsAndRegions();
1350 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001351}
Anna Zakse172e8b2011-08-17 23:00:25 +00001352
Ted Kremenek07189522012-04-04 18:11:35 +00001353const Decl *BugReport::getDeclWithIssue() const {
1354 if (DeclWithIssue)
1355 return DeclWithIssue;
1356
1357 const ExplodedNode *N = getErrorNode();
1358 if (!N)
1359 return 0;
1360
1361 const LocationContext *LC = N->getLocationContext();
1362 return LC->getCurrentStackFrame()->getDecl();
1363}
1364
Anna Zakse172e8b2011-08-17 23:00:25 +00001365void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1366 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001367 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001368 if (UniqueingLocation.isValid()) {
1369 UniqueingLocation.Profile(hash);
1370 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001371 Location.Profile(hash);
1372 } else {
1373 assert(ErrorNode);
1374 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1375 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001376
1377 for (SmallVectorImpl<SourceRange>::const_iterator I =
1378 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1379 const SourceRange range = *I;
1380 if (!range.isValid())
1381 continue;
1382 hash.AddInteger(range.getBegin().getRawEncoding());
1383 hash.AddInteger(range.getEnd().getRawEncoding());
1384 }
1385}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386
Ted Kremenek76aadc32012-03-09 01:13:14 +00001387void BugReport::markInteresting(SymbolRef sym) {
1388 if (!sym)
1389 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001390
1391 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001392 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001393 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001394
1395 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001396 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001397}
1398
1399void BugReport::markInteresting(const MemRegion *R) {
1400 if (!R)
1401 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001402
1403 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001404 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001405 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001406 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001407
Ted Kremenek76aadc32012-03-09 01:13:14 +00001408 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001409 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001410}
1411
1412void BugReport::markInteresting(SVal V) {
1413 markInteresting(V.getAsRegion());
1414 markInteresting(V.getAsSymbol());
1415}
1416
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001417bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001418 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1419}
1420
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001421bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001422 if (!sym)
1423 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001424 // We don't currently consider metadata symbols to be interesting
1425 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001426 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001427}
1428
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001429bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001430 if (!R)
1431 return false;
1432 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001433 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001434 if (b)
1435 return true;
1436 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001437 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001438 return false;
1439}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001440
1441void BugReport::lazyInitializeInterestingSets() {
1442 if (interestingSymbols.empty()) {
1443 interestingSymbols.push_back(new Symbols());
1444 interestingRegions.push_back(new Regions());
1445 }
1446}
1447
1448BugReport::Symbols &BugReport::getInterestingSymbols() {
1449 lazyInitializeInterestingSets();
1450 return *interestingSymbols.back();
1451}
1452
1453BugReport::Regions &BugReport::getInterestingRegions() {
1454 lazyInitializeInterestingSets();
1455 return *interestingRegions.back();
1456}
1457
1458void BugReport::pushInterestingSymbolsAndRegions() {
1459 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1460 interestingRegions.push_back(new Regions(getInterestingRegions()));
1461}
1462
1463void BugReport::popInterestingSymbolsAndRegions() {
1464 delete interestingSymbols.back();
1465 interestingSymbols.pop_back();
1466 delete interestingRegions.back();
1467 interestingRegions.pop_back();
1468}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001469
Ted Kremenek9c378f72011-08-12 23:37:29 +00001470const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001471 if (!ErrorNode)
1472 return 0;
1473
Tom Care212f6d32010-09-16 03:50:38 +00001474 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001475 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Ted Kremenek9c378f72011-08-12 23:37:29 +00001477 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001478 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001479 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001480 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001481 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001482 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001483 S = GetStmt(ProgP);
1484
1485 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001486}
1487
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001488std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001489BugReport::getRanges() {
1490 // If no custom ranges, add the range of the statement corresponding to
1491 // the error node.
1492 if (Ranges.empty()) {
1493 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1494 addRange(E->getSourceRange());
1495 else
1496 return std::make_pair(ranges_iterator(), ranges_iterator());
1497 }
1498
Anna Zaks14924262011-08-24 20:31:06 +00001499 // User-specified absence of range info.
1500 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1501 return std::make_pair(ranges_iterator(), ranges_iterator());
1502
Anna Zakse172e8b2011-08-17 23:00:25 +00001503 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001504}
1505
Anna Zaks590dd8e2011-09-20 21:38:35 +00001506PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001507 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001508 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001509 "Either Location or ErrorNode should be specified but not both.");
1510
Ted Kremenek9c378f72011-08-12 23:37:29 +00001511 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001512 const LocationContext *LC = ErrorNode->getLocationContext();
1513
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001514 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001515 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001516 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001517 // For binary operators, return the location of the operator.
1518 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001519 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001520
Anna Zaks590dd8e2011-09-20 21:38:35 +00001521 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001522 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001523 } else {
1524 assert(Location.isValid());
1525 return Location;
1526 }
1527
Anna Zaks590dd8e2011-09-20 21:38:35 +00001528 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001529}
1530
Ted Kremenekcf118d42009-02-04 23:49:09 +00001531//===----------------------------------------------------------------------===//
1532// Methods for BugReporter and subclasses.
1533//===----------------------------------------------------------------------===//
1534
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001535BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001536GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001537BugReporterData::~BugReporterData() {}
1538
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001539ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001540
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001541ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001542GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1543
Anna Zaks3b030a22011-08-19 01:57:09 +00001544BugReporter::~BugReporter() {
1545 FlushReports();
1546
1547 // Free the bug reports we are tracking.
1548 typedef std::vector<BugReportEquivClass *> ContTy;
1549 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1550 I != E; ++I) {
1551 delete *I;
1552 }
1553}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001554
1555void BugReporter::FlushReports() {
1556 if (BugTypes.isEmpty())
1557 return;
1558
1559 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001560 // warnings and new BugTypes.
1561 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1562 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001563 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001564 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001565 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001566 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001567 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001568 const_cast<BugType*>(*I)->FlushReports(*this);
1569
Anna Zaksd015f4f2012-08-02 23:41:05 +00001570 // We need to flush reports in deterministic order to ensure the order
1571 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001572 typedef std::vector<BugReportEquivClass *> ContVecTy;
1573 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1574 EI != EE; ++EI){
1575 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001576 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001577 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001578
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001579 // BugReporter owns and deletes only BugTypes created implicitly through
1580 // EmitBasicReport.
1581 // FIXME: There are leaks from checkers that assume that the BugTypes they
1582 // create will be destroyed by the BugReporter.
1583 for (llvm::StringMap<BugType*>::iterator
1584 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1585 delete I->second;
1586
Ted Kremenekcf118d42009-02-04 23:49:09 +00001587 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001588 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001589}
1590
1591//===----------------------------------------------------------------------===//
1592// PathDiagnostics generation.
1593//===----------------------------------------------------------------------===//
1594
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001595static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001596 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001597MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001598 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenekcf118d42009-02-04 23:49:09 +00001600 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001601 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001602 // error node unless there are two or more error nodes with the same minimum
1603 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001604 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001605 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001606
1607 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001608 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1609 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenekcf118d42009-02-04 23:49:09 +00001611 // Create owning pointers for GTrim and NMap just to ensure that they are
1612 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001613 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1614 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Ted Kremenekcf118d42009-02-04 23:49:09 +00001616 // Find the (first) error node in the trimmed graph. We just need to consult
1617 // the node map (NMap) which maps from nodes in the original graph to nodes
1618 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001619
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001620 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001621 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001622 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001623
Ted Kremenek40406fe2010-12-03 06:52:30 +00001624 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1625 const ExplodedNode *originalNode = nodes[nodeIndex];
1626 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001627 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001628 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001629 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001630 }
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek938332c2009-05-16 01:11:58 +00001632 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001633
1634 // Create a new (third!) graph with a single path. This is the graph
1635 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001636 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Ted Kremenek10aa5542009-03-12 23:41:59 +00001638 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001639 // to the root node, and then construct a new graph that contains only
1640 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001641 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001643 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001644 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001646 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001647 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001648 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001650 if (Visited.find(Node) != Visited.end())
1651 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001653 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001655 if (Node->pred_empty()) {
1656 Root = Node;
1657 break;
1658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001660 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001661 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001662 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Ted Kremenek938332c2009-05-16 01:11:58 +00001665 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Ted Kremenek10aa5542009-03-12 23:41:59 +00001667 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001668 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001669 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001670 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001671 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001673 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001674 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001675 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001676 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001678 // Create the equivalent node in the new graph with the same state
1679 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001680 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001682 // Store the mapping to the original node.
1683 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1684 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001685 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001687 // Link up the new node with the previous node.
1688 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001689 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001691 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001693 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001694 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001695 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001696 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001697 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001698 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001699 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001700 }
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001702 // Find the next successor node. We choose the node that is marked
1703 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001704 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1705 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001706 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001708 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001710 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001712 if (I == Visited.end())
1713 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001715 if (!N || I->second < MinVal) {
1716 N = *SI;
1717 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001718 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001719 }
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Ted Kremenek938332c2009-05-16 01:11:58 +00001721 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001722 }
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Ted Kremenek938332c2009-05-16 01:11:58 +00001724 assert(First);
1725
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001726 return std::make_pair(std::make_pair(GNew, BM),
1727 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001728}
1729
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001730/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1731/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001732static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001733 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1734 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001736 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001737 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001739 MacroStackTy MacroStack;
1740 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Ted Kremenek77d09442012-03-02 01:27:31 +00001742 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001743 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001744
1745 PathDiagnosticPiece *piece = I->getPtr();
1746
1747 // Recursively compact calls.
1748 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1749 CompactPathDiagnostic(call->path, SM);
1750 }
1751
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001752 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001753 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001755 // Determine the instantiation location, which is the location we group
1756 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001757 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001758 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001759 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001761 if (Loc.isFileID()) {
1762 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001763 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001764 continue;
1765 }
1766
1767 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001769 // Is the PathDiagnosticPiece within the same macro group?
1770 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001771 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001772 continue;
1773 }
1774
1775 // We aren't in the same group. Are we descending into a new macro
1776 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001777 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001778
1779 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001780 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001781 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001783 // Walk the entire macro stack.
1784 while (!MacroStack.empty()) {
1785 if (InstantiationLoc == MacroStack.back().second) {
1786 MacroGroup = MacroStack.back().first;
1787 break;
1788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001790 if (ParentInstantiationLoc == MacroStack.back().second) {
1791 MacroGroup = MacroStack.back().first;
1792 break;
1793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001795 MacroStack.pop_back();
1796 }
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001798 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1799 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001800 PathDiagnosticMacroPiece *NewGroup =
1801 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001802 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001803
1804 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001805 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001806 else {
1807 assert(InstantiationLoc.isFileID());
1808 Pieces.push_back(NewGroup);
1809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001811 MacroGroup = NewGroup;
1812 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1813 }
1814
1815 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001816 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001819 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001820 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremenek77d09442012-03-02 01:27:31 +00001822 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1823 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001824}
1825
Ted Kremenek7dc86642009-03-31 20:22:36 +00001826void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001827 PathDiagnosticConsumer &PC,
1828 ArrayRef<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Ted Kremenek40406fe2010-12-03 06:52:30 +00001830 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001831 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001832 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1833 E = bugReports.end(); I != E; ++I) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001834 errorNodes.push_back((*I)->getErrorNode());
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001837 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001838 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001839 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001840 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001841 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Ted Kremenekcf118d42009-02-04 23:49:09 +00001843 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001844 assert(GPair.second.second < bugReports.size());
1845 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001846 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001848 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1849 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001850 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001851
1852 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001853 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Anna Zaks8e6431a2011-08-18 22:37:56 +00001855 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001856 R->addVisitor(new NilReceiverBRVisitor());
1857 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001859 BugReport::VisitorList visitors;
1860 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001861
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001862 // While generating diagnostics, it's possible the visitors will decide
1863 // new symbols and regions are interesting, or add other visitors based on
1864 // the information they find. If they do, we need to regenerate the path
1865 // based on our new report configuration.
1866 do {
1867 // Get a clean copy of all the visitors.
1868 for (BugReport::visitor_iterator I = R->visitor_begin(),
1869 E = R->visitor_end(); I != E; ++I)
1870 visitors.push_back((*I)->clone());
1871
1872 // Clear out the active path from any previous work.
1873 PD.getActivePath().clear();
1874 originalReportConfigToken = R->getConfigurationChangeToken();
1875
1876 // Generate the very last diagnostic piece - the piece is visible before
1877 // the trace is expanded.
1878 PathDiagnosticPiece *LastPiece = 0;
1879 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1880 I != E; ++I) {
1881 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1882 assert (!LastPiece &&
1883 "There can only be one final piece in a diagnostic.");
1884 LastPiece = Piece;
1885 }
1886 }
1887 if (!LastPiece)
1888 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1889 if (LastPiece)
1890 PD.getActivePath().push_back(LastPiece);
1891 else
1892 return;
1893
1894 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001895 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001896 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001897 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001898 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001899 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001900 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001901 case PathDiagnosticConsumer::None:
1902 llvm_unreachable("PathDiagnosticConsumer::None should never appear here");
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001903 }
1904
1905 // Clean up the visitors we used.
1906 llvm::DeleteContainerPointers(visitors);
1907
1908 // Did anything change while generating this path?
1909 finalReportConfigToken = R->getConfigurationChangeToken();
1910 } while(finalReportConfigToken != originalReportConfigToken);
1911
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001912 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001913 if (R->shouldPrunePath()) {
1914 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1915 assert(hasSomethingInteresting);
1916 (void) hasSomethingInteresting;
1917 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001918}
1919
Ted Kremenekcf118d42009-02-04 23:49:09 +00001920void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001921 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001922}
1923
Mike Stump1eb44332009-09-09 15:08:12 +00001924void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001925 // Compute the bug report's hash to determine its equivalence class.
1926 llvm::FoldingSetNodeID ID;
1927 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001928
1929 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001930 BugType& BT = R->getBugType();
1931 Register(&BT);
1932 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001933 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Ted Kremenekcf118d42009-02-04 23:49:09 +00001935 if (!EQ) {
1936 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001937 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001938 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001939 }
1940 else
1941 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001942}
1943
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001944
1945//===----------------------------------------------------------------------===//
1946// Emitting reports in equivalence classes.
1947//===----------------------------------------------------------------------===//
1948
1949namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001950struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001951 const ExplodedNode *N;
1952 ExplodedNode::const_succ_iterator I, E;
1953
1954 FRIEC_WLItem(const ExplodedNode *n)
1955 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1956};
1957}
1958
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001959static BugReport *
1960FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001961 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001962
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001963 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1964 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001965 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001966
Ted Kremenek40406fe2010-12-03 06:52:30 +00001967 // If we don't need to suppress any of the nodes because they are
1968 // post-dominated by a sink, simply add all the nodes in the equivalence class
1969 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001970 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001971 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001972 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001973 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001974 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001975 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001976 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001977 }
1978 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001979 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001980 }
1981
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001982 // For bug reports that should be suppressed when all paths are post-dominated
1983 // by a sink node, iterate through the reports in the equivalence class
1984 // until we find one that isn't post-dominated (if one exists). We use a
1985 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1986 // this as a recursive function, but we don't want to risk blowing out the
1987 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001988 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001989
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001990 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001991 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001992
Ted Kremenek40406fe2010-12-03 06:52:30 +00001993 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001994 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001995 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001996 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001997 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001998 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001999 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002000 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002001 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002002 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002003 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002004 continue;
2005 }
2006
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002007 // At this point we know that 'N' is not a sink and it has at least one
2008 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2009 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002010 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002011 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2012
2013 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002014 WL.push_back(errorNode);
2015 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002016
2017 while (!WL.empty()) {
2018 WLItem &WI = WL.back();
2019 assert(!WI.N->succ_empty());
2020
2021 for (; WI.I != WI.E; ++WI.I) {
2022 const ExplodedNode *Succ = *WI.I;
2023 // End-of-path node?
2024 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002025 // If we found an end-of-path node that is not a sink.
2026 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002027 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002028 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002029 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002030 WL.clear();
2031 break;
2032 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002033 // Found a sink? Continue on to the next successor.
2034 continue;
2035 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002036 // Mark the successor as visited. If it hasn't been explored,
2037 // enqueue it to the DFS worklist.
2038 unsigned &mark = Visited[Succ];
2039 if (!mark) {
2040 mark = 1;
2041 WL.push_back(Succ);
2042 break;
2043 }
2044 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002045
2046 // The worklist may have been cleared at this point. First
2047 // check if it is empty before checking the last item.
2048 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002049 WL.pop_back();
2050 }
2051 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002052
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002053 // ExampleReport will be NULL if all the nodes in the equivalence class
2054 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002055 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002056}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002057
Ted Kremenekcf118d42009-02-04 23:49:09 +00002058void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002059 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002060 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002061 if (exampleReport) {
2062 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2063 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2064 E=C.end(); I != E; ++I) {
2065 FlushReport(exampleReport, **I, bugReports);
2066 }
2067 }
2068}
2069
2070void BugReporter::FlushReport(BugReport *exampleReport,
2071 PathDiagnosticConsumer &PD,
2072 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Ted Kremenekcf118d42009-02-04 23:49:09 +00002074 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002075 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002076 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002078 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002079 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2080 exampleReport->getBugType().getName(),
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002081 PD.useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00002082 ? exampleReport->getDescription()
2083 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002084 BT.getCategory()));
2085
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002086 // Generate the full path diagnostic, using the generation scheme
2087 // specified by the PathDiagnosticConsumer.
2088 if (PD.getGenerationScheme() != PathDiagnosticConsumer::None) {
2089 if (!bugReports.empty())
2090 GeneratePathDiagnostic(*D.get(), PD, bugReports);
2091 }
2092
2093 // If the path is empty, generate a single step path with the location
2094 // of the issue.
2095 if (D->path.empty()) {
2096 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2097 PathDiagnosticPiece *piece =
2098 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2099 BugReport::ranges_iterator Beg, End;
2100 llvm::tie(Beg, End) = exampleReport->getRanges();
2101 for ( ; Beg != End; ++Beg)
2102 piece->addRange(*Beg);
2103 D->getActivePath().push_back(piece);
2104 }
2105
Ted Kremenek072192b2008-04-30 23:47:44 +00002106 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002107 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002108 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2109 e = Meta.end(); i != e; ++i) {
2110 D->addMeta(*i);
2111 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002112
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002113 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002114}
Ted Kremenek57202072008-07-14 17:40:50 +00002115
Ted Kremenek07189522012-04-04 18:11:35 +00002116void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002117 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002118 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002119 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002120 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002122 // 'BT' is owned by BugReporter.
2123 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002124 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002125 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002126 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2127 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002128}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002129
Chris Lattner5f9e2722011-07-23 10:55:15 +00002130BugType *BugReporter::getBugTypeForName(StringRef name,
2131 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002132 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002133 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2134 llvm::StringMapEntry<BugType *> &
2135 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2136 BugType *BT = entry.getValue();
2137 if (!BT) {
2138 BT = new BugType(name, category);
2139 entry.setValue(BT);
2140 }
2141 return BT;
2142}