blob: d137cff3e7b1efa33e6361c528a1104d5bbd86f9 [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
168 ExplodedNode<ValueState>* Last = 0;
169
170 while (N) {
171 ExplodedNode<ValueState>* NewN =
172 G->getNode(N->getLocation(), N->getState());
173
174 if (Last) Last->addPredecessor(NewN);
175
176 Last = NewN;
177 N = N->pred_empty() ? 0 : *(N->pred_begin());
178 }
179
180 return std::make_pair(G, Last);
181}
182
183void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
184 BugReport& R) {
185
186 ExplodedNode<ValueState>* N = R.getEndNode();
187
188 if (!N) return;
189
190 // Construct a new graph that contains only a single path from the error
191 // node to a root.
192
193 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
194 GPair = MakeReportGraph(&getGraph(), N);
195
196 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
197 assert(GPair.second->getLocation() == N->getLocation());
198 N = GPair.second;
199
200 // Start building the path diagnostic...
201
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000202 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
203 PD.push_back(Piece);
204 else
205 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000206
207 ExplodedNode<ValueState>* NextNode = N->pred_empty()
208 ? NULL : *(N->pred_begin());
209
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000210 SourceManager& SMgr = Ctx.getSourceManager();
211
Ted Kremenek6837faa2008-04-09 00:20:43 +0000212 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000213
214 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000215 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000216 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000217
218 ProgramPoint P = N->getLocation();
219
220 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
221
222 CFGBlock* Src = BE->getSrc();
223 CFGBlock* Dst = BE->getDst();
224
225 Stmt* T = Src->getTerminator();
226
227 if (!T)
228 continue;
229
230 FullSourceLoc L(T->getLocStart(), SMgr);
231
232 switch (T->getStmtClass()) {
233 default:
234 break;
235
236 case Stmt::GotoStmtClass:
237 case Stmt::IndirectGotoStmtClass: {
238
239 Stmt* S = GetStmt(LastNode->getLocation());
240
241 if (!S)
242 continue;
243
244 std::ostringstream os;
245
246 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000247 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000248
249 PD.push_front(new PathDiagnosticPiece(L, os.str()));
250 break;
251 }
252
253 case Stmt::SwitchStmtClass: {
254
255 // Figure out what case arm we took.
256
257 Stmt* S = Dst->getLabel();
258
259 if (!S)
260 continue;
261
262 std::ostringstream os;
263
264 switch (S->getStmtClass()) {
265 default:
Ted Kremenek8b904552008-04-22 22:29:46 +0000266 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000267
268 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000269 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000270
271 break;
272 }
273
274 case Stmt::CaseStmtClass: {
275
276 os << "Control jumps to 'case ";
277
278 Expr* CondE = cast<SwitchStmt>(T)->getCond();
279 unsigned bits = Ctx.getTypeSize(CondE->getType());
280
281 llvm::APSInt V1(bits, false);
282
283 CaseStmt* Case = cast<CaseStmt>(S);
284
285 if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) {
286 assert (false &&
287 "Case condition must evaluate to an integer constant.");
288 continue;
289 }
290
291 os << V1.toString();
292
293 // Get the RHS of the case, if it exists.
294
295 if (Expr* E = Case->getRHS()) {
296
297 llvm::APSInt V2(bits, false);
298
299 if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) {
300 assert (false &&
301 "Case condition (RHS) must evaluate to an integer constant.");
302 continue;
303 }
304
305 os << " .. " << V2.toString();
306 }
307
308 os << ":' at line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000309 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000310
311 break;
312
313 }
314 }
315
316 PD.push_front(new PathDiagnosticPiece(L, os.str()));
317 break;
318 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000319
320 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000321
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000322 std::ostringstream os;
323 os << "'?' condition evaluates to ";
324
325 if (*(Src->succ_begin()+1) == Dst)
326 os << "false.";
327 else
328 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000329
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000330 PD.push_front(new PathDiagnosticPiece(L, os.str()));
331
332 break;
333 }
334
335 case Stmt::DoStmtClass: {
336
337 if (*(Src->succ_begin()) == Dst) {
338
339 std::ostringstream os;
340
341 os << "Loop condition is true. Execution continues on line "
342 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
343
344 PD.push_front(new PathDiagnosticPiece(L, os.str()));
345 }
346 else
347 PD.push_front(new PathDiagnosticPiece(L,
348 "Loop condition is false. Exiting loop."));
349
350 break;
351 }
352
Ted Kremenek61f3e052008-04-03 04:42:52 +0000353 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000354 case Stmt::ForStmtClass: {
355
356 if (*(Src->succ_begin()+1) == Dst) {
357
358 std::ostringstream os;
359
360 os << "Loop condition is false. Execution continues on line "
361 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
362
363 PD.push_front(new PathDiagnosticPiece(L, os.str()));
364 }
365 else
366 PD.push_front(new PathDiagnosticPiece(L,
367 "Loop condition is true. Entering loop body."));
368
369 break;
370 }
371
Ted Kremenek61f3e052008-04-03 04:42:52 +0000372 case Stmt::IfStmtClass: {
373
374 if (*(Src->succ_begin()+1) == Dst)
375 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
376 else
377 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
378
379 break;
380 }
381 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000382 }
383 else
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000384 if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode,
385 *ReportGraph, *this))
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000386 PD.push_front(piece);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000387 }
388}
389
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000390bool BugTypeCacheLocation::isCached(BugReport& R) {
391
392 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000393
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000394 if (!N)
395 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000396
397 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000398 // warning for the same error type that occurs at the same program
399 // location but along a different path.
400
401 void* p = N->getLocation().getRawData();
402
403 if (CachedErrors.count(p))
404 return true;
405
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000406 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000407 return false;
408}
409
Ted Kremenek75840e12008-04-18 01:56:37 +0000410void BugReporter::EmitWarning(BugReport& R) {
411
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000412 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000413 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000414
Ted Kremenek55851142008-04-22 16:15:03 +0000415 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
416 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek75840e12008-04-18 01:56:37 +0000417
418 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000419
Ted Kremenek55851142008-04-22 16:15:03 +0000420 if (PD && !D->empty()) {
421 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000422 return;
423 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000424
Ted Kremenek75840e12008-04-18 01:56:37 +0000425 // We don't have a PathDiagnosticClient, but we can still emit a single
426 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000427
Ted Kremenek55851142008-04-22 16:15:03 +0000428 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
429 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000430
431
432 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000433
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000434 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000435
Ted Kremenek55851142008-04-22 16:15:03 +0000436 if (!D->empty()) {
437 Beg = D->back()->ranges_begin();
438 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000439 }
440 else
441 R.getRanges(Beg, End);
442
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000443 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000444 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
445
446 for ( ; Beg != End; ++Beg)
447 piece->addRange(*Beg);
448
Ted Kremenek55851142008-04-22 16:15:03 +0000449 D->push_back(piece);
450 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000451 }
452 else {
453 std::ostringstream os;
454 os << "[CHECKER] ";
455
Ted Kremenek55851142008-04-22 16:15:03 +0000456 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000457 os << R.getDescription();
458 else
Ted Kremenek55851142008-04-22 16:15:03 +0000459 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000460
461
462 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
463 os.str().c_str());
464
465 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
466 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000467}