blob: 3936d561c5729890332a64aa7e30058d9d0e5621 [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"
Ted Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000021#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000029#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000030#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000031
32using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000033using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenek8966bc12009-05-06 21:39:49 +000035BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000036
Ted Kremenekcf118d42009-02-04 23:49:09 +000037//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000038// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000039//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000040
Ted Kremenek9c378f72011-08-12 23:37:29 +000041static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000042 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
43 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000044 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000045 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000046
Ted Kremenekb697b102009-02-23 22:44:26 +000047 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000048}
49
Zhongxing Xuc5619d92009-08-06 01:32:16 +000050static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000051GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000052 return N->pred_empty() ? NULL : *(N->pred_begin());
53}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000054
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000057 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000058}
59
Ted Kremenek9c378f72011-08-12 23:37:29 +000060static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000061 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000062 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000063 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenekb697b102009-02-23 22:44:26 +000065 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000066}
67
Ted Kremenek9c378f72011-08-12 23:37:29 +000068static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000069 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000070 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000071 // Check if the statement is '?' or '&&'/'||'. These are "merges",
72 // not actual statement points.
73 switch (S->getStmtClass()) {
74 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000075 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 case Stmt::ConditionalOperatorClass: continue;
77 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000078 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
79 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 continue;
81 break;
82 }
83 default:
84 break;
85 }
Ted Kremenekb697b102009-02-23 22:44:26 +000086 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenekb697b102009-02-23 22:44:26 +000089 return 0;
90}
91
Ted Kremenek5f85e172009-07-22 22:35:28 +000092static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000093GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000094 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremenekb697b102009-02-23 22:44:26 +000097 return GetPreviousStmt(N);
98}
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek5f85e172009-07-22 22:35:28 +0000100static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000101GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000102 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000103 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenekb697b102009-02-23 22:44:26 +0000105 return GetNextStmt(N);
106}
107
108//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000109// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000110//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000111
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000112typedef llvm::DenseMap<const ExplodedNode*,
113const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000114
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000115namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000116class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000117 NodeBackMap& M;
118public:
119 NodeMapClosure(NodeBackMap *m) : M(*m) {}
120 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek9c378f72011-08-12 23:37:29 +0000122 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000123 NodeBackMap::iterator I = M.find(N);
124 return I == M.end() ? 0 : I->second;
125 }
126};
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000128class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000129 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000130 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000131 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000132 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000133public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000134 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000135 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000136 PathDiagnosticClient *pdc)
137 : BugReporterContext(br),
Anna Zaks8e6431a2011-08-18 22:37:56 +0000138 R(r), PDC(pdc), NMC(Backmap) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenek9c378f72011-08-12 23:37:29 +0000140 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek9c378f72011-08-12 23:37:29 +0000142 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
143 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Anna Zaks8e6431a2011-08-18 22:37:56 +0000145 BugReport *getBugReport() { return R; }
146
Tom Care212f6d32010-09-16 03:50:38 +0000147 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000148
Anna Zaks220ac8c2011-09-15 01:08:34 +0000149 const LocationContext* getLocationContext() {
150 return R->getErrorNode()->getLocationContext();
151 }
152
Tom Care212f6d32010-09-16 03:50:38 +0000153 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000155 const Stmt *getParent(const Stmt *S) {
156 return getParentMap().getParent(S);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek8966bc12009-05-06 21:39:49 +0000159 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000160
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000161 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek7dc86642009-03-31 20:22:36 +0000163 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
164 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
165 }
166
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000167 bool supportsLogicalOpControlFlow() const {
168 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000169 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000170};
171} // end anonymous namespace
172
Ted Kremenek00605e02009-03-27 20:55:39 +0000173PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000174PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000175 if (const Stmt *S = GetNextStmt(N))
Anna Zaks220ac8c2011-09-15 01:08:34 +0000176 return PathDiagnosticLocation(S, getSourceManager(), getLocationContext());
Ted Kremenek00605e02009-03-27 20:55:39 +0000177
Anna Zaks0cd59482011-09-16 19:18:30 +0000178 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
179 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000180}
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek00605e02009-03-27 20:55:39 +0000182PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000183PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
184 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000185
Ted Kremenek143ca222008-05-06 18:11:09 +0000186 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000187 if (os.str().empty())
188 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek00605e02009-03-27 20:55:39 +0000190 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek00605e02009-03-27 20:55:39 +0000192 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000193 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000194 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000195 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000196 else {
197 os << "Execution jumps to the end of the ";
198 const Decl *D = N->getLocationContext()->getDecl();
199 if (isa<ObjCMethodDecl>(D))
200 os << "method";
201 else if (isa<FunctionDecl>(D))
202 os << "function";
203 else {
204 assert(isa<BlockDecl>(D));
205 os << "anonymous block";
206 }
207 os << '.';
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000210 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000211}
212
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000213static bool IsNested(const Stmt *S, ParentMap &PM) {
214 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
215 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000217 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000219 if (Parent)
220 switch (Parent->getStmtClass()) {
221 case Stmt::ForStmtClass:
222 case Stmt::DoStmtClass:
223 case Stmt::WhileStmtClass:
224 return true;
225 default:
226 break;
227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
229 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000230}
231
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000232PathDiagnosticLocation
233PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000234 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000235 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000236 SourceManager &SMgr = getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000237 const LocationContext *LC = getLocationContext();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000240 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000242 if (!Parent)
243 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000245 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000246 case Stmt::BinaryOperatorClass: {
247 const BinaryOperator *B = cast<BinaryOperator>(Parent);
248 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000249 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000250 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000251 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000252 case Stmt::CompoundStmtClass:
253 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000254 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000255 case Stmt::ChooseExprClass:
256 // Similar to '?' if we are referring to condition, just have the edge
257 // point to the entire choose expression.
258 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000259 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000260 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000261 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000262 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000263 case Stmt::ConditionalOperatorClass:
264 // For '?', if we are referring to condition, just have the edge point
265 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000266 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000267 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000268 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000269 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000270 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000271 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000272 case Stmt::ForStmtClass:
273 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000274 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000275 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000276 case Stmt::IfStmtClass:
277 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000278 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000279 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::ObjCForCollectionStmtClass:
281 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000282 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000283 break;
284 case Stmt::WhileStmtClass:
285 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000286 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000287 break;
288 default:
289 break;
290 }
291
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000292 S = Parent;
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000295 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000296
297 // Special case: DeclStmts can appear in for statement declarations, in which
298 // case the ForStmt is the context.
299 if (isa<DeclStmt>(S)) {
300 if (const Stmt *Parent = P.getParent(S)) {
301 switch (Parent->getStmtClass()) {
302 case Stmt::ForStmtClass:
303 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000304 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000305 default:
306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
308 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000309 }
310 else if (isa<BinaryOperator>(S)) {
311 // Special case: the binary operator represents the initialization
312 // code in a for statement (this can happen when the variable being
313 // initialized is an old variable.
314 if (const ForStmt *FS =
315 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
316 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000317 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000318 }
319 }
320
Anna Zaks220ac8c2011-09-15 01:08:34 +0000321 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000322}
323
Ted Kremenekcf118d42009-02-04 23:49:09 +0000324//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000325// ScanNotableSymbols: closure-like callback for scanning Store bindings.
326//===----------------------------------------------------------------------===//
327
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000328static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
329 ProgramStateManager& VMgr,
330 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek31061982009-03-31 23:00:32 +0000332 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek31061982009-03-31 23:00:32 +0000334 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek31061982009-03-31 23:00:32 +0000336 if (!isa<PostStmt>(P))
337 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek9c378f72011-08-12 23:37:29 +0000339 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek31061982009-03-31 23:00:32 +0000341 if (!DR)
342 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek13976632010-02-08 16:18:51 +0000344 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek31061982009-03-31 23:00:32 +0000346 if (X != Y)
347 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek9c378f72011-08-12 23:37:29 +0000349 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 if (!VD)
352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 return VD;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 return 0;
358}
359
360namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000361class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000362: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek31061982009-03-31 23:00:32 +0000364 SymbolRef Sym;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000365 const ProgramState *PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000366 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000367 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000368 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000369 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000370 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372public:
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000374 NotableSymbolHandler(SymbolRef sym,
375 const ProgramState *prevst,
376 const Stmt *s,
377 ProgramStateManager& vmgr,
378 const ExplodedNode *pred,
379 PathDiagnostic& pd,
380 BugReporter& br)
381 : Sym(sym),
382 PrevSt(prevst),
383 S(s),
384 VMgr(vmgr),
385 Pred(pred),
386 PD(pd),
387 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek31061982009-03-31 23:00:32 +0000389 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
390 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek31061982009-03-31 23:00:32 +0000394 if (ScanSym != Sym)
395 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
397 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000398 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 if (X == V) // Same binding?
401 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000405 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek31061982009-03-31 23:00:32 +0000407 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
410 if (!B->isAssignmentOp())
411 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek31061982009-03-31 23:00:32 +0000413 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000414 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 if (!DR)
417 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 VD = dyn_cast<VarDecl>(DR->getDecl());
420 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000421 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000422 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
423 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000424 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000425 VD = dyn_cast<VarDecl>(*DS->decl_begin());
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 if (!VD)
429 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000432 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 if (!MostRecent)
435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000438 if (Loc::isLocType(VD->getType())) {
Jordy Rose7df12342011-08-21 05:25:15 +0000439 llvm::SmallString<64> buf;
440 llvm::raw_svector_ostream os(buf);
441 os << '\'' << VD << "' now aliases '" << MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000442 PathDiagnosticLocation L =
443 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
444 Pred->getLocationContext());
Jordy Rose7df12342011-08-21 05:25:15 +0000445 PD.push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000449 }
Ted Kremenek31061982009-03-31 23:00:32 +0000450};
451}
452
Ted Kremenek9c378f72011-08-12 23:37:29 +0000453static void HandleNotableSymbol(const ExplodedNode *N,
454 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000455 SymbolRef Sym, BugReporter& BR,
456 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek9c378f72011-08-12 23:37:29 +0000458 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000459 const ProgramState *PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek31061982009-03-31 23:00:32 +0000461 if (!PrevSt)
462 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 // Look at the region bindings of the current state that map to the
465 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000466 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000467 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
468 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
469}
470
471namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000472class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000473: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek31061982009-03-31 23:00:32 +0000475 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000476 const ExplodedNode *N;
477 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000478 GRBugReporter& BR;
479 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek31061982009-03-31 23:00:32 +0000481public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000482 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000483 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000484 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486 bool HandleBinding(StoreManager& SMgr, Store store,
487 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491 if (!ScanSym)
492 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 if (!BR.isNotable(ScanSym))
495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 if (AlreadyProcessed.count(ScanSym))
498 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 HandleNotableSymbol(N, S, ScanSym, BR, PD);
503 return true;
504 }
505};
506} // end anonymous namespace
507
508//===----------------------------------------------------------------------===//
509// "Minimal" path diagnostic generation algorithm.
510//===----------------------------------------------------------------------===//
511
Ted Kremenek14856d72009-04-06 23:06:54 +0000512static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
513
Ted Kremenek31061982009-03-31 23:00:32 +0000514static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
515 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000516 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000517
Ted Kremenek31061982009-03-31 23:00:32 +0000518 SourceManager& SMgr = PDB.getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000519 const LocationContext *LC = PDB.getLocationContext();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000520 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000521 ? NULL : *(N->pred_begin());
522 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000524 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek31061982009-03-31 23:00:32 +0000526 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek9c378f72011-08-12 23:37:29 +0000528 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
529 const CFGBlock *Src = BE->getSrc();
530 const CFGBlock *Dst = BE->getDst();
531 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek31061982009-03-31 23:00:32 +0000533 if (!T)
534 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Anna Zaks590dd8e2011-09-20 21:38:35 +0000536 PathDiagnosticLocation Start =
537 PathDiagnosticLocation::createBegin(T, SMgr,
538 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 switch (T->getStmtClass()) {
541 default:
542 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000545 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000546 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 if (!S)
549 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000552 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000553 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000556 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000557 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
558 os.str()));
559 break;
560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
562 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000563 // Figure out what case arm we took.
564 std::string sbuf;
565 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek9c378f72011-08-12 23:37:29 +0000567 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000568 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremenek31061982009-03-31 23:00:32 +0000570 switch (S->getStmtClass()) {
571 default:
572 os << "No cases match in the switch statement. "
573 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000574 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000575 break;
576 case Stmt::DefaultStmtClass:
577 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000578 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000579 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek31061982009-03-31 23:00:32 +0000581 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000582 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000583 const CaseStmt *Case = cast<CaseStmt>(S);
584 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000585
586 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000587 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Ted Kremenek9c378f72011-08-12 23:37:29 +0000589 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000590 // FIXME: Maybe this should be an assertion. Are there cases
591 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000592 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000593 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremenek31061982009-03-31 23:00:32 +0000595 if (D) {
596 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000597 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000598 }
599 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000600
601 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000602 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000603
Ted Kremenek31061982009-03-31 23:00:32 +0000604 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000605 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000606 break;
607 }
608 }
609 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
610 os.str()));
611 }
612 else {
613 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000614 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000615 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
616 os.str()));
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek31061982009-03-31 23:00:32 +0000619 break;
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek31061982009-03-31 23:00:32 +0000622 case Stmt::BreakStmtClass:
623 case Stmt::ContinueStmtClass: {
624 std::string sbuf;
625 llvm::raw_string_ostream os(sbuf);
626 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
627 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
628 os.str()));
629 break;
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenek31061982009-03-31 23:00:32 +0000632 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000633 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000634 case Stmt::ConditionalOperatorClass: {
635 std::string sbuf;
636 llvm::raw_string_ostream os(sbuf);
637 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 if (*(Src->succ_begin()+1) == Dst)
640 os << "false";
641 else
642 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek31061982009-03-31 23:00:32 +0000644 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek31061982009-03-31 23:00:32 +0000646 if (const Stmt *S = End.asStmt())
647 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
650 os.str()));
651 break;
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek31061982009-03-31 23:00:32 +0000654 // Determine control-flow for short-circuited '&&' and '||'.
655 case Stmt::BinaryOperatorClass: {
656 if (!PDB.supportsLogicalOpControlFlow())
657 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000659 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000660 std::string sbuf;
661 llvm::raw_string_ostream os(sbuf);
662 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000663
John McCall2de56d12010-08-25 11:45:40 +0000664 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000665 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek31061982009-03-31 23:00:32 +0000667 if (*(Src->succ_begin()+1) == Dst) {
668 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000669 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000670 PathDiagnosticLocation Start =
671 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek31061982009-03-31 23:00:32 +0000672 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
673 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000674 }
Ted Kremenek31061982009-03-31 23:00:32 +0000675 else {
676 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000677 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000678 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
679 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
680 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000681 }
Ted Kremenek31061982009-03-31 23:00:32 +0000682 }
683 else {
John McCall2de56d12010-08-25 11:45:40 +0000684 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000685 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek31061982009-03-31 23:00:32 +0000687 if (*(Src->succ_begin()+1) == Dst) {
688 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000689 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000690 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
691 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000692 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000693 }
694 else {
695 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000696 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000697 PathDiagnosticLocation Start =
698 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek31061982009-03-31 23:00:32 +0000699 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000701 }
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek31061982009-03-31 23:00:32 +0000704 break;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
707 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000708 if (*(Src->succ_begin()) == Dst) {
709 std::string sbuf;
710 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 os << "Loop condition is true. ";
713 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenek31061982009-03-31 23:00:32 +0000715 if (const Stmt *S = End.asStmt())
716 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
719 os.str()));
720 }
721 else {
722 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek31061982009-03-31 23:00:32 +0000724 if (const Stmt *S = End.asStmt())
725 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek31061982009-03-31 23:00:32 +0000727 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
728 "Loop condition is false. Exiting loop"));
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek31061982009-03-31 23:00:32 +0000731 break;
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000735 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000736 if (*(Src->succ_begin()+1) == Dst) {
737 std::string sbuf;
738 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek31061982009-03-31 23:00:32 +0000740 os << "Loop condition is false. ";
741 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
742 if (const Stmt *S = End.asStmt())
743 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek31061982009-03-31 23:00:32 +0000745 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
746 os.str()));
747 }
748 else {
749 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
750 if (const Stmt *S = End.asStmt())
751 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek31061982009-03-31 23:00:32 +0000753 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000754 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek31061982009-03-31 23:00:32 +0000757 break;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 case Stmt::IfStmtClass: {
761 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek31061982009-03-31 23:00:32 +0000763 if (const Stmt *S = End.asStmt())
764 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek31061982009-03-31 23:00:32 +0000766 if (*(Src->succ_begin()+1) == Dst)
767 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000768 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000769 else
Ted Kremenek31061982009-03-31 23:00:32 +0000770 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000771 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek31061982009-03-31 23:00:32 +0000773 break;
774 }
775 }
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000778 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000779 // Add diagnostic pieces from custom visitors.
780 BugReport *R = PDB.getBugReport();
781 for (BugReport::visitor_iterator I = R->visitor_begin(),
782 E = R->visitor_end(); I!=E; ++I) {
783 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000784 PD.push_front(p);
785 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek9c378f72011-08-12 23:37:29 +0000788 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000789 // Scan the region bindings, and see if a "notable" symbol has a new
790 // lval binding.
791 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
792 PDB.getStateManager().iterBindings(N->getState(), SNS);
793 }
794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek14856d72009-04-06 23:06:54 +0000796 // After constructing the full PathDiagnostic, do a pass over it to compact
797 // PathDiagnosticPieces that occur within a macro.
798 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000799}
800
801//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000802// "Extensive" PathDiagnostic generation.
803//===----------------------------------------------------------------------===//
804
805static bool IsControlFlowExpr(const Stmt *S) {
806 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000808 if (!E)
809 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 E = E->IgnoreParenCasts();
812
John McCall56ca35d2011-02-17 10:25:35 +0000813 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000814 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000816 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
817 if (B->isLogicalOp())
818 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
820 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000821}
822
Ted Kremenek14856d72009-04-06 23:06:54 +0000823namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000824class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000825 bool IsDead;
826public:
827 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
828 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000829
830 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000831 bool isDead() const { return IsDead; }
832};
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000834class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000835 std::vector<ContextLocation> CLocs;
836 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000837 PathDiagnostic &PD;
838 PathDiagnosticBuilder &PDB;
839 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000841 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek14856d72009-04-06 23:06:54 +0000843 bool containsLocation(const PathDiagnosticLocation &Container,
844 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek14856d72009-04-06 23:06:54 +0000846 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek9650cf32009-05-11 21:42:34 +0000848 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
849 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000850 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000851 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000852 while (1) {
853 // Adjust the location for some expressions that are best referenced
854 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000855 switch (S->getStmtClass()) {
856 default:
857 break;
858 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000859 case Stmt::GenericSelectionExprClass:
860 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000861 firstCharOnly = true;
862 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000863 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000864 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000865 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000866 firstCharOnly = true;
867 continue;
868 case Stmt::ChooseExprClass:
869 S = cast<ChooseExpr>(S)->getCond();
870 firstCharOnly = true;
871 continue;
872 case Stmt::BinaryOperatorClass:
873 S = cast<BinaryOperator>(S)->getLHS();
874 firstCharOnly = true;
875 continue;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek9650cf32009-05-11 21:42:34 +0000878 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 if (S != Original)
Anna Zaks23803372011-09-20 16:23:37 +0000882 L = PathDiagnosticLocation(S, L.getManager(), PDB.getLocationContext());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenek9650cf32009-05-11 21:42:34 +0000885 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000886 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000887
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000888 return L;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek14856d72009-04-06 23:06:54 +0000891 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000892 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000893 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000894 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000895 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000896 CLocs.pop_back();
897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Ted Kremenek14856d72009-04-06 23:06:54 +0000899public:
900 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
901 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremeneka301a672009-04-22 18:16:20 +0000903 // If the PathDiagnostic already has pieces, add the enclosing statement
904 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000905 if (!PD.empty()) {
906 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek14856d72009-04-06 23:06:54 +0000908 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000909 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 }
911 }
912
913 ~EdgeBuilder() {
914 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000915
Ted Kremeneka301a672009-04-22 18:16:20 +0000916 // Finally, add an initial edge from the start location of the first
917 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000918 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
919 PDB.getLocationContext(),
920 PDB.getSourceManager());
921 if (L.isValid())
922 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000923 }
924
925 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000927 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek14856d72009-04-06 23:06:54 +0000929 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000930 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000931};
Ted Kremenek14856d72009-04-06 23:06:54 +0000932} // end anonymous namespace
933
934
935PathDiagnosticLocation
936EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
937 if (const Stmt *S = L.asStmt()) {
938 if (IsControlFlowExpr(S))
939 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000940
941 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 return L;
945}
946
947bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
948 const PathDiagnosticLocation &Containee) {
949
950 if (Container == Containee)
951 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 if (Container.asDecl())
954 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 if (const Stmt *S = Containee.asStmt())
957 if (const Stmt *ContainerS = Container.asStmt()) {
958 while (S) {
959 if (S == ContainerS)
960 return true;
961 S = PDB.getParent(S);
962 }
963 return false;
964 }
965
966 // Less accurate: compare using source ranges.
967 SourceRange ContainerR = Container.asRange();
968 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek14856d72009-04-06 23:06:54 +0000970 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000971 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
972 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
973 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
974 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chandler Carruth64211622011-07-25 21:09:52 +0000976 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
977 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
978 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
979 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000982 assert(ContaineeBegLine <= ContaineeEndLine);
983
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 return (ContainerBegLine <= ContaineeBegLine &&
985 ContainerEndLine >= ContaineeEndLine &&
986 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000987 SM.getExpansionColumnNumber(ContainerRBeg) <=
988 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000989 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000990 SM.getExpansionColumnNumber(ContainerREnd) >=
991 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000992}
993
Ted Kremenek14856d72009-04-06 23:06:54 +0000994void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
995 if (!PrevLoc.isValid()) {
996 PrevLoc = NewLoc;
997 return;
998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001000 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1001 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001003 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001007 if (NewLocClean.asLocation().getExpansionLoc() ==
1008 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 return;
1010
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001011 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1012 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001013}
1014
1015void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremeneka301a672009-04-22 18:16:20 +00001017 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1018 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek14856d72009-04-06 23:06:54 +00001020 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1021
1022 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001023 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 // Is the top location context the same as the one for the new location?
1026 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001027 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001028 if (IsConsumedExpr(TopContextLoc) &&
1029 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001030 TopContextLoc.markDead();
1031
Ted Kremenek14856d72009-04-06 23:06:54 +00001032 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001034
1035 return;
1036 }
1037
1038 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001040 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001042 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 CLocs.push_back(ContextLocation(CLoc, true));
1044 return;
1045 }
1046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001049 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001050 }
1051
1052 // Context does not contain the location. Flush it.
1053 popLocation();
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001056 // If we reach here, there is no enclosing context. Just add the edge.
1057 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001058}
1059
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001060bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1061 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1062 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001064 return false;
1065}
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremeneke1baed32009-05-05 23:13:38 +00001067void EdgeBuilder::addExtendedContext(const Stmt *S) {
1068 if (!S)
1069 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001070
1071 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001072 while (Parent) {
1073 if (isa<CompoundStmt>(Parent))
1074 Parent = PDB.getParent(Parent);
1075 else
1076 break;
1077 }
1078
1079 if (Parent) {
1080 switch (Parent->getStmtClass()) {
1081 case Stmt::DoStmtClass:
1082 case Stmt::ObjCAtSynchronizedStmtClass:
1083 addContext(Parent);
1084 default:
1085 break;
1086 }
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremeneke1baed32009-05-05 23:13:38 +00001089 addContext(S);
1090}
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek14856d72009-04-06 23:06:54 +00001092void EdgeBuilder::addContext(const Stmt *S) {
1093 if (!S)
1094 return;
1095
Anna Zaks220ac8c2011-09-15 01:08:34 +00001096 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremenek14856d72009-04-06 23:06:54 +00001098 while (!CLocs.empty()) {
1099 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1100
1101 // Is the top location context the same as the one for the new location?
1102 if (TopContextLoc == L)
1103 return;
1104
1105 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001106 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001107 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001108 }
1109
1110 // Context does not contain the location. Flush it.
1111 popLocation();
1112 }
1113
1114 CLocs.push_back(L);
1115}
1116
1117static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1118 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001119 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001121 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001122
Ted Kremenek9c378f72011-08-12 23:37:29 +00001123 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001124 while (NextNode) {
1125 N = NextNode;
1126 NextNode = GetPredecessorNode(N);
1127 ProgramPoint P = N->getLocation();
1128
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001129 do {
1130 // Block edges.
1131 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1132 const CFGBlock &Blk = *BE->getSrc();
1133 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001135 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001136 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001137 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001138 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001140 if (!Term) {
1141 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1142 CS = dyn_cast<CompoundStmt>(FS->getBody());
1143 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001144 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001147 PathDiagnosticEventPiece *p =
1148 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001149 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 EB.addEdge(p->getLocation(), true);
1152 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001154 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001155 PathDiagnosticLocation BL =
1156 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001157 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001158 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001161 if (Term)
1162 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001164 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001165 }
1166
Mike Stump1eb44332009-09-09 15:08:12 +00001167 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001168 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1169 const Stmt *stmt = S->getStmt();
1170 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001171 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001172 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001173 }
1174 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001175 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001177
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001178 break;
1179 }
1180 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001183 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Anna Zaks8e6431a2011-08-18 22:37:56 +00001185 // Add pieces from custom visitors.
1186 BugReport *R = PDB.getBugReport();
1187 for (BugReport::visitor_iterator I = R->visitor_begin(),
1188 E = R->visitor_end(); I!=E; ++I) {
1189 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001190 const PathDiagnosticLocation &Loc = p->getLocation();
1191 EB.addEdge(Loc, true);
1192 PD.push_front(p);
1193 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001194 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001195 }
Mike Stump1eb44332009-09-09 15:08:12 +00001196 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001197 }
1198}
1199
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001200//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001201// Methods for BugType and subclasses.
1202//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001203BugType::~BugType() { }
1204
Ted Kremenekcf118d42009-02-04 23:49:09 +00001205void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001206
Ted Kremenekcf118d42009-02-04 23:49:09 +00001207//===----------------------------------------------------------------------===//
1208// Methods for BugReport and subclasses.
1209//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001210
Anna Zaks8e6431a2011-08-18 22:37:56 +00001211void BugReport::addVisitor(BugReporterVisitor* visitor) {
1212 if (!visitor)
1213 return;
1214
1215 llvm::FoldingSetNodeID ID;
1216 visitor->Profile(ID);
1217 void *InsertPos;
1218
1219 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1220 delete visitor;
1221 return;
1222 }
1223
1224 CallbacksSet.InsertNode(visitor, InsertPos);
1225 Callbacks = F.add(visitor, Callbacks);
1226}
1227
1228BugReport::~BugReport() {
1229 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001230 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001231 }
1232}
Anna Zakse172e8b2011-08-17 23:00:25 +00001233
1234void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1235 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001236 hash.AddString(Description);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001237 if (Location.isValid()) {
1238 Location.Profile(hash);
1239 } else {
1240 assert(ErrorNode);
1241 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1242 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001243
1244 for (SmallVectorImpl<SourceRange>::const_iterator I =
1245 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1246 const SourceRange range = *I;
1247 if (!range.isValid())
1248 continue;
1249 hash.AddInteger(range.getBegin().getRawEncoding());
1250 hash.AddInteger(range.getEnd().getRawEncoding());
1251 }
1252}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001253
Ted Kremenek9c378f72011-08-12 23:37:29 +00001254const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001255 if (!ErrorNode)
1256 return 0;
1257
Tom Care212f6d32010-09-16 03:50:38 +00001258 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001259 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek9c378f72011-08-12 23:37:29 +00001261 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001262 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001263 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001264 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001265 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001266 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001267 S = GetStmt(ProgP);
1268
1269 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001270}
1271
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001272std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001273BugReport::getRanges() {
1274 // If no custom ranges, add the range of the statement corresponding to
1275 // the error node.
1276 if (Ranges.empty()) {
1277 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1278 addRange(E->getSourceRange());
1279 else
1280 return std::make_pair(ranges_iterator(), ranges_iterator());
1281 }
1282
Anna Zaks14924262011-08-24 20:31:06 +00001283 // User-specified absence of range info.
1284 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1285 return std::make_pair(ranges_iterator(), ranges_iterator());
1286
Anna Zakse172e8b2011-08-17 23:00:25 +00001287 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001288}
1289
Anna Zaks590dd8e2011-09-20 21:38:35 +00001290PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001291 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001292 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001293 "Either Location or ErrorNode should be specified but not both.");
1294
Ted Kremenek9c378f72011-08-12 23:37:29 +00001295 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001296 const LocationContext *LC = ErrorNode->getLocationContext();
1297
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001298 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001299 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001300 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001301 // For binary operators, return the location of the operator.
1302 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001303 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001304
Anna Zaks590dd8e2011-09-20 21:38:35 +00001305 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001306 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001307 } else {
1308 assert(Location.isValid());
1309 return Location;
1310 }
1311
Anna Zaks590dd8e2011-09-20 21:38:35 +00001312 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001313}
1314
Ted Kremenekcf118d42009-02-04 23:49:09 +00001315//===----------------------------------------------------------------------===//
1316// Methods for BugReporter and subclasses.
1317//===----------------------------------------------------------------------===//
1318
1319BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001320 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001321}
1322
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001323GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001324BugReporterData::~BugReporterData() {}
1325
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001326ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001327
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001328ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001329GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1330
Anna Zaks3b030a22011-08-19 01:57:09 +00001331BugReporter::~BugReporter() {
1332 FlushReports();
1333
1334 // Free the bug reports we are tracking.
1335 typedef std::vector<BugReportEquivClass *> ContTy;
1336 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1337 I != E; ++I) {
1338 delete *I;
1339 }
1340}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001341
1342void BugReporter::FlushReports() {
1343 if (BugTypes.isEmpty())
1344 return;
1345
1346 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001347 // warnings and new BugTypes.
1348 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1349 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001350 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001351 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001352 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001353 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001354 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001355 const_cast<BugType*>(*I)->FlushReports(*this);
1356
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001357 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1358 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1359 BugReportEquivClass& EQ = *EI;
1360 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001361 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001362
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001363 // BugReporter owns and deletes only BugTypes created implicitly through
1364 // EmitBasicReport.
1365 // FIXME: There are leaks from checkers that assume that the BugTypes they
1366 // create will be destroyed by the BugReporter.
1367 for (llvm::StringMap<BugType*>::iterator
1368 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1369 delete I->second;
1370
Ted Kremenekcf118d42009-02-04 23:49:09 +00001371 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001372 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001373}
1374
1375//===----------------------------------------------------------------------===//
1376// PathDiagnostics generation.
1377//===----------------------------------------------------------------------===//
1378
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001379static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001380 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001381MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001382 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremenekcf118d42009-02-04 23:49:09 +00001384 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001385 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386 // error node unless there are two or more error nodes with the same minimum
1387 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001388 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001389 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001390
1391 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001392 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1393 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Ted Kremenekcf118d42009-02-04 23:49:09 +00001395 // Create owning pointers for GTrim and NMap just to ensure that they are
1396 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001397 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001398 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Ted Kremenekcf118d42009-02-04 23:49:09 +00001400 // Find the (first) error node in the trimmed graph. We just need to consult
1401 // the node map (NMap) which maps from nodes in the original graph to nodes
1402 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001403
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001404 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001405 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001406 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001407
Ted Kremenek40406fe2010-12-03 06:52:30 +00001408 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1409 const ExplodedNode *originalNode = nodes[nodeIndex];
1410 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001411 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001412 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001413 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek938332c2009-05-16 01:11:58 +00001416 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001417
1418 // Create a new (third!) graph with a single path. This is the graph
1419 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001420 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenek10aa5542009-03-12 23:41:59 +00001422 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 // to the root node, and then construct a new graph that contains only
1424 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001425 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001428 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001430 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001431 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001432 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001434 if (Visited.find(Node) != Visited.end())
1435 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001437 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001439 if (Node->pred_empty()) {
1440 Root = Node;
1441 break;
1442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001444 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001445 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001446 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Ted Kremenek938332c2009-05-16 01:11:58 +00001449 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek10aa5542009-03-12 23:41:59 +00001451 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001452 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001453 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001454 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001455 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001457 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001458 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001459 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001460 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001462 // Create the equivalent node in the new graph with the same state
1463 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001464 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001466 // Store the mapping to the original node.
1467 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1468 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001469 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001471 // Link up the new node with the previous node.
1472 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001473 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001475 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001477 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001478 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001479 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001480 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001481 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001482 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001483 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001486 // Find the next successor node. We choose the node that is marked
1487 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001488 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1489 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001490 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001492 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001494 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001496 if (I == Visited.end())
1497 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001499 if (!N || I->second < MinVal) {
1500 N = *SI;
1501 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001502 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek938332c2009-05-16 01:11:58 +00001505 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek938332c2009-05-16 01:11:58 +00001508 assert(First);
1509
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001510 return std::make_pair(std::make_pair(GNew, BM),
1511 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001512}
1513
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001514/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1515/// and collapses PathDiagosticPieces that are expanded by macros.
1516static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1517 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1518 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001520 typedef std::vector<PathDiagnosticPiece*>
1521 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001523 MacroStackTy MacroStack;
1524 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001526 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1527 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001528 const FullSourceLoc Loc = I->getLocation().asLocation();
1529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 // Determine the instantiation location, which is the location we group
1531 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001532 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001533 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001534 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001536 if (Loc.isFileID()) {
1537 MacroStack.clear();
1538 Pieces.push_back(&*I);
1539 continue;
1540 }
1541
1542 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 // Is the PathDiagnosticPiece within the same macro group?
1545 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1546 MacroStack.back().first->push_back(&*I);
1547 continue;
1548 }
1549
1550 // We aren't in the same group. Are we descending into a new macro
1551 // or are part of an old one?
1552 PathDiagnosticMacroPiece *MacroGroup = 0;
1553
1554 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001555 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 // Walk the entire macro stack.
1559 while (!MacroStack.empty()) {
1560 if (InstantiationLoc == MacroStack.back().second) {
1561 MacroGroup = MacroStack.back().first;
1562 break;
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565 if (ParentInstantiationLoc == MacroStack.back().second) {
1566 MacroGroup = MacroStack.back().first;
1567 break;
1568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 MacroStack.pop_back();
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001573 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1574 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001575 PathDiagnosticMacroPiece *NewGroup =
1576 new PathDiagnosticMacroPiece(
1577 PathDiagnosticLocation::createSingleLocation(I->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001578
1579 if (MacroGroup)
1580 MacroGroup->push_back(NewGroup);
1581 else {
1582 assert(InstantiationLoc.isFileID());
1583 Pieces.push_back(NewGroup);
1584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001586 MacroGroup = NewGroup;
1587 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1588 }
1589
1590 // Finally, add the PathDiagnosticPiece to the group.
1591 MacroGroup->push_back(&*I);
1592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001594 // Now take the pieces and construct a new PathDiagnostic.
1595 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001597 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1598 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1599 if (!MP->containsEvent()) {
1600 delete MP;
1601 continue;
1602 }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001604 PD.push_back(*I);
1605 }
1606}
1607
Ted Kremenek7dc86642009-03-31 20:22:36 +00001608void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001609 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenek40406fe2010-12-03 06:52:30 +00001611 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001612 SmallVector<const ExplodedNode *, 10> errorNodes;
1613 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001614 E = bugReports.end(); I != E; ++I) {
1615 errorNodes.push_back((*I)->getErrorNode());
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001618 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001619 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001620 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001621 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001622 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenekcf118d42009-02-04 23:49:09 +00001624 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001625 assert(GPair.second.second < bugReports.size());
1626 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001627 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001629 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001630 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001631 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
1633 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001634 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Anna Zaks8e6431a2011-08-18 22:37:56 +00001636 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001637 R->addVisitor(new NilReceiverBRVisitor());
1638 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Anna Zaks23f395e2011-08-20 01:27:22 +00001640 // Generate the very last diagnostic piece - the piece is visible before
1641 // the trace is expanded.
1642 PathDiagnosticPiece *LastPiece = 0;
1643 for (BugReport::visitor_iterator I = R->visitor_begin(),
1644 E = R->visitor_end(); I!=E; ++I) {
1645 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1646 assert (!LastPiece &&
1647 "There can only be one final piece in a diagnostic.");
1648 LastPiece = Piece;
1649 }
1650 }
1651 if (!LastPiece)
1652 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1653 if (LastPiece)
1654 PD.push_back(LastPiece);
1655 else
1656 return;
1657
Ted Kremenek7dc86642009-03-31 20:22:36 +00001658 switch (PDB.getGenerationScheme()) {
1659 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001660 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001661 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001662 case PathDiagnosticClient::Minimal:
1663 GenerateMinimalPathDiagnostic(PD, PDB, N);
1664 break;
1665 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001666}
1667
Ted Kremenekcf118d42009-02-04 23:49:09 +00001668void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001669 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001670}
1671
Mike Stump1eb44332009-09-09 15:08:12 +00001672void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001673 // Compute the bug report's hash to determine its equivalence class.
1674 llvm::FoldingSetNodeID ID;
1675 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001676
1677 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001678 BugType& BT = R->getBugType();
1679 Register(&BT);
1680 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001681 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Ted Kremenekcf118d42009-02-04 23:49:09 +00001683 if (!EQ) {
1684 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001685 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001686 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001687 }
1688 else
1689 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001690}
1691
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001692
1693//===----------------------------------------------------------------------===//
1694// Emitting reports in equivalence classes.
1695//===----------------------------------------------------------------------===//
1696
1697namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001698struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001699 const ExplodedNode *N;
1700 ExplodedNode::const_succ_iterator I, E;
1701
1702 FRIEC_WLItem(const ExplodedNode *n)
1703 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1704};
1705}
1706
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001707static BugReport *
1708FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001709 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001710
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001711 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1712 assert(I != E);
1713 BugReport *R = *I;
1714 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001715
Ted Kremenek40406fe2010-12-03 06:52:30 +00001716 // If we don't need to suppress any of the nodes because they are
1717 // post-dominated by a sink, simply add all the nodes in the equivalence class
1718 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001719 if (!BT.isSuppressOnSink()) {
1720 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001721 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001722 if (N) {
1723 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001724 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001725 }
1726 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001727 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001728 }
1729
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001730 // For bug reports that should be suppressed when all paths are post-dominated
1731 // by a sink node, iterate through the reports in the equivalence class
1732 // until we find one that isn't post-dominated (if one exists). We use a
1733 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1734 // this as a recursive function, but we don't want to risk blowing out the
1735 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001736 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001737
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001738 for (; I != E; ++I) {
1739 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001740 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001741
Ted Kremenek40406fe2010-12-03 06:52:30 +00001742 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001743 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001744 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001745 assert(false &&
1746 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001747 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001748 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001749 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001750 if (errorNode->succ_empty()) {
1751 bugReports.push_back(R);
1752 if (!exampleReport)
1753 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001754 continue;
1755 }
1756
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001757 // At this point we know that 'N' is not a sink and it has at least one
1758 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1759 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001760 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001761 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1762
1763 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001764 WL.push_back(errorNode);
1765 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001766
1767 while (!WL.empty()) {
1768 WLItem &WI = WL.back();
1769 assert(!WI.N->succ_empty());
1770
1771 for (; WI.I != WI.E; ++WI.I) {
1772 const ExplodedNode *Succ = *WI.I;
1773 // End-of-path node?
1774 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001775 // If we found an end-of-path node that is not a sink.
1776 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001777 bugReports.push_back(R);
1778 if (!exampleReport)
1779 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001780 WL.clear();
1781 break;
1782 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001783 // Found a sink? Continue on to the next successor.
1784 continue;
1785 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001786 // Mark the successor as visited. If it hasn't been explored,
1787 // enqueue it to the DFS worklist.
1788 unsigned &mark = Visited[Succ];
1789 if (!mark) {
1790 mark = 1;
1791 WL.push_back(Succ);
1792 break;
1793 }
1794 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001795
1796 // The worklist may have been cleared at this point. First
1797 // check if it is empty before checking the last item.
1798 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001799 WL.pop_back();
1800 }
1801 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001802
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001803 // ExampleReport will be NULL if all the nodes in the equivalence class
1804 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001805 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001806}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001807
1808//===----------------------------------------------------------------------===//
1809// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1810// uses global state, which eventually should go elsewhere.
1811//===----------------------------------------------------------------------===//
1812namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001813class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001814 llvm::FoldingSetNodeID ID;
1815public:
1816 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001817 R->Profile(ID);
1818 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001819 }
1820
1821 void Profile(llvm::FoldingSetNodeID &id) {
1822 id = ID;
1823 }
1824
1825 llvm::FoldingSetNodeID &getID() { return ID; }
1826};
1827}
1828
1829static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1830 // FIXME: Eventually this diagnostic cache should reside in something
1831 // like AnalysisManager instead of being a static variable. This is
1832 // really unsafe in the long term.
1833 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1834 static DiagnosticCache DC;
1835
1836 void *InsertPos;
1837 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1838
1839 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1840 delete Item;
1841 return true;
1842 }
1843
1844 DC.InsertNode(Item, InsertPos);
1845 return false;
1846}
1847
Ted Kremenekcf118d42009-02-04 23:49:09 +00001848void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001849 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001850 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1851 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001852 return;
1853
Ted Kremenekd49967f2009-04-29 21:58:13 +00001854 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Ted Kremenekcf118d42009-02-04 23:49:09 +00001856 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001857 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001858 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Ted Kremenekd49967f2009-04-29 21:58:13 +00001860 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001861 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001862 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001863 ? exampleReport->getDescription()
1864 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001865 BT.getCategory()));
1866
Ted Kremenek40406fe2010-12-03 06:52:30 +00001867 if (!bugReports.empty())
1868 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenek40406fe2010-12-03 06:52:30 +00001870 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001871 return;
1872
Ted Kremenek072192b2008-04-30 23:47:44 +00001873 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001874 const BugReport::ExtraTextList &Meta =
1875 exampleReport->getExtraText();
1876 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1877 e = Meta.end(); i != e; ++i) {
1878 D->addMeta(*i);
1879 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001880
Ted Kremenek3148eb42009-01-24 00:55:43 +00001881 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001882 BugReport::ranges_iterator Beg, End;
1883 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 Diagnostic &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001885
1886 // Search the description for '%', as that will be interpretted as a
1887 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001888 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001889 unsigned ErrorDiag;
1890 {
1891 llvm::SmallString<512> TmpStr;
1892 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001893 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001894 if (*I == '%')
1895 Out << "%%";
1896 else
1897 Out << *I;
1898
1899 Out.flush();
1900 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1901 }
Ted Kremenek57202072008-07-14 17:40:50 +00001902
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001903 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001904 DiagnosticBuilder diagBuilder = Diag.Report(
1905 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001906 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001907 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001908 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001909
1910 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1911 if (!PD)
1912 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001913
1914 if (D->empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001915 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1916 exampleReport->getLocation(getSourceManager()),
1917 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001918
Ted Kremenek3148eb42009-01-24 00:55:43 +00001919 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1920 D->push_back(piece);
1921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Ted Kremenek3148eb42009-01-24 00:55:43 +00001923 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001924}
Ted Kremenek57202072008-07-14 17:40:50 +00001925
Chris Lattner5f9e2722011-07-23 10:55:15 +00001926void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001927 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001928 SourceRange* RBeg, unsigned NumRanges) {
1929 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1930}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001931
Chris Lattner5f9e2722011-07-23 10:55:15 +00001932void BugReporter::EmitBasicReport(StringRef name,
1933 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001934 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001935 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001937 // 'BT' is owned by BugReporter.
1938 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001939 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001940 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1941 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001942}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001943
Chris Lattner5f9e2722011-07-23 10:55:15 +00001944BugType *BugReporter::getBugTypeForName(StringRef name,
1945 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001946 llvm::SmallString<136> fullDesc;
1947 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1948 llvm::StringMapEntry<BugType *> &
1949 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1950 BugType *BT = entry.getValue();
1951 if (!BT) {
1952 BT = new BugType(name, category);
1953 entry.setValue(BT);
1954 }
1955 return BT;
1956}