blob: 8ee530c425af29ddbc1a1d31220e4ecd699f288f [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) {
Ted Kremenek143ca222008-05-06 18:11:09 +000051 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000052 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000053 else
54 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000055}
56
Ted Kremenekbd7efa82008-04-17 23:44:37 +000057static inline ExplodedNode<ValueState>*
58GetNextNode(ExplodedNode<ValueState>* N) {
59 return N->pred_empty() ? NULL : *(N->pred_begin());
60}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000061
Ted Kremenekbd7efa82008-04-17 23:44:37 +000062static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) {
63 assert (isa<BlockEntrance>(N->getLocation()));
64
65 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
66
67 ProgramPoint P = N->getLocation();
68
69 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
70 return PS->getStmt();
71 }
72
73 return NULL;
74}
75
Ted Kremenek143ca222008-05-06 18:11:09 +000076static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
77 Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000078
79 if (!S)
80 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000081
82 // Slow, but probably doesn't matter.
83 if (os.str().empty())
84 os << ' ';
85
86 os << "Execution continues on line "
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000087 << SMgr.getLogicalLineNumber(S->getLocStart()) << '.';
88}
Ted Kremenek143ca222008-05-06 18:11:09 +000089
90
91static inline void ExecutionContinues(std::ostringstream& os,
92 SourceManager& SMgr,
93 ExplodedNode<ValueState>* N) {
94
95 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
96}
97
98static inline void ExecutionContinues(std::ostringstream& os,
99 SourceManager& SMgr,
100 const CFGBlock* B) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000101
Ted Kremenek143ca222008-05-06 18:11:09 +0000102 ExecutionContinues(os, SMgr, GetStmt(B));
103}
104
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000105
106Stmt* BugReport::getStmt(BugReporter& BR) const {
107
Ted Kremenek200ed922008-05-02 23:21:21 +0000108 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000109 Stmt *S = NULL;
110
111 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP))
112 if (BE->getBlock() == &BR.getCFG().getExit())
Ted Kremenek200ed922008-05-02 23:21:21 +0000113 S = GetLastStmt(EndNode);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000114 if (!S)
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000115 S = GetStmt(ProgP);
116
117 return S;
118}
119
120PathDiagnosticPiece*
121BugReport::getEndPath(BugReporter& BR,
122 ExplodedNode<ValueState>* EndPathNode) {
123
124 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000125
126 if (!S)
127 return NULL;
128
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000129 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
130 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000131
Ted Kremenekde7161f2008-04-03 18:00:37 +0000132 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000133 getRanges(BR, Beg, End);
134
135 for (; Beg != End; ++Beg)
136 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000137
138 return P;
139}
140
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000141void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
142 const SourceRange*& end) {
143
144 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
145 R = E->getSourceRange();
146 beg = &R;
147 end = beg+1;
148 }
149 else
150 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000151}
152
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000153FullSourceLoc BugReport::getLocation(SourceManager& Mgr) {
154
Ted Kremenek200ed922008-05-02 23:21:21 +0000155 if (!EndNode)
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000156 return FullSourceLoc();
157
Ted Kremenek200ed922008-05-02 23:21:21 +0000158 Stmt* S = GetStmt(EndNode->getLocation());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000159
160 if (!S)
161 return FullSourceLoc();
162
163 return FullSourceLoc(S->getLocStart(), Mgr);
164}
165
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000166PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N,
167 ExplodedNode<ValueState>* PrevN,
168 ExplodedGraph<ValueState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000169 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 return NULL;
171}
172
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000173static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*>
174MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000175
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000176 llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1));
177
Ted Kremenek910e9992008-04-25 01:25:15 +0000178 // Find the error node in the trimmed graph.
Ted Kremenek94826a72008-04-03 04:59:14 +0000179
Ted Kremenek910e9992008-04-25 01:25:15 +0000180 ExplodedNode<ValueState>* NOld = N;
181 N = 0;
Ted Kremenek94826a72008-04-03 04:59:14 +0000182
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183 for (ExplodedGraph<ValueState>::node_iterator
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000184 I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000185
Ted Kremenek910e9992008-04-25 01:25:15 +0000186 if (I->getState() == NOld->getState() &&
187 I->getLocation() == NOld->getLocation()) {
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000188 N = &*I;
Ted Kremenek94826a72008-04-03 04:59:14 +0000189 break;
190 }
191 }
192
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000193 assert(N);
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000194
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000195 // Create a new graph with a single path.
196
197 G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(),
198 GTrim->getContext());
199
200
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000201 ExplodedNode<ValueState> *Last = 0, *First = 0;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000202
203 while (N) {
204 ExplodedNode<ValueState>* NewN =
205 G->getNode(N->getLocation(), N->getState());
206
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000207 if (!First) First = NewN;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000208 if (Last) Last->addPredecessor(NewN);
209
210 Last = NewN;
211 N = N->pred_empty() ? 0 : *(N->pred_begin());
212 }
213
Ted Kremenek6aaca9c2008-04-23 23:04:32 +0000214 return std::make_pair(G, First);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000215}
216
217void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
218 BugReport& R) {
219
220 ExplodedNode<ValueState>* N = R.getEndNode();
221
222 if (!N) return;
223
224 // Construct a new graph that contains only a single path from the error
225 // node to a root.
226
227 const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*>
228 GPair = MakeReportGraph(&getGraph(), N);
229
230 llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first);
231 assert(GPair.second->getLocation() == N->getLocation());
232 N = GPair.second;
233
234 // Start building the path diagnostic...
235
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000236 if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N))
237 PD.push_back(Piece);
238 else
239 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000240
241 ExplodedNode<ValueState>* NextNode = N->pred_empty()
242 ? NULL : *(N->pred_begin());
243
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000244 SourceManager& SMgr = Ctx.getSourceManager();
245
Ted Kremenek6837faa2008-04-09 00:20:43 +0000246 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000247
248 ExplodedNode<ValueState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000249 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000250 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000251
252 ProgramPoint P = N->getLocation();
253
254 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
255
256 CFGBlock* Src = BE->getSrc();
257 CFGBlock* Dst = BE->getDst();
258
259 Stmt* T = Src->getTerminator();
260
261 if (!T)
262 continue;
263
264 FullSourceLoc L(T->getLocStart(), SMgr);
265
266 switch (T->getStmtClass()) {
267 default:
268 break;
269
270 case Stmt::GotoStmtClass:
271 case Stmt::IndirectGotoStmtClass: {
272
273 Stmt* S = GetStmt(LastNode->getLocation());
274
275 if (!S)
276 continue;
277
278 std::ostringstream os;
279
280 os << "Control jumps to line "
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000281 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000282
283 PD.push_front(new PathDiagnosticPiece(L, os.str()));
284 break;
285 }
286
287 case Stmt::SwitchStmtClass: {
288
289 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000290
Ted Kremenek61f3e052008-04-03 04:42:52 +0000291 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000292
293 if (Stmt* S = Dst->getLabel())
294 switch (S->getStmtClass()) {
295
Ted Kremenek61f3e052008-04-03 04:42:52 +0000296 default:
Ted Kremenek5a429952008-04-23 23:35:07 +0000297 assert(false && "Not a valid switch label.");
298 continue;
299
Ted Kremenek8b904552008-04-22 22:29:46 +0000300 case Stmt::DefaultStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000301
302 os << "Control jumps to the 'default' case at line "
Ted Kremenek6837faa2008-04-09 00:20:43 +0000303 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000304
305 break;
306 }
307
308 case Stmt::CaseStmtClass: {
309
310 os << "Control jumps to 'case ";
311
Ted Kremenek5a429952008-04-23 23:35:07 +0000312 CaseStmt* Case = cast<CaseStmt>(S);
313 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000314
Ted Kremenek5a429952008-04-23 23:35:07 +0000315 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000316
Ted Kremenek5a429952008-04-23 23:35:07 +0000317 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000318
Ted Kremenek5a429952008-04-23 23:35:07 +0000319 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
320
321 // FIXME: Maybe this should be an assertion. Are there cases
322 // were it is not an EnumConstantDecl?
323
324 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
325
326 if (D) {
327 GetRawInt = false;
328 os << D->getName();
329 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000330 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000331
332 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000333
Ted Kremenek5a429952008-04-23 23:35:07 +0000334 // Not an enum.
335 Expr* CondE = cast<SwitchStmt>(T)->getCond();
336 unsigned bits = Ctx.getTypeSize(CondE->getType());
337 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000338
Ted Kremenek5a429952008-04-23 23:35:07 +0000339 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
340 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000341 continue;
342 }
343
Ted Kremenek5a429952008-04-23 23:35:07 +0000344 os << V.toString();
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000345 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000346
Ted Kremenek61f3e052008-04-03 04:42:52 +0000347 os << ":' at line "
Ted Kremenek5a429952008-04-23 23:35:07 +0000348 << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000349
350 break;
351
352 }
353 }
Ted Kremenek56783922008-04-25 01:29:56 +0000354 else {
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000355 os << "'Default' branch taken.";
356 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000357 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000358
359 PD.push_front(new PathDiagnosticPiece(L, os.str()));
360 break;
361 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000362
363 case Stmt::BreakStmtClass:
364 case Stmt::ContinueStmtClass: {
365 std::ostringstream os;
366 ExecutionContinues(os, SMgr, LastNode);
367 PD.push_front(new PathDiagnosticPiece(L, os.str()));
368 break;
369 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000370
371 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000372
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000373 std::ostringstream os;
374 os << "'?' condition evaluates to ";
375
376 if (*(Src->succ_begin()+1) == Dst)
377 os << "false.";
378 else
379 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000380
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000381 PD.push_front(new PathDiagnosticPiece(L, os.str()));
382
383 break;
384 }
385
386 case Stmt::DoStmtClass: {
387
388 if (*(Src->succ_begin()) == Dst) {
389
390 std::ostringstream os;
391
Ted Kremenek143ca222008-05-06 18:11:09 +0000392 os << "Loop condition is true.";
393 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000394
395 PD.push_front(new PathDiagnosticPiece(L, os.str()));
396 }
397 else
398 PD.push_front(new PathDiagnosticPiece(L,
399 "Loop condition is false. Exiting loop."));
400
401 break;
402 }
403
Ted Kremenek61f3e052008-04-03 04:42:52 +0000404 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000405 case Stmt::ForStmtClass: {
406
407 if (*(Src->succ_begin()+1) == Dst) {
408
409 std::ostringstream os;
410
Ted Kremenek143ca222008-05-06 18:11:09 +0000411 os << "Loop condition is false.";
412 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000413
414 PD.push_front(new PathDiagnosticPiece(L, os.str()));
415 }
416 else
417 PD.push_front(new PathDiagnosticPiece(L,
418 "Loop condition is true. Entering loop body."));
419
420 break;
421 }
422
Ted Kremenek61f3e052008-04-03 04:42:52 +0000423 case Stmt::IfStmtClass: {
424
425 if (*(Src->succ_begin()+1) == Dst)
426 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
427 else
428 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
429
430 break;
431 }
432 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000433 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000434
435 if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this))
436 PD.push_front(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000437 }
438}
439
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000440bool BugTypeCacheLocation::isCached(BugReport& R) {
441
442 ExplodedNode<ValueState>* N = R.getEndNode();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000443
Ted Kremenek329d2cc2008-04-18 02:24:50 +0000444 if (!N)
445 return false;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000446
447 // Cache the location of the error. Don't emit the same
Ted Kremenek61f3e052008-04-03 04:42:52 +0000448 // warning for the same error type that occurs at the same program
449 // location but along a different path.
450
Ted Kremenek76d90c82008-05-16 18:33:14 +0000451 return isCached(N->getLocation());
452}
453
454bool BugTypeCacheLocation::isCached(ProgramPoint P) {
455
456 void* p = P.getRawData();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000457
458 if (CachedErrors.count(p))
459 return true;
460
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000461 CachedErrors.insert(p);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000462 return false;
463}
464
Ted Kremenek75840e12008-04-18 01:56:37 +0000465void BugReporter::EmitWarning(BugReport& R) {
466
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000467 if (R.getBugType().isCached(R))
Ted Kremenek61f3e052008-04-03 04:42:52 +0000468 return;
Ted Kremenek75840e12008-04-18 01:56:37 +0000469
Ted Kremenek55851142008-04-22 16:15:03 +0000470 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName()));
471 GeneratePathDiagnostic(*D.get(), R);
Ted Kremenek072192b2008-04-30 23:47:44 +0000472
473 // Get the meta data.
474
475 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
476
477 for (const char** s = Meta.first; s != Meta.second; ++s)
478 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000479
480 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
Ted Kremenek70d17222008-04-03 07:33:55 +0000481
Ted Kremenek55851142008-04-22 16:15:03 +0000482 if (PD && !D->empty()) {
483 PD->HandlePathDiagnostic(D.take());
Ted Kremenek75840e12008-04-18 01:56:37 +0000484 return;
485 }
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000486
Ted Kremenek75840e12008-04-18 01:56:37 +0000487 // We don't have a PathDiagnosticClient, but we can still emit a single
488 // line diagnostic. Determine the location.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000489
Ted Kremenek55851142008-04-22 16:15:03 +0000490 FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager())
491 : D->back()->getLocation();
Ted Kremenek75840e12008-04-18 01:56:37 +0000492
493
494 // Determine the range.
Ted Kremenek5fcca682008-04-14 18:06:42 +0000495
Ted Kremenek4bb6ac22008-04-10 16:12:38 +0000496 const SourceRange *Beg, *End;
Ted Kremenek5fcca682008-04-14 18:06:42 +0000497
Ted Kremenek55851142008-04-22 16:15:03 +0000498 if (!D->empty()) {
499 Beg = D->back()->ranges_begin();
500 End = D->back()->ranges_end();
Ted Kremenek75840e12008-04-18 01:56:37 +0000501 }
502 else
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000503 R.getRanges(*this, Beg, End);
Ted Kremenek75840e12008-04-18 01:56:37 +0000504
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000505 if (PD) {
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000506 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
507
508 for ( ; Beg != End; ++Beg)
509 piece->addRange(*Beg);
510
Ted Kremenek55851142008-04-22 16:15:03 +0000511 D->push_back(piece);
512 PD->HandlePathDiagnostic(D.take());
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000513 }
514 else {
515 std::ostringstream os;
516 os << "[CHECKER] ";
517
Ted Kremenek55851142008-04-22 16:15:03 +0000518 if (D->empty())
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000519 os << R.getDescription();
520 else
Ted Kremenek55851142008-04-22 16:15:03 +0000521 os << D->back()->getString();
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000522
523
524 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
525 os.str().c_str());
526
527 Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg);
528 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000529}