blob: 240ecd19fed3db0e0fa9ee62e13c9527c8cd4586 [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"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include <sstream>
26
27using namespace clang;
28
29BugReporter::~BugReporter() {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000030BugType::~BugType() {}
31BugReport::~BugReport() {}
Ted Kremenek5e55cda2008-04-11 18:40:29 +000032RangedBugReport::~RangedBugReport() {}
33
Ted Kremenek1aa44c72008-05-22 23:45:19 +000034ExplodedGraph<ValueState>& BugReporter::getGraph() {
35 return Eng.getGraph();
36}
37
38ValueStateManager& BugReporter::getStateManager() {
39 return Eng.getStateManager();
40}
Ted Kremenek61f3e052008-04-03 04:42:52 +000041
42static inline Stmt* GetStmt(const ProgramPoint& P) {
43 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
44 return PS->getStmt();
45 }
46 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
47 return BE->getSrc()->getTerminator();
48 }
49 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
50 return BE->getFirstStmt();
51 }
52
53 assert (false && "Unsupported ProgramPoint.");
54 return NULL;
55}
56
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000058 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000060 else
61 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000062}
63
Ted Kremenekbd7efa82008-04-17 23:44:37 +000064static inline ExplodedNode<ValueState>*
65GetNextNode(ExplodedNode<ValueState>* N) {
66 return N->pred_empty() ? NULL : *(N->pred_begin());
67}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000068
Ted Kremenekbd7efa82008-04-17 23:44:37 +000069static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) {
70 assert (isa<BlockEntrance>(N->getLocation()));
71
72 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
73
74 ProgramPoint P = N->getLocation();
75
76 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
77 return PS->getStmt();
78 }
79
80 return NULL;
81}
82
Ted Kremenek143ca222008-05-06 18:11:09 +000083static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
84 Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000085
86 if (!S)
87 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000088
89 // Slow, but probably doesn't matter.
90 if (os.str().empty())
91 os << ' ';
92
93 os << "Execution continues on line "
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000094 << SMgr.getLogicalLineNumber(S->getLocStart()) << '.';
95}
Ted Kremenek143ca222008-05-06 18:11:09 +000096
97
98static inline void ExecutionContinues(std::ostringstream& os,
99 SourceManager& SMgr,
100 ExplodedNode<ValueState>* N) {
101
102 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
103}
104
105static inline void ExecutionContinues(std::ostringstream& os,
106 SourceManager& SMgr,
107 const CFGBlock* B) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000108
Ted Kremenek143ca222008-05-06 18:11:09 +0000109 ExecutionContinues(os, SMgr, GetStmt(B));
110}
111
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000112
113Stmt* BugReport::getStmt(BugReporter& BR) const {
114
Ted Kremenek200ed922008-05-02 23:21:21 +0000115 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000116 Stmt *S = NULL;
117
118 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
119 if (BE->getBlock() == &BR.getCFG().getExit())
Ted Kremenek200ed922008-05-02 23:21:21 +0000120 S = GetLastStmt(EndNode);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000121 if (!S)
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000122 S = GetStmt(ProgP);
123
124 return S;
125}
126
127PathDiagnosticPiece*
128BugReport::getEndPath(BugReporter& BR,
129 ExplodedNode<ValueState>* EndPathNode) {
130
131 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000132
133 if (!S)
134 return NULL;
135
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000136 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
137 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000138
Ted Kremenekde7161f2008-04-03 18:00:37 +0000139 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000140 getRanges(BR, Beg, End);
141
142 for (; Beg != End; ++Beg)
143 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000144
145 return P;
146}
147
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000148void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
149 const SourceRange*& end) {
150
151 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
152 R = E->getSourceRange();
153 beg = &R;
154 end = beg+1;
155 }
156 else
157 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000158}
159
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000160FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
161
Ted Kremenek200ed922008-05-02 23:21:21 +0000162 if (!EndNode)
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000163 return FullSourceLoc();
164
Ted Kremenek200ed922008-05-02 23:21:21 +0000165 Stmt* S = GetStmt(EndNode->getLocation());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000166
167 if (!S)
168 return FullSourceLoc();
169
170 return FullSourceLoc(S->getLocStart(), Mgr);
171}
172
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000173PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
174 ExplodedNode<ValueState>* PrevN,
175 ExplodedGraph<ValueState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000176 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000177 return NULL;
178}
179
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000180static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*>
181MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000182
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000183 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1));
184
Ted Kremenek910e9992008-04-25 01:25:15 +0000185 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000186
Ted Kremenek910e9992008-04-25 01:25:15 +0000187 ExplodedNode<ValueState>* NOld = N;
188 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000189
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000191 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000192
Ted Kremenek910e9992008-04-25 01:25:15 +0000193 if (I->getState() == NOld->getState() &&
194 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000195 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000196 break;
197 }
198 }
199
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000200 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000201
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000202 // Create a new graph with a single path.
203
204 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
205 GTrim->getContext());
206
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000207 // Sometimes TrimGraph can contain a cycle. Perform a reverse DFS
208 // to the root node, and then construct a new graph that contains only
209 // a single path.
210 llvm::DenseMap<void*,unsigned> Visited;
211 llvm::SmallVector<ExplodedNode<ValueState>*, 10> WS;
212 WS.push_back(N);
213 unsigned cnt = 0;
214 ExplodedNode<ValueState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000215
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000216 while (!WS.empty()) {
217 ExplodedNode<ValueState>* Node = WS.back();
218 WS.pop_back();
219
220 if (Visited.find(Node) != Visited.end())
221 continue;
222
223 Visited[Node] = cnt++;
224
225 if (Node->pred_empty()) {
226 Root = Node;
227 break;
228 }
229
230 for (ExplodedNode<ValueState>::pred_iterator I=Node->pred_begin(),
231 E=Node->pred_end(); I!=E; ++I)
232 WS.push_back(*I);
233 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000234
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000235 assert (Root);
236
237 // Now walk from the root down the DFS path, always taking the successor
238 // with the lowest number.
239 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000240
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000241 for ( N = Root ;;) {
242
243 // Lookup the number associated with the current node.
244 llvm::DenseMap<void*,unsigned>::iterator I=Visited.find(N);
245 assert (I != Visited.end());
246
247 // Create the equivalent node in the new graph with the same state
248 // and location.
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000249 ExplodedNode<ValueState>* NewN =
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000250 G->getNode(N->getLocation(), N->getState());
251
252 // Link up the new node with the previous node.
253 if (Last)
254 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000255
256 Last = NewN;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000257
258 // Are we at the final node?
259 if (I->second == 0) {
260 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000261 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000262 }
263
264 // Find the next successor node. We choose the node that is marked
265 // with the lowest DFS number.
266 ExplodedNode<ValueState>::succ_iterator SI = N->succ_begin();
267 ExplodedNode<ValueState>::succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000268 N = 0;
269
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000270 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekc1da4412008-06-17 19:14:06 +0000271
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000272 I = Visited.find(*SI);
273
274 if (I == Visited.end())
275 continue;
276
277 if (!N || I->second < MinVal) {
278 N = *SI;
279 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000280 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000281 }
282
283 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000284 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000285
286 assert (First);
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000287 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000288}
289
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000290static VarDecl* GetMostRecentVarDeclBinding(ExplodedNode<ValueState>* N,
291 ValueStateManager& VMgr,
292 RVal X) {
293
294 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
295
296 ProgramPoint P = N->getLocation();
297
298 if (!isa<PostStmt>(P))
299 continue;
300
301 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
302
303 if (!DR)
304 continue;
305
306 RVal Y = VMgr.GetRVal(N->getState(), DR);
307
308 if (X != Y)
309 continue;
310
311 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
312
313 if (!VD)
314 continue;
315
316 return VD;
317 }
318
319 return 0;
320}
321
322
323static void HandleNotableSymbol(ExplodedNode<ValueState>* N, Stmt* S,
324 SymbolID Sym, BugReporter& BR,
325 PathDiagnostic& PD) {
326
327 ExplodedNode<ValueState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
328 ValueState* PrevSt = Pred ? Pred->getState() : 0;
329
330 if (!PrevSt)
331 return;
332
333 // Look at the variable bindings of the current state that map to the
334 // specified symbol. Are any of them not in the previous state.
335
336 ValueState* St = N->getState();
337 ValueStateManager& VMgr = BR.getStateManager();
338
339 // FIXME: Later generalize for a broader memory model.
340
341 // FIXME: This is quadratic, since its nested in another loop. Probably
342 // doesn't matter, but keep an eye out for performance issues. It's
343 // also a bunch of copy-paste. Bad. Cleanup later.
344
345 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
346
347 RVal V = I.getData();
348 SymbolID ScanSym;
349
350 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
351 ScanSym = SV->getSymbol();
352 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
353 ScanSym = SV->getSymbol();
354 else
355 continue;
356
357 if (ScanSym != Sym)
358 continue;
359
360 // Check if the previous state has this binding.
361
362 RVal X = VMgr.GetRVal(PrevSt, lval::DeclVal(I.getKey()));
363
364 if (X == V) // Same binding?
365 continue;
366
367 // Different binding. Only handle assignments for now. We don't pull
368 // this check out of the loop because we will eventually handle other
369 // cases.
370
371 VarDecl *VD = 0;
372
373 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
374 if (!B->isAssignmentOp())
375 continue;
376
377 // What variable did we assign to?
378 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
379
380 if (!DR)
381 continue;
382
383 VD = dyn_cast<VarDecl>(DR->getDecl());
384 }
385 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
386 VD = dyn_cast<VarDecl>(DS->getDecl());
387
388 if (!VD)
389 continue;
390
391 // What is the most recently referenced variable with this binding?
392 VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
393
394 if (!MostRecent)
395 continue;
396
397 // Create the diagnostic.
398
399 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
400
401 if (VD->getType()->isPointerLikeType()) {
402 std::string msg = "'" + std::string(VD->getName()) +
403 "' now aliases '" + MostRecent->getName() + "'";
404
405 PD.push_front(new PathDiagnosticPiece(L, msg));
406 }
407 }
408}
409
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000410void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
411 BugReport& R) {
412
413 ExplodedNode<ValueState>* N = R.getEndNode();
414
415 if (!N) return;
416
417 // Construct a new graph that contains only a single path from the error
418 // node to a root.
419
420 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
421 GPair = MakeReportGraph(&getGraph(), N);
422
423 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
424 assert(GPair.second->getLocation() == N->getLocation());
425 N = GPair.second;
426
427 // Start building the path diagnostic...
428
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000429 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
430 PD.push_back(Piece);
431 else
432 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000433
434 ExplodedNode<ValueState>* NextNode = N->pred_empty()
435 ? NULL : *(N->pred_begin());
436
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000437 SourceManager& SMgr = Ctx.getSourceManager();
438
Ted Kremenek6837faa2008-04-09 00:20:43 +0000439 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000440
441 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000442 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000443 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000444
445 ProgramPoint P = N->getLocation();
446
447 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
448
449 CFGBlock* Src = BE->getSrc();
450 CFGBlock* Dst = BE->getDst();
451
452 Stmt* T = Src->getTerminator();
453
454 if (!T)
455 continue;
456
457 FullSourceLoc L(T->getLocStart(), SMgr);
458
459 switch (T->getStmtClass()) {
460 default:
461 break;
462
463 case Stmt::GotoStmtClass:
464 case Stmt::IndirectGotoStmtClass: {
465
466 Stmt* S = GetStmt(LastNode->getLocation());
467
468 if (!S)
469 continue;
470
471 std::ostringstream os;
472
473 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000474 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000475
476 PD.push_front(new PathDiagnosticPiece(L, os.str()));
477 break;
478 }
479
480 case Stmt::SwitchStmtClass: {
481
482 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000483
Ted Kremenek61f3e052008-04-03 04:42:52 +0000484 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000485
486 if (Stmt* S = Dst->getLabel())
487 switch (S->getStmtClass()) {
488
Ted Kremenek61f3e052008-04-03 04:42:52 +0000489 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000490 assert(false && "Not a valid switch label.");
491 continue;
492
Ted Kremenek8b904552008-04-22 22:29:46 +0000493 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000494
495 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000496 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000497
498 break;
499 }
500
501 case Stmt::CaseStmtClass: {
502
503 os << "Control jumps to 'case ";
504
Ted Kremenek5a429952008-04-23 23:35:07 +0000505 CaseStmt* Case = cast<CaseStmt>(S);
506 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000507
Ted Kremenek5a429952008-04-23 23:35:07 +0000508 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000509
Ted Kremenek5a429952008-04-23 23:35:07 +0000510 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000511
Ted Kremenek5a429952008-04-23 23:35:07 +0000512 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
513
514 // FIXME: Maybe this should be an assertion. Are there cases
515 // were it is not an EnumConstantDecl?
516
517 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
518
519 if (D) {
520 GetRawInt = false;
521 os << D->getName();
522 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000523 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000524
525 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000526
Ted Kremenek5a429952008-04-23 23:35:07 +0000527 // Not an enum.
528 Expr* CondE = cast<SwitchStmt>(T)->getCond();
529 unsigned bits = Ctx.getTypeSize(CondE->getType());
530 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000531
Ted Kremenek5a429952008-04-23 23:35:07 +0000532 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
533 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000534 continue;
535 }
536
Ted Kremenek5a429952008-04-23 23:35:07 +0000537 os << V.toString();
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000538 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000539
Ted Kremenek61f3e052008-04-03 04:42:52 +0000540 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000541 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000542
543 break;
544
545 }
546 }
Ted Kremenek56783922008-04-25 01:29:56 +0000547 else {
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000548 os << "'Default' branch taken.";
549 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000550 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000551
552 PD.push_front(new PathDiagnosticPiece(L, os.str()));
553 break;
554 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000555
556 case Stmt::BreakStmtClass:
557 case Stmt::ContinueStmtClass: {
558 std::ostringstream os;
559 ExecutionContinues(os, SMgr, LastNode);
560 PD.push_front(new PathDiagnosticPiece(L, os.str()));
561 break;
562 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000563
564 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000565
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000566 std::ostringstream os;
567 os << "'?' condition evaluates to ";
568
569 if (*(Src->succ_begin()+1) == Dst)
570 os << "false.";
571 else
572 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000573
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000574 PD.push_front(new PathDiagnosticPiece(L, os.str()));
575
576 break;
577 }
578
579 case Stmt::DoStmtClass: {
580
581 if (*(Src->succ_begin()) == Dst) {
582
583 std::ostringstream os;
584
Ted Kremenek143ca222008-05-06 18:11:09 +0000585 os << "Loop condition is true.";
586 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000587
588 PD.push_front(new PathDiagnosticPiece(L, os.str()));
589 }
590 else
591 PD.push_front(new PathDiagnosticPiece(L,
592 "Loop condition is false. Exiting loop."));
593
594 break;
595 }
596
Ted Kremenek61f3e052008-04-03 04:42:52 +0000597 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000598 case Stmt::ForStmtClass: {
599
600 if (*(Src->succ_begin()+1) == Dst) {
601
602 std::ostringstream os;
603
Ted Kremenek143ca222008-05-06 18:11:09 +0000604 os << "Loop condition is false.";
605 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000606
607 PD.push_front(new PathDiagnosticPiece(L, os.str()));
608 }
609 else
610 PD.push_front(new PathDiagnosticPiece(L,
611 "Loop condition is true. Entering loop body."));
612
613 break;
614 }
615
Ted Kremenek61f3e052008-04-03 04:42:52 +0000616 case Stmt::IfStmtClass: {
617
618 if (*(Src->succ_begin()+1) == Dst)
619 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
620 else
621 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
622
623 break;
624 }
625 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000626 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000627
628 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000629 PD.push_front(p);
630
631 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
632
633 ValueState* St = N->getState();
634
635 // Scan the lval bindings, and see if a "notable" symbol has a new
636 // lval binding.
637
638 // FIXME: In the future, when we generalize the memory model, we'll
639 // need a way to iterate over binded locations.
640
641 llvm::SmallSet<SymbolID, 10> AlreadyProcessed;
642
643 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
644
645 RVal V = I.getData();
646 SymbolID ScanSym;
647
648 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
649 ScanSym = SV->getSymbol();
650 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
651 ScanSym = SV->getSymbol();
652 else
653 continue;
654
655 assert (ScanSym.isInitialized());
656
657 if (!isNotable(ScanSym))
658 continue;
659
660 if (AlreadyProcessed.count(ScanSym))
661 continue;
662
663 AlreadyProcessed.insert(ScanSym);
664
665 HandleNotableSymbol(N, PS->getStmt(), ScanSym, *this, PD);
666 }
667 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000668 }
669}
670
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000671
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000672bool BugTypeCacheLocation::isCached(BugReport& R) {
673
674 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000675
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000676 if (!N)
677 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000678
679 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000680 // warning for the same error type that occurs at the same program
681 // location but along a different path.
682
Ted Kremenek76d90c82008-05-16 18:33:14 +0000683 return isCached(N->getLocation());
684}
685
686bool BugTypeCacheLocation::isCached(ProgramPoint P) {
687
688 void* p = P.getRawData();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000689
690 if (CachedErrors.count(p))
691 return true;
692
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000693 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000694 return false;
695}
696
Ted Kremenek75840e12008-04-18 01:56:37 +0000697void BugReporter::EmitWarning(BugReport& R) {
698
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000699 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000700 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000701
Ted Kremenek55851142008-04-22 16:15:03 +0000702 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
703 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek072192b2008-04-30 23:47:44 +0000704
705 // Get the meta data.
706
707 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
708
709 for (const char** s = Meta.first; s != Meta.second; ++s)
710 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000711
712 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000713
Ted Kremenek55851142008-04-22 16:15:03 +0000714 if (PD && !D->empty()) {
715 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000716 return;
717 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000718
Ted Kremenek75840e12008-04-18 01:56:37 +0000719 // We don't have a PathDiagnosticClient, but we can still emit a single
720 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000721
Ted Kremenek55851142008-04-22 16:15:03 +0000722 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
723 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000724
725
726 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000727
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000728 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000729
Ted Kremenek55851142008-04-22 16:15:03 +0000730 if (!D->empty()) {
731 Beg = D->back()->ranges_begin();
732 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000733 }
734 else
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000735 R.getRanges(*this, Beg, End);
Ted Kremenek75840e12008-04-18 01:56:37 +0000736
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000737 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000738 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
739
740 for ( ; Beg != End; ++Beg)
741 piece->addRange(*Beg);
742
Ted Kremenek55851142008-04-22 16:15:03 +0000743 D->push_back(piece);
744 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000745 }
746 else {
747 std::ostringstream os;
748 os << "[CHECKER] ";
749
Ted Kremenek55851142008-04-22 16:15:03 +0000750 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000751 os << R.getDescription();
752 else
Ted Kremenek55851142008-04-22 16:15:03 +0000753 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000754
755
756 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
757 os.str().c_str());
758
759 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
760 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000761}