blob: 9c55333e4c606279a4ab366f658626eb49897462 [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
Ted Kremenek910e9992008-04-25 01:25:15 +0000147 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000148
Ted Kremenek910e9992008-04-25 01:25:15 +0000149 ExplodedNode<ValueState>* NOld = N;
150 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000151
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000152 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000153 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000154
Ted Kremenek910e9992008-04-25 01:25:15 +0000155 if (I->getState() == NOld->getState() &&
156 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000157 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000158 break;
159 }
160 }
161
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000162 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000163
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000164 // Create a new graph with a single path.
165
166 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
167 GTrim->getContext());
168
169
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000170 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000171
172 while (N) {
173 ExplodedNode<ValueState>* NewN =
174 G->getNode(N->getLocation(), N->getState());
175
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000176 if (!First) First = NewN;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000177 if (Last) Last->addPredecessor(NewN);
178
179 Last = NewN;
180 N = N->pred_empty() ? 0 : *(N->pred_begin());
181 }
182
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000183 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000184}
185
186void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
187 BugReport& R) {
188
189 ExplodedNode<ValueState>* N = R.getEndNode();
190
191 if (!N) return;
192
193 // Construct a new graph that contains only a single path from the error
194 // node to a root.
195
196 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
197 GPair = MakeReportGraph(&getGraph(), N);
198
199 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
200 assert(GPair.second->getLocation() == N->getLocation());
201 N = GPair.second;
202
203 // Start building the path diagnostic...
204
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000205 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
206 PD.push_back(Piece);
207 else
208 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000209
210 ExplodedNode<ValueState>* NextNode = N->pred_empty()
211 ? NULL : *(N->pred_begin());
212
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000213 SourceManager& SMgr = Ctx.getSourceManager();
214
Ted Kremenek6837faa2008-04-09 00:20:43 +0000215 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000216
217 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000218 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000219 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000220
221 ProgramPoint P = N->getLocation();
222
223 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
224
225 CFGBlock* Src = BE->getSrc();
226 CFGBlock* Dst = BE->getDst();
227
228 Stmt* T = Src->getTerminator();
229
230 if (!T)
231 continue;
232
233 FullSourceLoc L(T->getLocStart(), SMgr);
234
235 switch (T->getStmtClass()) {
236 default:
237 break;
238
239 case Stmt::GotoStmtClass:
240 case Stmt::IndirectGotoStmtClass: {
241
242 Stmt* S = GetStmt(LastNode->getLocation());
243
244 if (!S)
245 continue;
246
247 std::ostringstream os;
248
249 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000250 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000251
252 PD.push_front(new PathDiagnosticPiece(L, os.str()));
253 break;
254 }
255
256 case Stmt::SwitchStmtClass: {
257
258 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000259
Ted Kremenek61f3e052008-04-03 04:42:52 +0000260 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000261
262 if (Stmt* S = Dst->getLabel())
263 switch (S->getStmtClass()) {
264
Ted Kremenek61f3e052008-04-03 04:42:52 +0000265 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000266 assert(false && "Not a valid switch label.");
267 continue;
268
Ted Kremenek8b904552008-04-22 22:29:46 +0000269 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000270
271 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000272 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000273
274 break;
275 }
276
277 case Stmt::CaseStmtClass: {
278
279 os << "Control jumps to 'case ";
280
Ted Kremenek5a429952008-04-23 23:35:07 +0000281 CaseStmt* Case = cast<CaseStmt>(S);
282 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000283
Ted Kremenek5a429952008-04-23 23:35:07 +0000284 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000285
Ted Kremenek5a429952008-04-23 23:35:07 +0000286 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000287
Ted Kremenek5a429952008-04-23 23:35:07 +0000288 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
289
290 // FIXME: Maybe this should be an assertion. Are there cases
291 // were it is not an EnumConstantDecl?
292
293 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
294
295 if (D) {
296 GetRawInt = false;
297 os << D->getName();
298 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000299 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000300
301 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000302
Ted Kremenek5a429952008-04-23 23:35:07 +0000303 // Not an enum.
304 Expr* CondE = cast<SwitchStmt>(T)->getCond();
305 unsigned bits = Ctx.getTypeSize(CondE->getType());
306 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000307
Ted Kremenek5a429952008-04-23 23:35:07 +0000308 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
309 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000310 continue;
311 }
312
Ted Kremenek5a429952008-04-23 23:35:07 +0000313 os << V.toString();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000314 }
315
Ted Kremenek5a429952008-04-23 23:35:07 +0000316
317
Ted Kremenek61f3e052008-04-03 04:42:52 +0000318 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000319 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000320
321 break;
322
323 }
324 }
Ted Kremenek56783922008-04-25 01:29:56 +0000325 else {
326
327 // FIXME: Get line number.
328
329 os << "'Default' branch taken. "
330 "Execution continues after switch statement.";
331 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000332
333 PD.push_front(new PathDiagnosticPiece(L, os.str()));
334 break;
335 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000336
337 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000338
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000339 std::ostringstream os;
340 os << "'?' condition evaluates to ";
341
342 if (*(Src->succ_begin()+1) == Dst)
343 os << "false.";
344 else
345 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000346
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000347 PD.push_front(new PathDiagnosticPiece(L, os.str()));
348
349 break;
350 }
351
352 case Stmt::DoStmtClass: {
353
354 if (*(Src->succ_begin()) == Dst) {
355
356 std::ostringstream os;
357
358 os << "Loop condition is true. Execution continues on line "
359 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
360
361 PD.push_front(new PathDiagnosticPiece(L, os.str()));
362 }
363 else
364 PD.push_front(new PathDiagnosticPiece(L,
365 "Loop condition is false. Exiting loop."));
366
367 break;
368 }
369
Ted Kremenek61f3e052008-04-03 04:42:52 +0000370 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000371 case Stmt::ForStmtClass: {
372
373 if (*(Src->succ_begin()+1) == Dst) {
374
375 std::ostringstream os;
376
377 os << "Loop condition is false. Execution continues on line "
378 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
379
380 PD.push_front(new PathDiagnosticPiece(L, os.str()));
381 }
382 else
383 PD.push_front(new PathDiagnosticPiece(L,
384 "Loop condition is true. Entering loop body."));
385
386 break;
387 }
388
Ted Kremenek61f3e052008-04-03 04:42:52 +0000389 case Stmt::IfStmtClass: {
390
391 if (*(Src->succ_begin()+1) == Dst)
392 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
393 else
394 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
395
396 break;
397 }
398 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000399 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000400
401 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
402 PD.push_front(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000403 }
404}
405
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000406bool BugTypeCacheLocation::isCached(BugReport& R) {
407
408 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000409
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000410 if (!N)
411 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000412
413 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000414 // warning for the same error type that occurs at the same program
415 // location but along a different path.
416
417 void* p = N->getLocation().getRawData();
418
419 if (CachedErrors.count(p))
420 return true;
421
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000422 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000423 return false;
424}
425
Ted Kremenek75840e12008-04-18 01:56:37 +0000426void BugReporter::EmitWarning(BugReport& R) {
427
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000428 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000429 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000430
Ted Kremenek55851142008-04-22 16:15:03 +0000431 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
432 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek75840e12008-04-18 01:56:37 +0000433
434 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000435
Ted Kremenek55851142008-04-22 16:15:03 +0000436 if (PD && !D->empty()) {
437 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000438 return;
439 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000440
Ted Kremenek75840e12008-04-18 01:56:37 +0000441 // We don't have a PathDiagnosticClient, but we can still emit a single
442 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000443
Ted Kremenek55851142008-04-22 16:15:03 +0000444 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
445 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000446
447
448 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000449
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000450 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000451
Ted Kremenek55851142008-04-22 16:15:03 +0000452 if (!D->empty()) {
453 Beg = D->back()->ranges_begin();
454 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000455 }
456 else
457 R.getRanges(Beg, End);
458
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000459 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000460 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
461
462 for ( ; Beg != End; ++Beg)
463 piece->addRange(*Beg);
464
Ted Kremenek55851142008-04-22 16:15:03 +0000465 D->push_back(piece);
466 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000467 }
468 else {
469 std::ostringstream os;
470 os << "[CHECKER] ";
471
Ted Kremenek55851142008-04-22 16:15:03 +0000472 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000473 os << R.getDescription();
474 else
Ted Kremenek55851142008-04-22 16:15:03 +0000475 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000476
477
478 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
479 os.str().c_str());
480
481 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
482 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000483}