blob: d213d993f3fb37f5db5572110635bb9a307457a4 [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.
Ted Kremenek5a429952008-04-23 23:35:07 +0000257
Ted Kremenek61f3e052008-04-03 04:42:52 +0000258 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000259
260 if (Stmt* S = Dst->getLabel())
261 switch (S->getStmtClass()) {
262
Ted Kremenek61f3e052008-04-03 04:42:52 +0000263 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000264 assert(false && "Not a valid switch label.");
265 continue;
266
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
Ted Kremenek5a429952008-04-23 23:35:07 +0000279 CaseStmt* Case = cast<CaseStmt>(S);
280 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000281
Ted Kremenek5a429952008-04-23 23:35:07 +0000282 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000283
Ted Kremenek5a429952008-04-23 23:35:07 +0000284 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000285
Ted Kremenek5a429952008-04-23 23:35:07 +0000286 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
287
288 // FIXME: Maybe this should be an assertion. Are there cases
289 // were it is not an EnumConstantDecl?
290
291 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
292
293 if (D) {
294 GetRawInt = false;
295 os << D->getName();
296 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000297 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000298
299 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000300
Ted Kremenek5a429952008-04-23 23:35:07 +0000301 // Not an enum.
302 Expr* CondE = cast<SwitchStmt>(T)->getCond();
303 unsigned bits = Ctx.getTypeSize(CondE->getType());
304 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000305
Ted Kremenek5a429952008-04-23 23:35:07 +0000306 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
307 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000308 continue;
309 }
310
Ted Kremenek5a429952008-04-23 23:35:07 +0000311 os << V.toString();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000312 }
313
Ted Kremenek5a429952008-04-23 23:35:07 +0000314
315
Ted Kremenek61f3e052008-04-03 04:42:52 +0000316 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000317 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000318
319 break;
320
321 }
322 }
323
324 PD.push_front(new PathDiagnosticPiece(L, os.str()));
325 break;
326 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000327
328 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000329
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000330 std::ostringstream os;
331 os << "'?' condition evaluates to ";
332
333 if (*(Src->succ_begin()+1) == Dst)
334 os << "false.";
335 else
336 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000337
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000338 PD.push_front(new PathDiagnosticPiece(L, os.str()));
339
340 break;
341 }
342
343 case Stmt::DoStmtClass: {
344
345 if (*(Src->succ_begin()) == Dst) {
346
347 std::ostringstream os;
348
349 os << "Loop condition is true. Execution continues on line "
350 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
351
352 PD.push_front(new PathDiagnosticPiece(L, os.str()));
353 }
354 else
355 PD.push_front(new PathDiagnosticPiece(L,
356 "Loop condition is false. Exiting loop."));
357
358 break;
359 }
360
Ted Kremenek61f3e052008-04-03 04:42:52 +0000361 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000362 case Stmt::ForStmtClass: {
363
364 if (*(Src->succ_begin()+1) == Dst) {
365
366 std::ostringstream os;
367
368 os << "Loop condition is false. Execution continues on line "
369 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
370
371 PD.push_front(new PathDiagnosticPiece(L, os.str()));
372 }
373 else
374 PD.push_front(new PathDiagnosticPiece(L,
375 "Loop condition is true. Entering loop body."));
376
377 break;
378 }
379
Ted Kremenek61f3e052008-04-03 04:42:52 +0000380 case Stmt::IfStmtClass: {
381
382 if (*(Src->succ_begin()+1) == Dst)
383 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
384 else
385 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
386
387 break;
388 }
389 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000390 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000391
392 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
393 PD.push_front(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000394 }
395}
396
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000397bool BugTypeCacheLocation::isCached(BugReport& R) {
398
399 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000400
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000401 if (!N)
402 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000403
404 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000405 // warning for the same error type that occurs at the same program
406 // location but along a different path.
407
408 void* p = N->getLocation().getRawData();
409
410 if (CachedErrors.count(p))
411 return true;
412
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000413 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000414 return false;
415}
416
Ted Kremenek75840e12008-04-18 01:56:37 +0000417void BugReporter::EmitWarning(BugReport& R) {
418
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000419 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000420 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000421
Ted Kremenek55851142008-04-22 16:15:03 +0000422 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
423 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek75840e12008-04-18 01:56:37 +0000424
425 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000426
Ted Kremenek55851142008-04-22 16:15:03 +0000427 if (PD && !D->empty()) {
428 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000429 return;
430 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000431
Ted Kremenek75840e12008-04-18 01:56:37 +0000432 // We don't have a PathDiagnosticClient, but we can still emit a single
433 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000434
Ted Kremenek55851142008-04-22 16:15:03 +0000435 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
436 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000437
438
439 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000440
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000441 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000442
Ted Kremenek55851142008-04-22 16:15:03 +0000443 if (!D->empty()) {
444 Beg = D->back()->ranges_begin();
445 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000446 }
447 else
448 R.getRanges(Beg, End);
449
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000450 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000451 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
452
453 for ( ; Beg != End; ++Beg)
454 piece->addRange(*Beg);
455
Ted Kremenek55851142008-04-22 16:15:03 +0000456 D->push_back(piece);
457 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000458 }
459 else {
460 std::ostringstream os;
461 os << "[CHECKER] ";
462
Ted Kremenek55851142008-04-22 16:15:03 +0000463 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000464 os << R.getDescription();
465 else
Ted Kremenek55851142008-04-22 16:15:03 +0000466 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000467
468
469 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
470 os.str().c_str());
471
472 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
473 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000474}