blob: cccd71bd9677dfd3726eba9c2844784e9237d729 [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
11// PathDiagnostics for analyses based on GRSimpleVals.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/CFG.h"
21#include "clang/AST/Expr.h"
22#include "clang/Analysis/ProgramPoint.h"
23#include "clang/Analysis/PathDiagnostic.h"
24#include <sstream>
25
26using namespace clang;
27
28BugReporter::~BugReporter() {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000029BugType::~BugType() {}
30BugReport::~BugReport() {}
Ted Kremenek5e55cda2008-04-11 18:40:29 +000031RangedBugReport::~RangedBugReport() {}
32
Ted Kremenek1aa44c72008-05-22 23:45:19 +000033ExplodedGraph<ValueState>& BugReporter::getGraph() {
34 return Eng.getGraph();
35}
36
37ValueStateManager& BugReporter::getStateManager() {
38 return Eng.getStateManager();
39}
Ted Kremenek61f3e052008-04-03 04:42:52 +000040
41static inline Stmt* GetStmt(const ProgramPoint& P) {
42 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
43 return PS->getStmt();
44 }
45 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
46 return BE->getSrc()->getTerminator();
47 }
48 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
49 return BE->getFirstStmt();
50 }
51
52 assert (false && "Unsupported ProgramPoint.");
53 return NULL;
54}
55
Ted Kremenek706e3cf2008-04-07 23:35:17 +000056static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000057 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000058 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059 else
60 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000061}
62
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063static inline ExplodedNode<ValueState>*
64GetNextNode(ExplodedNode<ValueState>* N) {
65 return N->pred_empty() ? NULL : *(N->pred_begin());
66}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000067
Ted Kremenekbd7efa82008-04-17 23:44:37 +000068static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) {
69 assert (isa<BlockEntrance>(N->getLocation()));
70
71 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
72
73 ProgramPoint P = N->getLocation();
74
75 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
76 return PS->getStmt();
77 }
78
79 return NULL;
80}
81
Ted Kremenek143ca222008-05-06 18:11:09 +000082static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
83 Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000084
85 if (!S)
86 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000087
88 // Slow, but probably doesn't matter.
89 if (os.str().empty())
90 os << ' ';
91
92 os << "Execution continues on line "
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000093 << SMgr.getLogicalLineNumber(S->getLocStart()) << '.';
94}
Ted Kremenek143ca222008-05-06 18:11:09 +000095
96
97static inline void ExecutionContinues(std::ostringstream& os,
98 SourceManager& SMgr,
99 ExplodedNode<ValueState>* N) {
100
101 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
102}
103
104static inline void ExecutionContinues(std::ostringstream& os,
105 SourceManager& SMgr,
106 const CFGBlock* B) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000107
Ted Kremenek143ca222008-05-06 18:11:09 +0000108 ExecutionContinues(os, SMgr, GetStmt(B));
109}
110
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000111
112Stmt* BugReport::getStmt(BugReporter& BR) const {
113
Ted Kremenek200ed922008-05-02 23:21:21 +0000114 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000115 Stmt *S = NULL;
116
117 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
118 if (BE->getBlock() == &BR.getCFG().getExit())
Ted Kremenek200ed922008-05-02 23:21:21 +0000119 S = GetLastStmt(EndNode);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000120 if (!S)
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000121 S = GetStmt(ProgP);
122
123 return S;
124}
125
126PathDiagnosticPiece*
127BugReport::getEndPath(BugReporter& BR,
128 ExplodedNode<ValueState>* EndPathNode) {
129
130 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000131
132 if (!S)
133 return NULL;
134
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000135 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
136 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000137
Ted Kremenekde7161f2008-04-03 18:00:37 +0000138 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000139 getRanges(BR, Beg, End);
140
141 for (; Beg != End; ++Beg)
142 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000143
144 return P;
145}
146
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000147void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
148 const SourceRange*& end) {
149
150 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
151 R = E->getSourceRange();
152 beg = &R;
153 end = beg+1;
154 }
155 else
156 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000157}
158
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000159FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
160
Ted Kremenek200ed922008-05-02 23:21:21 +0000161 if (!EndNode)
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000162 return FullSourceLoc();
163
Ted Kremenek200ed922008-05-02 23:21:21 +0000164 Stmt* S = GetStmt(EndNode->getLocation());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000165
166 if (!S)
167 return FullSourceLoc();
168
169 return FullSourceLoc(S->getLocStart(), Mgr);
170}
171
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000172PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
173 ExplodedNode<ValueState>* PrevN,
174 ExplodedGraph<ValueState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000175 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 return NULL;
177}
178
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000179static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*>
180MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000181
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000182 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1));
183
Ted Kremenek910e9992008-04-25 01:25:15 +0000184 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000185
Ted Kremenek910e9992008-04-25 01:25:15 +0000186 ExplodedNode<ValueState>* NOld = N;
187 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000188
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000189 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000190 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000191
Ted Kremenek910e9992008-04-25 01:25:15 +0000192 if (I->getState() == NOld->getState() &&
193 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000194 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000195 break;
196 }
197 }
198
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000199 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000200
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000201 // Create a new graph with a single path.
202
203 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
204 GTrim->getContext());
205
206
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000207 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000208
209 while (N) {
210 ExplodedNode<ValueState>* NewN =
211 G->getNode(N->getLocation(), N->getState());
212
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000213 if (!First) First = NewN;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000214 if (Last) Last->addPredecessor(NewN);
215
216 Last = NewN;
217 N = N->pred_empty() ? 0 : *(N->pred_begin());
218 }
219
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000220 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000221}
222
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000223static VarDecl* GetMostRecentVarDeclBinding(ExplodedNode<ValueState>* N,
224 ValueStateManager& VMgr,
225 RVal X) {
226
227 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
228
229 ProgramPoint P = N->getLocation();
230
231 if (!isa<PostStmt>(P))
232 continue;
233
234 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
235
236 if (!DR)
237 continue;
238
239 RVal Y = VMgr.GetRVal(N->getState(), DR);
240
241 if (X != Y)
242 continue;
243
244 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
245
246 if (!VD)
247 continue;
248
249 return VD;
250 }
251
252 return 0;
253}
254
255
256static void HandleNotableSymbol(ExplodedNode<ValueState>* N, Stmt* S,
257 SymbolID Sym, BugReporter& BR,
258 PathDiagnostic& PD) {
259
260 ExplodedNode<ValueState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
261 ValueState* PrevSt = Pred ? Pred->getState() : 0;
262
263 if (!PrevSt)
264 return;
265
266 // Look at the variable bindings of the current state that map to the
267 // specified symbol. Are any of them not in the previous state.
268
269 ValueState* St = N->getState();
270 ValueStateManager& VMgr = BR.getStateManager();
271
272 // FIXME: Later generalize for a broader memory model.
273
274 // FIXME: This is quadratic, since its nested in another loop. Probably
275 // doesn't matter, but keep an eye out for performance issues. It's
276 // also a bunch of copy-paste. Bad. Cleanup later.
277
278 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
279
280 RVal V = I.getData();
281 SymbolID ScanSym;
282
283 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
284 ScanSym = SV->getSymbol();
285 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
286 ScanSym = SV->getSymbol();
287 else
288 continue;
289
290 if (ScanSym != Sym)
291 continue;
292
293 // Check if the previous state has this binding.
294
295 RVal X = VMgr.GetRVal(PrevSt, lval::DeclVal(I.getKey()));
296
297 if (X == V) // Same binding?
298 continue;
299
300 // Different binding. Only handle assignments for now. We don't pull
301 // this check out of the loop because we will eventually handle other
302 // cases.
303
304 VarDecl *VD = 0;
305
306 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
307 if (!B->isAssignmentOp())
308 continue;
309
310 // What variable did we assign to?
311 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
312
313 if (!DR)
314 continue;
315
316 VD = dyn_cast<VarDecl>(DR->getDecl());
317 }
318 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
319 VD = dyn_cast<VarDecl>(DS->getDecl());
320
321 if (!VD)
322 continue;
323
324 // What is the most recently referenced variable with this binding?
325 VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
326
327 if (!MostRecent)
328 continue;
329
330 // Create the diagnostic.
331
332 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
333
334 if (VD->getType()->isPointerLikeType()) {
335 std::string msg = "'" + std::string(VD->getName()) +
336 "' now aliases '" + MostRecent->getName() + "'";
337
338 PD.push_front(new PathDiagnosticPiece(L, msg));
339 }
340 }
341}
342
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000343void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
344 BugReport& R) {
345
346 ExplodedNode<ValueState>* N = R.getEndNode();
347
348 if (!N) return;
349
350 // Construct a new graph that contains only a single path from the error
351 // node to a root.
352
353 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
354 GPair = MakeReportGraph(&getGraph(), N);
355
356 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
357 assert(GPair.second->getLocation() == N->getLocation());
358 N = GPair.second;
359
360 // Start building the path diagnostic...
361
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000362 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
363 PD.push_back(Piece);
364 else
365 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000366
367 ExplodedNode<ValueState>* NextNode = N->pred_empty()
368 ? NULL : *(N->pred_begin());
369
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000370 SourceManager& SMgr = Ctx.getSourceManager();
371
Ted Kremenek6837faa2008-04-09 00:20:43 +0000372 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000373
374 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000375 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000376 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000377
378 ProgramPoint P = N->getLocation();
379
380 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
381
382 CFGBlock* Src = BE->getSrc();
383 CFGBlock* Dst = BE->getDst();
384
385 Stmt* T = Src->getTerminator();
386
387 if (!T)
388 continue;
389
390 FullSourceLoc L(T->getLocStart(), SMgr);
391
392 switch (T->getStmtClass()) {
393 default:
394 break;
395
396 case Stmt::GotoStmtClass:
397 case Stmt::IndirectGotoStmtClass: {
398
399 Stmt* S = GetStmt(LastNode->getLocation());
400
401 if (!S)
402 continue;
403
404 std::ostringstream os;
405
406 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000407 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000408
409 PD.push_front(new PathDiagnosticPiece(L, os.str()));
410 break;
411 }
412
413 case Stmt::SwitchStmtClass: {
414
415 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000416
Ted Kremenek61f3e052008-04-03 04:42:52 +0000417 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000418
419 if (Stmt* S = Dst->getLabel())
420 switch (S->getStmtClass()) {
421
Ted Kremenek61f3e052008-04-03 04:42:52 +0000422 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000423 assert(false && "Not a valid switch label.");
424 continue;
425
Ted Kremenek8b904552008-04-22 22:29:46 +0000426 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000427
428 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000429 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000430
431 break;
432 }
433
434 case Stmt::CaseStmtClass: {
435
436 os << "Control jumps to 'case ";
437
Ted Kremenek5a429952008-04-23 23:35:07 +0000438 CaseStmt* Case = cast<CaseStmt>(S);
439 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000440
Ted Kremenek5a429952008-04-23 23:35:07 +0000441 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000442
Ted Kremenek5a429952008-04-23 23:35:07 +0000443 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000444
Ted Kremenek5a429952008-04-23 23:35:07 +0000445 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
446
447 // FIXME: Maybe this should be an assertion. Are there cases
448 // were it is not an EnumConstantDecl?
449
450 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
451
452 if (D) {
453 GetRawInt = false;
454 os << D->getName();
455 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000456 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000457
458 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000459
Ted Kremenek5a429952008-04-23 23:35:07 +0000460 // Not an enum.
461 Expr* CondE = cast<SwitchStmt>(T)->getCond();
462 unsigned bits = Ctx.getTypeSize(CondE->getType());
463 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000464
Ted Kremenek5a429952008-04-23 23:35:07 +0000465 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
466 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000467 continue;
468 }
469
Ted Kremenek5a429952008-04-23 23:35:07 +0000470 os << V.toString();
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000471 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000472
Ted Kremenek61f3e052008-04-03 04:42:52 +0000473 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000474 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000475
476 break;
477
478 }
479 }
Ted Kremenek56783922008-04-25 01:29:56 +0000480 else {
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000481 os << "'Default' branch taken.";
482 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000483 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000484
485 PD.push_front(new PathDiagnosticPiece(L, os.str()));
486 break;
487 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000488
489 case Stmt::BreakStmtClass:
490 case Stmt::ContinueStmtClass: {
491 std::ostringstream os;
492 ExecutionContinues(os, SMgr, LastNode);
493 PD.push_front(new PathDiagnosticPiece(L, os.str()));
494 break;
495 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000496
497 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000498
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000499 std::ostringstream os;
500 os << "'?' condition evaluates to ";
501
502 if (*(Src->succ_begin()+1) == Dst)
503 os << "false.";
504 else
505 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000506
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000507 PD.push_front(new PathDiagnosticPiece(L, os.str()));
508
509 break;
510 }
511
512 case Stmt::DoStmtClass: {
513
514 if (*(Src->succ_begin()) == Dst) {
515
516 std::ostringstream os;
517
Ted Kremenek143ca222008-05-06 18:11:09 +0000518 os << "Loop condition is true.";
519 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000520
521 PD.push_front(new PathDiagnosticPiece(L, os.str()));
522 }
523 else
524 PD.push_front(new PathDiagnosticPiece(L,
525 "Loop condition is false. Exiting loop."));
526
527 break;
528 }
529
Ted Kremenek61f3e052008-04-03 04:42:52 +0000530 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000531 case Stmt::ForStmtClass: {
532
533 if (*(Src->succ_begin()+1) == Dst) {
534
535 std::ostringstream os;
536
Ted Kremenek143ca222008-05-06 18:11:09 +0000537 os << "Loop condition is false.";
538 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000539
540 PD.push_front(new PathDiagnosticPiece(L, os.str()));
541 }
542 else
543 PD.push_front(new PathDiagnosticPiece(L,
544 "Loop condition is true. Entering loop body."));
545
546 break;
547 }
548
Ted Kremenek61f3e052008-04-03 04:42:52 +0000549 case Stmt::IfStmtClass: {
550
551 if (*(Src->succ_begin()+1) == Dst)
552 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
553 else
554 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
555
556 break;
557 }
558 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000559 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000560
561 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000562 PD.push_front(p);
563
564 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
565
566 ValueState* St = N->getState();
567
568 // Scan the lval bindings, and see if a "notable" symbol has a new
569 // lval binding.
570
571 // FIXME: In the future, when we generalize the memory model, we'll
572 // need a way to iterate over binded locations.
573
574 llvm::SmallSet<SymbolID, 10> AlreadyProcessed;
575
576 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
577
578 RVal V = I.getData();
579 SymbolID ScanSym;
580
581 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
582 ScanSym = SV->getSymbol();
583 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
584 ScanSym = SV->getSymbol();
585 else
586 continue;
587
588 assert (ScanSym.isInitialized());
589
590 if (!isNotable(ScanSym))
591 continue;
592
593 if (AlreadyProcessed.count(ScanSym))
594 continue;
595
596 AlreadyProcessed.insert(ScanSym);
597
598 HandleNotableSymbol(N, PS->getStmt(), ScanSym, *this, PD);
599 }
600 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000601 }
602}
603
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000604
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000605bool BugTypeCacheLocation::isCached(BugReport& R) {
606
607 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000608
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000609 if (!N)
610 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000611
612 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000613 // warning for the same error type that occurs at the same program
614 // location but along a different path.
615
Ted Kremenek76d90c82008-05-16 18:33:14 +0000616 return isCached(N->getLocation());
617}
618
619bool BugTypeCacheLocation::isCached(ProgramPoint P) {
620
621 void* p = P.getRawData();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000622
623 if (CachedErrors.count(p))
624 return true;
625
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000626 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000627 return false;
628}
629
Ted Kremenek75840e12008-04-18 01:56:37 +0000630void BugReporter::EmitWarning(BugReport& R) {
631
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000632 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000633 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000634
Ted Kremenek55851142008-04-22 16:15:03 +0000635 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
636 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek072192b2008-04-30 23:47:44 +0000637
638 // Get the meta data.
639
640 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
641
642 for (const char** s = Meta.first; s != Meta.second; ++s)
643 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000644
645 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000646
Ted Kremenek55851142008-04-22 16:15:03 +0000647 if (PD && !D->empty()) {
648 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000649 return;
650 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000651
Ted Kremenek75840e12008-04-18 01:56:37 +0000652 // We don't have a PathDiagnosticClient, but we can still emit a single
653 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000654
Ted Kremenek55851142008-04-22 16:15:03 +0000655 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
656 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000657
658
659 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000660
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000661 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000662
Ted Kremenek55851142008-04-22 16:15:03 +0000663 if (!D->empty()) {
664 Beg = D->back()->ranges_begin();
665 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000666 }
667 else
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000668 R.getRanges(*this, Beg, End);
Ted Kremenek75840e12008-04-18 01:56:37 +0000669
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000670 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000671 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
672
673 for ( ; Beg != End; ++Beg)
674 piece->addRange(*Beg);
675
Ted Kremenek55851142008-04-22 16:15:03 +0000676 D->push_back(piece);
677 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000678 }
679 else {
680 std::ostringstream os;
681 os << "[CHECKER] ";
682
Ted Kremenek55851142008-04-22 16:15:03 +0000683 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000684 os << R.getDescription();
685 else
Ted Kremenek55851142008-04-22 16:15:03 +0000686 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000687
688
689 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
690 os.str().c_str());
691
692 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
693 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000694}