blob: 33633736b1d52fb6a97a8fefe40156599954f9db [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 Kremenek50a6d0c2008-04-09 21:41:14 +000033ExplodedGraph<ValueState>& BugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35static inline Stmt* GetStmt(const ProgramPoint& P) {
36 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
37 return PS->getStmt();
38 }
39 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
40 return BE->getSrc()->getTerminator();
41 }
42 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
43 return BE->getFirstStmt();
44 }
45
46 assert (false && "Unsupported ProgramPoint.");
47 return NULL;
48}
49
Ted Kremenek706e3cf2008-04-07 23:35:17 +000050static inline Stmt* GetStmt(const CFGBlock* B) {
51 assert (!B->empty());
52 return (*B)[0];
53}
54
Ted Kremenekd2f642b2008-04-14 17:39:48 +000055Stmt* BugReport::getStmt() const {
56 return N ? GetStmt(N->getLocation()) : NULL;
57}
Ted Kremenekbd7efa82008-04-17 23:44:37 +000058
59static inline ExplodedNode<ValueState>*
60GetNextNode(ExplodedNode<ValueState>* N) {
61 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek61f3e052008-04-03 04:42:52 +000063
Ted Kremenekd2f642b2008-04-14 17:39:48 +000064
Ted Kremenekbd7efa82008-04-17 23:44:37 +000065static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) {
66 assert (isa<BlockEntrance>(N->getLocation()));
67
68 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
69
70 ProgramPoint P = N->getLocation();
71
72 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
73 return PS->getStmt();
74 }
75
76 return NULL;
77}
78
79PathDiagnosticPiece*
80BugReport::getEndPath(BugReporter& BR,
81 ExplodedNode<ValueState>* EndPathNode) const {
82
83 ProgramPoint ProgP = EndPathNode->getLocation();
84 Stmt *S = NULL;
85
86 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
87 if (BE->getBlock() == &BR.getCFG().getExit())
88 S = GetLastStmt(EndPathNode);
89 if (!S)
90 S = GetStmt(ProgP);
Ted Kremenek61f3e052008-04-03 04:42:52 +000091
92 if (!S)
93 return NULL;
94
Ted Kremenekbd7efa82008-04-17 23:44:37 +000095 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000096
97 PathDiagnosticPiece* P =
98 new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +000099
Ted Kremenekde7161f2008-04-03 18:00:37 +0000100 const SourceRange *Beg, *End;
101 getRanges(Beg, End);
102
103 if (Beg == End) {
104 if (Expr* E = dyn_cast<Expr>(S))
105 P->addRange(E->getSourceRange());
106 }
107 else {
108 assert (Beg < End);
109 for (; Beg != End; ++Beg)
110 P->addRange(*Beg);
111 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000112
113 return P;
114}
115
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000116void BugReport::getRanges(const SourceRange*& beg,
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000117 const SourceRange*& end) const {
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000118 beg = NULL;
119 end = NULL;
120}
121
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000122FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
123
124 if (!N)
125 return FullSourceLoc();
126
127 Stmt* S = GetStmt(N->getLocation());
128
129 if (!S)
130 return FullSourceLoc();
131
132 return FullSourceLoc(S->getLocStart(), Mgr);
133}
134
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000135PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
136 ExplodedNode<ValueState>* PrevN,
137 ExplodedGraph<ValueState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000138 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000139 return NULL;
140}
141
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000142static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*>
143MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000144
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000145 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1));
146
147 // Find the sink node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000148
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000149 N = NULL;
Ted Kremenek94826a72008-04-03 04:59:14 +0000150
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000151 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000152 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000153
154 if (I->isSink()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000155 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000156 break;
157 }
158 }
159
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000160 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000161
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000162 // Create a new graph with a single path.
163
164 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
165 GTrim->getContext());
166
167
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000168 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000169
170 while (N) {
171 ExplodedNode<ValueState>* NewN =
172 G->getNode(N->getLocation(), N->getState());
173
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000174 if (!First) First = NewN;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000175 if (Last) Last->addPredecessor(NewN);
176
177 Last = NewN;
178 N = N->pred_empty() ? 0 : *(N->pred_begin());
179 }
180
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000181 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000182}
183
184void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
185 BugReport& R) {
186
187 ExplodedNode<ValueState>* N = R.getEndNode();
188
189 if (!N) return;
190
191 // Construct a new graph that contains only a single path from the error
192 // node to a root.
193
194 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
195 GPair = MakeReportGraph(&getGraph(), N);
196
197 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
198 assert(GPair.second->getLocation() == N->getLocation());
199 N = GPair.second;
200
201 // Start building the path diagnostic...
202
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000203 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
204 PD.push_back(Piece);
205 else
206 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000207
208 ExplodedNode<ValueState>* NextNode = N->pred_empty()
209 ? NULL : *(N->pred_begin());
210
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000211 SourceManager& SMgr = Ctx.getSourceManager();
212
Ted Kremenek6837faa2008-04-09 00:20:43 +0000213 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000214
215 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000216 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000217 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000218
219 ProgramPoint P = N->getLocation();
220
221 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
222
223 CFGBlock* Src = BE->getSrc();
224 CFGBlock* Dst = BE->getDst();
225
226 Stmt* T = Src->getTerminator();
227
228 if (!T)
229 continue;
230
231 FullSourceLoc L(T->getLocStart(), SMgr);
232
233 switch (T->getStmtClass()) {
234 default:
235 break;
236
237 case Stmt::GotoStmtClass:
238 case Stmt::IndirectGotoStmtClass: {
239
240 Stmt* S = GetStmt(LastNode->getLocation());
241
242 if (!S)
243 continue;
244
245 std::ostringstream os;
246
247 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000248 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000249
250 PD.push_front(new PathDiagnosticPiece(L, os.str()));
251 break;
252 }
253
254 case Stmt::SwitchStmtClass: {
255
256 // Figure out what case arm we took.
257
258 Stmt* S = Dst->getLabel();
259
260 if (!S)
261 continue;
262
263 std::ostringstream os;
264
265 switch (S->getStmtClass()) {
266 default:
Ted Kremenek8b904552008-04-22 22:29:46 +0000267 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000268
269 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000270 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000271
272 break;
273 }
274
275 case Stmt::CaseStmtClass: {
276
277 os << "Control jumps to 'case ";
278
279 Expr* CondE = cast<SwitchStmt>(T)->getCond();
280 unsigned bits = Ctx.getTypeSize(CondE->getType());
281
282 llvm::APSInt V1(bits, false);
283
284 CaseStmt* Case = cast<CaseStmt>(S);
285
286 if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) {
287 assert (false &&
288 "Case condition must evaluate to an integer constant.");
289 continue;
290 }
291
292 os << V1.toString();
293
294 // Get the RHS of the case, if it exists.
295
296 if (Expr* E = Case->getRHS()) {
297
298 llvm::APSInt V2(bits, false);
299
300 if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) {
301 assert (false &&
302 "Case condition (RHS) must evaluate to an integer constant.");
303 continue;
304 }
305
306 os << " .. " << V2.toString();
307 }
308
309 os << ":' at line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000310 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000311
312 break;
313
314 }
315 }
316
317 PD.push_front(new PathDiagnosticPiece(L, os.str()));
318 break;
319 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000320
321 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000322
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000323 std::ostringstream os;
324 os << "'?' condition evaluates to ";
325
326 if (*(Src->succ_begin()+1) == Dst)
327 os << "false.";
328 else
329 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000330
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000331 PD.push_front(new PathDiagnosticPiece(L, os.str()));
332
333 break;
334 }
335
336 case Stmt::DoStmtClass: {
337
338 if (*(Src->succ_begin()) == Dst) {
339
340 std::ostringstream os;
341
342 os << "Loop condition is true. Execution continues on line "
343 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
344
345 PD.push_front(new PathDiagnosticPiece(L, os.str()));
346 }
347 else
348 PD.push_front(new PathDiagnosticPiece(L,
349 "Loop condition is false. Exiting loop."));
350
351 break;
352 }
353
Ted Kremenek61f3e052008-04-03 04:42:52 +0000354 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000355 case Stmt::ForStmtClass: {
356
357 if (*(Src->succ_begin()+1) == Dst) {
358
359 std::ostringstream os;
360
361 os << "Loop condition is false. Execution continues on line "
362 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
363
364 PD.push_front(new PathDiagnosticPiece(L, os.str()));
365 }
366 else
367 PD.push_front(new PathDiagnosticPiece(L,
368 "Loop condition is true. Entering loop body."));
369
370 break;
371 }
372
Ted Kremenek61f3e052008-04-03 04:42:52 +0000373 case Stmt::IfStmtClass: {
374
375 if (*(Src->succ_begin()+1) == Dst)
376 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
377 else
378 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
379
380 break;
381 }
382 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000383 }
384 else
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000385 if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode,
386 *ReportGraph, *this))
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000387 PD.push_front(piece);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000388 }
389}
390
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000391bool BugTypeCacheLocation::isCached(BugReport& R) {
392
393 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000394
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000395 if (!N)
396 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000397
398 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000399 // warning for the same error type that occurs at the same program
400 // location but along a different path.
401
402 void* p = N->getLocation().getRawData();
403
404 if (CachedErrors.count(p))
405 return true;
406
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000407 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000408 return false;
409}
410
Ted Kremenek75840e12008-04-18 01:56:37 +0000411void BugReporter::EmitWarning(BugReport& R) {
412
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000413 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000414 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000415
Ted Kremenek55851142008-04-22 16:15:03 +0000416 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
417 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek75840e12008-04-18 01:56:37 +0000418
419 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000420
Ted Kremenek55851142008-04-22 16:15:03 +0000421 if (PD && !D->empty()) {
422 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000423 return;
424 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000425
Ted Kremenek75840e12008-04-18 01:56:37 +0000426 // We don't have a PathDiagnosticClient, but we can still emit a single
427 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000428
Ted Kremenek55851142008-04-22 16:15:03 +0000429 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
430 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000431
432
433 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000434
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000435 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000436
Ted Kremenek55851142008-04-22 16:15:03 +0000437 if (!D->empty()) {
438 Beg = D->back()->ranges_begin();
439 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000440 }
441 else
442 R.getRanges(Beg, End);
443
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000444 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000445 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
446
447 for ( ; Beg != End; ++Beg)
448 piece->addRange(*Beg);
449
Ted Kremenek55851142008-04-22 16:15:03 +0000450 D->push_back(piece);
451 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000452 }
453 else {
454 std::ostringstream os;
455 os << "[CHECKER] ";
456
Ted Kremenek55851142008-04-22 16:15:03 +0000457 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000458 os << R.getDescription();
459 else
Ted Kremenek55851142008-04-22 16:15:03 +0000460 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000461
462
463 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
464 os.str().c_str());
465
466 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
467 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000468}