blob: 9bd9d5a59c077341972f7056ba1db5594c89d703 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000029#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000030#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000031#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000032
33using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000034using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000035
Ted Kremenek8966bc12009-05-06 21:39:49 +000036BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000037
David Blaikie99ba9e32011-12-20 02:48:34 +000038void BugReporterContext::anchor() {}
39
Ted Kremenekcf118d42009-02-04 23:49:09 +000040//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000041// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000043
Ted Kremenek9c378f72011-08-12 23:37:29 +000044static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000045 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
46 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000047 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000048 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekb697b102009-02-23 22:44:26 +000050 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000051}
52
Zhongxing Xuc5619d92009-08-06 01:32:16 +000053static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000054GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000055 return N->pred_empty() ? NULL : *(N->pred_begin());
56}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000057
Zhongxing Xuc5619d92009-08-06 01:32:16 +000058static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000059GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000060 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061}
62
Ted Kremenek9c378f72011-08-12 23:37:29 +000063static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000064 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000065 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000069}
70
Ted Kremenek9c378f72011-08-12 23:37:29 +000071static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000072 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000073 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000074 // Check if the statement is '?' or '&&'/'||'. These are "merges",
75 // not actual statement points.
76 switch (S->getStmtClass()) {
77 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000078 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000079 case Stmt::ConditionalOperatorClass: continue;
80 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000081 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
82 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000083 continue;
84 break;
85 }
86 default:
87 break;
88 }
Ted Kremenekb697b102009-02-23 22:44:26 +000089 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekb697b102009-02-23 22:44:26 +000092 return 0;
93}
94
Ted Kremenek5f85e172009-07-22 22:35:28 +000095static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000096GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000097 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return GetPreviousStmt(N);
101}
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000104GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return GetNextStmt(N);
109}
110
111//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000112// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000113//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000114
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000115typedef llvm::DenseMap<const ExplodedNode*,
116const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000117
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000118namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000119class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000120 NodeBackMap& M;
121public:
122 NodeMapClosure(NodeBackMap *m) : M(*m) {}
123 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek9c378f72011-08-12 23:37:29 +0000125 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000126 NodeBackMap::iterator I = M.find(N);
127 return I == M.end() ? 0 : I->second;
128 }
129};
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000131class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000132 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000133 PathDiagnosticConsumer *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000134 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000135 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000136public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000137 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000138 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000139 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000140 : BugReporterContext(br),
Anna Zaks8e6431a2011-08-18 22:37:56 +0000141 R(r), PDC(pdc), NMC(Backmap) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek9c378f72011-08-12 23:37:29 +0000143 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek9c378f72011-08-12 23:37:29 +0000145 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
146 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anna Zaks8e6431a2011-08-18 22:37:56 +0000148 BugReport *getBugReport() { return R; }
149
Tom Care212f6d32010-09-16 03:50:38 +0000150 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000151
Anna Zaks220ac8c2011-09-15 01:08:34 +0000152 const LocationContext* getLocationContext() {
153 return R->getErrorNode()->getLocationContext();
154 }
155
Tom Care212f6d32010-09-16 03:50:38 +0000156 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000158 const Stmt *getParent(const Stmt *S) {
159 return getParentMap().getParent(S);
160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek8966bc12009-05-06 21:39:49 +0000162 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000163
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000164 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
David Blaikieef3643f2011-09-26 00:51:36 +0000166 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
167 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000168 }
169
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000170 bool supportsLogicalOpControlFlow() const {
171 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000172 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000173};
174} // end anonymous namespace
175
Ted Kremenek00605e02009-03-27 20:55:39 +0000176PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000177PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000178 if (const Stmt *S = GetNextStmt(N))
Anna Zaks220ac8c2011-09-15 01:08:34 +0000179 return PathDiagnosticLocation(S, getSourceManager(), getLocationContext());
Ted Kremenek00605e02009-03-27 20:55:39 +0000180
Anna Zaks0cd59482011-09-16 19:18:30 +0000181 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
182 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000183}
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenek00605e02009-03-27 20:55:39 +0000185PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000186PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
187 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000188
Ted Kremenek143ca222008-05-06 18:11:09 +0000189 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000190 if (os.str().empty())
191 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek00605e02009-03-27 20:55:39 +0000193 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek00605e02009-03-27 20:55:39 +0000195 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000196 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000197 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000198 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000199 else {
200 os << "Execution jumps to the end of the ";
201 const Decl *D = N->getLocationContext()->getDecl();
202 if (isa<ObjCMethodDecl>(D))
203 os << "method";
204 else if (isa<FunctionDecl>(D))
205 os << "function";
206 else {
207 assert(isa<BlockDecl>(D));
208 os << "anonymous block";
209 }
210 os << '.';
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000213 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000214}
215
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000216static bool IsNested(const Stmt *S, ParentMap &PM) {
217 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
218 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000220 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000222 if (Parent)
223 switch (Parent->getStmtClass()) {
224 case Stmt::ForStmtClass:
225 case Stmt::DoStmtClass:
226 case Stmt::WhileStmtClass:
227 return true;
228 default:
229 break;
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
232 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000233}
234
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000235PathDiagnosticLocation
236PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000237 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000238 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000239 SourceManager &SMgr = getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000240 const LocationContext *LC = getLocationContext();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000241
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000242 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000243 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000245 if (!Parent)
246 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000248 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000249 case Stmt::BinaryOperatorClass: {
250 const BinaryOperator *B = cast<BinaryOperator>(Parent);
251 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000252 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000253 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000254 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000255 case Stmt::CompoundStmtClass:
256 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000257 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000258 case Stmt::ChooseExprClass:
259 // Similar to '?' if we are referring to condition, just have the edge
260 // point to the entire choose expression.
261 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000262 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000263 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000264 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000265 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000266 case Stmt::ConditionalOperatorClass:
267 // For '?', if we are referring to condition, just have the edge point
268 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000269 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000270 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000271 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000272 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000273 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000274 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000275 case Stmt::ForStmtClass:
276 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000277 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000278 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000279 case Stmt::IfStmtClass:
280 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000281 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000282 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000283 case Stmt::ObjCForCollectionStmtClass:
284 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000285 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000286 break;
287 case Stmt::WhileStmtClass:
288 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000289 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000290 break;
291 default:
292 break;
293 }
294
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000295 S = Parent;
296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000298 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000299
300 // Special case: DeclStmts can appear in for statement declarations, in which
301 // case the ForStmt is the context.
302 if (isa<DeclStmt>(S)) {
303 if (const Stmt *Parent = P.getParent(S)) {
304 switch (Parent->getStmtClass()) {
305 case Stmt::ForStmtClass:
306 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000307 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000308 default:
309 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000310 }
311 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000312 }
313 else if (isa<BinaryOperator>(S)) {
314 // Special case: the binary operator represents the initialization
315 // code in a for statement (this can happen when the variable being
316 // initialized is an old variable.
317 if (const ForStmt *FS =
318 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
319 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000320 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000321 }
322 }
323
Anna Zaks220ac8c2011-09-15 01:08:34 +0000324 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000325}
326
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000328// ScanNotableSymbols: closure-like callback for scanning Store bindings.
329//===----------------------------------------------------------------------===//
330
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000331static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
332 ProgramStateManager& VMgr,
333 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek31061982009-03-31 23:00:32 +0000335 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek31061982009-03-31 23:00:32 +0000337 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek31061982009-03-31 23:00:32 +0000339 if (!isa<PostStmt>(P))
340 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek9c378f72011-08-12 23:37:29 +0000342 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek31061982009-03-31 23:00:32 +0000344 if (!DR)
345 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek5eca4822012-01-06 22:09:28 +0000347 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek31061982009-03-31 23:00:32 +0000349 if (X != Y)
350 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek9c378f72011-08-12 23:37:29 +0000352 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 if (!VD)
355 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 return VD;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 return 0;
361}
362
363namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000364class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000365: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek31061982009-03-31 23:00:32 +0000367 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000368 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000369 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000370 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000371 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000372 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000373 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek31061982009-03-31 23:00:32 +0000375public:
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000377 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000378 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000379 const Stmt *s,
380 ProgramStateManager& vmgr,
381 const ExplodedNode *pred,
382 PathDiagnostic& pd,
383 BugReporter& br)
384 : Sym(sym),
385 PrevSt(prevst),
386 S(s),
387 VMgr(vmgr),
388 Pred(pred),
389 PD(pd),
390 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
393 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek31061982009-03-31 23:00:32 +0000395 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek31061982009-03-31 23:00:32 +0000397 if (ScanSym != Sym)
398 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
400 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000401 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 if (X == V) // Same binding?
404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000407 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000408 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek31061982009-03-31 23:00:32 +0000410 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
413 if (!B->isAssignmentOp())
414 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000417 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 if (!DR)
420 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek31061982009-03-31 23:00:32 +0000422 VD = dyn_cast<VarDecl>(DR->getDecl());
423 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000424 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000425 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
426 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000427 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000428 VD = dyn_cast<VarDecl>(*DS->decl_begin());
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 if (!VD)
432 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000435 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 if (!MostRecent)
438 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000441 if (Loc::isLocType(VD->getType())) {
Jordy Rose7df12342011-08-21 05:25:15 +0000442 llvm::SmallString<64> buf;
443 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000444 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000445 PathDiagnosticLocation L =
446 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
447 Pred->getLocationContext());
Jordy Rose7df12342011-08-21 05:25:15 +0000448 PD.push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek31061982009-03-31 23:00:32 +0000451 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000452 }
Ted Kremenek31061982009-03-31 23:00:32 +0000453};
454}
455
Ted Kremenek9c378f72011-08-12 23:37:29 +0000456static void HandleNotableSymbol(const ExplodedNode *N,
457 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000458 SymbolRef Sym, BugReporter& BR,
459 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek9c378f72011-08-12 23:37:29 +0000461 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000462 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 if (!PrevSt)
465 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek31061982009-03-31 23:00:32 +0000467 // Look at the region bindings of the current state that map to the
468 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000469 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000470 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
471 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
472}
473
474namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000475class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000476: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek31061982009-03-31 23:00:32 +0000478 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000479 const ExplodedNode *N;
480 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000481 GRBugReporter& BR;
482 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek31061982009-03-31 23:00:32 +0000484public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000485 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000486 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000487 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 bool HandleBinding(StoreManager& SMgr, Store store,
490 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek31061982009-03-31 23:00:32 +0000492 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 if (!ScanSym)
495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 if (!BR.isNotable(ScanSym))
498 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 if (AlreadyProcessed.count(ScanSym))
501 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek31061982009-03-31 23:00:32 +0000503 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 HandleNotableSymbol(N, S, ScanSym, BR, PD);
506 return true;
507 }
508};
509} // end anonymous namespace
510
511//===----------------------------------------------------------------------===//
512// "Minimal" path diagnostic generation algorithm.
513//===----------------------------------------------------------------------===//
514
Ted Kremenek14856d72009-04-06 23:06:54 +0000515static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
516
Ted Kremenek31061982009-03-31 23:00:32 +0000517static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
518 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000519 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 SourceManager& SMgr = PDB.getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000522 const LocationContext *LC = PDB.getLocationContext();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000523 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000524 ? NULL : *(N->pred_begin());
525 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000527 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek31061982009-03-31 23:00:32 +0000529 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek9c378f72011-08-12 23:37:29 +0000531 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
532 const CFGBlock *Src = BE->getSrc();
533 const CFGBlock *Dst = BE->getDst();
534 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 if (!T)
537 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Anna Zaks590dd8e2011-09-20 21:38:35 +0000539 PathDiagnosticLocation Start =
540 PathDiagnosticLocation::createBegin(T, SMgr,
541 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 switch (T->getStmtClass()) {
544 default:
545 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000548 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000549 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 if (!S)
552 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek31061982009-03-31 23:00:32 +0000554 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000555 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000556 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek31061982009-03-31 23:00:32 +0000558 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000559 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000560 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
561 os.str()));
562 break;
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
565 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000566 // Figure out what case arm we took.
567 std::string sbuf;
568 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremenek9c378f72011-08-12 23:37:29 +0000570 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000571 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Ted Kremenek31061982009-03-31 23:00:32 +0000573 switch (S->getStmtClass()) {
574 default:
575 os << "No cases match in the switch statement. "
576 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000577 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000578 break;
579 case Stmt::DefaultStmtClass:
580 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000581 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000582 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek31061982009-03-31 23:00:32 +0000584 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000585 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000586 const CaseStmt *Case = cast<CaseStmt>(S);
587 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000588
589 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000590 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenek9c378f72011-08-12 23:37:29 +0000592 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000593 // FIXME: Maybe this should be an assertion. Are there cases
594 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000595 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000596 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenek31061982009-03-31 23:00:32 +0000598 if (D) {
599 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000600 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000601 }
602 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000603
604 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000605 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000606
Ted Kremenek31061982009-03-31 23:00:32 +0000607 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000608 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000609 break;
610 }
611 }
612 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
613 os.str()));
614 }
615 else {
616 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000617 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000618 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
619 os.str()));
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek31061982009-03-31 23:00:32 +0000622 break;
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek31061982009-03-31 23:00:32 +0000625 case Stmt::BreakStmtClass:
626 case Stmt::ContinueStmtClass: {
627 std::string sbuf;
628 llvm::raw_string_ostream os(sbuf);
629 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
630 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
631 os.str()));
632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000636 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000637 case Stmt::ConditionalOperatorClass: {
638 std::string sbuf;
639 llvm::raw_string_ostream os(sbuf);
640 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 if (*(Src->succ_begin()+1) == Dst)
643 os << "false";
644 else
645 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek31061982009-03-31 23:00:32 +0000647 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 if (const Stmt *S = End.asStmt())
650 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek31061982009-03-31 23:00:32 +0000652 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
653 os.str()));
654 break;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek31061982009-03-31 23:00:32 +0000657 // Determine control-flow for short-circuited '&&' and '||'.
658 case Stmt::BinaryOperatorClass: {
659 if (!PDB.supportsLogicalOpControlFlow())
660 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000662 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000663 std::string sbuf;
664 llvm::raw_string_ostream os(sbuf);
665 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000666
John McCall2de56d12010-08-25 11:45:40 +0000667 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000668 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 if (*(Src->succ_begin()+1) == Dst) {
671 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000672 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000673 PathDiagnosticLocation Start =
674 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek31061982009-03-31 23:00:32 +0000675 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
676 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000677 }
Ted Kremenek31061982009-03-31 23:00:32 +0000678 else {
679 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000680 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000681 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
682 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
683 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000684 }
Ted Kremenek31061982009-03-31 23:00:32 +0000685 }
686 else {
John McCall2de56d12010-08-25 11:45:40 +0000687 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000688 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 if (*(Src->succ_begin()+1) == Dst) {
691 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000692 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000693 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
694 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000696 }
697 else {
698 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000699 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000700 PathDiagnosticLocation Start =
701 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek31061982009-03-31 23:00:32 +0000702 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000703 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000704 }
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 break;
708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
710 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000711 if (*(Src->succ_begin()) == Dst) {
712 std::string sbuf;
713 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenek31061982009-03-31 23:00:32 +0000715 os << "Loop condition is true. ";
716 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (const Stmt *S = End.asStmt())
719 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek31061982009-03-31 23:00:32 +0000721 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
722 os.str()));
723 }
724 else {
725 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek31061982009-03-31 23:00:32 +0000727 if (const Stmt *S = End.asStmt())
728 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek31061982009-03-31 23:00:32 +0000730 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
731 "Loop condition is false. Exiting loop"));
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 break;
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek31061982009-03-31 23:00:32 +0000737 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000738 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000739 if (*(Src->succ_begin()+1) == Dst) {
740 std::string sbuf;
741 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek31061982009-03-31 23:00:32 +0000743 os << "Loop condition is false. ";
744 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
745 if (const Stmt *S = End.asStmt())
746 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek31061982009-03-31 23:00:32 +0000748 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
749 os.str()));
750 }
751 else {
752 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
753 if (const Stmt *S = End.asStmt())
754 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek31061982009-03-31 23:00:32 +0000756 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000757 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 break;
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek31061982009-03-31 23:00:32 +0000763 case Stmt::IfStmtClass: {
764 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek31061982009-03-31 23:00:32 +0000766 if (const Stmt *S = End.asStmt())
767 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenek31061982009-03-31 23:00:32 +0000769 if (*(Src->succ_begin()+1) == Dst)
770 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000771 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000772 else
Ted Kremenek31061982009-03-31 23:00:32 +0000773 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000774 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek31061982009-03-31 23:00:32 +0000776 break;
777 }
778 }
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000781 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000782 // Add diagnostic pieces from custom visitors.
783 BugReport *R = PDB.getBugReport();
784 for (BugReport::visitor_iterator I = R->visitor_begin(),
785 E = R->visitor_end(); I!=E; ++I) {
786 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000787 PD.push_front(p);
788 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek9c378f72011-08-12 23:37:29 +0000791 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000792 // Scan the region bindings, and see if a "notable" symbol has a new
793 // lval binding.
794 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
795 PDB.getStateManager().iterBindings(N->getState(), SNS);
796 }
797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek14856d72009-04-06 23:06:54 +0000799 // After constructing the full PathDiagnostic, do a pass over it to compact
800 // PathDiagnosticPieces that occur within a macro.
801 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000802}
803
804//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000805// "Extensive" PathDiagnostic generation.
806//===----------------------------------------------------------------------===//
807
808static bool IsControlFlowExpr(const Stmt *S) {
809 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000811 if (!E)
812 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
814 E = E->IgnoreParenCasts();
815
John McCall56ca35d2011-02-17 10:25:35 +0000816 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000817 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000819 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
820 if (B->isLogicalOp())
821 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
823 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000824}
825
Ted Kremenek14856d72009-04-06 23:06:54 +0000826namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000827class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000828 bool IsDead;
829public:
830 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
831 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000832
833 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000834 bool isDead() const { return IsDead; }
835};
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000837class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000838 std::vector<ContextLocation> CLocs;
839 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000840 PathDiagnostic &PD;
841 PathDiagnosticBuilder &PDB;
842 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000844 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek14856d72009-04-06 23:06:54 +0000846 bool containsLocation(const PathDiagnosticLocation &Container,
847 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek14856d72009-04-06 23:06:54 +0000849 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek9650cf32009-05-11 21:42:34 +0000851 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
852 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000853 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000854 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000855 while (1) {
856 // Adjust the location for some expressions that are best referenced
857 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000858 switch (S->getStmtClass()) {
859 default:
860 break;
861 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000862 case Stmt::GenericSelectionExprClass:
863 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000864 firstCharOnly = true;
865 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000866 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000867 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000868 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000869 firstCharOnly = true;
870 continue;
871 case Stmt::ChooseExprClass:
872 S = cast<ChooseExpr>(S)->getCond();
873 firstCharOnly = true;
874 continue;
875 case Stmt::BinaryOperatorClass:
876 S = cast<BinaryOperator>(S)->getLHS();
877 firstCharOnly = true;
878 continue;
879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek9650cf32009-05-11 21:42:34 +0000884 if (S != Original)
Anna Zaks23803372011-09-20 16:23:37 +0000885 L = PathDiagnosticLocation(S, L.getManager(), PDB.getLocationContext());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek9650cf32009-05-11 21:42:34 +0000888 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000889 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000890
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000891 return L;
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000895 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000896 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000897 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000898 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000899 CLocs.pop_back();
900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremenek14856d72009-04-06 23:06:54 +0000902public:
903 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
904 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremeneka301a672009-04-22 18:16:20 +0000906 // If the PathDiagnostic already has pieces, add the enclosing statement
907 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000908 if (!PD.empty()) {
909 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek14856d72009-04-06 23:06:54 +0000911 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000912 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000913 }
914 }
915
916 ~EdgeBuilder() {
917 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000918
Ted Kremeneka301a672009-04-22 18:16:20 +0000919 // Finally, add an initial edge from the start location of the first
920 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000921 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
922 PDB.getLocationContext(),
923 PDB.getSourceManager());
924 if (L.isValid())
925 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 }
927
928 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000930 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Ted Kremenek14856d72009-04-06 23:06:54 +0000932 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000933 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000934};
Ted Kremenek14856d72009-04-06 23:06:54 +0000935} // end anonymous namespace
936
937
938PathDiagnosticLocation
939EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
940 if (const Stmt *S = L.asStmt()) {
941 if (IsControlFlowExpr(S))
942 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
944 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 }
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek14856d72009-04-06 23:06:54 +0000947 return L;
948}
949
950bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
951 const PathDiagnosticLocation &Containee) {
952
953 if (Container == Containee)
954 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 if (Container.asDecl())
957 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek14856d72009-04-06 23:06:54 +0000959 if (const Stmt *S = Containee.asStmt())
960 if (const Stmt *ContainerS = Container.asStmt()) {
961 while (S) {
962 if (S == ContainerS)
963 return true;
964 S = PDB.getParent(S);
965 }
966 return false;
967 }
968
969 // Less accurate: compare using source ranges.
970 SourceRange ContainerR = Container.asRange();
971 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000974 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
975 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
976 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
977 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chandler Carruth64211622011-07-25 21:09:52 +0000979 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
980 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
981 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
982 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 assert(ContaineeBegLine <= ContaineeEndLine);
986
Ted Kremenek14856d72009-04-06 23:06:54 +0000987 return (ContainerBegLine <= ContaineeBegLine &&
988 ContainerEndLine >= ContaineeEndLine &&
989 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000990 SM.getExpansionColumnNumber(ContainerRBeg) <=
991 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000992 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000993 SM.getExpansionColumnNumber(ContainerREnd) >=
994 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000995}
996
Ted Kremenek14856d72009-04-06 23:06:54 +0000997void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
998 if (!PrevLoc.isValid()) {
999 PrevLoc = NewLoc;
1000 return;
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001003 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1004 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001006 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001007 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001010 if (NewLocClean.asLocation().getExpansionLoc() ==
1011 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001012 return;
1013
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001014 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1015 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001016}
1017
1018void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremeneka301a672009-04-22 18:16:20 +00001020 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1021 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1024
1025 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001026 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Ted Kremenek14856d72009-04-06 23:06:54 +00001028 // Is the top location context the same as the one for the new location?
1029 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001030 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001031 if (IsConsumedExpr(TopContextLoc) &&
1032 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 TopContextLoc.markDead();
1034
Ted Kremenek14856d72009-04-06 23:06:54 +00001035 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001036 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001037
1038 return;
1039 }
1040
1041 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001042 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001045 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001046 CLocs.push_back(ContextLocation(CLoc, true));
1047 return;
1048 }
1049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Ted Kremenek14856d72009-04-06 23:06:54 +00001051 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001052 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001053 }
1054
1055 // Context does not contain the location. Flush it.
1056 popLocation();
1057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001059 // If we reach here, there is no enclosing context. Just add the edge.
1060 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001061}
1062
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001063bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1064 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1065 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001067 return false;
1068}
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Ted Kremeneke1baed32009-05-05 23:13:38 +00001070void EdgeBuilder::addExtendedContext(const Stmt *S) {
1071 if (!S)
1072 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
1074 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001075 while (Parent) {
1076 if (isa<CompoundStmt>(Parent))
1077 Parent = PDB.getParent(Parent);
1078 else
1079 break;
1080 }
1081
1082 if (Parent) {
1083 switch (Parent->getStmtClass()) {
1084 case Stmt::DoStmtClass:
1085 case Stmt::ObjCAtSynchronizedStmtClass:
1086 addContext(Parent);
1087 default:
1088 break;
1089 }
1090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremeneke1baed32009-05-05 23:13:38 +00001092 addContext(S);
1093}
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremenek14856d72009-04-06 23:06:54 +00001095void EdgeBuilder::addContext(const Stmt *S) {
1096 if (!S)
1097 return;
1098
Anna Zaks220ac8c2011-09-15 01:08:34 +00001099 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Ted Kremenek14856d72009-04-06 23:06:54 +00001101 while (!CLocs.empty()) {
1102 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1103
1104 // Is the top location context the same as the one for the new location?
1105 if (TopContextLoc == L)
1106 return;
1107
1108 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001109 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001110 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001111 }
1112
1113 // Context does not contain the location. Flush it.
1114 popLocation();
1115 }
1116
1117 CLocs.push_back(L);
1118}
1119
1120static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1121 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001122 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001123 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001124 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001125
Ted Kremenek9c378f72011-08-12 23:37:29 +00001126 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001127 while (NextNode) {
1128 N = NextNode;
1129 NextNode = GetPredecessorNode(N);
1130 ProgramPoint P = N->getLocation();
1131
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001132 do {
1133 // Block edges.
1134 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1135 const CFGBlock &Blk = *BE->getSrc();
1136 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001138 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001139 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001140 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001141 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001143 if (!Term) {
1144 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1145 CS = dyn_cast<CompoundStmt>(FS->getBody());
1146 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001147 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001150 PathDiagnosticEventPiece *p =
1151 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001152 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001154 EB.addEdge(p->getLocation(), true);
1155 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001157 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001158 PathDiagnosticLocation BL =
1159 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001160 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001164 if (Term)
1165 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001167 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001168 }
1169
Mike Stump1eb44332009-09-09 15:08:12 +00001170 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001171 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1172 const Stmt *stmt = S->getStmt();
1173 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001174 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001175 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001176 }
1177 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001178 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001179 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001180
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001181 break;
1182 }
1183 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001185 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001186 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Anna Zaks8e6431a2011-08-18 22:37:56 +00001188 // Add pieces from custom visitors.
1189 BugReport *R = PDB.getBugReport();
1190 for (BugReport::visitor_iterator I = R->visitor_begin(),
1191 E = R->visitor_end(); I!=E; ++I) {
1192 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001193 const PathDiagnosticLocation &Loc = p->getLocation();
1194 EB.addEdge(Loc, true);
1195 PD.push_front(p);
1196 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001197 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001200 }
1201}
1202
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001203//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001204// Methods for BugType and subclasses.
1205//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001206BugType::~BugType() { }
1207
Ted Kremenekcf118d42009-02-04 23:49:09 +00001208void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001209
David Blaikie99ba9e32011-12-20 02:48:34 +00001210void BuiltinBug::anchor() {}
1211
Ted Kremenekcf118d42009-02-04 23:49:09 +00001212//===----------------------------------------------------------------------===//
1213// Methods for BugReport and subclasses.
1214//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001215
David Blaikie99ba9e32011-12-20 02:48:34 +00001216void BugReport::NodeResolver::anchor() {}
1217
Anna Zaks8e6431a2011-08-18 22:37:56 +00001218void BugReport::addVisitor(BugReporterVisitor* visitor) {
1219 if (!visitor)
1220 return;
1221
1222 llvm::FoldingSetNodeID ID;
1223 visitor->Profile(ID);
1224 void *InsertPos;
1225
1226 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1227 delete visitor;
1228 return;
1229 }
1230
1231 CallbacksSet.InsertNode(visitor, InsertPos);
1232 Callbacks = F.add(visitor, Callbacks);
1233}
1234
1235BugReport::~BugReport() {
1236 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001237 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001238 }
1239}
Anna Zakse172e8b2011-08-17 23:00:25 +00001240
1241void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1242 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001243 hash.AddString(Description);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001244 if (Location.isValid()) {
1245 Location.Profile(hash);
1246 } else {
1247 assert(ErrorNode);
1248 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1249 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001250
1251 for (SmallVectorImpl<SourceRange>::const_iterator I =
1252 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1253 const SourceRange range = *I;
1254 if (!range.isValid())
1255 continue;
1256 hash.AddInteger(range.getBegin().getRawEncoding());
1257 hash.AddInteger(range.getEnd().getRawEncoding());
1258 }
1259}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001260
Ted Kremenek9c378f72011-08-12 23:37:29 +00001261const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001262 if (!ErrorNode)
1263 return 0;
1264
Tom Care212f6d32010-09-16 03:50:38 +00001265 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001266 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek9c378f72011-08-12 23:37:29 +00001268 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001269 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001270 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001271 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001272 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001273 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001274 S = GetStmt(ProgP);
1275
1276 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001277}
1278
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001279std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001280BugReport::getRanges() {
1281 // If no custom ranges, add the range of the statement corresponding to
1282 // the error node.
1283 if (Ranges.empty()) {
1284 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1285 addRange(E->getSourceRange());
1286 else
1287 return std::make_pair(ranges_iterator(), ranges_iterator());
1288 }
1289
Anna Zaks14924262011-08-24 20:31:06 +00001290 // User-specified absence of range info.
1291 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1292 return std::make_pair(ranges_iterator(), ranges_iterator());
1293
Anna Zakse172e8b2011-08-17 23:00:25 +00001294 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001295}
1296
Anna Zaks590dd8e2011-09-20 21:38:35 +00001297PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001298 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001299 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001300 "Either Location or ErrorNode should be specified but not both.");
1301
Ted Kremenek9c378f72011-08-12 23:37:29 +00001302 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001303 const LocationContext *LC = ErrorNode->getLocationContext();
1304
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001305 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001306 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001307 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001308 // For binary operators, return the location of the operator.
1309 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001310 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001311
Anna Zaks590dd8e2011-09-20 21:38:35 +00001312 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001313 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001314 } else {
1315 assert(Location.isValid());
1316 return Location;
1317 }
1318
Anna Zaks590dd8e2011-09-20 21:38:35 +00001319 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001320}
1321
Ted Kremenekcf118d42009-02-04 23:49:09 +00001322//===----------------------------------------------------------------------===//
1323// Methods for BugReporter and subclasses.
1324//===----------------------------------------------------------------------===//
1325
1326BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001327 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001328}
1329
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001330GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001331BugReporterData::~BugReporterData() {}
1332
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001333ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001334
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001335ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001336GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1337
Anna Zaks3b030a22011-08-19 01:57:09 +00001338BugReporter::~BugReporter() {
1339 FlushReports();
1340
1341 // Free the bug reports we are tracking.
1342 typedef std::vector<BugReportEquivClass *> ContTy;
1343 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1344 I != E; ++I) {
1345 delete *I;
1346 }
1347}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001348
1349void BugReporter::FlushReports() {
1350 if (BugTypes.isEmpty())
1351 return;
1352
1353 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001354 // warnings and new BugTypes.
1355 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1356 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001357 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001358 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001359 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001360 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001361 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001362 const_cast<BugType*>(*I)->FlushReports(*this);
1363
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001364 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1365 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1366 BugReportEquivClass& EQ = *EI;
1367 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001368 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001369
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001370 // BugReporter owns and deletes only BugTypes created implicitly through
1371 // EmitBasicReport.
1372 // FIXME: There are leaks from checkers that assume that the BugTypes they
1373 // create will be destroyed by the BugReporter.
1374 for (llvm::StringMap<BugType*>::iterator
1375 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1376 delete I->second;
1377
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001379 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001380}
1381
1382//===----------------------------------------------------------------------===//
1383// PathDiagnostics generation.
1384//===----------------------------------------------------------------------===//
1385
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001386static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001387 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001388MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001389 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenekcf118d42009-02-04 23:49:09 +00001391 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001392 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001393 // error node unless there are two or more error nodes with the same minimum
1394 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001395 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001396 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001397
1398 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001399 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1400 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402 // Create owning pointers for GTrim and NMap just to ensure that they are
1403 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001404 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001405 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Ted Kremenekcf118d42009-02-04 23:49:09 +00001407 // Find the (first) error node in the trimmed graph. We just need to consult
1408 // the node map (NMap) which maps from nodes in the original graph to nodes
1409 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001410
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001411 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001412 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001413 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001414
Ted Kremenek40406fe2010-12-03 06:52:30 +00001415 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1416 const ExplodedNode *originalNode = nodes[nodeIndex];
1417 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001418 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001419 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001420 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Ted Kremenek938332c2009-05-16 01:11:58 +00001423 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001424
1425 // Create a new (third!) graph with a single path. This is the graph
1426 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001427 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek10aa5542009-03-12 23:41:59 +00001429 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001430 // to the root node, and then construct a new graph that contains only
1431 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001432 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001434 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001435 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001437 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001438 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001439 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001441 if (Visited.find(Node) != Visited.end())
1442 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001444 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 if (Node->pred_empty()) {
1447 Root = Node;
1448 break;
1449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001451 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001452 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001453 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenek938332c2009-05-16 01:11:58 +00001456 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Ted Kremenek10aa5542009-03-12 23:41:59 +00001458 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001460 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001461 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001462 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001464 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001466 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001467 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 // Create the equivalent node in the new graph with the same state
1470 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001471 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001473 // Store the mapping to the original node.
1474 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1475 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001476 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001478 // Link up the new node with the previous node.
1479 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001480 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001482 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001485 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001486 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001487 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001488 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001489 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001490 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 // Find the next successor node. We choose the node that is marked
1494 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001495 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1496 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001497 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001499 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001501 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 if (I == Visited.end())
1504 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 if (!N || I->second < MinVal) {
1507 N = *SI;
1508 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001509 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek938332c2009-05-16 01:11:58 +00001512 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek938332c2009-05-16 01:11:58 +00001515 assert(First);
1516
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001517 return std::make_pair(std::make_pair(GNew, BM),
1518 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001519}
1520
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001521/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1522/// and collapses PathDiagosticPieces that are expanded by macros.
1523static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1524 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1525 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001527 typedef std::vector<PathDiagnosticPiece*>
1528 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 MacroStackTy MacroStack;
1531 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001533 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1534 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001535 const FullSourceLoc Loc = I->getLocation().asLocation();
1536
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001537 // Determine the instantiation location, which is the location we group
1538 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001539 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001540 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001541 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001543 if (Loc.isFileID()) {
1544 MacroStack.clear();
1545 Pieces.push_back(&*I);
1546 continue;
1547 }
1548
1549 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001551 // Is the PathDiagnosticPiece within the same macro group?
1552 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1553 MacroStack.back().first->push_back(&*I);
1554 continue;
1555 }
1556
1557 // We aren't in the same group. Are we descending into a new macro
1558 // or are part of an old one?
1559 PathDiagnosticMacroPiece *MacroGroup = 0;
1560
1561 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001562 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001563 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565 // Walk the entire macro stack.
1566 while (!MacroStack.empty()) {
1567 if (InstantiationLoc == MacroStack.back().second) {
1568 MacroGroup = MacroStack.back().first;
1569 break;
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001572 if (ParentInstantiationLoc == MacroStack.back().second) {
1573 MacroGroup = MacroStack.back().first;
1574 break;
1575 }
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001577 MacroStack.pop_back();
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001580 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1581 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001582 PathDiagnosticMacroPiece *NewGroup =
1583 new PathDiagnosticMacroPiece(
1584 PathDiagnosticLocation::createSingleLocation(I->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001585
1586 if (MacroGroup)
1587 MacroGroup->push_back(NewGroup);
1588 else {
1589 assert(InstantiationLoc.isFileID());
1590 Pieces.push_back(NewGroup);
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001593 MacroGroup = NewGroup;
1594 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1595 }
1596
1597 // Finally, add the PathDiagnosticPiece to the group.
1598 MacroGroup->push_back(&*I);
1599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001601 // Now take the pieces and construct a new PathDiagnostic.
1602 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001604 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1605 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1606 if (!MP->containsEvent()) {
1607 delete MP;
1608 continue;
1609 }
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001611 PD.push_back(*I);
1612 }
1613}
1614
Ted Kremenek7dc86642009-03-31 20:22:36 +00001615void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001616 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek40406fe2010-12-03 06:52:30 +00001618 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001619 SmallVector<const ExplodedNode *, 10> errorNodes;
1620 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001621 E = bugReports.end(); I != E; ++I) {
1622 errorNodes.push_back((*I)->getErrorNode());
1623 }
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001625 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001626 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001627 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001628 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001629 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenekcf118d42009-02-04 23:49:09 +00001631 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001632 assert(GPair.second.second < bugReports.size());
1633 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001634 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001636 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001637 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001638 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001639
1640 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001641 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1642 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Anna Zaks8e6431a2011-08-18 22:37:56 +00001644 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001645 R->addVisitor(new NilReceiverBRVisitor());
1646 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Anna Zaks23f395e2011-08-20 01:27:22 +00001648 // Generate the very last diagnostic piece - the piece is visible before
1649 // the trace is expanded.
1650 PathDiagnosticPiece *LastPiece = 0;
1651 for (BugReport::visitor_iterator I = R->visitor_begin(),
1652 E = R->visitor_end(); I!=E; ++I) {
1653 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1654 assert (!LastPiece &&
1655 "There can only be one final piece in a diagnostic.");
1656 LastPiece = Piece;
1657 }
1658 }
1659 if (!LastPiece)
1660 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1661 if (LastPiece)
1662 PD.push_back(LastPiece);
1663 else
1664 return;
1665
Ted Kremenek7dc86642009-03-31 20:22:36 +00001666 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001667 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001668 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001669 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001670 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001671 GenerateMinimalPathDiagnostic(PD, PDB, N);
1672 break;
1673 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001674}
1675
Ted Kremenekcf118d42009-02-04 23:49:09 +00001676void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001677 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001678}
1679
Mike Stump1eb44332009-09-09 15:08:12 +00001680void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001681 // Compute the bug report's hash to determine its equivalence class.
1682 llvm::FoldingSetNodeID ID;
1683 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001684
1685 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001686 BugType& BT = R->getBugType();
1687 Register(&BT);
1688 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001689 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Ted Kremenekcf118d42009-02-04 23:49:09 +00001691 if (!EQ) {
1692 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001693 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001694 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001695 }
1696 else
1697 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001698}
1699
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001700
1701//===----------------------------------------------------------------------===//
1702// Emitting reports in equivalence classes.
1703//===----------------------------------------------------------------------===//
1704
1705namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001706struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001707 const ExplodedNode *N;
1708 ExplodedNode::const_succ_iterator I, E;
1709
1710 FRIEC_WLItem(const ExplodedNode *n)
1711 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1712};
1713}
1714
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001715static BugReport *
1716FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001717 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001718
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001719 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1720 assert(I != E);
1721 BugReport *R = *I;
1722 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001723
Ted Kremenek40406fe2010-12-03 06:52:30 +00001724 // If we don't need to suppress any of the nodes because they are
1725 // post-dominated by a sink, simply add all the nodes in the equivalence class
1726 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001727 if (!BT.isSuppressOnSink()) {
1728 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001729 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001730 if (N) {
1731 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001732 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001733 }
1734 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001735 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001736 }
1737
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001738 // For bug reports that should be suppressed when all paths are post-dominated
1739 // by a sink node, iterate through the reports in the equivalence class
1740 // until we find one that isn't post-dominated (if one exists). We use a
1741 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1742 // this as a recursive function, but we don't want to risk blowing out the
1743 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001744 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001745
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001746 for (; I != E; ++I) {
1747 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001748 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001749
Ted Kremenek40406fe2010-12-03 06:52:30 +00001750 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001751 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001752 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001753 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001754 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001755 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001756 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001757 if (errorNode->succ_empty()) {
1758 bugReports.push_back(R);
1759 if (!exampleReport)
1760 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001761 continue;
1762 }
1763
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001764 // At this point we know that 'N' is not a sink and it has at least one
1765 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1766 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001767 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001768 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1769
1770 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001771 WL.push_back(errorNode);
1772 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001773
1774 while (!WL.empty()) {
1775 WLItem &WI = WL.back();
1776 assert(!WI.N->succ_empty());
1777
1778 for (; WI.I != WI.E; ++WI.I) {
1779 const ExplodedNode *Succ = *WI.I;
1780 // End-of-path node?
1781 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001782 // If we found an end-of-path node that is not a sink.
1783 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001784 bugReports.push_back(R);
1785 if (!exampleReport)
1786 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001787 WL.clear();
1788 break;
1789 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001790 // Found a sink? Continue on to the next successor.
1791 continue;
1792 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001793 // Mark the successor as visited. If it hasn't been explored,
1794 // enqueue it to the DFS worklist.
1795 unsigned &mark = Visited[Succ];
1796 if (!mark) {
1797 mark = 1;
1798 WL.push_back(Succ);
1799 break;
1800 }
1801 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001802
1803 // The worklist may have been cleared at this point. First
1804 // check if it is empty before checking the last item.
1805 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001806 WL.pop_back();
1807 }
1808 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001809
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001810 // ExampleReport will be NULL if all the nodes in the equivalence class
1811 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001812 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001813}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001814
1815//===----------------------------------------------------------------------===//
1816// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1817// uses global state, which eventually should go elsewhere.
1818//===----------------------------------------------------------------------===//
1819namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001820class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001821 llvm::FoldingSetNodeID ID;
1822public:
1823 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001824 R->Profile(ID);
1825 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001826 }
1827
1828 void Profile(llvm::FoldingSetNodeID &id) {
1829 id = ID;
1830 }
1831
1832 llvm::FoldingSetNodeID &getID() { return ID; }
1833};
1834}
1835
1836static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1837 // FIXME: Eventually this diagnostic cache should reside in something
1838 // like AnalysisManager instead of being a static variable. This is
1839 // really unsafe in the long term.
1840 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1841 static DiagnosticCache DC;
1842
1843 void *InsertPos;
1844 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1845
1846 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1847 delete Item;
1848 return true;
1849 }
1850
1851 DC.InsertNode(Item, InsertPos);
1852 return false;
1853}
1854
Ted Kremenekcf118d42009-02-04 23:49:09 +00001855void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001856 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001857 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1858 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001859 return;
1860
David Blaikieef3643f2011-09-26 00:51:36 +00001861 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Ted Kremenekcf118d42009-02-04 23:49:09 +00001863 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001864 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001865 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Ted Kremenekd49967f2009-04-29 21:58:13 +00001867 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001868 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001869 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001870 ? exampleReport->getDescription()
1871 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001872 BT.getCategory()));
1873
Ted Kremenek40406fe2010-12-03 06:52:30 +00001874 if (!bugReports.empty())
1875 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Ted Kremenek40406fe2010-12-03 06:52:30 +00001877 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001878 return;
1879
Ted Kremenek072192b2008-04-30 23:47:44 +00001880 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001881 const BugReport::ExtraTextList &Meta =
1882 exampleReport->getExtraText();
1883 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1884 e = Meta.end(); i != e; ++i) {
1885 D->addMeta(*i);
1886 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001887
Ted Kremenek3148eb42009-01-24 00:55:43 +00001888 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001889 BugReport::ranges_iterator Beg, End;
1890 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001891 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001892
1893 // Search the description for '%', as that will be interpretted as a
1894 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001895 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001896 unsigned ErrorDiag;
1897 {
1898 llvm::SmallString<512> TmpStr;
1899 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001900 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001901 if (*I == '%')
1902 Out << "%%";
1903 else
1904 Out << *I;
1905
1906 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001907 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001908 }
Ted Kremenek57202072008-07-14 17:40:50 +00001909
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001910 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001911 DiagnosticBuilder diagBuilder = Diag.Report(
1912 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001913 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001914 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001915 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001916
David Blaikieef3643f2011-09-26 00:51:36 +00001917 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001918 if (!PD)
1919 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
1921 if (D->empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001922 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1923 exampleReport->getLocation(getSourceManager()),
1924 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001925
Ted Kremenek3148eb42009-01-24 00:55:43 +00001926 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1927 D->push_back(piece);
1928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Ted Kremenek3148eb42009-01-24 00:55:43 +00001930 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001931}
Ted Kremenek57202072008-07-14 17:40:50 +00001932
Chris Lattner5f9e2722011-07-23 10:55:15 +00001933void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001934 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001935 SourceRange* RBeg, unsigned NumRanges) {
1936 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1937}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001938
Chris Lattner5f9e2722011-07-23 10:55:15 +00001939void BugReporter::EmitBasicReport(StringRef name,
1940 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001941 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001942 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001944 // 'BT' is owned by BugReporter.
1945 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001946 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001947 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1948 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001949}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001950
Chris Lattner5f9e2722011-07-23 10:55:15 +00001951BugType *BugReporter::getBugTypeForName(StringRef name,
1952 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001953 llvm::SmallString<136> fullDesc;
1954 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1955 llvm::StringMapEntry<BugType *> &
1956 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1957 BugType *BT = entry.getValue();
1958 if (!BT) {
1959 BT = new BugType(name, category);
1960 entry.setValue(BT);
1961 }
1962 return BT;
1963}