blob: fba31f81af3aa77f52976a78bc64d9cc42552bcb [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 Kremenekc0959972008-07-02 21:24:01 +000030GRBugReporter::~GRBugReporter() {}
31BugReporterData::~BugReporterData() {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000032BugType::~BugType() {}
33BugReport::~BugReport() {}
Ted Kremenek5e55cda2008-04-11 18:40:29 +000034RangedBugReport::~RangedBugReport() {}
35
Ted Kremenekc0959972008-07-02 21:24:01 +000036ExplodedGraph<ValueState>& GRBugReporter::getGraph() {
Ted Kremenek1aa44c72008-05-22 23:45:19 +000037 return Eng.getGraph();
38}
39
Ted Kremenekc0959972008-07-02 21:24:01 +000040ValueStateManager& GRBugReporter::getStateManager() {
Ted Kremenek1aa44c72008-05-22 23:45:19 +000041 return Eng.getStateManager();
42}
Ted Kremenek61f3e052008-04-03 04:42:52 +000043
44static inline Stmt* GetStmt(const ProgramPoint& P) {
45 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
46 return PS->getStmt();
47 }
48 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
49 return BE->getSrc()->getTerminator();
50 }
51 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
52 return BE->getFirstStmt();
53 }
54
55 assert (false && "Unsupported ProgramPoint.");
56 return NULL;
57}
58
Ted Kremenek706e3cf2008-04-07 23:35:17 +000059static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000060 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000061 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000062 else
63 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000064}
65
Ted Kremenekbd7efa82008-04-17 23:44:37 +000066static inline ExplodedNode<ValueState>*
67GetNextNode(ExplodedNode<ValueState>* N) {
68 return N->pred_empty() ? NULL : *(N->pred_begin());
69}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000070
Ted Kremenekbd7efa82008-04-17 23:44:37 +000071static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) {
72 assert (isa<BlockEntrance>(N->getLocation()));
73
74 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
75
76 ProgramPoint P = N->getLocation();
77
78 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
79 return PS->getStmt();
80 }
81
82 return NULL;
83}
84
Ted Kremenek143ca222008-05-06 18:11:09 +000085static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
86 Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000087
88 if (!S)
89 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000090
91 // Slow, but probably doesn't matter.
92 if (os.str().empty())
93 os << ' ';
94
95 os << "Execution continues on line "
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000096 << SMgr.getLogicalLineNumber(S->getLocStart()) << '.';
97}
Ted Kremenek143ca222008-05-06 18:11:09 +000098
99
100static inline void ExecutionContinues(std::ostringstream& os,
101 SourceManager& SMgr,
102 ExplodedNode<ValueState>* N) {
103
104 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
105}
106
107static inline void ExecutionContinues(std::ostringstream& os,
108 SourceManager& SMgr,
109 const CFGBlock* B) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000110
Ted Kremenek143ca222008-05-06 18:11:09 +0000111 ExecutionContinues(os, SMgr, GetStmt(B));
112}
113
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000114
115Stmt* BugReport::getStmt(BugReporter& BR) const {
116
Ted Kremenek200ed922008-05-02 23:21:21 +0000117 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000118 Stmt *S = NULL;
119
120 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
Ted Kremenek7032f462008-07-03 05:26:14 +0000121 if (BE->getBlock() == &BR.getCFG()->getExit())
Ted Kremenek200ed922008-05-02 23:21:21 +0000122 S = GetLastStmt(EndNode);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000123 if (!S)
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000124 S = GetStmt(ProgP);
125
126 return S;
127}
128
129PathDiagnosticPiece*
130BugReport::getEndPath(BugReporter& BR,
131 ExplodedNode<ValueState>* EndPathNode) {
132
133 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000134
135 if (!S)
136 return NULL;
137
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000138 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
139 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000140
Ted Kremenekde7161f2008-04-03 18:00:37 +0000141 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000142 getRanges(BR, Beg, End);
143
144 for (; Beg != End; ++Beg)
145 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000146
147 return P;
148}
149
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000150void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
151 const SourceRange*& end) {
152
153 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
154 R = E->getSourceRange();
155 beg = &R;
156 end = beg+1;
157 }
158 else
159 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000160}
161
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000162FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
163
Ted Kremenek200ed922008-05-02 23:21:21 +0000164 if (!EndNode)
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000165 return FullSourceLoc();
166
Ted Kremenek200ed922008-05-02 23:21:21 +0000167 Stmt* S = GetStmt(EndNode->getLocation());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000168
169 if (!S)
170 return FullSourceLoc();
171
172 return FullSourceLoc(S->getLocStart(), Mgr);
173}
174
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000175PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
176 ExplodedNode<ValueState>* PrevN,
177 ExplodedGraph<ValueState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000178 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000179 return NULL;
180}
181
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000182static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*>
183MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000184
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000185 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1));
186
Ted Kremenek910e9992008-04-25 01:25:15 +0000187 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000188
Ted Kremenek910e9992008-04-25 01:25:15 +0000189 ExplodedNode<ValueState>* NOld = N;
190 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000191
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000192 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000193 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000194
Ted Kremenek910e9992008-04-25 01:25:15 +0000195 if (I->getState() == NOld->getState() &&
196 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000197 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000198 break;
199 }
200 }
201
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000202 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000203
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000204 // Create a new graph with a single path.
205
206 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
207 GTrim->getContext());
208
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000209 // Sometimes TrimGraph can contain a cycle. Perform a reverse DFS
210 // to the root node, and then construct a new graph that contains only
211 // a single path.
212 llvm::DenseMap<void*,unsigned> Visited;
213 llvm::SmallVector<ExplodedNode<ValueState>*, 10> WS;
214 WS.push_back(N);
215 unsigned cnt = 0;
216 ExplodedNode<ValueState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000217
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000218 while (!WS.empty()) {
219 ExplodedNode<ValueState>* Node = WS.back();
220 WS.pop_back();
221
222 if (Visited.find(Node) != Visited.end())
223 continue;
224
225 Visited[Node] = cnt++;
226
227 if (Node->pred_empty()) {
228 Root = Node;
229 break;
230 }
231
232 for (ExplodedNode<ValueState>::pred_iterator I=Node->pred_begin(),
233 E=Node->pred_end(); I!=E; ++I)
234 WS.push_back(*I);
235 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000236
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000237 assert (Root);
238
239 // Now walk from the root down the DFS path, always taking the successor
240 // with the lowest number.
241 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000242
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000243 for ( N = Root ;;) {
244
245 // Lookup the number associated with the current node.
246 llvm::DenseMap<void*,unsigned>::iterator I=Visited.find(N);
247 assert (I != Visited.end());
248
249 // Create the equivalent node in the new graph with the same state
250 // and location.
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000251 ExplodedNode<ValueState>* NewN =
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000252 G->getNode(N->getLocation(), N->getState());
253
254 // Link up the new node with the previous node.
255 if (Last)
256 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000257
258 Last = NewN;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000259
260 // Are we at the final node?
261 if (I->second == 0) {
262 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000263 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000264 }
265
266 // Find the next successor node. We choose the node that is marked
267 // with the lowest DFS number.
268 ExplodedNode<ValueState>::succ_iterator SI = N->succ_begin();
269 ExplodedNode<ValueState>::succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000270 N = 0;
271
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000272 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekc1da4412008-06-17 19:14:06 +0000273
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000274 I = Visited.find(*SI);
275
276 if (I == Visited.end())
277 continue;
278
279 if (!N || I->second < MinVal) {
280 N = *SI;
281 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000282 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000283 }
284
285 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000286 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000287
288 assert (First);
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000289 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000290}
291
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000292static VarDecl* GetMostRecentVarDeclBinding(ExplodedNode<ValueState>* N,
293 ValueStateManager& VMgr,
294 RVal X) {
295
296 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
297
298 ProgramPoint P = N->getLocation();
299
300 if (!isa<PostStmt>(P))
301 continue;
302
303 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
304
305 if (!DR)
306 continue;
307
308 RVal Y = VMgr.GetRVal(N->getState(), DR);
309
310 if (X != Y)
311 continue;
312
313 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
314
315 if (!VD)
316 continue;
317
318 return VD;
319 }
320
321 return 0;
322}
323
324
325static void HandleNotableSymbol(ExplodedNode<ValueState>* N, Stmt* S,
326 SymbolID Sym, BugReporter& BR,
327 PathDiagnostic& PD) {
328
329 ExplodedNode<ValueState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
330 ValueState* PrevSt = Pred ? Pred->getState() : 0;
331
332 if (!PrevSt)
333 return;
334
335 // Look at the variable bindings of the current state that map to the
336 // specified symbol. Are any of them not in the previous state.
337
338 ValueState* St = N->getState();
Ted Kremenekc0959972008-07-02 21:24:01 +0000339 ValueStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000340
341 // FIXME: Later generalize for a broader memory model.
342
343 // FIXME: This is quadratic, since its nested in another loop. Probably
344 // doesn't matter, but keep an eye out for performance issues. It's
345 // also a bunch of copy-paste. Bad. Cleanup later.
346
347 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
348
349 RVal V = I.getData();
350 SymbolID ScanSym;
351
352 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
353 ScanSym = SV->getSymbol();
354 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
355 ScanSym = SV->getSymbol();
356 else
357 continue;
358
359 if (ScanSym != Sym)
360 continue;
361
362 // Check if the previous state has this binding.
363
364 RVal X = VMgr.GetRVal(PrevSt, lval::DeclVal(I.getKey()));
365
366 if (X == V) // Same binding?
367 continue;
368
369 // Different binding. Only handle assignments for now. We don't pull
370 // this check out of the loop because we will eventually handle other
371 // cases.
372
373 VarDecl *VD = 0;
374
375 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
376 if (!B->isAssignmentOp())
377 continue;
378
379 // What variable did we assign to?
380 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
381
382 if (!DR)
383 continue;
384
385 VD = dyn_cast<VarDecl>(DR->getDecl());
386 }
387 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
388 VD = dyn_cast<VarDecl>(DS->getDecl());
389
390 if (!VD)
391 continue;
392
393 // What is the most recently referenced variable with this binding?
394 VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
395
396 if (!MostRecent)
397 continue;
398
399 // Create the diagnostic.
400
401 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
402
403 if (VD->getType()->isPointerLikeType()) {
404 std::string msg = "'" + std::string(VD->getName()) +
405 "' now aliases '" + MostRecent->getName() + "'";
406
407 PD.push_front(new PathDiagnosticPiece(L, msg));
408 }
409 }
410}
411
Ted Kremenekc0959972008-07-02 21:24:01 +0000412void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
413 BugReport& R) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000414
415 ExplodedNode<ValueState>* N = R.getEndNode();
416
417 if (!N) return;
418
419 // Construct a new graph that contains only a single path from the error
420 // node to a root.
421
422 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
423 GPair = MakeReportGraph(&getGraph(), N);
424
425 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
426 assert(GPair.second->getLocation() == N->getLocation());
427 N = GPair.second;
428
429 // Start building the path diagnostic...
430
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000431 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
432 PD.push_back(Piece);
433 else
434 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000435
436 ExplodedNode<ValueState>* NextNode = N->pred_empty()
437 ? NULL : *(N->pred_begin());
438
Ted Kremenekc0959972008-07-02 21:24:01 +0000439 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000440 SourceManager& SMgr = Ctx.getSourceManager();
441
Ted Kremenek6837faa2008-04-09 00:20:43 +0000442 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000443
444 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000445 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000446 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000447
448 ProgramPoint P = N->getLocation();
449
450 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
451
452 CFGBlock* Src = BE->getSrc();
453 CFGBlock* Dst = BE->getDst();
454
455 Stmt* T = Src->getTerminator();
456
457 if (!T)
458 continue;
459
460 FullSourceLoc L(T->getLocStart(), SMgr);
461
462 switch (T->getStmtClass()) {
463 default:
464 break;
465
466 case Stmt::GotoStmtClass:
467 case Stmt::IndirectGotoStmtClass: {
468
469 Stmt* S = GetStmt(LastNode->getLocation());
470
471 if (!S)
472 continue;
473
474 std::ostringstream os;
475
476 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000477 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000478
479 PD.push_front(new PathDiagnosticPiece(L, os.str()));
480 break;
481 }
482
483 case Stmt::SwitchStmtClass: {
484
485 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000486
Ted Kremenek61f3e052008-04-03 04:42:52 +0000487 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000488
489 if (Stmt* S = Dst->getLabel())
490 switch (S->getStmtClass()) {
491
Ted Kremenek61f3e052008-04-03 04:42:52 +0000492 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000493 assert(false && "Not a valid switch label.");
494 continue;
495
Ted Kremenek8b904552008-04-22 22:29:46 +0000496 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000497
498 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000499 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000500
501 break;
502 }
503
504 case Stmt::CaseStmtClass: {
505
506 os << "Control jumps to 'case ";
507
Ted Kremenek5a429952008-04-23 23:35:07 +0000508 CaseStmt* Case = cast<CaseStmt>(S);
509 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000510
Ted Kremenek5a429952008-04-23 23:35:07 +0000511 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000512
Ted Kremenek5a429952008-04-23 23:35:07 +0000513 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000514
Ted Kremenek5a429952008-04-23 23:35:07 +0000515 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
516
517 // FIXME: Maybe this should be an assertion. Are there cases
518 // were it is not an EnumConstantDecl?
519
520 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
521
522 if (D) {
523 GetRawInt = false;
524 os << D->getName();
525 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000526 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000527
528 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000529
Ted Kremenek5a429952008-04-23 23:35:07 +0000530 // Not an enum.
531 Expr* CondE = cast<SwitchStmt>(T)->getCond();
532 unsigned bits = Ctx.getTypeSize(CondE->getType());
533 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000534
Ted Kremenek5a429952008-04-23 23:35:07 +0000535 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
536 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000537 continue;
538 }
539
Ted Kremenek5a429952008-04-23 23:35:07 +0000540 os << V.toString();
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000541 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000542
Ted Kremenek61f3e052008-04-03 04:42:52 +0000543 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000544 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000545
546 break;
547
548 }
549 }
Ted Kremenek56783922008-04-25 01:29:56 +0000550 else {
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000551 os << "'Default' branch taken.";
552 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000553 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000554
555 PD.push_front(new PathDiagnosticPiece(L, os.str()));
556 break;
557 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000558
559 case Stmt::BreakStmtClass:
560 case Stmt::ContinueStmtClass: {
561 std::ostringstream os;
562 ExecutionContinues(os, SMgr, LastNode);
563 PD.push_front(new PathDiagnosticPiece(L, os.str()));
564 break;
565 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000566
567 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000568
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000569 std::ostringstream os;
570 os << "'?' condition evaluates to ";
571
572 if (*(Src->succ_begin()+1) == Dst)
573 os << "false.";
574 else
575 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000576
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000577 PD.push_front(new PathDiagnosticPiece(L, os.str()));
578
579 break;
580 }
581
582 case Stmt::DoStmtClass: {
583
584 if (*(Src->succ_begin()) == Dst) {
585
586 std::ostringstream os;
587
Ted Kremenek143ca222008-05-06 18:11:09 +0000588 os << "Loop condition is true.";
589 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000590
591 PD.push_front(new PathDiagnosticPiece(L, os.str()));
592 }
593 else
594 PD.push_front(new PathDiagnosticPiece(L,
595 "Loop condition is false. Exiting loop."));
596
597 break;
598 }
599
Ted Kremenek61f3e052008-04-03 04:42:52 +0000600 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000601 case Stmt::ForStmtClass: {
602
603 if (*(Src->succ_begin()+1) == Dst) {
604
605 std::ostringstream os;
606
Ted Kremenek143ca222008-05-06 18:11:09 +0000607 os << "Loop condition is false.";
608 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000609
610 PD.push_front(new PathDiagnosticPiece(L, os.str()));
611 }
612 else
613 PD.push_front(new PathDiagnosticPiece(L,
614 "Loop condition is true. Entering loop body."));
615
616 break;
617 }
618
Ted Kremenek61f3e052008-04-03 04:42:52 +0000619 case Stmt::IfStmtClass: {
620
621 if (*(Src->succ_begin()+1) == Dst)
622 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
623 else
624 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
625
626 break;
627 }
628 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000629 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000630
631 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000632 PD.push_front(p);
633
634 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
635
636 ValueState* St = N->getState();
637
638 // Scan the lval bindings, and see if a "notable" symbol has a new
639 // lval binding.
640
641 // FIXME: In the future, when we generalize the memory model, we'll
642 // need a way to iterate over binded locations.
643
644 llvm::SmallSet<SymbolID, 10> AlreadyProcessed;
645
646 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
647
648 RVal V = I.getData();
649 SymbolID ScanSym;
650
651 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
652 ScanSym = SV->getSymbol();
653 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
654 ScanSym = SV->getSymbol();
655 else
656 continue;
657
658 assert (ScanSym.isInitialized());
659
660 if (!isNotable(ScanSym))
661 continue;
662
663 if (AlreadyProcessed.count(ScanSym))
664 continue;
665
666 AlreadyProcessed.insert(ScanSym);
667
668 HandleNotableSymbol(N, PS->getStmt(), ScanSym, *this, PD);
669 }
670 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000671 }
672}
673
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000674
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000675bool BugTypeCacheLocation::isCached(BugReport& R) {
676
677 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000678
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000679 if (!N)
680 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000681
682 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000683 // warning for the same error type that occurs at the same program
684 // location but along a different path.
685
Ted Kremenek76d90c82008-05-16 18:33:14 +0000686 return isCached(N->getLocation());
687}
688
689bool BugTypeCacheLocation::isCached(ProgramPoint P) {
690
691 void* p = P.getRawData();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000692
693 if (CachedErrors.count(p))
694 return true;
695
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000696 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000697 return false;
698}
699
Ted Kremenek75840e12008-04-18 01:56:37 +0000700void BugReporter::EmitWarning(BugReport& R) {
701
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000702 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000703 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000704
Ted Kremenek55851142008-04-22 16:15:03 +0000705 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
706 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek072192b2008-04-30 23:47:44 +0000707
708 // Get the meta data.
709
710 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
711
712 for (const char** s = Meta.first; s != Meta.second; ++s)
713 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000714
715 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000716
Ted Kremenekc0959972008-07-02 21:24:01 +0000717 PathDiagnosticClient* PD = getPathDiagnosticClient();
718
Ted Kremenek55851142008-04-22 16:15:03 +0000719 if (PD && !D->empty()) {
720 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000721 return;
722 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000723
Ted Kremenek75840e12008-04-18 01:56:37 +0000724 // We don't have a PathDiagnosticClient, but we can still emit a single
725 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000726
Ted Kremenekc0959972008-07-02 21:24:01 +0000727 FullSourceLoc L = D->empty() ? R.getLocation(getSourceManager())
Ted Kremenek55851142008-04-22 16:15:03 +0000728 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000729
730
731 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000732
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000733 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000734
Ted Kremenek55851142008-04-22 16:15:03 +0000735 if (!D->empty()) {
736 Beg = D->back()->ranges_begin();
737 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000738 }
739 else
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000740 R.getRanges(*this, Beg, End);
Ted Kremenek75840e12008-04-18 01:56:37 +0000741
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000742 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000743 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
744
745 for ( ; Beg != End; ++Beg)
746 piece->addRange(*Beg);
747
Ted Kremenek55851142008-04-22 16:15:03 +0000748 D->push_back(piece);
749 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000750 }
751 else {
752 std::ostringstream os;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000753
Ted Kremenek55851142008-04-22 16:15:03 +0000754 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000755 os << R.getDescription();
756 else
Ted Kremenek55851142008-04-22 16:15:03 +0000757 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000758
759
Ted Kremenekc0959972008-07-02 21:24:01 +0000760 Diagnostic& Diag = getDiagnostic();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000761 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
762 os.str().c_str());
763
764 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
765 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000766}