blob: 08588f60abfd1675b8e80450954d0fdfa0ed0a7d [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Jordan Rose852aa0d2012-07-10 22:07:52 +000051 else if (const CallEnter *CE = dyn_cast<CallEnter>(&P))
52 return CE->getCallExpr();
53 else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P))
54 return CEE->getCalleeContext()->getCallSite();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekb697b102009-02-23 22:44:26 +000056 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057}
58
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000065GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067}
68
Ted Kremenek9c378f72011-08-12 23:37:29 +000069static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000070 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000071 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000072 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000075}
76
Ted Kremenek9c378f72011-08-12 23:37:29 +000077static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 // Check if the statement is '?' or '&&'/'||'. These are "merges",
81 // not actual statement points.
82 switch (S->getStmtClass()) {
83 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000084 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000087 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000089 continue;
90 break;
91 }
92 default:
93 break;
94 }
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return 0;
99}
100
Ted Kremenek5f85e172009-07-22 22:35:28 +0000101static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return GetPreviousStmt(N);
107}
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek5f85e172009-07-22 22:35:28 +0000109static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000110GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000111 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return GetNextStmt(N);
115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000118// Diagnostic cleanup.
119//===----------------------------------------------------------------------===//
120
121/// Recursively scan through a path and prune out calls and macros pieces
122/// that aren't needed. Return true if afterwards the path contains
123/// "interesting stuff" which means it should be pruned from the parent path.
124static bool RemoveUneededCalls(PathPieces &pieces) {
125 bool containsSomethingInteresting = false;
126 const unsigned N = pieces.size();
127
128 for (unsigned i = 0 ; i < N ; ++i) {
129 // Remove the front piece from the path. If it is still something we
130 // want to keep once we are done, we will push it back on the end.
131 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
132 pieces.pop_front();
133
Ted Kremenek72516742012-03-01 00:05:06 +0000134 switch (piece->getKind()) {
135 case PathDiagnosticPiece::Call: {
136 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
137 // Recursively clean out the subclass. Keep this call around if
138 // it contains any informative diagnostics.
139 if (!RemoveUneededCalls(call->path))
140 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000141 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000142 break;
143 }
144 case PathDiagnosticPiece::Macro: {
145 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
146 if (!RemoveUneededCalls(macro->subPieces))
147 continue;
148 containsSomethingInteresting = true;
149 break;
150 }
151 case PathDiagnosticPiece::Event: {
152 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
153 // We never throw away an event, but we do throw it away wholesale
154 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000155 if (event->isPrunable())
156 continue;
157 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000158 break;
159 }
160 case PathDiagnosticPiece::ControlFlow:
161 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000162 }
163
164 pieces.push_back(piece);
165 }
166
167 return containsSomethingInteresting;
168}
169
170//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000171// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000172//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000173
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000174typedef llvm::DenseMap<const ExplodedNode*,
175const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000176
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000177namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000178class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000179 NodeBackMap& M;
180public:
181 NodeMapClosure(NodeBackMap *m) : M(*m) {}
182 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek9c378f72011-08-12 23:37:29 +0000184 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000185 NodeBackMap::iterator I = M.find(N);
186 return I == M.end() ? 0 : I->second;
187 }
188};
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000190class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000191 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000192 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000193 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000194 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000195public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000196 const LocationContext *LC;
197
Ted Kremenek8966bc12009-05-06 21:39:49 +0000198 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000199 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000200 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000201 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000202 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
203 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek9c378f72011-08-12 23:37:29 +0000205 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek9c378f72011-08-12 23:37:29 +0000207 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
208 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anna Zaks8e6431a2011-08-18 22:37:56 +0000210 BugReport *getBugReport() { return R; }
211
Tom Care212f6d32010-09-16 03:50:38 +0000212 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000213
214 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000216 const Stmt *getParent(const Stmt *S) {
217 return getParentMap().getParent(S);
218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenek8966bc12009-05-06 21:39:49 +0000220 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000221
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000222 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
David Blaikieef3643f2011-09-26 00:51:36 +0000224 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
225 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000226 }
227
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000228 bool supportsLogicalOpControlFlow() const {
229 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000230 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000231};
232} // end anonymous namespace
233
Ted Kremenek00605e02009-03-27 20:55:39 +0000234PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000235PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000236 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000237 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000238
Anna Zaks0cd59482011-09-16 19:18:30 +0000239 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
240 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000241}
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek00605e02009-03-27 20:55:39 +0000243PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000244PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
245 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000246
Ted Kremenek143ca222008-05-06 18:11:09 +0000247 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000248 if (os.str().empty())
249 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek00605e02009-03-27 20:55:39 +0000251 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek00605e02009-03-27 20:55:39 +0000253 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000254 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000255 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000256 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000257 else {
258 os << "Execution jumps to the end of the ";
259 const Decl *D = N->getLocationContext()->getDecl();
260 if (isa<ObjCMethodDecl>(D))
261 os << "method";
262 else if (isa<FunctionDecl>(D))
263 os << "function";
264 else {
265 assert(isa<BlockDecl>(D));
266 os << "anonymous block";
267 }
268 os << '.';
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000271 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000272}
273
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000274static bool IsNested(const Stmt *S, ParentMap &PM) {
275 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
276 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000278 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000280 if (Parent)
281 switch (Parent->getStmtClass()) {
282 case Stmt::ForStmtClass:
283 case Stmt::DoStmtClass:
284 case Stmt::WhileStmtClass:
285 return true;
286 default:
287 break;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
290 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000291}
292
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000293PathDiagnosticLocation
294PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000295 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000296 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000297 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000298
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000299 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000300 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000302 if (!Parent)
303 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000305 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000306 case Stmt::BinaryOperatorClass: {
307 const BinaryOperator *B = cast<BinaryOperator>(Parent);
308 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000309 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000310 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000311 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000312 case Stmt::CompoundStmtClass:
313 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000314 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000315 case Stmt::ChooseExprClass:
316 // Similar to '?' if we are referring to condition, just have the edge
317 // point to the entire choose expression.
318 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000319 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000320 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000321 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000322 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000323 case Stmt::ConditionalOperatorClass:
324 // For '?', if we are referring to condition, just have the edge point
325 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000326 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000328 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000329 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000330 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000331 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::ForStmtClass:
333 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000335 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::IfStmtClass:
337 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000339 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000340 case Stmt::ObjCForCollectionStmtClass:
341 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000343 break;
344 case Stmt::WhileStmtClass:
345 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000346 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000347 break;
348 default:
349 break;
350 }
351
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000352 S = Parent;
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000355 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000356
357 // Special case: DeclStmts can appear in for statement declarations, in which
358 // case the ForStmt is the context.
359 if (isa<DeclStmt>(S)) {
360 if (const Stmt *Parent = P.getParent(S)) {
361 switch (Parent->getStmtClass()) {
362 case Stmt::ForStmtClass:
363 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000364 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000365 default:
366 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000367 }
368 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000369 }
370 else if (isa<BinaryOperator>(S)) {
371 // Special case: the binary operator represents the initialization
372 // code in a for statement (this can happen when the variable being
373 // initialized is an old variable.
374 if (const ForStmt *FS =
375 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
376 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000377 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000378 }
379 }
380
Anna Zaks220ac8c2011-09-15 01:08:34 +0000381 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000382}
383
Ted Kremenekcf118d42009-02-04 23:49:09 +0000384//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000385// "Minimal" path diagnostic generation algorithm.
386//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000387typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
388typedef SmallVector<StackDiagPair, 6> StackDiagVector;
389
Anna Zaks368a0d52012-03-15 21:13:02 +0000390static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000391 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000392 // If the piece contains a special message, add it to all the call
393 // pieces on the active stack.
394 if (PathDiagnosticEventPiece *ep =
395 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000396
Anna Zaks56a938f2012-03-16 23:24:20 +0000397 if (ep->hasCallStackHint())
398 for (StackDiagVector::iterator I = CallStack.begin(),
399 E = CallStack.end(); I != E; ++I) {
400 PathDiagnosticCallPiece *CP = I->first;
401 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000402 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000403
Anna Zaks368a0d52012-03-15 21:13:02 +0000404 // The last message on the path to final bug is the most important
405 // one. Since we traverse the path backwards, do not add the message
406 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000407 if (!CP->hasCallStackMessage())
408 CP->setCallStackMessage(stackMsg);
409 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000410 }
411}
Ted Kremenek31061982009-03-31 23:00:32 +0000412
Ted Kremenek77d09442012-03-02 01:27:31 +0000413static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
416 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000417 const ExplodedNode *N,
418 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000421 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000422 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000423 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000424
Anna Zaks56a938f2012-03-16 23:24:20 +0000425 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000429 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000430 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek31061982009-03-31 23:00:32 +0000432 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000433
Anna Zaks0b3ade82012-04-20 21:59:08 +0000434 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000435 PathDiagnosticCallPiece *C =
436 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
437 PD.getActivePath().push_front(C);
438 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +0000439 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek2042fc12012-02-24 06:00:00 +0000440 continue;
441 }
442
443 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
444 PD.popActivePath();
445 // The current active path should never be empty. Either we
446 // just added a bunch of stuff to the top-level path, or
Anna Zaks0b3ade82012-04-20 21:59:08 +0000447 // we have a previous CallExitEnd. If the front of the active
Ted Kremenek2042fc12012-02-24 06:00:00 +0000448 // path is not a PathDiagnosticCallPiece, it means that the
449 // path terminated within a function call. We must then take the
450 // current contents of the active path and place it within
451 // a new PathDiagnosticCallPiece.
452 assert(!PD.getActivePath().empty());
453 PathDiagnosticCallPiece *C =
454 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +0000455 if (!C) {
456 const Decl *Caller = CE->getLocationContext()->getDecl();
457 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
458 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000459 C->setCallee(*CE, SMgr);
Anna Zaks368a0d52012-03-15 21:13:02 +0000460 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +0000461 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +0000462 CallStack.pop_back();
463 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000464 continue;
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek9c378f72011-08-12 23:37:29 +0000467 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
468 const CFGBlock *Src = BE->getSrc();
469 const CFGBlock *Dst = BE->getDst();
470 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek31061982009-03-31 23:00:32 +0000472 if (!T)
473 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Anna Zaks590dd8e2011-09-20 21:38:35 +0000475 PathDiagnosticLocation Start =
476 PathDiagnosticLocation::createBegin(T, SMgr,
477 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479 switch (T->getStmtClass()) {
480 default:
481 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek31061982009-03-31 23:00:32 +0000483 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000484 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000485 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremenek31061982009-03-31 23:00:32 +0000487 if (!S)
488 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000491 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000492 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000495 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000496 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000497 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000498 break;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
501 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000502 // Figure out what case arm we took.
503 std::string sbuf;
504 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek9c378f72011-08-12 23:37:29 +0000506 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000507 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek31061982009-03-31 23:00:32 +0000509 switch (S->getStmtClass()) {
510 default:
511 os << "No cases match in the switch statement. "
512 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000513 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000514 break;
515 case Stmt::DefaultStmtClass:
516 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000517 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000518 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek31061982009-03-31 23:00:32 +0000520 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000521 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000522 const CaseStmt *Case = cast<CaseStmt>(S);
523 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000524
525 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000526 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek9c378f72011-08-12 23:37:29 +0000528 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000529 // FIXME: Maybe this should be an assertion. Are there cases
530 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000531 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000532 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 if (D) {
535 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000536 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000537 }
538 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000539
540 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000541 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000544 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000545 break;
546 }
547 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000548 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000549 os.str()));
550 }
551 else {
552 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000553 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000554 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000555 os.str()));
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek31061982009-03-31 23:00:32 +0000558 break;
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek31061982009-03-31 23:00:32 +0000561 case Stmt::BreakStmtClass:
562 case Stmt::ContinueStmtClass: {
563 std::string sbuf;
564 llvm::raw_string_ostream os(sbuf);
565 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000566 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000567 os.str()));
568 break;
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenek31061982009-03-31 23:00:32 +0000571 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000572 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000573 case Stmt::ConditionalOperatorClass: {
574 std::string sbuf;
575 llvm::raw_string_ostream os(sbuf);
576 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek31061982009-03-31 23:00:32 +0000578 if (*(Src->succ_begin()+1) == Dst)
579 os << "false";
580 else
581 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Ted Kremenek31061982009-03-31 23:00:32 +0000583 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 if (const Stmt *S = End.asStmt())
586 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek2042fc12012-02-24 06:00:00 +0000588 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000589 os.str()));
590 break;
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Ted Kremenek31061982009-03-31 23:00:32 +0000593 // Determine control-flow for short-circuited '&&' and '||'.
594 case Stmt::BinaryOperatorClass: {
595 if (!PDB.supportsLogicalOpControlFlow())
596 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000598 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000599 std::string sbuf;
600 llvm::raw_string_ostream os(sbuf);
601 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000602
John McCall2de56d12010-08-25 11:45:40 +0000603 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000604 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremenek31061982009-03-31 23:00:32 +0000606 if (*(Src->succ_begin()+1) == Dst) {
607 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000608 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000609 PathDiagnosticLocation Start =
610 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000611 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000612 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000613 }
Ted Kremenek31061982009-03-31 23:00:32 +0000614 else {
615 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000616 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000617 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000618 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000619 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000620 }
Ted Kremenek31061982009-03-31 23:00:32 +0000621 }
622 else {
John McCall2de56d12010-08-25 11:45:40 +0000623 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000624 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 if (*(Src->succ_begin()+1) == Dst) {
627 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000628 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000629 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000630 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000631 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000632 }
633 else {
634 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000635 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000636 PathDiagnosticLocation Start =
637 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000638 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000639 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000640 }
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 break;
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
646 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000647 if (*(Src->succ_begin()) == Dst) {
648 std::string sbuf;
649 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek31061982009-03-31 23:00:32 +0000651 os << "Loop condition is true. ";
652 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek31061982009-03-31 23:00:32 +0000654 if (const Stmt *S = End.asStmt())
655 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek2042fc12012-02-24 06:00:00 +0000657 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000658 os.str()));
659 }
660 else {
661 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 if (const Stmt *S = End.asStmt())
664 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremenek2042fc12012-02-24 06:00:00 +0000666 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000667 "Loop condition is false. Exiting loop"));
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 break;
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Ted Kremenek31061982009-03-31 23:00:32 +0000673 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000674 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000675 if (*(Src->succ_begin()+1) == Dst) {
676 std::string sbuf;
677 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek31061982009-03-31 23:00:32 +0000679 os << "Loop condition is false. ";
680 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
681 if (const Stmt *S = End.asStmt())
682 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek2042fc12012-02-24 06:00:00 +0000684 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000685 os.str()));
686 }
687 else {
688 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
689 if (const Stmt *S = End.asStmt())
690 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek2042fc12012-02-24 06:00:00 +0000692 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000693 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremenek31061982009-03-31 23:00:32 +0000696 break;
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenek31061982009-03-31 23:00:32 +0000699 case Stmt::IfStmtClass: {
700 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Ted Kremenek31061982009-03-31 23:00:32 +0000702 if (const Stmt *S = End.asStmt())
703 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek31061982009-03-31 23:00:32 +0000705 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000706 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000707 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000708 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000709 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000710 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 break;
713 }
714 }
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000717 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000718 // Add diagnostic pieces from custom visitors.
719 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000720 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
721 E = visitors.end();
722 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000723 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000724 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000725 updateStackPiecesWithMessage(p, CallStack);
726 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000727 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000728 }
Ted Kremenek31061982009-03-31 23:00:32 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek14856d72009-04-06 23:06:54 +0000731 // After constructing the full PathDiagnostic, do a pass over it to compact
732 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000733 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000734}
735
736//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000737// "Extensive" PathDiagnostic generation.
738//===----------------------------------------------------------------------===//
739
740static bool IsControlFlowExpr(const Stmt *S) {
741 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000743 if (!E)
744 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
746 E = E->IgnoreParenCasts();
747
John McCall56ca35d2011-02-17 10:25:35 +0000748 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000749 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000751 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
752 if (B->isLogicalOp())
753 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
755 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000756}
757
Ted Kremenek14856d72009-04-06 23:06:54 +0000758namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000759class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000760 bool IsDead;
761public:
762 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
763 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000764
765 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000766 bool isDead() const { return IsDead; }
767};
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000769class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000770 std::vector<ContextLocation> CLocs;
771 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000772 PathDiagnostic &PD;
773 PathDiagnosticBuilder &PDB;
774 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000776 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek14856d72009-04-06 23:06:54 +0000778 bool containsLocation(const PathDiagnosticLocation &Container,
779 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenek14856d72009-04-06 23:06:54 +0000781 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek9650cf32009-05-11 21:42:34 +0000783 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
784 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000785 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000786 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000787 while (1) {
788 // Adjust the location for some expressions that are best referenced
789 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000790 switch (S->getStmtClass()) {
791 default:
792 break;
793 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000794 case Stmt::GenericSelectionExprClass:
795 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000796 firstCharOnly = true;
797 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000798 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000799 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000800 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000801 firstCharOnly = true;
802 continue;
803 case Stmt::ChooseExprClass:
804 S = cast<ChooseExpr>(S)->getCond();
805 firstCharOnly = true;
806 continue;
807 case Stmt::BinaryOperatorClass:
808 S = cast<BinaryOperator>(S)->getLHS();
809 firstCharOnly = true;
810 continue;
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek9650cf32009-05-11 21:42:34 +0000813 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek9650cf32009-05-11 21:42:34 +0000816 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000817 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek9650cf32009-05-11 21:42:34 +0000820 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000821 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000822
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000823 return L;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek14856d72009-04-06 23:06:54 +0000826 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000827 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000828 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000829 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000830 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000831 CLocs.pop_back();
832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek14856d72009-04-06 23:06:54 +0000834public:
835 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
836 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremeneka301a672009-04-22 18:16:20 +0000838 // If the PathDiagnostic already has pieces, add the enclosing statement
839 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000840 if (!PD.path.empty()) {
841 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek14856d72009-04-06 23:06:54 +0000843 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000844 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 }
846 }
847
848 ~EdgeBuilder() {
849 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000850
Ted Kremeneka301a672009-04-22 18:16:20 +0000851 // Finally, add an initial edge from the start location of the first
852 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000853 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000854 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000855 PDB.getSourceManager());
856 if (L.isValid())
857 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000858 }
859
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000860 void flushLocations() {
861 while (!CLocs.empty())
862 popLocation();
863 PrevLoc = PathDiagnosticLocation();
864 }
865
Ted Kremenek14856d72009-04-06 23:06:54 +0000866 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000868 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek14856d72009-04-06 23:06:54 +0000870 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000871 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000872};
Ted Kremenek14856d72009-04-06 23:06:54 +0000873} // end anonymous namespace
874
875
876PathDiagnosticLocation
877EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
878 if (const Stmt *S = L.asStmt()) {
879 if (IsControlFlowExpr(S))
880 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
882 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenek14856d72009-04-06 23:06:54 +0000885 return L;
886}
887
888bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
889 const PathDiagnosticLocation &Containee) {
890
891 if (Container == Containee)
892 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 if (Container.asDecl())
895 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek14856d72009-04-06 23:06:54 +0000897 if (const Stmt *S = Containee.asStmt())
898 if (const Stmt *ContainerS = Container.asStmt()) {
899 while (S) {
900 if (S == ContainerS)
901 return true;
902 S = PDB.getParent(S);
903 }
904 return false;
905 }
906
907 // Less accurate: compare using source ranges.
908 SourceRange ContainerR = Container.asRange();
909 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek14856d72009-04-06 23:06:54 +0000911 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000912 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
913 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
914 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
915 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chandler Carruth64211622011-07-25 21:09:52 +0000917 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
918 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
919 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
920 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenek14856d72009-04-06 23:06:54 +0000922 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000923 assert(ContaineeBegLine <= ContaineeEndLine);
924
Ted Kremenek14856d72009-04-06 23:06:54 +0000925 return (ContainerBegLine <= ContaineeBegLine &&
926 ContainerEndLine >= ContaineeEndLine &&
927 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000928 SM.getExpansionColumnNumber(ContainerRBeg) <=
929 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000930 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000931 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000932 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000933}
934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
936 if (!PrevLoc.isValid()) {
937 PrevLoc = NewLoc;
938 return;
939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000941 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
942 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000944 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek14856d72009-04-06 23:06:54 +0000947 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000948 if (NewLocClean.asLocation().getExpansionLoc() ==
949 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 return;
951
Ted Kremenek2042fc12012-02-24 06:00:00 +0000952 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000953 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000954}
955
956void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremeneka301a672009-04-22 18:16:20 +0000958 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
959 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Ted Kremenek14856d72009-04-06 23:06:54 +0000961 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
962
963 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000964 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek14856d72009-04-06 23:06:54 +0000966 // Is the top location context the same as the one for the new location?
967 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000968 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000969 if (IsConsumedExpr(TopContextLoc) &&
970 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000971 TopContextLoc.markDead();
972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000974 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000975
976 return;
977 }
978
979 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000980 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000983 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000984 CLocs.push_back(ContextLocation(CLoc, true));
985 return;
986 }
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Ted Kremenek14856d72009-04-06 23:06:54 +0000989 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000990 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000991 }
992
993 // Context does not contain the location. Flush it.
994 popLocation();
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000997 // If we reach here, there is no enclosing context. Just add the edge.
998 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000999}
1000
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001001bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1002 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1003 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001005 return false;
1006}
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremeneke1baed32009-05-05 23:13:38 +00001008void EdgeBuilder::addExtendedContext(const Stmt *S) {
1009 if (!S)
1010 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
1012 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001013 while (Parent) {
1014 if (isa<CompoundStmt>(Parent))
1015 Parent = PDB.getParent(Parent);
1016 else
1017 break;
1018 }
1019
1020 if (Parent) {
1021 switch (Parent->getStmtClass()) {
1022 case Stmt::DoStmtClass:
1023 case Stmt::ObjCAtSynchronizedStmtClass:
1024 addContext(Parent);
1025 default:
1026 break;
1027 }
1028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremeneke1baed32009-05-05 23:13:38 +00001030 addContext(S);
1031}
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremenek14856d72009-04-06 23:06:54 +00001033void EdgeBuilder::addContext(const Stmt *S) {
1034 if (!S)
1035 return;
1036
Ted Kremenek59950d32012-02-24 07:12:52 +00001037 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremenek14856d72009-04-06 23:06:54 +00001039 while (!CLocs.empty()) {
1040 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1041
1042 // Is the top location context the same as the one for the new location?
1043 if (TopContextLoc == L)
1044 return;
1045
1046 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001047 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001048 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001049 }
1050
1051 // Context does not contain the location. Flush it.
1052 popLocation();
1053 }
1054
1055 CLocs.push_back(L);
1056}
1057
Ted Kremenek11abcec2012-05-02 00:31:29 +00001058// Cone-of-influence: support the reverse propagation of "interesting" symbols
1059// and values by tracing interesting calculations backwards through evaluated
1060// expressions along a path. This is probably overly complicated, but the idea
1061// is that if an expression computed an "interesting" value, the child
1062// expressions are are also likely to be "interesting" as well (which then
1063// propagates to the values they in turn compute). This reverse propagation
1064// is needed to track interesting correlations across function call boundaries,
1065// where formal arguments bind to actual arguments, etc. This is also needed
1066// because the constraint solver sometimes simplifies certain symbolic values
1067// into constants when appropriate, and this complicates reasoning about
1068// interesting values.
1069typedef llvm::DenseSet<const Expr *> InterestingExprs;
1070
1071static void reversePropagateIntererstingSymbols(BugReport &R,
1072 InterestingExprs &IE,
1073 const ProgramState *State,
1074 const Expr *Ex,
1075 const LocationContext *LCtx) {
1076 SVal V = State->getSVal(Ex, LCtx);
1077 if (!(R.isInteresting(V) || IE.count(Ex)))
1078 return;
1079
1080 switch (Ex->getStmtClass()) {
1081 default:
1082 if (!isa<CastExpr>(Ex))
1083 break;
1084 // Fall through.
1085 case Stmt::BinaryOperatorClass:
1086 case Stmt::UnaryOperatorClass: {
1087 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1088 CE = Ex->child_end();
1089 CI != CE; ++CI) {
1090 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1091 IE.insert(child);
1092 SVal ChildV = State->getSVal(child, LCtx);
1093 R.markInteresting(ChildV);
1094 }
1095 break;
1096 }
1097 }
1098 }
1099
1100 R.markInteresting(V);
1101}
1102
1103static void reversePropagateInterestingSymbols(BugReport &R,
1104 InterestingExprs &IE,
1105 const ProgramState *State,
1106 const LocationContext *CalleeCtx,
1107 const LocationContext *CallerCtx)
1108{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001109 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001110 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1111 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001112 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001113 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1114 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1115 PE = FD->param_end();
1116 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1117 for (; AI != AE && PI != PE; ++AI, ++PI) {
1118 if (const Expr *ArgE = *AI) {
1119 if (const ParmVarDecl *PD = *PI) {
1120 Loc LV = State->getLValue(PD, CalleeCtx);
1121 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1122 IE.insert(ArgE);
1123 }
1124 }
1125 }
1126 }
1127 }
1128}
1129
Ted Kremenek14856d72009-04-06 23:06:54 +00001130static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1131 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001132 const ExplodedNode *N,
1133 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001134 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001135 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001136 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001137 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001138
Ted Kremenek9c378f72011-08-12 23:37:29 +00001139 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001140 while (NextNode) {
1141 N = NextNode;
1142 NextNode = GetPredecessorNode(N);
1143 ProgramPoint P = N->getLocation();
1144
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001145 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001146 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1147 if (const Expr *Ex = PS->getStmtAs<Expr>())
1148 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1149 N->getState().getPtr(), Ex,
1150 N->getLocationContext());
1151 }
1152
Anna Zaks0b3ade82012-04-20 21:59:08 +00001153 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001154 const StackFrameContext *LCtx =
Jordan Rose852aa0d2012-07-10 22:07:52 +00001155 CE->getLocationContext()->getCurrentStackFrame();
1156 // FIXME: This needs to handle implicit calls.
1157 if (const Stmt *S = CE->getCalleeContext()->getCallSite()) {
1158 if (const Expr *Ex = dyn_cast<Expr>(S))
1159 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1160 N->getState().getPtr(), Ex,
1161 N->getLocationContext());
1162 PathDiagnosticLocation Loc(S,
1163 PDB.getSourceManager(),
1164 LCtx);
1165 EB.addEdge(Loc, true);
1166 EB.flushLocations();
1167 PathDiagnosticCallPiece *C =
1168 PathDiagnosticCallPiece::construct(N, *CE, SM);
1169 PD.getActivePath().push_front(C);
1170 PD.pushActivePath(&C->path);
1171 CallStack.push_back(StackDiagPair(C, N));
1172 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001173 break;
1174 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001175
Ted Kremenek2042fc12012-02-24 06:00:00 +00001176 // Pop the call hierarchy if we are done walking the contents
1177 // of a function call.
1178 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001179 // Add an edge to the start of the function.
1180 const Decl *D = CE->getCalleeContext()->getDecl();
1181 PathDiagnosticLocation pos =
1182 PathDiagnosticLocation::createBegin(D, SM);
1183 EB.addEdge(pos);
1184
1185 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001186 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001187 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001188 assert(!PD.getActivePath().empty());
1189 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001190
Ted Kremenek2042fc12012-02-24 06:00:00 +00001191 // The current active path should never be empty. Either we
1192 // just added a bunch of stuff to the top-level path, or
Anna Zaks0b3ade82012-04-20 21:59:08 +00001193 // we have a previous CallExitEnd. If the front of the active
Ted Kremenek2042fc12012-02-24 06:00:00 +00001194 // path is not a PathDiagnosticCallPiece, it means that the
1195 // path terminated within a function call. We must then take the
1196 // current contents of the active path and place it within
1197 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001198 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001199 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +00001200 if (!C) {
1201 const Decl * Caller = CE->getLocationContext()->getDecl();
1202 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1203 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001204
1205 // FIXME: We still need a location for implicit calls.
1206 if (CE->getCallExpr()) {
1207 C->setCallee(*CE, SM);
1208 EB.addContext(CE->getCallExpr());
1209 }
Anna Zaks368a0d52012-03-15 21:13:02 +00001210
1211 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001212 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001213 CallStack.pop_back();
1214 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001215 break;
1216 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001217
1218 // Note that is important that we update the LocationContext
1219 // after looking at CallExits. CallExit basically adds an
1220 // edge in the *caller*, so we don't want to update the LocationContext
1221 // too soon.
1222 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001223
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001224 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001225 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1226 // Does this represent entering a call? If so, look at propagating
1227 // interesting symbols across call boundaries.
1228 if (NextNode) {
1229 const LocationContext *CallerCtx = NextNode->getLocationContext();
1230 const LocationContext *CalleeCtx = PDB.LC;
1231 if (CallerCtx != CalleeCtx) {
1232 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1233 N->getState().getPtr(),
1234 CalleeCtx, CallerCtx);
1235 }
1236 }
1237
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001238 const CFGBlock &Blk = *BE->getSrc();
1239 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001241 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001242 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001243 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001244 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001246 if (!Term) {
1247 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1248 CS = dyn_cast<CompoundStmt>(FS->getBody());
1249 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001250 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001253 PathDiagnosticEventPiece *p =
1254 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001255 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001256 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001258 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001259 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001261 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001262 PathDiagnosticLocation BL =
1263 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001264 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001265 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001268 if (Term)
1269 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001271 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001272 }
1273
Mike Stump1eb44332009-09-09 15:08:12 +00001274 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001275 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1276 const Stmt *stmt = S->getStmt();
1277 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001278 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001279 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001280 }
1281 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001282 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001283 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001284
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001285 break;
1286 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001287
1288
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001289 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001291 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001292 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Anna Zaks8e6431a2011-08-18 22:37:56 +00001294 // Add pieces from custom visitors.
1295 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001296 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1297 E = visitors.end();
1298 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001299 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001300 const PathDiagnosticLocation &Loc = p->getLocation();
1301 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001302 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001303 updateStackPiecesWithMessage(p, CallStack);
1304
Ted Kremenek8966bc12009-05-06 21:39:49 +00001305 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001306 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001307 }
Mike Stump1eb44332009-09-09 15:08:12 +00001308 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001309 }
1310}
1311
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001312//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001313// Methods for BugType and subclasses.
1314//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001315BugType::~BugType() { }
1316
Ted Kremenekcf118d42009-02-04 23:49:09 +00001317void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001318
David Blaikie99ba9e32011-12-20 02:48:34 +00001319void BuiltinBug::anchor() {}
1320
Ted Kremenekcf118d42009-02-04 23:49:09 +00001321//===----------------------------------------------------------------------===//
1322// Methods for BugReport and subclasses.
1323//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001324
David Blaikie99ba9e32011-12-20 02:48:34 +00001325void BugReport::NodeResolver::anchor() {}
1326
Anna Zaks8e6431a2011-08-18 22:37:56 +00001327void BugReport::addVisitor(BugReporterVisitor* visitor) {
1328 if (!visitor)
1329 return;
1330
1331 llvm::FoldingSetNodeID ID;
1332 visitor->Profile(ID);
1333 void *InsertPos;
1334
1335 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1336 delete visitor;
1337 return;
1338 }
1339
1340 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001341 Callbacks.push_back(visitor);
1342 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001343}
1344
1345BugReport::~BugReport() {
1346 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001347 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001348 }
1349}
Anna Zakse172e8b2011-08-17 23:00:25 +00001350
Ted Kremenek07189522012-04-04 18:11:35 +00001351const Decl *BugReport::getDeclWithIssue() const {
1352 if (DeclWithIssue)
1353 return DeclWithIssue;
1354
1355 const ExplodedNode *N = getErrorNode();
1356 if (!N)
1357 return 0;
1358
1359 const LocationContext *LC = N->getLocationContext();
1360 return LC->getCurrentStackFrame()->getDecl();
1361}
1362
Anna Zakse172e8b2011-08-17 23:00:25 +00001363void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1364 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001365 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001366 if (UniqueingLocation.isValid()) {
1367 UniqueingLocation.Profile(hash);
1368 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001369 Location.Profile(hash);
1370 } else {
1371 assert(ErrorNode);
1372 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1373 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001374
1375 for (SmallVectorImpl<SourceRange>::const_iterator I =
1376 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1377 const SourceRange range = *I;
1378 if (!range.isValid())
1379 continue;
1380 hash.AddInteger(range.getBegin().getRawEncoding());
1381 hash.AddInteger(range.getEnd().getRawEncoding());
1382 }
1383}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001384
Ted Kremenek76aadc32012-03-09 01:13:14 +00001385void BugReport::markInteresting(SymbolRef sym) {
1386 if (!sym)
1387 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001388
1389 // If the symbol wasn't already in our set, note a configuration change.
1390 if (interestingSymbols.insert(sym).second)
1391 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001392
1393 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
1394 interestingRegions.insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001395}
1396
1397void BugReport::markInteresting(const MemRegion *R) {
1398 if (!R)
1399 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001400
1401 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001402 R = R->getBaseRegion();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001403 if (interestingRegions.insert(R).second)
1404 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001405
Ted Kremenek76aadc32012-03-09 01:13:14 +00001406 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1407 interestingSymbols.insert(SR->getSymbol());
1408}
1409
1410void BugReport::markInteresting(SVal V) {
1411 markInteresting(V.getAsRegion());
1412 markInteresting(V.getAsSymbol());
1413}
1414
1415bool BugReport::isInteresting(SVal V) const {
1416 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1417}
1418
1419bool BugReport::isInteresting(SymbolRef sym) const {
1420 if (!sym)
1421 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001422 // We don't currently consider metadata symbols to be interesting
1423 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenek76aadc32012-03-09 01:13:14 +00001424 return interestingSymbols.count(sym);
1425}
1426
1427bool BugReport::isInteresting(const MemRegion *R) const {
1428 if (!R)
1429 return false;
1430 R = R->getBaseRegion();
1431 bool b = interestingRegions.count(R);
1432 if (b)
1433 return true;
1434 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1435 return interestingSymbols.count(SR->getSymbol());
1436 return false;
1437}
1438
1439
Ted Kremenek9c378f72011-08-12 23:37:29 +00001440const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001441 if (!ErrorNode)
1442 return 0;
1443
Tom Care212f6d32010-09-16 03:50:38 +00001444 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001445 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Ted Kremenek9c378f72011-08-12 23:37:29 +00001447 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001448 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001449 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001450 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001451 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001452 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001453 S = GetStmt(ProgP);
1454
1455 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001456}
1457
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001458std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001459BugReport::getRanges() {
1460 // If no custom ranges, add the range of the statement corresponding to
1461 // the error node.
1462 if (Ranges.empty()) {
1463 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1464 addRange(E->getSourceRange());
1465 else
1466 return std::make_pair(ranges_iterator(), ranges_iterator());
1467 }
1468
Anna Zaks14924262011-08-24 20:31:06 +00001469 // User-specified absence of range info.
1470 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1471 return std::make_pair(ranges_iterator(), ranges_iterator());
1472
Anna Zakse172e8b2011-08-17 23:00:25 +00001473 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001474}
1475
Anna Zaks590dd8e2011-09-20 21:38:35 +00001476PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001477 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001478 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001479 "Either Location or ErrorNode should be specified but not both.");
1480
Ted Kremenek9c378f72011-08-12 23:37:29 +00001481 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001482 const LocationContext *LC = ErrorNode->getLocationContext();
1483
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001484 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001485 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001486 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001487 // For binary operators, return the location of the operator.
1488 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001489 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001490
Anna Zaks590dd8e2011-09-20 21:38:35 +00001491 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001492 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001493 } else {
1494 assert(Location.isValid());
1495 return Location;
1496 }
1497
Anna Zaks590dd8e2011-09-20 21:38:35 +00001498 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001499}
1500
Ted Kremenekcf118d42009-02-04 23:49:09 +00001501//===----------------------------------------------------------------------===//
1502// Methods for BugReporter and subclasses.
1503//===----------------------------------------------------------------------===//
1504
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001505BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001506GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001507BugReporterData::~BugReporterData() {}
1508
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001509ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001510
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001511ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001512GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1513
Anna Zaks3b030a22011-08-19 01:57:09 +00001514BugReporter::~BugReporter() {
1515 FlushReports();
1516
1517 // Free the bug reports we are tracking.
1518 typedef std::vector<BugReportEquivClass *> ContTy;
1519 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1520 I != E; ++I) {
1521 delete *I;
1522 }
1523}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001524
1525void BugReporter::FlushReports() {
1526 if (BugTypes.isEmpty())
1527 return;
1528
1529 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001530 // warnings and new BugTypes.
1531 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1532 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001533 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001534 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001535 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001536 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001537 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001538 const_cast<BugType*>(*I)->FlushReports(*this);
1539
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001540 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1541 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1542 BugReportEquivClass& EQ = *EI;
1543 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001544 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001545
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001546 // BugReporter owns and deletes only BugTypes created implicitly through
1547 // EmitBasicReport.
1548 // FIXME: There are leaks from checkers that assume that the BugTypes they
1549 // create will be destroyed by the BugReporter.
1550 for (llvm::StringMap<BugType*>::iterator
1551 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1552 delete I->second;
1553
Ted Kremenekcf118d42009-02-04 23:49:09 +00001554 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001555 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001556}
1557
1558//===----------------------------------------------------------------------===//
1559// PathDiagnostics generation.
1560//===----------------------------------------------------------------------===//
1561
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001562static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001563 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001564MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001565 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Ted Kremenekcf118d42009-02-04 23:49:09 +00001567 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001568 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001569 // error node unless there are two or more error nodes with the same minimum
1570 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001571 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001572 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001573
1574 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001575 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1576 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenekcf118d42009-02-04 23:49:09 +00001578 // Create owning pointers for GTrim and NMap just to ensure that they are
1579 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001580 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1581 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenekcf118d42009-02-04 23:49:09 +00001583 // Find the (first) error node in the trimmed graph. We just need to consult
1584 // the node map (NMap) which maps from nodes in the original graph to nodes
1585 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001586
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001587 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001588 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001589 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001590
Ted Kremenek40406fe2010-12-03 06:52:30 +00001591 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1592 const ExplodedNode *originalNode = nodes[nodeIndex];
1593 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001594 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001595 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001596 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek938332c2009-05-16 01:11:58 +00001599 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001600
1601 // Create a new (third!) graph with a single path. This is the graph
1602 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001603 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremenek10aa5542009-03-12 23:41:59 +00001605 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001606 // to the root node, and then construct a new graph that contains only
1607 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001608 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001610 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001611 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001613 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001614 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001615 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001617 if (Visited.find(Node) != Visited.end())
1618 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001620 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001622 if (Node->pred_empty()) {
1623 Root = Node;
1624 break;
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001627 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001628 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001629 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001630 }
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek938332c2009-05-16 01:11:58 +00001632 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenek10aa5542009-03-12 23:41:59 +00001634 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001635 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001636 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001637 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001638 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001640 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001641 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001642 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001643 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001645 // Create the equivalent node in the new graph with the same state
1646 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001647 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001649 // Store the mapping to the original node.
1650 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1651 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001652 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001654 // Link up the new node with the previous node.
1655 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001656 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001658 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001660 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001661 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001662 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001663 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001664 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001665 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001666 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001669 // Find the next successor node. We choose the node that is marked
1670 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001671 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1672 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001673 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001675 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001677 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001679 if (I == Visited.end())
1680 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001682 if (!N || I->second < MinVal) {
1683 N = *SI;
1684 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001685 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001686 }
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Ted Kremenek938332c2009-05-16 01:11:58 +00001688 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001689 }
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Ted Kremenek938332c2009-05-16 01:11:58 +00001691 assert(First);
1692
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001693 return std::make_pair(std::make_pair(GNew, BM),
1694 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001695}
1696
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001697/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1698/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001699static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001700 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1701 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001703 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001704 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001706 MacroStackTy MacroStack;
1707 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenek77d09442012-03-02 01:27:31 +00001709 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001710 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001711
1712 PathDiagnosticPiece *piece = I->getPtr();
1713
1714 // Recursively compact calls.
1715 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1716 CompactPathDiagnostic(call->path, SM);
1717 }
1718
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001719 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001720 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001722 // Determine the instantiation location, which is the location we group
1723 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001724 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001725 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001726 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001728 if (Loc.isFileID()) {
1729 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001730 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001731 continue;
1732 }
1733
1734 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001736 // Is the PathDiagnosticPiece within the same macro group?
1737 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001738 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001739 continue;
1740 }
1741
1742 // We aren't in the same group. Are we descending into a new macro
1743 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001744 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001745
1746 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001747 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001748 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001750 // Walk the entire macro stack.
1751 while (!MacroStack.empty()) {
1752 if (InstantiationLoc == MacroStack.back().second) {
1753 MacroGroup = MacroStack.back().first;
1754 break;
1755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001757 if (ParentInstantiationLoc == MacroStack.back().second) {
1758 MacroGroup = MacroStack.back().first;
1759 break;
1760 }
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001762 MacroStack.pop_back();
1763 }
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001765 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1766 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001767 PathDiagnosticMacroPiece *NewGroup =
1768 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001769 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001770
1771 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001772 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001773 else {
1774 assert(InstantiationLoc.isFileID());
1775 Pieces.push_back(NewGroup);
1776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001778 MacroGroup = NewGroup;
1779 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1780 }
1781
1782 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001783 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001786 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001787 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Ted Kremenek77d09442012-03-02 01:27:31 +00001789 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1790 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001791}
1792
Ted Kremenek7dc86642009-03-31 20:22:36 +00001793void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001794 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Ted Kremenek40406fe2010-12-03 06:52:30 +00001796 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001797 SmallVector<const ExplodedNode *, 10> errorNodes;
1798 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001799 E = bugReports.end(); I != E; ++I) {
1800 errorNodes.push_back((*I)->getErrorNode());
1801 }
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001803 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001804 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001805 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001806 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001807 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenekcf118d42009-02-04 23:49:09 +00001809 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001810 assert(GPair.second.second < bugReports.size());
1811 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001812 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001814 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1815 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001816 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001817
1818 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001819 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1820 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Anna Zaks8e6431a2011-08-18 22:37:56 +00001822 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001823 R->addVisitor(new NilReceiverBRVisitor());
1824 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001826 BugReport::VisitorList visitors;
1827 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001828
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001829 // While generating diagnostics, it's possible the visitors will decide
1830 // new symbols and regions are interesting, or add other visitors based on
1831 // the information they find. If they do, we need to regenerate the path
1832 // based on our new report configuration.
1833 do {
1834 // Get a clean copy of all the visitors.
1835 for (BugReport::visitor_iterator I = R->visitor_begin(),
1836 E = R->visitor_end(); I != E; ++I)
1837 visitors.push_back((*I)->clone());
1838
1839 // Clear out the active path from any previous work.
1840 PD.getActivePath().clear();
1841 originalReportConfigToken = R->getConfigurationChangeToken();
1842
1843 // Generate the very last diagnostic piece - the piece is visible before
1844 // the trace is expanded.
1845 PathDiagnosticPiece *LastPiece = 0;
1846 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1847 I != E; ++I) {
1848 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1849 assert (!LastPiece &&
1850 "There can only be one final piece in a diagnostic.");
1851 LastPiece = Piece;
1852 }
1853 }
1854 if (!LastPiece)
1855 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1856 if (LastPiece)
1857 PD.getActivePath().push_back(LastPiece);
1858 else
1859 return;
1860
1861 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001862 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001863 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001864 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001865 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001866 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001867 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001868 }
1869
1870 // Clean up the visitors we used.
1871 llvm::DeleteContainerPointers(visitors);
1872
1873 // Did anything change while generating this path?
1874 finalReportConfigToken = R->getConfigurationChangeToken();
1875 } while(finalReportConfigToken != originalReportConfigToken);
1876
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001877 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001878 if (R->shouldPrunePath()) {
1879 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1880 assert(hasSomethingInteresting);
1881 (void) hasSomethingInteresting;
1882 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001883}
1884
Ted Kremenekcf118d42009-02-04 23:49:09 +00001885void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001886 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001887}
1888
Mike Stump1eb44332009-09-09 15:08:12 +00001889void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001890 // Compute the bug report's hash to determine its equivalence class.
1891 llvm::FoldingSetNodeID ID;
1892 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001893
1894 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001895 BugType& BT = R->getBugType();
1896 Register(&BT);
1897 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001898 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Ted Kremenekcf118d42009-02-04 23:49:09 +00001900 if (!EQ) {
1901 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001902 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001903 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001904 }
1905 else
1906 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001907}
1908
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001909
1910//===----------------------------------------------------------------------===//
1911// Emitting reports in equivalence classes.
1912//===----------------------------------------------------------------------===//
1913
1914namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001915struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001916 const ExplodedNode *N;
1917 ExplodedNode::const_succ_iterator I, E;
1918
1919 FRIEC_WLItem(const ExplodedNode *n)
1920 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1921};
1922}
1923
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001924static BugReport *
1925FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001926 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001927
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001928 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1929 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001930 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001931
Ted Kremenek40406fe2010-12-03 06:52:30 +00001932 // If we don't need to suppress any of the nodes because they are
1933 // post-dominated by a sink, simply add all the nodes in the equivalence class
1934 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001935 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001936 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001937 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001938 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001939 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001940 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001941 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001942 }
1943 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001944 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001945 }
1946
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001947 // For bug reports that should be suppressed when all paths are post-dominated
1948 // by a sink node, iterate through the reports in the equivalence class
1949 // until we find one that isn't post-dominated (if one exists). We use a
1950 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1951 // this as a recursive function, but we don't want to risk blowing out the
1952 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001953 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001954
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001955 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001956 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001957
Ted Kremenek40406fe2010-12-03 06:52:30 +00001958 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001959 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001960 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001961 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001962 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001963 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001964 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001965 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001966 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001967 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001968 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001969 continue;
1970 }
1971
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001972 // At this point we know that 'N' is not a sink and it has at least one
1973 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1974 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001975 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001976 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1977
1978 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001979 WL.push_back(errorNode);
1980 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001981
1982 while (!WL.empty()) {
1983 WLItem &WI = WL.back();
1984 assert(!WI.N->succ_empty());
1985
1986 for (; WI.I != WI.E; ++WI.I) {
1987 const ExplodedNode *Succ = *WI.I;
1988 // End-of-path node?
1989 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001990 // If we found an end-of-path node that is not a sink.
1991 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001992 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001993 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001994 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001995 WL.clear();
1996 break;
1997 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001998 // Found a sink? Continue on to the next successor.
1999 continue;
2000 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002001 // Mark the successor as visited. If it hasn't been explored,
2002 // enqueue it to the DFS worklist.
2003 unsigned &mark = Visited[Succ];
2004 if (!mark) {
2005 mark = 1;
2006 WL.push_back(Succ);
2007 break;
2008 }
2009 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002010
2011 // The worklist may have been cleared at this point. First
2012 // check if it is empty before checking the last item.
2013 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002014 WL.pop_back();
2015 }
2016 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002017
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002018 // ExampleReport will be NULL if all the nodes in the equivalence class
2019 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002020 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002021}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002022
2023//===----------------------------------------------------------------------===//
2024// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
2025// uses global state, which eventually should go elsewhere.
2026//===----------------------------------------------------------------------===//
2027namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002028class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00002029 llvm::FoldingSetNodeID ID;
2030public:
2031 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00002032 R->Profile(ID);
2033 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00002034 }
2035
2036 void Profile(llvm::FoldingSetNodeID &id) {
2037 id = ID;
2038 }
2039
2040 llvm::FoldingSetNodeID &getID() { return ID; }
2041};
2042}
2043
2044static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
2045 // FIXME: Eventually this diagnostic cache should reside in something
2046 // like AnalysisManager instead of being a static variable. This is
2047 // really unsafe in the long term.
2048 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
2049 static DiagnosticCache DC;
2050
2051 void *InsertPos;
2052 DiagCacheItem *Item = new DiagCacheItem(R, PD);
2053
2054 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
2055 delete Item;
2056 return true;
2057 }
2058
2059 DC.InsertNode(Item, InsertPos);
2060 return false;
2061}
2062
Ted Kremenekcf118d42009-02-04 23:49:09 +00002063void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002064 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002065 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
2066 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002067 return;
2068
David Blaikieef3643f2011-09-26 00:51:36 +00002069 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Ted Kremenekcf118d42009-02-04 23:49:09 +00002071 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002072 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002073 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002075 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002076 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2077 exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00002078 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00002079 ? exampleReport->getDescription()
2080 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002081 BT.getCategory()));
2082
Ted Kremenek40406fe2010-12-03 06:52:30 +00002083 if (!bugReports.empty())
2084 GeneratePathDiagnostic(*D.get(), bugReports);
Ted Kremeneke0a58072009-09-18 22:37:37 +00002085
Ted Kremenek072192b2008-04-30 23:47:44 +00002086 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00002087 const BugReport::ExtraTextList &Meta =
2088 exampleReport->getExtraText();
2089 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2090 e = Meta.end(); i != e; ++i) {
2091 D->addMeta(*i);
2092 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002093
Ted Kremenek3148eb42009-01-24 00:55:43 +00002094 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002095 BugReport::ranges_iterator Beg, End;
2096 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00002097 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00002098
Ted Kremenek88fc1812012-04-04 00:55:29 +00002099 if (!IsCachedDiagnostic(exampleReport, D.get())) {
2100 // Search the description for '%', as that will be interpretted as a
2101 // format character by FormatDiagnostics.
2102 StringRef desc = exampleReport->getShortDescription();
2103
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002104 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00002105 llvm::raw_svector_ostream Out(TmpStr);
Ted Kremenek88fc1812012-04-04 00:55:29 +00002106 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I) {
Ted Kremenekc213b482010-01-15 07:56:51 +00002107 if (*I == '%')
2108 Out << "%%";
2109 else
2110 Out << *I;
Ted Kremenek88fc1812012-04-04 00:55:29 +00002111 }
Ted Kremenekc213b482010-01-15 07:56:51 +00002112
2113 Out.flush();
Ted Kremenek88fc1812012-04-04 00:55:29 +00002114 unsigned ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenek57202072008-07-14 17:40:50 +00002115
Anna Zaks590dd8e2011-09-20 21:38:35 +00002116 DiagnosticBuilder diagBuilder = Diag.Report(
2117 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002118 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002119 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00002120 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00002121
David Blaikieef3643f2011-09-26 00:51:36 +00002122 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002123 if (!PD)
2124 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Ted Kremenek802e0242012-02-08 04:32:34 +00002126 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002127 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2128 exampleReport->getLocation(getSourceManager()),
2129 exampleReport->getDescription());
Ted Kremenek07189522012-04-04 18:11:35 +00002130 for ( ; Beg != End; ++Beg)
2131 piece->addRange(*Beg);
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002132
Ted Kremenek2042fc12012-02-24 06:00:00 +00002133 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002134 }
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Ted Kremenek3148eb42009-01-24 00:55:43 +00002136 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002137}
Ted Kremenek57202072008-07-14 17:40:50 +00002138
Ted Kremenek07189522012-04-04 18:11:35 +00002139void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002140 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002141 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002142 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002143 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002145 // 'BT' is owned by BugReporter.
2146 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002147 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002148 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002149 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2150 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002151}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002152
Chris Lattner5f9e2722011-07-23 10:55:15 +00002153BugType *BugReporter::getBugTypeForName(StringRef name,
2154 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002155 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002156 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2157 llvm::StringMapEntry<BugType *> &
2158 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2159 BugType *BT = entry.getValue();
2160 if (!BT) {
2161 BT = new BugType(name, category);
2162 entry.setValue(BT);
2163 }
2164 return BT;
2165}