blob: 0f7b2359444db652a2d4a7bb032fef65d2f16d5d [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() {}
31ExplodedGraph<ValueState>& BugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenek61f3e052008-04-03 04:42:52 +000032
33static inline Stmt* GetStmt(const ProgramPoint& P) {
34 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
35 return PS->getStmt();
36 }
37 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
38 return BE->getSrc()->getTerminator();
39 }
40 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
41 return BE->getFirstStmt();
42 }
43
44 assert (false && "Unsupported ProgramPoint.");
45 return NULL;
46}
47
Ted Kremenek706e3cf2008-04-07 23:35:17 +000048static inline Stmt* GetStmt(const CFGBlock* B) {
49 assert (!B->empty());
50 return (*B)[0];
51}
52
Ted Kremenek61f3e052008-04-03 04:42:52 +000053
54PathDiagnosticPiece*
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000055BugReport::getEndPath(ASTContext& Ctx, ExplodedNode<ValueState> *N) const {
Ted Kremenek61f3e052008-04-03 04:42:52 +000056
57 Stmt* S = GetStmt(N->getLocation());
58
59 if (!S)
60 return NULL;
61
62 FullSourceLoc L(S->getLocStart(), Ctx.getSourceManager());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000063
64 PathDiagnosticPiece* P =
65 new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +000066
Ted Kremenekde7161f2008-04-03 18:00:37 +000067 const SourceRange *Beg, *End;
68 getRanges(Beg, End);
69
70 if (Beg == End) {
71 if (Expr* E = dyn_cast<Expr>(S))
72 P->addRange(E->getSourceRange());
73 }
74 else {
75 assert (Beg < End);
76 for (; Beg != End; ++Beg)
77 P->addRange(*Beg);
78 }
Ted Kremenek61f3e052008-04-03 04:42:52 +000079
80 return P;
81}
82
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000083void BugReport::getRanges(const SourceRange*& beg,
84 const SourceRange*& end) const {
Ted Kremenekf1ae7052008-04-03 17:57:38 +000085 beg = NULL;
86 end = NULL;
87}
88
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000089PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
90 ExplodedNode<ValueState>* PrevN,
91 ExplodedGraph<ValueState>& G,
92 ASTContext& Ctx) {
93 return NULL;
94}
95
96void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
97 BugReport& R,
98 ExplodedNode<ValueState>* N) {
99
100 if (PathDiagnosticPiece* Piece = R.getEndPath(Ctx,N))
Ted Kremenek70d17222008-04-03 07:33:55 +0000101 PD.push_back(Piece);
102 else
103 return;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000104
105 SourceManager& SMgr = Ctx.getSourceManager();
106
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000107 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(getGraph().Trim(&N, &N+1));
Ted Kremenek94826a72008-04-03 04:59:14 +0000108
109 // Find the sink in the trimmed graph.
110 // FIXME: Should we eventually have a sink iterator?
111
112 ExplodedNode<ValueState>* NewN = 0;
113
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000114 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenek94826a72008-04-03 04:59:14 +0000115 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
116
117 if (I->isSink()) {
118 NewN = &*I;
119 break;
120 }
121 }
122
123 assert (NewN);
124 assert (NewN->getLocation() == N->getLocation());
125
126 N = NewN;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000127
128 ExplodedNode<ValueState>* NextNode = N->pred_empty()
129 ? NULL : *(N->pred_begin());
130
131 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000132
133 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000134 N = NextNode;
135 NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000136
137 ProgramPoint P = N->getLocation();
138
139 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
140
141 CFGBlock* Src = BE->getSrc();
142 CFGBlock* Dst = BE->getDst();
143
144 Stmt* T = Src->getTerminator();
145
146 if (!T)
147 continue;
148
149 FullSourceLoc L(T->getLocStart(), SMgr);
150
151 switch (T->getStmtClass()) {
152 default:
153 break;
154
155 case Stmt::GotoStmtClass:
156 case Stmt::IndirectGotoStmtClass: {
157
158 Stmt* S = GetStmt(LastNode->getLocation());
159
160 if (!S)
161 continue;
162
163 std::ostringstream os;
164
165 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000166 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000167
168 PD.push_front(new PathDiagnosticPiece(L, os.str()));
169 break;
170 }
171
172 case Stmt::SwitchStmtClass: {
173
174 // Figure out what case arm we took.
175
176 Stmt* S = Dst->getLabel();
177
178 if (!S)
179 continue;
180
181 std::ostringstream os;
182
183 switch (S->getStmtClass()) {
184 default:
185 continue;
186
187 case Stmt::DefaultStmtClass: {
188
189 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000190 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000191
192 break;
193 }
194
195 case Stmt::CaseStmtClass: {
196
197 os << "Control jumps to 'case ";
198
199 Expr* CondE = cast<SwitchStmt>(T)->getCond();
200 unsigned bits = Ctx.getTypeSize(CondE->getType());
201
202 llvm::APSInt V1(bits, false);
203
204 CaseStmt* Case = cast<CaseStmt>(S);
205
206 if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) {
207 assert (false &&
208 "Case condition must evaluate to an integer constant.");
209 continue;
210 }
211
212 os << V1.toString();
213
214 // Get the RHS of the case, if it exists.
215
216 if (Expr* E = Case->getRHS()) {
217
218 llvm::APSInt V2(bits, false);
219
220 if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) {
221 assert (false &&
222 "Case condition (RHS) must evaluate to an integer constant.");
223 continue;
224 }
225
226 os << " .. " << V2.toString();
227 }
228
229 os << ":' at line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000230 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000231
232 break;
233
234 }
235 }
236
237 PD.push_front(new PathDiagnosticPiece(L, os.str()));
238 break;
239 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000240
241 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000242
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000243 std::ostringstream os;
244 os << "'?' condition evaluates to ";
245
246 if (*(Src->succ_begin()+1) == Dst)
247 os << "false.";
248 else
249 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000250
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000251 PD.push_front(new PathDiagnosticPiece(L, os.str()));
252
253 break;
254 }
255
256 case Stmt::DoStmtClass: {
257
258 if (*(Src->succ_begin()) == Dst) {
259
260 std::ostringstream os;
261
262 os << "Loop condition is true. Execution continues on line "
263 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
264
265 PD.push_front(new PathDiagnosticPiece(L, os.str()));
266 }
267 else
268 PD.push_front(new PathDiagnosticPiece(L,
269 "Loop condition is false. Exiting loop."));
270
271 break;
272 }
273
Ted Kremenek61f3e052008-04-03 04:42:52 +0000274 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000275 case Stmt::ForStmtClass: {
276
277 if (*(Src->succ_begin()+1) == Dst) {
278
279 std::ostringstream os;
280
281 os << "Loop condition is false. Execution continues on line "
282 << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.';
283
284 PD.push_front(new PathDiagnosticPiece(L, os.str()));
285 }
286 else
287 PD.push_front(new PathDiagnosticPiece(L,
288 "Loop condition is true. Entering loop body."));
289
290 break;
291 }
292
Ted Kremenek61f3e052008-04-03 04:42:52 +0000293 case Stmt::IfStmtClass: {
294
295 if (*(Src->succ_begin()+1) == Dst)
296 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
297 else
298 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
299
300 break;
301 }
302 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000303 }
304 else
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000305 if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, *GTrim, Ctx))
306 PD.push_front(piece);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000307 }
308}
309
310bool BugReporter::IsCached(ExplodedNode<ValueState>* N) {
311
312 // HACK: Cache the location of the error. Don't emit the same
313 // warning for the same error type that occurs at the same program
314 // location but along a different path.
315
316 void* p = N->getLocation().getRawData();
317
318 if (CachedErrors.count(p))
319 return true;
320
321 CachedErrors.insert(p);
322
323 return false;
324}
325
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000326void BugReporter::EmitPathWarning(BugReport& R, ExplodedNode<ValueState>* N) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000327
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000328 if (!PD) {
329 EmitWarning(R, N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000330 return;
331 }
332
333 if (IsCached(N))
334 return;
335
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000336 PathDiagnostic D(R.getName());
337 GeneratePathDiagnostic(D, R, N);
Ted Kremenek70d17222008-04-03 07:33:55 +0000338
339 if (!D.empty())
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000340 PD->HandlePathDiagnostic(D);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000341}
342
343
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000344void BugReporter::EmitWarning(BugReport& R, ExplodedNode<ValueState>* N) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000345 if (IsCached(N))
346 return;
347
348 std::ostringstream os;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000349 os << "[CHECKER] " << R.getDescription();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000350
351 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
352 os.str().c_str());
353
354 // FIXME: Add support for multiple ranges.
355
356 Stmt* S = GetStmt(N->getLocation());
357
358 if (!S)
359 return;
360
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000361 const SourceRange *Beg, *End;
362 R.getRanges(Beg, End);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000363
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000364 if (Beg == End) {
365 SourceRange Range = S->getSourceRange();
366
367 Diag.Report(FullSourceLoc(S->getLocStart(), Ctx.getSourceManager()),
368 ErrorDiag, NULL, 0, &Range, 1);
369
370 }
371 else
372 Diag.Report(FullSourceLoc(S->getLocStart(), Ctx.getSourceManager()),
373 ErrorDiag, NULL, 0, Beg, End - Beg);
374
Ted Kremenek61f3e052008-04-03 04:42:52 +0000375}