blob: 310fdc9accb272a7fec569a1fe34f152f32088eb [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 Kremenek61f3e052008-04-03 04:42:52 +000058
Ted Kremenekd2f642b2008-04-14 17:39:48 +000059PathDiagnosticPiece* BugReport::getEndPath(ASTContext& Ctx) const {
60
61 Stmt* S = getStmt();
Ted Kremenek61f3e052008-04-03 04:42:52 +000062
63 if (!S)
64 return NULL;
65
66 FullSourceLoc L(S->getLocStart(), Ctx.getSourceManager());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000067
68 PathDiagnosticPiece* P =
69 new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +000070
Ted Kremenekde7161f2008-04-03 18:00:37 +000071 const SourceRange *Beg, *End;
72 getRanges(Beg, End);
73
74 if (Beg == End) {
75 if (Expr* E = dyn_cast<Expr>(S))
76 P->addRange(E->getSourceRange());
77 }
78 else {
79 assert (Beg < End);
80 for (; Beg != End; ++Beg)
81 P->addRange(*Beg);
82 }
Ted Kremenek61f3e052008-04-03 04:42:52 +000083
84 return P;
85}
86
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000087void BugReport::getRanges(const SourceRange*& beg,
Ted Kremenekd2f642b2008-04-14 17:39:48 +000088 const SourceRange*& end) const {
Ted Kremenekf1ae7052008-04-03 17:57:38 +000089 beg = NULL;
90 end = NULL;
91}
92
Ted Kremenekd2f642b2008-04-14 17:39:48 +000093FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
94
95 if (!N)
96 return FullSourceLoc();
97
98 Stmt* S = GetStmt(N->getLocation());
99
100 if (!S)
101 return FullSourceLoc();
102
103 return FullSourceLoc(S->getLocStart(), Mgr);
104}
105
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000106PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
107 ExplodedNode<ValueState>* PrevN,
108 ExplodedGraph<ValueState>& G,
109 ASTContext& Ctx) {
110 return NULL;
111}
112
113void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000114 BugReport& R) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000115
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000116 ExplodedNode<ValueState>* N = R.getEndNode();
117
118 assert (N && "Path diagnostic requires a ExplodedNode.");
119
120 if (PathDiagnosticPiece* Piece = R.getEndPath(Ctx))
Ted Kremenek70d17222008-04-03 07:33:55 +0000121 PD.push_back(Piece);
122 else
123 return;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000124
125 SourceManager& SMgr = Ctx.getSourceManager();
126
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000127 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(getGraph().Trim(&N, &N+1));
Ted Kremenek94826a72008-04-03 04:59:14 +0000128
129 // Find the sink in the trimmed graph.
130 // FIXME: Should we eventually have a sink iterator?
131
132 ExplodedNode<ValueState>* NewN = 0;
133
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000134 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenek94826a72008-04-03 04:59:14 +0000135 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
136
137 if (I->isSink()) {
138 NewN = &*I;
139 break;
140 }
141 }
142
143 assert (NewN);
144 assert (NewN->getLocation() == N->getLocation());
145
146 N = NewN;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000147
148 ExplodedNode<ValueState>* NextNode = N->pred_empty()
149 ? NULL : *(N->pred_begin());
150
151 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000152
153 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000154 N = NextNode;
155 NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000156
157 ProgramPoint P = N->getLocation();
158
159 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
160
161 CFGBlock* Src = BE->getSrc();
162 CFGBlock* Dst = BE->getDst();
163
164 Stmt* T = Src->getTerminator();
165
166 if (!T)
167 continue;
168
169 FullSourceLoc L(T->getLocStart(), SMgr);
170
171 switch (T->getStmtClass()) {
172 default:
173 break;
174
175 case Stmt::GotoStmtClass:
176 case Stmt::IndirectGotoStmtClass: {
177
178 Stmt* S = GetStmt(LastNode->getLocation());
179
180 if (!S)
181 continue;
182
183 std::ostringstream os;
184
185 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000186 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000187
188 PD.push_front(new PathDiagnosticPiece(L, os.str()));
189 break;
190 }
191
192 case Stmt::SwitchStmtClass: {
193
194 // Figure out what case arm we took.
195
196 Stmt* S = Dst->getLabel();
197
198 if (!S)
199 continue;
200
201 std::ostringstream os;
202
203 switch (S->getStmtClass()) {
204 default:
205 continue;
206
207 case Stmt::DefaultStmtClass: {
208
209 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000210 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000211
212 break;
213 }
214
215 case Stmt::CaseStmtClass: {
216
217 os << "Control jumps to 'case ";
218
219 Expr* CondE = cast<SwitchStmt>(T)->getCond();
220 unsigned bits = Ctx.getTypeSize(CondE->getType());
221
222 llvm::APSInt V1(bits, false);
223
224 CaseStmt* Case = cast<CaseStmt>(S);
225
226 if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) {
227 assert (false &&
228 "Case condition must evaluate to an integer constant.");
229 continue;
230 }
231
232 os << V1.toString();
233
234 // Get the RHS of the case, if it exists.
235
236 if (Expr* E = Case->getRHS()) {
237
238 llvm::APSInt V2(bits, false);
239
240 if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) {
241 assert (false &&
242 "Case condition (RHS) must evaluate to an integer constant.");
243 continue;
244 }
245
246 os << " .. " << V2.toString();
247 }
248
249 os << ":' at line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000250 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000251
252 break;
253
254 }
255 }
256
257 PD.push_front(new PathDiagnosticPiece(L, os.str()));
258 break;
259 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000260
261 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000262
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000263 std::ostringstream os;
264 os << "'?' condition evaluates to ";
265
266 if (*(Src->succ_begin()+1) == Dst)
267 os << "false.";
268 else
269 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000270
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000271 PD.push_front(new PathDiagnosticPiece(L, os.str()));
272
273 break;
274 }
275
276 case Stmt::DoStmtClass: {
277
278 if (*(Src->succ_begin()) == Dst) {
279
280 std::ostringstream os;
281
282 os << "Loop condition is true. Execution continues on line "
283 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
284
285 PD.push_front(new PathDiagnosticPiece(L, os.str()));
286 }
287 else
288 PD.push_front(new PathDiagnosticPiece(L,
289 "Loop condition is false. Exiting loop."));
290
291 break;
292 }
293
Ted Kremenek61f3e052008-04-03 04:42:52 +0000294 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000295 case Stmt::ForStmtClass: {
296
297 if (*(Src->succ_begin()+1) == Dst) {
298
299 std::ostringstream os;
300
301 os << "Loop condition is false. Execution continues on line "
302 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
303
304 PD.push_front(new PathDiagnosticPiece(L, os.str()));
305 }
306 else
307 PD.push_front(new PathDiagnosticPiece(L,
308 "Loop condition is true. Entering loop body."));
309
310 break;
311 }
312
Ted Kremenek61f3e052008-04-03 04:42:52 +0000313 case Stmt::IfStmtClass: {
314
315 if (*(Src->succ_begin()+1) == Dst)
316 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
317 else
318 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
319
320 break;
321 }
322 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000323 }
324 else
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000325 if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, *GTrim, Ctx))
326 PD.push_front(piece);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000327 }
328}
329
330bool BugReporter::IsCached(ExplodedNode<ValueState>* N) {
331
332 // HACK: Cache the location of the error. Don't emit the same
333 // warning for the same error type that occurs at the same program
334 // location but along a different path.
335
336 void* p = N->getLocation().getRawData();
337
338 if (CachedErrors.count(p))
339 return true;
340
341 CachedErrors.insert(p);
342
343 return false;
344}
345
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000346void BugReporter::EmitPathWarning(BugReport& R) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000347
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000348 ExplodedNode<ValueState>* N = R.getEndNode();
349
350 if (!PD || !N) {
351 EmitWarning(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000352 return;
353 }
354
355 if (IsCached(N))
356 return;
357
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000358 PathDiagnostic D(R.getName());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000359 GeneratePathDiagnostic(D, R);
Ted Kremenek70d17222008-04-03 07:33:55 +0000360
361 if (!D.empty())
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000362 PD->HandlePathDiagnostic(D);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000363}
364
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000365void BugReporter::EmitWarning(BugReport& R) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000366
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000367 ExplodedNode<ValueState>* N = R.getEndNode();
368
369 if (N && IsCached(N))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000370 return;
371
372 std::ostringstream os;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000373 os << "[CHECKER] " << R.getDescription();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000374
375 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
376 os.str().c_str());
377
378 // FIXME: Add support for multiple ranges.
379
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000380 FullSourceLoc L = R.getLocation(Ctx.getSourceManager());
381
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000382 const SourceRange *Beg, *End;
383 R.getRanges(Beg, End);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000384 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000385}