blob: b163eea1b82d2c019284a98ec2d1c14cf8dc021a [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"
Chris Lattner405674c2008-08-23 22:23:37 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000025#include "llvm/ADT/DenseMap.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000026#include <sstream>
27
28using namespace clang;
29
30BugReporter::~BugReporter() {}
Ted Kremenekc0959972008-07-02 21:24:01 +000031GRBugReporter::~GRBugReporter() {}
32BugReporterData::~BugReporterData() {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000033BugType::~BugType() {}
34BugReport::~BugReport() {}
Ted Kremenek5e55cda2008-04-11 18:40:29 +000035RangedBugReport::~RangedBugReport() {}
36
Ted Kremenek4adc81e2008-08-13 04:27:00 +000037ExplodedGraph<GRState>& GRBugReporter::getGraph() {
Ted Kremenek1aa44c72008-05-22 23:45:19 +000038 return Eng.getGraph();
39}
40
Ted Kremenek4adc81e2008-08-13 04:27:00 +000041GRStateManager& GRBugReporter::getStateManager() {
Ted Kremenek1aa44c72008-05-22 23:45:19 +000042 return Eng.getStateManager();
43}
Ted Kremenek61f3e052008-04-03 04:42:52 +000044
45static inline Stmt* GetStmt(const ProgramPoint& P) {
46 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
47 return PS->getStmt();
48 }
49 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
50 return BE->getSrc()->getTerminator();
51 }
52 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
53 return BE->getFirstStmt();
54 }
55
56 assert (false && "Unsupported ProgramPoint.");
57 return NULL;
58}
59
Ted Kremenek706e3cf2008-04-07 23:35:17 +000060static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000061 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000062 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063 else
64 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000065}
66
Ted Kremenek4adc81e2008-08-13 04:27:00 +000067static inline ExplodedNode<GRState>*
68GetNextNode(ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000069 return N->pred_empty() ? NULL : *(N->pred_begin());
70}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000071
Ted Kremenek4adc81e2008-08-13 04:27:00 +000072static Stmt* GetLastStmt(ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000073 assert (isa<BlockEntrance>(N->getLocation()));
74
75 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
76
77 ProgramPoint P = N->getLocation();
78
79 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
80 return PS->getStmt();
81 }
82
83 return NULL;
84}
85
Ted Kremenek143ca222008-05-06 18:11:09 +000086static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
87 Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000088
89 if (!S)
90 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000091
92 // Slow, but probably doesn't matter.
93 if (os.str().empty())
94 os << ' ';
95
96 os << "Execution continues on line "
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000097 << SMgr.getLogicalLineNumber(S->getLocStart()) << '.';
98}
Ted Kremenek143ca222008-05-06 18:11:09 +000099
100
101static inline void ExecutionContinues(std::ostringstream& os,
102 SourceManager& SMgr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000103 ExplodedNode<GRState>* N) {
Ted Kremenek143ca222008-05-06 18:11:09 +0000104
105 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
106}
107
108static inline void ExecutionContinues(std::ostringstream& os,
109 SourceManager& SMgr,
110 const CFGBlock* B) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000111
Ted Kremenek143ca222008-05-06 18:11:09 +0000112 ExecutionContinues(os, SMgr, GetStmt(B));
113}
114
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000115
116Stmt* BugReport::getStmt(BugReporter& BR) const {
117
Ted Kremenek200ed922008-05-02 23:21:21 +0000118 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000119 Stmt *S = NULL;
120
121 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
Ted Kremenek7032f462008-07-03 05:26:14 +0000122 if (BE->getBlock() == &BR.getCFG()->getExit())
Ted Kremenek200ed922008-05-02 23:21:21 +0000123 S = GetLastStmt(EndNode);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000124 if (!S)
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000125 S = GetStmt(ProgP);
126
127 return S;
128}
129
130PathDiagnosticPiece*
131BugReport::getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000132 ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000133
134 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000135
136 if (!S)
137 return NULL;
138
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000139 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
140 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000141
Ted Kremenekde7161f2008-04-03 18:00:37 +0000142 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000143 getRanges(BR, Beg, End);
144
145 for (; Beg != End; ++Beg)
146 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000147
148 return P;
149}
150
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000151void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
152 const SourceRange*& end) {
153
154 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
155 R = E->getSourceRange();
156 beg = &R;
157 end = beg+1;
158 }
159 else
160 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000161}
162
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000163FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
164
Ted Kremenek200ed922008-05-02 23:21:21 +0000165 if (!EndNode)
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000166 return FullSourceLoc();
167
Ted Kremenek200ed922008-05-02 23:21:21 +0000168 Stmt* S = GetStmt(EndNode->getLocation());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000169
170 if (!S)
171 return FullSourceLoc();
172
173 return FullSourceLoc(S->getLocStart(), Mgr);
174}
175
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000176PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<GRState>* N,
177 ExplodedNode<GRState>* PrevN,
178 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000179 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000180 return NULL;
181}
182
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000183static std::pair<ExplodedGraph<GRState>*, ExplodedNode<GRState>*>
184MakeReportGraph(ExplodedGraph<GRState>* G, ExplodedNode<GRState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000185
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000186 llvm::OwningPtr<ExplodedGraph<GRState> > GTrim(G->Trim(&N, &N+1));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000187
Ted Kremenek910e9992008-04-25 01:25:15 +0000188 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000189
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000190 ExplodedNode<GRState>* NOld = N;
Ted Kremenek910e9992008-04-25 01:25:15 +0000191 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000192
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000193 for (ExplodedGraph<GRState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000194 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000195
Ted Kremenek910e9992008-04-25 01:25:15 +0000196 if (I->getState() == NOld->getState() &&
197 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000198 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000199 break;
200 }
201 }
202
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000203 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000204
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000205 // Create a new graph with a single path.
206
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000207 G = new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000208 GTrim->getContext());
209
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000210 // Sometimes TrimGraph can contain a cycle. Perform a reverse DFS
211 // to the root node, and then construct a new graph that contains only
212 // a single path.
213 llvm::DenseMap<void*,unsigned> Visited;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000214 llvm::SmallVector<ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000215 WS.push_back(N);
216 unsigned cnt = 0;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000217 ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000218
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000219 while (!WS.empty()) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000220 ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000221 WS.pop_back();
222
223 if (Visited.find(Node) != Visited.end())
224 continue;
225
226 Visited[Node] = cnt++;
227
228 if (Node->pred_empty()) {
229 Root = Node;
230 break;
231 }
232
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000233 for (ExplodedNode<GRState>::pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000234 E=Node->pred_end(); I!=E; ++I)
235 WS.push_back(*I);
236 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000237
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000238 assert (Root);
239
240 // Now walk from the root down the DFS path, always taking the successor
241 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000242 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000243
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000244 for ( N = Root ;;) {
245
246 // Lookup the number associated with the current node.
247 llvm::DenseMap<void*,unsigned>::iterator I=Visited.find(N);
248 assert (I != Visited.end());
249
250 // Create the equivalent node in the new graph with the same state
251 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000252 ExplodedNode<GRState>* NewN =
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000253 G->getNode(N->getLocation(), N->getState());
254
255 // Link up the new node with the previous node.
256 if (Last)
257 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000258
259 Last = NewN;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000260
261 // Are we at the final node?
262 if (I->second == 0) {
263 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000264 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000265 }
266
267 // Find the next successor node. We choose the node that is marked
268 // with the lowest DFS number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000269 ExplodedNode<GRState>::succ_iterator SI = N->succ_begin();
270 ExplodedNode<GRState>::succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000271 N = 0;
272
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000273 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekc1da4412008-06-17 19:14:06 +0000274
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000275 I = Visited.find(*SI);
276
277 if (I == Visited.end())
278 continue;
279
280 if (!N || I->second < MinVal) {
281 N = *SI;
282 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000283 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000284 }
285
286 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000287 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000288
289 assert (First);
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000290 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000291}
292
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000293static VarDecl* GetMostRecentVarDeclBinding(ExplodedNode<GRState>* N,
294 GRStateManager& VMgr,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000295 RVal X) {
296
297 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
298
299 ProgramPoint P = N->getLocation();
300
301 if (!isa<PostStmt>(P))
302 continue;
303
304 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
305
306 if (!DR)
307 continue;
308
309 RVal Y = VMgr.GetRVal(N->getState(), DR);
310
311 if (X != Y)
312 continue;
313
314 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
315
316 if (!VD)
317 continue;
318
319 return VD;
320 }
321
322 return 0;
323}
324
325
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000326static void HandleNotableSymbol(ExplodedNode<GRState>* N, Stmt* S,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000327 SymbolID Sym, BugReporter& BR,
328 PathDiagnostic& PD) {
329
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000330 ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
331 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000332
333 if (!PrevSt)
334 return;
335
336 // Look at the variable bindings of the current state that map to the
337 // specified symbol. Are any of them not in the previous state.
338
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000339 const GRState* St = N->getState();
340 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000341
342 // FIXME: Later generalize for a broader memory model.
343
344 // FIXME: This is quadratic, since its nested in another loop. Probably
345 // doesn't matter, but keep an eye out for performance issues. It's
346 // also a bunch of copy-paste. Bad. Cleanup later.
347
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000348 for (GRState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000349
350 RVal V = I.getData();
351 SymbolID ScanSym;
352
353 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
354 ScanSym = SV->getSymbol();
355 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
356 ScanSym = SV->getSymbol();
357 else
358 continue;
359
360 if (ScanSym != Sym)
361 continue;
362
363 // Check if the previous state has this binding.
364
365 RVal X = VMgr.GetRVal(PrevSt, lval::DeclVal(I.getKey()));
366
367 if (X == V) // Same binding?
368 continue;
369
370 // Different binding. Only handle assignments for now. We don't pull
371 // this check out of the loop because we will eventually handle other
372 // cases.
373
374 VarDecl *VD = 0;
375
376 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
377 if (!B->isAssignmentOp())
378 continue;
379
380 // What variable did we assign to?
381 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
382
383 if (!DR)
384 continue;
385
386 VD = dyn_cast<VarDecl>(DR->getDecl());
387 }
388 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
389 VD = dyn_cast<VarDecl>(DS->getDecl());
390
391 if (!VD)
392 continue;
393
394 // What is the most recently referenced variable with this binding?
395 VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
396
397 if (!MostRecent)
398 continue;
399
400 // Create the diagnostic.
401
402 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
403
404 if (VD->getType()->isPointerLikeType()) {
405 std::string msg = "'" + std::string(VD->getName()) +
406 "' now aliases '" + MostRecent->getName() + "'";
407
408 PD.push_front(new PathDiagnosticPiece(L, msg));
409 }
410 }
411}
412
Ted Kremenekc0959972008-07-02 21:24:01 +0000413void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
414 BugReport& R) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000415
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000416 ExplodedNode<GRState>* N = R.getEndNode();
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000417
418 if (!N) return;
419
420 // Construct a new graph that contains only a single path from the error
421 // node to a root.
422
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000423 const std::pair<ExplodedGraph<GRState>*,ExplodedNode<GRState>*>
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000424 GPair = MakeReportGraph(&getGraph(), N);
425
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000426 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000427 assert(GPair.second->getLocation() == N->getLocation());
428 N = GPair.second;
429
430 // Start building the path diagnostic...
431
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000432 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
433 PD.push_back(Piece);
434 else
435 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000436
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000437 ExplodedNode<GRState>* NextNode = N->pred_empty()
Ted Kremenek6837faa2008-04-09 00:20:43 +0000438 ? NULL : *(N->pred_begin());
439
Ted Kremenekc0959972008-07-02 21:24:01 +0000440 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000441 SourceManager& SMgr = Ctx.getSourceManager();
442
Ted Kremenek6837faa2008-04-09 00:20:43 +0000443 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000444
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000445 ExplodedNode<GRState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000446 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000447 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000448
449 ProgramPoint P = N->getLocation();
450
451 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
452
453 CFGBlock* Src = BE->getSrc();
454 CFGBlock* Dst = BE->getDst();
455
456 Stmt* T = Src->getTerminator();
457
458 if (!T)
459 continue;
460
461 FullSourceLoc L(T->getLocStart(), SMgr);
462
463 switch (T->getStmtClass()) {
464 default:
465 break;
466
467 case Stmt::GotoStmtClass:
468 case Stmt::IndirectGotoStmtClass: {
469
470 Stmt* S = GetStmt(LastNode->getLocation());
471
472 if (!S)
473 continue;
474
475 std::ostringstream os;
476
477 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000478 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000479
480 PD.push_front(new PathDiagnosticPiece(L, os.str()));
481 break;
482 }
483
484 case Stmt::SwitchStmtClass: {
485
486 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000487
Ted Kremenek61f3e052008-04-03 04:42:52 +0000488 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000489
490 if (Stmt* S = Dst->getLabel())
491 switch (S->getStmtClass()) {
492
Ted Kremenek61f3e052008-04-03 04:42:52 +0000493 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000494 assert(false && "Not a valid switch label.");
495 continue;
496
Ted Kremenek8b904552008-04-22 22:29:46 +0000497 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000498
499 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000500 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000501
502 break;
503 }
504
505 case Stmt::CaseStmtClass: {
506
507 os << "Control jumps to 'case ";
508
Ted Kremenek5a429952008-04-23 23:35:07 +0000509 CaseStmt* Case = cast<CaseStmt>(S);
510 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000511
Ted Kremenek5a429952008-04-23 23:35:07 +0000512 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000513
Ted Kremenek5a429952008-04-23 23:35:07 +0000514 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000515
Ted Kremenek5a429952008-04-23 23:35:07 +0000516 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
517
518 // FIXME: Maybe this should be an assertion. Are there cases
519 // were it is not an EnumConstantDecl?
520
521 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
522
523 if (D) {
524 GetRawInt = false;
525 os << D->getName();
526 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000527 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000528
529 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000530
Ted Kremenek5a429952008-04-23 23:35:07 +0000531 // Not an enum.
532 Expr* CondE = cast<SwitchStmt>(T)->getCond();
533 unsigned bits = Ctx.getTypeSize(CondE->getType());
534 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000535
Ted Kremenek5a429952008-04-23 23:35:07 +0000536 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
537 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000538 continue;
539 }
540
Chris Lattner405674c2008-08-23 22:23:37 +0000541 llvm::raw_os_ostream OS(os);
542 OS << V;
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000543 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000544
Ted Kremenek61f3e052008-04-03 04:42:52 +0000545 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000546 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000547
548 break;
549
550 }
551 }
Ted Kremenek56783922008-04-25 01:29:56 +0000552 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000553 os << "'Default' branch taken. ";
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000554 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000555 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000556
557 PD.push_front(new PathDiagnosticPiece(L, os.str()));
558 break;
559 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000560
561 case Stmt::BreakStmtClass:
562 case Stmt::ContinueStmtClass: {
563 std::ostringstream os;
564 ExecutionContinues(os, SMgr, LastNode);
565 PD.push_front(new PathDiagnosticPiece(L, os.str()));
566 break;
567 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000568
569 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000570
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000571 std::ostringstream os;
572 os << "'?' condition evaluates to ";
573
574 if (*(Src->succ_begin()+1) == Dst)
575 os << "false.";
576 else
577 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000578
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000579 PD.push_front(new PathDiagnosticPiece(L, os.str()));
580
581 break;
582 }
583
584 case Stmt::DoStmtClass: {
585
586 if (*(Src->succ_begin()) == Dst) {
587
588 std::ostringstream os;
589
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000590 os << "Loop condition is true. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000591 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000592
593 PD.push_front(new PathDiagnosticPiece(L, os.str()));
594 }
595 else
596 PD.push_front(new PathDiagnosticPiece(L,
597 "Loop condition is false. Exiting loop."));
598
599 break;
600 }
601
Ted Kremenek61f3e052008-04-03 04:42:52 +0000602 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000603 case Stmt::ForStmtClass: {
604
605 if (*(Src->succ_begin()+1) == Dst) {
606
607 std::ostringstream os;
608
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000609 os << "Loop condition is false. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000610 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000611
612 PD.push_front(new PathDiagnosticPiece(L, os.str()));
613 }
614 else
615 PD.push_front(new PathDiagnosticPiece(L,
616 "Loop condition is true. Entering loop body."));
617
618 break;
619 }
620
Ted Kremenek61f3e052008-04-03 04:42:52 +0000621 case Stmt::IfStmtClass: {
622
623 if (*(Src->succ_begin()+1) == Dst)
624 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
625 else
626 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
627
628 break;
629 }
630 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000631 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000632
633 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000634 PD.push_front(p);
635
636 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
637
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000638 const GRState* St = N->getState();
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000639
640 // Scan the lval bindings, and see if a "notable" symbol has a new
641 // lval binding.
642
643 // FIXME: In the future, when we generalize the memory model, we'll
644 // need a way to iterate over binded locations.
645
646 llvm::SmallSet<SymbolID, 10> AlreadyProcessed;
647
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000648 for (GRState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I){
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000649
650 RVal V = I.getData();
651 SymbolID ScanSym;
652
653 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&V))
654 ScanSym = SV->getSymbol();
655 else if (nonlval::SymbolVal* SV = dyn_cast<nonlval::SymbolVal>(&V))
656 ScanSym = SV->getSymbol();
657 else
658 continue;
659
660 assert (ScanSym.isInitialized());
661
662 if (!isNotable(ScanSym))
663 continue;
664
665 if (AlreadyProcessed.count(ScanSym))
666 continue;
667
668 AlreadyProcessed.insert(ScanSym);
669
670 HandleNotableSymbol(N, PS->getStmt(), ScanSym, *this, PD);
671 }
672 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000673 }
674}
675
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000676
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000677bool BugTypeCacheLocation::isCached(BugReport& R) {
678
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000679 ExplodedNode<GRState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000680
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000681 if (!N)
682 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000683
684 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000685 // warning for the same error type that occurs at the same program
686 // location but along a different path.
687
Ted Kremenek76d90c82008-05-16 18:33:14 +0000688 return isCached(N->getLocation());
689}
690
691bool BugTypeCacheLocation::isCached(ProgramPoint P) {
Ted Kremenekd4527582008-09-16 18:44:52 +0000692 if (CachedErrors.count(P))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000693 return true;
694
Ted Kremenekd4527582008-09-16 18:44:52 +0000695 CachedErrors.insert(P);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000696 return false;
697}
698
Ted Kremenek75840e12008-04-18 01:56:37 +0000699void BugReporter::EmitWarning(BugReport& R) {
700
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000701 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000702 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000703
Ted Kremenek55851142008-04-22 16:15:03 +0000704 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
705 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek072192b2008-04-30 23:47:44 +0000706
707 // Get the meta data.
708
709 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
710
711 for (const char** s = Meta.first; s != Meta.second; ++s)
712 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000713
714 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000715
Ted Kremenekc0959972008-07-02 21:24:01 +0000716 PathDiagnosticClient* PD = getPathDiagnosticClient();
717
Ted Kremenek55851142008-04-22 16:15:03 +0000718 if (PD && !D->empty()) {
719 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000720 return;
721 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000722
Ted Kremenek75840e12008-04-18 01:56:37 +0000723 // We don't have a PathDiagnosticClient, but we can still emit a single
724 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000725
Ted Kremenekc0959972008-07-02 21:24:01 +0000726 FullSourceLoc L = D->empty() ? R.getLocation(getSourceManager())
Ted Kremenek55851142008-04-22 16:15:03 +0000727 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000728
729
730 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000731
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000732 const SourceRange *Beg, *End;
Ted Kremenek57202072008-07-14 17:40:50 +0000733
Ted Kremenek55851142008-04-22 16:15:03 +0000734 if (!D->empty()) {
735 Beg = D->back()->ranges_begin();
736 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000737 }
Ted Kremenek57202072008-07-14 17:40:50 +0000738 else
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000739 R.getRanges(*this, Beg, End);
Ted Kremenek75840e12008-04-18 01:56:37 +0000740
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000741 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000742 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
743
744 for ( ; Beg != End; ++Beg)
745 piece->addRange(*Beg);
746
Ted Kremenek57202072008-07-14 17:40:50 +0000747 D->push_back(piece);
Ted Kremenek55851142008-04-22 16:15:03 +0000748 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000749 }
750 else {
Ted Kremenek57202072008-07-14 17:40:50 +0000751 std::ostringstream os;
752
Ted Kremenek55851142008-04-22 16:15:03 +0000753 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000754 os << R.getDescription();
755 else
Ted Kremenek55851142008-04-22 16:15:03 +0000756 os << D->back()->getString();
Ted Kremenek57202072008-07-14 17:40:50 +0000757
758
Ted Kremenekc0959972008-07-02 21:24:01 +0000759 Diagnostic& Diag = getDiagnostic();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000760 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
761 os.str().c_str());
762
763 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
764 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000765}
Ted Kremenek57202072008-07-14 17:40:50 +0000766
767void
768BugReporter::EmitBasicReport(const char* name, const char* str,
Ted Kremenek8f269862008-07-14 20:56:04 +0000769 SourceLocation Loc,
770 SourceRange* RBeg, unsigned NumRanges) {
Ted Kremenek57202072008-07-14 17:40:50 +0000771
772 SimpleBugType BT(name);
773 DiagCollector C(BT);
774 Diagnostic& Diag = getDiagnostic();
775 Diag.Report(&C, getContext().getFullLoc(Loc),
776 Diag.getCustomDiagID(Diagnostic::Warning, str),
Ted Kremenek8f269862008-07-14 20:56:04 +0000777 0, 0, RBeg, NumRanges);
Ted Kremenek57202072008-07-14 17:40:50 +0000778
779 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
780 EmitWarning(*I);
781}
782