blob: 53fedaf2a12d1a7b542401814446ef2f8a55c1f9 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenekb697b102009-02-23 22:44:26 +000052 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000053}
54
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000057 return N->pred_empty() ? NULL : *(N->pred_begin());
58}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059
Zhongxing Xuc5619d92009-08-06 01:32:16 +000060static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000061GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000062 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063}
64
Ted Kremenek9c378f72011-08-12 23:37:29 +000065static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000067 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekb697b102009-02-23 22:44:26 +000070 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000071}
72
Ted Kremenek9c378f72011-08-12 23:37:29 +000073static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000074 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000075 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 // Check if the statement is '?' or '&&'/'||'. These are "merges",
77 // not actual statement points.
78 switch (S->getStmtClass()) {
79 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000080 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000081 case Stmt::ConditionalOperatorClass: continue;
82 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000083 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
84 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 continue;
86 break;
87 }
88 default:
89 break;
90 }
Ted Kremenekb697b102009-02-23 22:44:26 +000091 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 return 0;
95}
96
Ted Kremenek5f85e172009-07-22 22:35:28 +000097static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000098GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000099 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekb697b102009-02-23 22:44:26 +0000102 return GetPreviousStmt(N);
103}
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000106GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000107 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return GetNextStmt(N);
111}
112
113//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000114// Diagnostic cleanup.
115//===----------------------------------------------------------------------===//
116
117/// Recursively scan through a path and prune out calls and macros pieces
118/// that aren't needed. Return true if afterwards the path contains
119/// "interesting stuff" which means it should be pruned from the parent path.
120static bool RemoveUneededCalls(PathPieces &pieces) {
121 bool containsSomethingInteresting = false;
122 const unsigned N = pieces.size();
123
124 for (unsigned i = 0 ; i < N ; ++i) {
125 // Remove the front piece from the path. If it is still something we
126 // want to keep once we are done, we will push it back on the end.
127 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
128 pieces.pop_front();
129
Ted Kremenek72516742012-03-01 00:05:06 +0000130 switch (piece->getKind()) {
131 case PathDiagnosticPiece::Call: {
132 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
133 // Recursively clean out the subclass. Keep this call around if
134 // it contains any informative diagnostics.
135 if (!RemoveUneededCalls(call->path))
136 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000137 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000138 break;
139 }
140 case PathDiagnosticPiece::Macro: {
141 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
142 if (!RemoveUneededCalls(macro->subPieces))
143 continue;
144 containsSomethingInteresting = true;
145 break;
146 }
147 case PathDiagnosticPiece::Event: {
148 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
149 // We never throw away an event, but we do throw it away wholesale
150 // as part of a path if we throw the entire path away.
151 if (!event->isPrunable())
152 containsSomethingInteresting = true;
153 break;
154 }
155 case PathDiagnosticPiece::ControlFlow:
156 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000157 }
158
159 pieces.push_back(piece);
160 }
161
162 return containsSomethingInteresting;
163}
164
165//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000166// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000167//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000168
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000169typedef llvm::DenseMap<const ExplodedNode*,
170const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000171
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000172namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000173class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000174 NodeBackMap& M;
175public:
176 NodeMapClosure(NodeBackMap *m) : M(*m) {}
177 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek9c378f72011-08-12 23:37:29 +0000179 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000180 NodeBackMap::iterator I = M.find(N);
181 return I == M.end() ? 0 : I->second;
182 }
183};
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000185class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000186 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000187 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000188 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000189 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000190public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000191 const LocationContext *LC;
192
Ted Kremenek8966bc12009-05-06 21:39:49 +0000193 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000194 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000195 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000196 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000197 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
198 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek9c378f72011-08-12 23:37:29 +0000200 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek9c378f72011-08-12 23:37:29 +0000202 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
203 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Anna Zaks8e6431a2011-08-18 22:37:56 +0000205 BugReport *getBugReport() { return R; }
206
Tom Care212f6d32010-09-16 03:50:38 +0000207 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000208
209 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000211 const Stmt *getParent(const Stmt *S) {
212 return getParentMap().getParent(S);
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek8966bc12009-05-06 21:39:49 +0000215 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000216
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000217 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
David Blaikieef3643f2011-09-26 00:51:36 +0000219 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
220 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000221 }
222
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000223 bool supportsLogicalOpControlFlow() const {
224 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000225 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000226};
227} // end anonymous namespace
228
Ted Kremenek00605e02009-03-27 20:55:39 +0000229PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000230PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000231 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000232 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000233
Anna Zaks0cd59482011-09-16 19:18:30 +0000234 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
235 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000236}
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek00605e02009-03-27 20:55:39 +0000238PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000239PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
240 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000241
Ted Kremenek143ca222008-05-06 18:11:09 +0000242 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000243 if (os.str().empty())
244 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenek00605e02009-03-27 20:55:39 +0000246 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek00605e02009-03-27 20:55:39 +0000248 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000249 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000250 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000251 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000252 else {
253 os << "Execution jumps to the end of the ";
254 const Decl *D = N->getLocationContext()->getDecl();
255 if (isa<ObjCMethodDecl>(D))
256 os << "method";
257 else if (isa<FunctionDecl>(D))
258 os << "function";
259 else {
260 assert(isa<BlockDecl>(D));
261 os << "anonymous block";
262 }
263 os << '.';
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000266 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000267}
268
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000269static bool IsNested(const Stmt *S, ParentMap &PM) {
270 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
271 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000273 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000275 if (Parent)
276 switch (Parent->getStmtClass()) {
277 case Stmt::ForStmtClass:
278 case Stmt::DoStmtClass:
279 case Stmt::WhileStmtClass:
280 return true;
281 default:
282 break;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
285 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000286}
287
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000288PathDiagnosticLocation
289PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000290 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000291 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000292 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000293
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000294 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000295 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000297 if (!Parent)
298 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000300 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000301 case Stmt::BinaryOperatorClass: {
302 const BinaryOperator *B = cast<BinaryOperator>(Parent);
303 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000304 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000305 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000306 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000307 case Stmt::CompoundStmtClass:
308 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000309 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000310 case Stmt::ChooseExprClass:
311 // Similar to '?' if we are referring to condition, just have the edge
312 // point to the entire choose expression.
313 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000314 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000315 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000316 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000317 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000318 case Stmt::ConditionalOperatorClass:
319 // For '?', if we are referring to condition, just have the edge point
320 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000321 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000322 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000323 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000324 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000325 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000326 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000327 case Stmt::ForStmtClass:
328 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000329 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000330 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000331 case Stmt::IfStmtClass:
332 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000333 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000334 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000335 case Stmt::ObjCForCollectionStmtClass:
336 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000337 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000338 break;
339 case Stmt::WhileStmtClass:
340 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000341 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000342 break;
343 default:
344 break;
345 }
346
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000347 S = Parent;
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000350 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000351
352 // Special case: DeclStmts can appear in for statement declarations, in which
353 // case the ForStmt is the context.
354 if (isa<DeclStmt>(S)) {
355 if (const Stmt *Parent = P.getParent(S)) {
356 switch (Parent->getStmtClass()) {
357 case Stmt::ForStmtClass:
358 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000359 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000360 default:
361 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000362 }
363 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000364 }
365 else if (isa<BinaryOperator>(S)) {
366 // Special case: the binary operator represents the initialization
367 // code in a for statement (this can happen when the variable being
368 // initialized is an old variable.
369 if (const ForStmt *FS =
370 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
371 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000372 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000373 }
374 }
375
Anna Zaks220ac8c2011-09-15 01:08:34 +0000376 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000377}
378
Ted Kremenekcf118d42009-02-04 23:49:09 +0000379//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000380// ScanNotableSymbols: closure-like callback for scanning Store bindings.
381//===----------------------------------------------------------------------===//
382
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000383static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
384 ProgramStateManager& VMgr,
385 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek31061982009-03-31 23:00:32 +0000387 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek31061982009-03-31 23:00:32 +0000389 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek31061982009-03-31 23:00:32 +0000391 if (!isa<PostStmt>(P))
392 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek9c378f72011-08-12 23:37:29 +0000394 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek31061982009-03-31 23:00:32 +0000396 if (!DR)
397 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek5eca4822012-01-06 22:09:28 +0000399 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek31061982009-03-31 23:00:32 +0000401 if (X != Y)
402 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremenek9c378f72011-08-12 23:37:29 +0000404 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 if (!VD)
407 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 return VD;
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 return 0;
413}
414
415namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000416class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000417: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000420 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000421 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000422 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000423 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000424 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000425 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427public:
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000429 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000430 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000431 const Stmt *s,
432 ProgramStateManager& vmgr,
433 const ExplodedNode *pred,
434 PathDiagnostic& pd,
435 BugReporter& br)
436 : Sym(sym),
437 PrevSt(prevst),
438 S(s),
439 VMgr(vmgr),
440 Pred(pred),
441 PD(pd),
442 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek31061982009-03-31 23:00:32 +0000444 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
445 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek31061982009-03-31 23:00:32 +0000449 if (ScanSym != Sym)
450 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
452 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000453 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek31061982009-03-31 23:00:32 +0000455 if (X == V) // Same binding?
456 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek31061982009-03-31 23:00:32 +0000458 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000459 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000460 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek31061982009-03-31 23:00:32 +0000462 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
465 if (!B->isAssignmentOp())
466 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek31061982009-03-31 23:00:32 +0000468 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000469 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek31061982009-03-31 23:00:32 +0000471 if (!DR)
472 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 VD = dyn_cast<VarDecl>(DR->getDecl());
475 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000476 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000477 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
478 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000479 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000480 VD = dyn_cast<VarDecl>(*DS->decl_begin());
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek31061982009-03-31 23:00:32 +0000483 if (!VD)
484 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000487 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 if (!MostRecent)
490 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek31061982009-03-31 23:00:32 +0000492 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000493 if (Loc::isLocType(VD->getType())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000494 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000495 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000496 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000497 PathDiagnosticLocation L =
498 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
499 Pred->getLocationContext());
Ted Kremenek2042fc12012-02-24 06:00:00 +0000500 PD.getActivePath().push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek31061982009-03-31 23:00:32 +0000503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000504 }
Ted Kremenek31061982009-03-31 23:00:32 +0000505};
506}
507
Ted Kremenek9c378f72011-08-12 23:37:29 +0000508static void HandleNotableSymbol(const ExplodedNode *N,
509 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000510 SymbolRef Sym, BugReporter& BR,
511 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenek9c378f72011-08-12 23:37:29 +0000513 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000514 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek31061982009-03-31 23:00:32 +0000516 if (!PrevSt)
517 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Ted Kremenek31061982009-03-31 23:00:32 +0000519 // Look at the region bindings of the current state that map to the
520 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000521 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000522 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
523 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
524}
525
526namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000527class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000528: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000531 const ExplodedNode *N;
532 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000533 GRBugReporter& BR;
534 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000537 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000538 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000539 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek31061982009-03-31 23:00:32 +0000541 bool HandleBinding(StoreManager& SMgr, Store store,
542 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek31061982009-03-31 23:00:32 +0000546 if (!ScanSym)
547 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenek31061982009-03-31 23:00:32 +0000549 if (!BR.isNotable(ScanSym))
550 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 if (AlreadyProcessed.count(ScanSym))
553 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Ted Kremenek31061982009-03-31 23:00:32 +0000557 HandleNotableSymbol(N, S, ScanSym, BR, PD);
558 return true;
559 }
560};
561} // end anonymous namespace
562
563//===----------------------------------------------------------------------===//
564// "Minimal" path diagnostic generation algorithm.
565//===----------------------------------------------------------------------===//
566
Ted Kremenek77d09442012-03-02 01:27:31 +0000567static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000568
Ted Kremenek31061982009-03-31 23:00:32 +0000569static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
570 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000571 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000572
Ted Kremenek31061982009-03-31 23:00:32 +0000573 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000574 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000575 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000576 ? NULL : *(N->pred_begin());
577 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000578 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000579 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000580 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Ted Kremenek31061982009-03-31 23:00:32 +0000582 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000583
584 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
585 PathDiagnosticCallPiece *C =
586 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
587 PD.getActivePath().push_front(C);
588 PD.pushActivePath(&C->path);
589 continue;
590 }
591
592 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
593 PD.popActivePath();
594 // The current active path should never be empty. Either we
595 // just added a bunch of stuff to the top-level path, or
596 // we have a previous CallExit. If the front of the active
597 // path is not a PathDiagnosticCallPiece, it means that the
598 // path terminated within a function call. We must then take the
599 // current contents of the active path and place it within
600 // a new PathDiagnosticCallPiece.
601 assert(!PD.getActivePath().empty());
602 PathDiagnosticCallPiece *C =
603 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
604 if (!C)
605 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
606 C->setCallee(*CE, SMgr);
607 continue;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Ted Kremenek9c378f72011-08-12 23:37:29 +0000610 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
611 const CFGBlock *Src = BE->getSrc();
612 const CFGBlock *Dst = BE->getDst();
613 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenek31061982009-03-31 23:00:32 +0000615 if (!T)
616 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anna Zaks590dd8e2011-09-20 21:38:35 +0000618 PathDiagnosticLocation Start =
619 PathDiagnosticLocation::createBegin(T, SMgr,
620 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek31061982009-03-31 23:00:32 +0000622 switch (T->getStmtClass()) {
623 default:
624 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000627 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000628 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek31061982009-03-31 23:00:32 +0000630 if (!S)
631 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek31061982009-03-31 23:00:32 +0000633 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000634 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000635 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek31061982009-03-31 23:00:32 +0000637 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000638 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000639 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000640 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000641 break;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
644 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000645 // Figure out what case arm we took.
646 std::string sbuf;
647 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek9c378f72011-08-12 23:37:29 +0000649 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000650 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek31061982009-03-31 23:00:32 +0000652 switch (S->getStmtClass()) {
653 default:
654 os << "No cases match in the switch statement. "
655 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000656 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000657 break;
658 case Stmt::DefaultStmtClass:
659 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000660 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000661 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000664 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000665 const CaseStmt *Case = cast<CaseStmt>(S);
666 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
668 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000669 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek9c378f72011-08-12 23:37:29 +0000671 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000672 // FIXME: Maybe this should be an assertion. Are there cases
673 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000674 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000675 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Ted Kremenek31061982009-03-31 23:00:32 +0000677 if (D) {
678 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000679 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000680 }
681 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000682
683 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000684 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000685
Ted Kremenek31061982009-03-31 23:00:32 +0000686 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000687 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000688 break;
689 }
690 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000691 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000692 os.str()));
693 }
694 else {
695 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000696 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000697 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000698 os.str()));
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek31061982009-03-31 23:00:32 +0000701 break;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek31061982009-03-31 23:00:32 +0000704 case Stmt::BreakStmtClass:
705 case Stmt::ContinueStmtClass: {
706 std::string sbuf;
707 llvm::raw_string_ostream os(sbuf);
708 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000709 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000710 os.str()));
711 break;
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000715 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000716 case Stmt::ConditionalOperatorClass: {
717 std::string sbuf;
718 llvm::raw_string_ostream os(sbuf);
719 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek31061982009-03-31 23:00:32 +0000721 if (*(Src->succ_begin()+1) == Dst)
722 os << "false";
723 else
724 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek31061982009-03-31 23:00:32 +0000728 if (const Stmt *S = End.asStmt())
729 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek2042fc12012-02-24 06:00:00 +0000731 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000732 os.str()));
733 break;
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek31061982009-03-31 23:00:32 +0000736 // Determine control-flow for short-circuited '&&' and '||'.
737 case Stmt::BinaryOperatorClass: {
738 if (!PDB.supportsLogicalOpControlFlow())
739 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000741 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000742 std::string sbuf;
743 llvm::raw_string_ostream os(sbuf);
744 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000745
John McCall2de56d12010-08-25 11:45:40 +0000746 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000747 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 if (*(Src->succ_begin()+1) == Dst) {
750 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000751 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000752 PathDiagnosticLocation Start =
753 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000754 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000755 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000756 }
Ted Kremenek31061982009-03-31 23:00:32 +0000757 else {
758 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000759 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000760 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000761 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000762 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000763 }
Ted Kremenek31061982009-03-31 23:00:32 +0000764 }
765 else {
John McCall2de56d12010-08-25 11:45:40 +0000766 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000767 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenek31061982009-03-31 23:00:32 +0000769 if (*(Src->succ_begin()+1) == Dst) {
770 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000771 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000772 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000773 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000774 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000775 }
776 else {
777 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000778 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000779 PathDiagnosticLocation Start =
780 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000781 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000782 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000783 }
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Ted Kremenek31061982009-03-31 23:00:32 +0000786 break;
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
789 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000790 if (*(Src->succ_begin()) == Dst) {
791 std::string sbuf;
792 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek31061982009-03-31 23:00:32 +0000794 os << "Loop condition is true. ";
795 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek31061982009-03-31 23:00:32 +0000797 if (const Stmt *S = End.asStmt())
798 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek2042fc12012-02-24 06:00:00 +0000800 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000801 os.str()));
802 }
803 else {
804 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek31061982009-03-31 23:00:32 +0000806 if (const Stmt *S = End.asStmt())
807 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek2042fc12012-02-24 06:00:00 +0000809 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000810 "Loop condition is false. Exiting loop"));
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek31061982009-03-31 23:00:32 +0000813 break;
814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek31061982009-03-31 23:00:32 +0000816 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000817 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000818 if (*(Src->succ_begin()+1) == Dst) {
819 std::string sbuf;
820 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek31061982009-03-31 23:00:32 +0000822 os << "Loop condition is false. ";
823 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
824 if (const Stmt *S = End.asStmt())
825 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek2042fc12012-02-24 06:00:00 +0000827 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000828 os.str()));
829 }
830 else {
831 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
832 if (const Stmt *S = End.asStmt())
833 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek2042fc12012-02-24 06:00:00 +0000835 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000836 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek31061982009-03-31 23:00:32 +0000839 break;
840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek31061982009-03-31 23:00:32 +0000842 case Stmt::IfStmtClass: {
843 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek31061982009-03-31 23:00:32 +0000845 if (const Stmt *S = End.asStmt())
846 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek31061982009-03-31 23:00:32 +0000848 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000849 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000850 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000851 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000852 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000853 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek31061982009-03-31 23:00:32 +0000855 break;
856 }
857 }
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000860 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000861 // Add diagnostic pieces from custom visitors.
862 BugReport *R = PDB.getBugReport();
863 for (BugReport::visitor_iterator I = R->visitor_begin(),
864 E = R->visitor_end(); I!=E; ++I) {
865 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000866 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000867 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek9c378f72011-08-12 23:37:29 +0000870 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000871 // Scan the region bindings, and see if a "notable" symbol has a new
872 // lval binding.
873 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
874 PDB.getStateManager().iterBindings(N->getState(), SNS);
875 }
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek14856d72009-04-06 23:06:54 +0000878 // After constructing the full PathDiagnostic, do a pass over it to compact
879 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000880 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000881}
882
883//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000884// "Extensive" PathDiagnostic generation.
885//===----------------------------------------------------------------------===//
886
887static bool IsControlFlowExpr(const Stmt *S) {
888 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000890 if (!E)
891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 E = E->IgnoreParenCasts();
894
John McCall56ca35d2011-02-17 10:25:35 +0000895 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000896 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000898 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
899 if (B->isLogicalOp())
900 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000901
902 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000903}
904
Ted Kremenek14856d72009-04-06 23:06:54 +0000905namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000906class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000907 bool IsDead;
908public:
909 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
910 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000911
912 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000913 bool isDead() const { return IsDead; }
914};
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000916class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000917 std::vector<ContextLocation> CLocs;
918 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000919 PathDiagnostic &PD;
920 PathDiagnosticBuilder &PDB;
921 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000923 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek14856d72009-04-06 23:06:54 +0000925 bool containsLocation(const PathDiagnosticLocation &Container,
926 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek14856d72009-04-06 23:06:54 +0000928 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek9650cf32009-05-11 21:42:34 +0000930 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
931 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000932 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000933 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000934 while (1) {
935 // Adjust the location for some expressions that are best referenced
936 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000937 switch (S->getStmtClass()) {
938 default:
939 break;
940 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000941 case Stmt::GenericSelectionExprClass:
942 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000943 firstCharOnly = true;
944 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000945 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000946 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000947 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000948 firstCharOnly = true;
949 continue;
950 case Stmt::ChooseExprClass:
951 S = cast<ChooseExpr>(S)->getCond();
952 firstCharOnly = true;
953 continue;
954 case Stmt::BinaryOperatorClass:
955 S = cast<BinaryOperator>(S)->getLHS();
956 firstCharOnly = true;
957 continue;
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek9650cf32009-05-11 21:42:34 +0000960 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Ted Kremenek9650cf32009-05-11 21:42:34 +0000963 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000964 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremenek9650cf32009-05-11 21:42:34 +0000967 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000968 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000969
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000970 return L;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000974 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000975 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000976 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000977 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000978 CLocs.pop_back();
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981public:
982 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
983 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremeneka301a672009-04-22 18:16:20 +0000985 // If the PathDiagnostic already has pieces, add the enclosing statement
986 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000987 if (!PD.path.empty()) {
988 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Ted Kremenek14856d72009-04-06 23:06:54 +0000990 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000991 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000992 }
993 }
994
995 ~EdgeBuilder() {
996 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000997
Ted Kremeneka301a672009-04-22 18:16:20 +0000998 // Finally, add an initial edge from the start location of the first
999 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +00001000 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +00001001 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +00001002 PDB.getSourceManager());
1003 if (L.isValid())
1004 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +00001005 }
1006
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001007 void flushLocations() {
1008 while (!CLocs.empty())
1009 popLocation();
1010 PrevLoc = PathDiagnosticLocation();
1011 }
1012
Ted Kremenek14856d72009-04-06 23:06:54 +00001013 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001015 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek14856d72009-04-06 23:06:54 +00001017 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001018 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001019};
Ted Kremenek14856d72009-04-06 23:06:54 +00001020} // end anonymous namespace
1021
1022
1023PathDiagnosticLocation
1024EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1025 if (const Stmt *S = L.asStmt()) {
1026 if (IsControlFlowExpr(S))
1027 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
1029 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Ted Kremenek14856d72009-04-06 23:06:54 +00001032 return L;
1033}
1034
1035bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1036 const PathDiagnosticLocation &Containee) {
1037
1038 if (Container == Containee)
1039 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 if (Container.asDecl())
1042 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Ted Kremenek14856d72009-04-06 23:06:54 +00001044 if (const Stmt *S = Containee.asStmt())
1045 if (const Stmt *ContainerS = Container.asStmt()) {
1046 while (S) {
1047 if (S == ContainerS)
1048 return true;
1049 S = PDB.getParent(S);
1050 }
1051 return false;
1052 }
1053
1054 // Less accurate: compare using source ranges.
1055 SourceRange ContainerR = Container.asRange();
1056 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Ted Kremenek14856d72009-04-06 23:06:54 +00001058 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001059 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1060 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1061 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1062 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Chandler Carruth64211622011-07-25 21:09:52 +00001064 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1065 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1066 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1067 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenek14856d72009-04-06 23:06:54 +00001069 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001070 assert(ContaineeBegLine <= ContaineeEndLine);
1071
Ted Kremenek14856d72009-04-06 23:06:54 +00001072 return (ContainerBegLine <= ContaineeBegLine &&
1073 ContainerEndLine >= ContaineeEndLine &&
1074 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001075 SM.getExpansionColumnNumber(ContainerRBeg) <=
1076 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001077 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001078 SM.getExpansionColumnNumber(ContainerREnd) >=
1079 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001080}
1081
Ted Kremenek14856d72009-04-06 23:06:54 +00001082void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1083 if (!PrevLoc.isValid()) {
1084 PrevLoc = NewLoc;
1085 return;
1086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001088 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1089 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001091 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001092 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremenek14856d72009-04-06 23:06:54 +00001094 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001095 if (NewLocClean.asLocation().getExpansionLoc() ==
1096 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001097 return;
1098
Ted Kremenek2042fc12012-02-24 06:00:00 +00001099 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001100 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001101}
1102
1103void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Ted Kremeneka301a672009-04-22 18:16:20 +00001105 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1106 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Ted Kremenek14856d72009-04-06 23:06:54 +00001108 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1109
1110 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001111 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Ted Kremenek14856d72009-04-06 23:06:54 +00001113 // Is the top location context the same as the one for the new location?
1114 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001115 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001116 if (IsConsumedExpr(TopContextLoc) &&
1117 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001118 TopContextLoc.markDead();
1119
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001121 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001122
1123 return;
1124 }
1125
1126 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001127 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001130 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001131 CLocs.push_back(ContextLocation(CLoc, true));
1132 return;
1133 }
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremenek14856d72009-04-06 23:06:54 +00001136 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001137 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001138 }
1139
1140 // Context does not contain the location. Flush it.
1141 popLocation();
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001144 // If we reach here, there is no enclosing context. Just add the edge.
1145 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001146}
1147
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001148bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1149 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1150 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001152 return false;
1153}
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremeneke1baed32009-05-05 23:13:38 +00001155void EdgeBuilder::addExtendedContext(const Stmt *S) {
1156 if (!S)
1157 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
1159 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001160 while (Parent) {
1161 if (isa<CompoundStmt>(Parent))
1162 Parent = PDB.getParent(Parent);
1163 else
1164 break;
1165 }
1166
1167 if (Parent) {
1168 switch (Parent->getStmtClass()) {
1169 case Stmt::DoStmtClass:
1170 case Stmt::ObjCAtSynchronizedStmtClass:
1171 addContext(Parent);
1172 default:
1173 break;
1174 }
1175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremeneke1baed32009-05-05 23:13:38 +00001177 addContext(S);
1178}
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenek14856d72009-04-06 23:06:54 +00001180void EdgeBuilder::addContext(const Stmt *S) {
1181 if (!S)
1182 return;
1183
Ted Kremenek59950d32012-02-24 07:12:52 +00001184 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenek14856d72009-04-06 23:06:54 +00001186 while (!CLocs.empty()) {
1187 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1188
1189 // Is the top location context the same as the one for the new location?
1190 if (TopContextLoc == L)
1191 return;
1192
1193 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001194 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001195 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001196 }
1197
1198 // Context does not contain the location. Flush it.
1199 popLocation();
1200 }
1201
1202 CLocs.push_back(L);
1203}
1204
1205static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1206 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001207 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001208 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001209 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001210
Ted Kremenek9c378f72011-08-12 23:37:29 +00001211 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001212 while (NextNode) {
1213 N = NextNode;
1214 NextNode = GetPredecessorNode(N);
1215 ProgramPoint P = N->getLocation();
1216
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001217 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001218 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1219 const StackFrameContext *LCtx =
1220 CE->getLocationContext()->getCurrentStackFrame();
1221 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1222 PDB.getSourceManager(),
1223 LCtx);
1224 EB.addEdge(Loc, true);
1225 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001226 PathDiagnosticCallPiece *C =
1227 PathDiagnosticCallPiece::construct(N, *CE, SM);
1228 PD.getActivePath().push_front(C);
1229 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001230 break;
1231 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001232
Ted Kremenek2042fc12012-02-24 06:00:00 +00001233 // Pop the call hierarchy if we are done walking the contents
1234 // of a function call.
1235 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001236 // Add an edge to the start of the function.
1237 const Decl *D = CE->getCalleeContext()->getDecl();
1238 PathDiagnosticLocation pos =
1239 PathDiagnosticLocation::createBegin(D, SM);
1240 EB.addEdge(pos);
1241
1242 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001243 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001244 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001245 assert(!PD.getActivePath().empty());
1246 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001247
Ted Kremenek2042fc12012-02-24 06:00:00 +00001248 // The current active path should never be empty. Either we
1249 // just added a bunch of stuff to the top-level path, or
1250 // we have a previous CallExit. If the front of the active
1251 // path is not a PathDiagnosticCallPiece, it means that the
1252 // path terminated within a function call. We must then take the
1253 // current contents of the active path and place it within
1254 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001255 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001256 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1257 if (!C)
1258 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
1259 C->setCallee(*CE, SM);
1260 EB.addContext(CE->getCallExpr());
1261 break;
1262 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001263
1264 // Note that is important that we update the LocationContext
1265 // after looking at CallExits. CallExit basically adds an
1266 // edge in the *caller*, so we don't want to update the LocationContext
1267 // too soon.
1268 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001269
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001270 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001271 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001272 const CFGBlock &Blk = *BE->getSrc();
1273 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001275 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001276 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001277 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001278 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001280 if (!Term) {
1281 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1282 CS = dyn_cast<CompoundStmt>(FS->getBody());
1283 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001284 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001287 PathDiagnosticEventPiece *p =
1288 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001289 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001290 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001292 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001293 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001295 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001296 PathDiagnosticLocation BL =
1297 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001298 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001299 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001302 if (Term)
1303 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001305 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001306 }
1307
Mike Stump1eb44332009-09-09 15:08:12 +00001308 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001309 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1310 const Stmt *stmt = S->getStmt();
1311 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001312 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001313 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001314 }
1315 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001316 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001317 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001318
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001319 break;
1320 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001321
1322
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001323 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001325 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001326 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Anna Zaks8e6431a2011-08-18 22:37:56 +00001328 // Add pieces from custom visitors.
1329 BugReport *R = PDB.getBugReport();
1330 for (BugReport::visitor_iterator I = R->visitor_begin(),
1331 E = R->visitor_end(); I!=E; ++I) {
1332 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001333 const PathDiagnosticLocation &Loc = p->getLocation();
1334 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001335 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001336 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001337 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001340 }
1341}
1342
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001343//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001344// Methods for BugType and subclasses.
1345//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001346BugType::~BugType() { }
1347
Ted Kremenekcf118d42009-02-04 23:49:09 +00001348void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001349
David Blaikie99ba9e32011-12-20 02:48:34 +00001350void BuiltinBug::anchor() {}
1351
Ted Kremenekcf118d42009-02-04 23:49:09 +00001352//===----------------------------------------------------------------------===//
1353// Methods for BugReport and subclasses.
1354//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001355
David Blaikie99ba9e32011-12-20 02:48:34 +00001356void BugReport::NodeResolver::anchor() {}
1357
Anna Zaks8e6431a2011-08-18 22:37:56 +00001358void BugReport::addVisitor(BugReporterVisitor* visitor) {
1359 if (!visitor)
1360 return;
1361
1362 llvm::FoldingSetNodeID ID;
1363 visitor->Profile(ID);
1364 void *InsertPos;
1365
1366 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1367 delete visitor;
1368 return;
1369 }
1370
1371 CallbacksSet.InsertNode(visitor, InsertPos);
1372 Callbacks = F.add(visitor, Callbacks);
1373}
1374
1375BugReport::~BugReport() {
1376 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001377 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001378 }
1379}
Anna Zakse172e8b2011-08-17 23:00:25 +00001380
1381void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1382 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001383 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001384 if (UniqueingLocation.isValid()) {
1385 UniqueingLocation.Profile(hash);
1386 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001387 Location.Profile(hash);
1388 } else {
1389 assert(ErrorNode);
1390 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1391 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001392
1393 for (SmallVectorImpl<SourceRange>::const_iterator I =
1394 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1395 const SourceRange range = *I;
1396 if (!range.isValid())
1397 continue;
1398 hash.AddInteger(range.getBegin().getRawEncoding());
1399 hash.AddInteger(range.getEnd().getRawEncoding());
1400 }
1401}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402
Ted Kremenek9c378f72011-08-12 23:37:29 +00001403const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001404 if (!ErrorNode)
1405 return 0;
1406
Tom Care212f6d32010-09-16 03:50:38 +00001407 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001408 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenek9c378f72011-08-12 23:37:29 +00001410 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001411 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001412 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001413 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001414 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001415 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001416 S = GetStmt(ProgP);
1417
1418 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001419}
1420
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001421std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001422BugReport::getRanges() {
1423 // If no custom ranges, add the range of the statement corresponding to
1424 // the error node.
1425 if (Ranges.empty()) {
1426 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1427 addRange(E->getSourceRange());
1428 else
1429 return std::make_pair(ranges_iterator(), ranges_iterator());
1430 }
1431
Anna Zaks14924262011-08-24 20:31:06 +00001432 // User-specified absence of range info.
1433 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1434 return std::make_pair(ranges_iterator(), ranges_iterator());
1435
Anna Zakse172e8b2011-08-17 23:00:25 +00001436 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001437}
1438
Anna Zaks590dd8e2011-09-20 21:38:35 +00001439PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001440 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001441 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001442 "Either Location or ErrorNode should be specified but not both.");
1443
Ted Kremenek9c378f72011-08-12 23:37:29 +00001444 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001445 const LocationContext *LC = ErrorNode->getLocationContext();
1446
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001447 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001448 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001449 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001450 // For binary operators, return the location of the operator.
1451 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001452 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001453
Anna Zaks590dd8e2011-09-20 21:38:35 +00001454 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001455 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001456 } else {
1457 assert(Location.isValid());
1458 return Location;
1459 }
1460
Anna Zaks590dd8e2011-09-20 21:38:35 +00001461 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001462}
1463
Ted Kremenekcf118d42009-02-04 23:49:09 +00001464//===----------------------------------------------------------------------===//
1465// Methods for BugReporter and subclasses.
1466//===----------------------------------------------------------------------===//
1467
1468BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001469 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001470}
1471
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001472GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001473BugReporterData::~BugReporterData() {}
1474
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001475ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001476
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001477ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001478GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1479
Anna Zaks3b030a22011-08-19 01:57:09 +00001480BugReporter::~BugReporter() {
1481 FlushReports();
1482
1483 // Free the bug reports we are tracking.
1484 typedef std::vector<BugReportEquivClass *> ContTy;
1485 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1486 I != E; ++I) {
1487 delete *I;
1488 }
1489}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001490
1491void BugReporter::FlushReports() {
1492 if (BugTypes.isEmpty())
1493 return;
1494
1495 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001496 // warnings and new BugTypes.
1497 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1498 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001499 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001500 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001501 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001502 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001503 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001504 const_cast<BugType*>(*I)->FlushReports(*this);
1505
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001506 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1507 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1508 BugReportEquivClass& EQ = *EI;
1509 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001510 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001511
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001512 // BugReporter owns and deletes only BugTypes created implicitly through
1513 // EmitBasicReport.
1514 // FIXME: There are leaks from checkers that assume that the BugTypes they
1515 // create will be destroyed by the BugReporter.
1516 for (llvm::StringMap<BugType*>::iterator
1517 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1518 delete I->second;
1519
Ted Kremenekcf118d42009-02-04 23:49:09 +00001520 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001521 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001522}
1523
1524//===----------------------------------------------------------------------===//
1525// PathDiagnostics generation.
1526//===----------------------------------------------------------------------===//
1527
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001528static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001529 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001530MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001531 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenekcf118d42009-02-04 23:49:09 +00001533 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001534 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001535 // error node unless there are two or more error nodes with the same minimum
1536 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001537 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001538 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001539
1540 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001541 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1542 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenekcf118d42009-02-04 23:49:09 +00001544 // Create owning pointers for GTrim and NMap just to ensure that they are
1545 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001546 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1547 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenekcf118d42009-02-04 23:49:09 +00001549 // Find the (first) error node in the trimmed graph. We just need to consult
1550 // the node map (NMap) which maps from nodes in the original graph to nodes
1551 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001552
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001553 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001554 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001555 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001556
Ted Kremenek40406fe2010-12-03 06:52:30 +00001557 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1558 const ExplodedNode *originalNode = nodes[nodeIndex];
1559 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001560 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001561 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001562 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek938332c2009-05-16 01:11:58 +00001565 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001566
1567 // Create a new (third!) graph with a single path. This is the graph
1568 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001569 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek10aa5542009-03-12 23:41:59 +00001571 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001572 // to the root node, and then construct a new graph that contains only
1573 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001574 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001576 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001577 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001579 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001580 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001581 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001583 if (Visited.find(Node) != Visited.end())
1584 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001586 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001588 if (Node->pred_empty()) {
1589 Root = Node;
1590 break;
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001593 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001594 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001595 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Ted Kremenek938332c2009-05-16 01:11:58 +00001598 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenek10aa5542009-03-12 23:41:59 +00001600 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001601 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001602 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001603 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001604 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001606 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001607 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001608 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001609 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001611 // Create the equivalent node in the new graph with the same state
1612 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001613 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001615 // Store the mapping to the original node.
1616 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1617 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001618 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001620 // Link up the new node with the previous node.
1621 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001622 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001624 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001626 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001627 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001628 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001629 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001630 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001631 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001632 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001635 // Find the next successor node. We choose the node that is marked
1636 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001637 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1638 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001639 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001641 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001643 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001645 if (I == Visited.end())
1646 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001648 if (!N || I->second < MinVal) {
1649 N = *SI;
1650 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001651 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Ted Kremenek938332c2009-05-16 01:11:58 +00001654 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Ted Kremenek938332c2009-05-16 01:11:58 +00001657 assert(First);
1658
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001659 return std::make_pair(std::make_pair(GNew, BM),
1660 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001661}
1662
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001663/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1664/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001665static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001666 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1667 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001669 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001670 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001672 MacroStackTy MacroStack;
1673 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Ted Kremenek77d09442012-03-02 01:27:31 +00001675 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001676 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001677
1678 PathDiagnosticPiece *piece = I->getPtr();
1679
1680 // Recursively compact calls.
1681 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1682 CompactPathDiagnostic(call->path, SM);
1683 }
1684
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001685 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001686 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001688 // Determine the instantiation location, which is the location we group
1689 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001691 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001692 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001694 if (Loc.isFileID()) {
1695 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001696 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001697 continue;
1698 }
1699
1700 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001702 // Is the PathDiagnosticPiece within the same macro group?
1703 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001704 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001705 continue;
1706 }
1707
1708 // We aren't in the same group. Are we descending into a new macro
1709 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001710 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001711
1712 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001713 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001714 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001716 // Walk the entire macro stack.
1717 while (!MacroStack.empty()) {
1718 if (InstantiationLoc == MacroStack.back().second) {
1719 MacroGroup = MacroStack.back().first;
1720 break;
1721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001723 if (ParentInstantiationLoc == MacroStack.back().second) {
1724 MacroGroup = MacroStack.back().first;
1725 break;
1726 }
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001728 MacroStack.pop_back();
1729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001731 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1732 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001733 PathDiagnosticMacroPiece *NewGroup =
1734 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001735 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001736
1737 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001738 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001739 else {
1740 assert(InstantiationLoc.isFileID());
1741 Pieces.push_back(NewGroup);
1742 }
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001744 MacroGroup = NewGroup;
1745 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1746 }
1747
1748 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001749 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001752 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001753 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Ted Kremenek77d09442012-03-02 01:27:31 +00001755 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1756 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001757}
1758
Ted Kremenek7dc86642009-03-31 20:22:36 +00001759void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001760 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Ted Kremenek40406fe2010-12-03 06:52:30 +00001762 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001763 SmallVector<const ExplodedNode *, 10> errorNodes;
1764 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001765 E = bugReports.end(); I != E; ++I) {
1766 errorNodes.push_back((*I)->getErrorNode());
1767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001769 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001770 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001771 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001772 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001773 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Ted Kremenekcf118d42009-02-04 23:49:09 +00001775 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001776 assert(GPair.second.second < bugReports.size());
1777 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001778 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001780 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1781 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001782 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001783
1784 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001785 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1786 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Anna Zaks8e6431a2011-08-18 22:37:56 +00001788 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001789 R->addVisitor(new NilReceiverBRVisitor());
1790 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Anna Zaks23f395e2011-08-20 01:27:22 +00001792 // Generate the very last diagnostic piece - the piece is visible before
1793 // the trace is expanded.
1794 PathDiagnosticPiece *LastPiece = 0;
1795 for (BugReport::visitor_iterator I = R->visitor_begin(),
1796 E = R->visitor_end(); I!=E; ++I) {
1797 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1798 assert (!LastPiece &&
1799 "There can only be one final piece in a diagnostic.");
1800 LastPiece = Piece;
1801 }
1802 }
1803 if (!LastPiece)
1804 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1805 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001806 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001807 else
1808 return;
1809
Ted Kremenek7dc86642009-03-31 20:22:36 +00001810 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001811 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001812 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001813 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001814 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001815 GenerateMinimalPathDiagnostic(PD, PDB, N);
1816 break;
1817 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001818
1819 // Finally, prune the diagnostic path of uninteresting stuff.
1820 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1821 assert(hasSomethingInteresting);
1822 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001823}
1824
Ted Kremenekcf118d42009-02-04 23:49:09 +00001825void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001826 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001827}
1828
Mike Stump1eb44332009-09-09 15:08:12 +00001829void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001830 // Compute the bug report's hash to determine its equivalence class.
1831 llvm::FoldingSetNodeID ID;
1832 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001833
1834 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001835 BugType& BT = R->getBugType();
1836 Register(&BT);
1837 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001838 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremenekcf118d42009-02-04 23:49:09 +00001840 if (!EQ) {
1841 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001842 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001843 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001844 }
1845 else
1846 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001847}
1848
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001849
1850//===----------------------------------------------------------------------===//
1851// Emitting reports in equivalence classes.
1852//===----------------------------------------------------------------------===//
1853
1854namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001855struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001856 const ExplodedNode *N;
1857 ExplodedNode::const_succ_iterator I, E;
1858
1859 FRIEC_WLItem(const ExplodedNode *n)
1860 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1861};
1862}
1863
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001864static BugReport *
1865FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001866 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001867
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001868 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1869 assert(I != E);
1870 BugReport *R = *I;
1871 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001872
Ted Kremenek40406fe2010-12-03 06:52:30 +00001873 // If we don't need to suppress any of the nodes because they are
1874 // post-dominated by a sink, simply add all the nodes in the equivalence class
1875 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001876 if (!BT.isSuppressOnSink()) {
1877 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001878 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001879 if (N) {
1880 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001881 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001882 }
1883 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001884 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001885 }
1886
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001887 // For bug reports that should be suppressed when all paths are post-dominated
1888 // by a sink node, iterate through the reports in the equivalence class
1889 // until we find one that isn't post-dominated (if one exists). We use a
1890 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1891 // this as a recursive function, but we don't want to risk blowing out the
1892 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001893 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001894
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001895 for (; I != E; ++I) {
1896 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001897 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001898
Ted Kremenek40406fe2010-12-03 06:52:30 +00001899 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001900 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001901 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001902 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001903 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001904 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001905 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001906 if (errorNode->succ_empty()) {
1907 bugReports.push_back(R);
1908 if (!exampleReport)
1909 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001910 continue;
1911 }
1912
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001913 // At this point we know that 'N' is not a sink and it has at least one
1914 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1915 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001916 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001917 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1918
1919 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001920 WL.push_back(errorNode);
1921 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001922
1923 while (!WL.empty()) {
1924 WLItem &WI = WL.back();
1925 assert(!WI.N->succ_empty());
1926
1927 for (; WI.I != WI.E; ++WI.I) {
1928 const ExplodedNode *Succ = *WI.I;
1929 // End-of-path node?
1930 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001931 // If we found an end-of-path node that is not a sink.
1932 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001933 bugReports.push_back(R);
1934 if (!exampleReport)
1935 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001936 WL.clear();
1937 break;
1938 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001939 // Found a sink? Continue on to the next successor.
1940 continue;
1941 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001942 // Mark the successor as visited. If it hasn't been explored,
1943 // enqueue it to the DFS worklist.
1944 unsigned &mark = Visited[Succ];
1945 if (!mark) {
1946 mark = 1;
1947 WL.push_back(Succ);
1948 break;
1949 }
1950 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001951
1952 // The worklist may have been cleared at this point. First
1953 // check if it is empty before checking the last item.
1954 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001955 WL.pop_back();
1956 }
1957 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001958
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001959 // ExampleReport will be NULL if all the nodes in the equivalence class
1960 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001961 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001962}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001963
1964//===----------------------------------------------------------------------===//
1965// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1966// uses global state, which eventually should go elsewhere.
1967//===----------------------------------------------------------------------===//
1968namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001969class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001970 llvm::FoldingSetNodeID ID;
1971public:
1972 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001973 R->Profile(ID);
1974 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001975 }
1976
1977 void Profile(llvm::FoldingSetNodeID &id) {
1978 id = ID;
1979 }
1980
1981 llvm::FoldingSetNodeID &getID() { return ID; }
1982};
1983}
1984
1985static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1986 // FIXME: Eventually this diagnostic cache should reside in something
1987 // like AnalysisManager instead of being a static variable. This is
1988 // really unsafe in the long term.
1989 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1990 static DiagnosticCache DC;
1991
1992 void *InsertPos;
1993 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1994
1995 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1996 delete Item;
1997 return true;
1998 }
1999
2000 DC.InsertNode(Item, InsertPos);
2001 return false;
2002}
2003
Ted Kremenekcf118d42009-02-04 23:49:09 +00002004void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002005 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002006 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
2007 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002008 return;
2009
David Blaikieef3643f2011-09-26 00:51:36 +00002010 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Ted Kremenekcf118d42009-02-04 23:49:09 +00002012 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002013 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002014 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002016 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00002017 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00002018 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00002019 ? exampleReport->getDescription()
2020 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002021 BT.getCategory()));
2022
Ted Kremenek40406fe2010-12-03 06:52:30 +00002023 if (!bugReports.empty())
2024 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Ted Kremenek40406fe2010-12-03 06:52:30 +00002026 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00002027 return;
2028
Ted Kremenek072192b2008-04-30 23:47:44 +00002029 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00002030 const BugReport::ExtraTextList &Meta =
2031 exampleReport->getExtraText();
2032 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2033 e = Meta.end(); i != e; ++i) {
2034 D->addMeta(*i);
2035 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002036
Ted Kremenek3148eb42009-01-24 00:55:43 +00002037 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002038 BugReport::ranges_iterator Beg, End;
2039 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00002040 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00002041
2042 // Search the description for '%', as that will be interpretted as a
2043 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002044 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00002045 unsigned ErrorDiag;
2046 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002047 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00002048 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002049 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00002050 if (*I == '%')
2051 Out << "%%";
2052 else
2053 Out << *I;
2054
2055 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00002056 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00002057 }
Ted Kremenek57202072008-07-14 17:40:50 +00002058
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002059 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002060 DiagnosticBuilder diagBuilder = Diag.Report(
2061 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002062 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002063 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00002064 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00002065
David Blaikieef3643f2011-09-26 00:51:36 +00002066 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002067 if (!PD)
2068 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Ted Kremenek802e0242012-02-08 04:32:34 +00002070 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002071 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2072 exampleReport->getLocation(getSourceManager()),
2073 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002074
Ted Kremenek3148eb42009-01-24 00:55:43 +00002075 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00002076 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002077 }
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Ted Kremenek3148eb42009-01-24 00:55:43 +00002079 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002080}
Ted Kremenek57202072008-07-14 17:40:50 +00002081
Chris Lattner5f9e2722011-07-23 10:55:15 +00002082void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002083 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002084 SourceRange* RBeg, unsigned NumRanges) {
2085 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
2086}
Ted Kremenekcf118d42009-02-04 23:49:09 +00002087
Chris Lattner5f9e2722011-07-23 10:55:15 +00002088void BugReporter::EmitBasicReport(StringRef name,
2089 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002090 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002091 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002093 // 'BT' is owned by BugReporter.
2094 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002095 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002096 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2097 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002098}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002099
Chris Lattner5f9e2722011-07-23 10:55:15 +00002100BugType *BugReporter::getBugTypeForName(StringRef name,
2101 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002102 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002103 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2104 llvm::StringMapEntry<BugType *> &
2105 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2106 BugType *BT = entry.getValue();
2107 if (!BT) {
2108 BT = new BugType(name, category);
2109 entry.setValue(BT);
2110 }
2111 return BT;
2112}