blob: 800496a1614292a35c37bdd6585c31f36d58ba21 [file] [log] [blame]
Ted Kremenekd3abcdf2008-03-27 03:49:32 +00001//===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- 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 the PathDiagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek97b40032009-03-26 21:39:39 +000015#include "clang/AST/Expr.h"
Ted Kremenekfea5f5a2009-04-06 22:33:35 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000018#include "clang/AST/StmtCXX.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000019#include "llvm/ADT/SmallString.h"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000020#include "llvm/Support/Casting.h"
Ted Kremenek11401862009-06-26 00:43:22 +000021
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000022using namespace clang;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000023using llvm::dyn_cast;
24using llvm::isa;
25
26bool PathDiagnosticMacroPiece::containsEvent() const {
27 for (const_iterator I = begin(), E = end(); I!=E; ++I) {
28 if (isa<PathDiagnosticEventPiece>(*I))
29 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000031 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
32 if (MP->containsEvent())
33 return true;
34 }
35
36 return false;
37}
Ted Kremenek8af29752009-02-26 21:30:32 +000038
39static size_t GetNumCharsToLastNonPeriod(const char *s) {
40 const char *start = s;
Mike Stump1eb44332009-09-09 15:08:12 +000041 const char *lastNonPeriod = 0;
Ted Kremenek8af29752009-02-26 21:30:32 +000042
43 for ( ; *s != '\0' ; ++s)
44 if (*s != '.') lastNonPeriod = s;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenek8af29752009-02-26 21:30:32 +000046 if (!lastNonPeriod)
47 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenek8af29752009-02-26 21:30:32 +000049 return (lastNonPeriod - start) + 1;
50}
51
52static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) {
53 return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]);
54}
55
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000056PathDiagnosticPiece::PathDiagnosticPiece(const std::string& s,
Ted Kremeneke3ce2652009-03-02 19:39:50 +000057 Kind k, DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000058 : str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000059
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000060PathDiagnosticPiece::PathDiagnosticPiece(const char* s, Kind k,
Ted Kremenek8af29752009-02-26 21:30:32 +000061 DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000062 : str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek48504512009-03-06 07:53:30 +000063
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000064PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
65 : kind(k), Hint(hint) {}
Ted Kremenek082cb8d2009-03-12 18:41:53 +000066
Ted Kremenek4e063872009-03-06 22:10:49 +000067PathDiagnosticPiece::~PathDiagnosticPiece() {}
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +000068PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
69PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
Ted Kremenek4e063872009-03-06 22:10:49 +000070
71PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {
72 for (iterator I = begin(), E = end(); I != E; ++I) delete *I;
73}
74
Ted Kremenek48504512009-03-06 07:53:30 +000075PathDiagnostic::PathDiagnostic() : Size(0) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000076
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000077PathDiagnostic::~PathDiagnostic() {
78 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
79}
80
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000081void PathDiagnostic::resetPath(bool deletePieces) {
82 Size = 0;
83
84 if (deletePieces)
85 for (iterator I=begin(), E=end(); I!=E; ++I)
86 delete &*I;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000088 path.clear();
89}
90
Ted Kremeneka127cca2009-03-06 07:08:50 +000091
92PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc,
93 const char* category)
Ted Kremenek48504512009-03-06 07:53:30 +000094 : Size(0),
95 BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +000096 Desc(desc, GetNumCharsToLastNonPeriod(desc)),
97 Category(category, GetNumCharsToLastNonPeriod(category)) {}
98
99PathDiagnostic::PathDiagnostic(const std::string& bugtype,
Mike Stump1eb44332009-09-09 15:08:12 +0000100 const std::string& desc,
Ted Kremeneka127cca2009-03-06 07:08:50 +0000101 const std::string& category)
Ted Kremenek48504512009-03-06 07:53:30 +0000102 : Size(0),
103 BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +0000104 Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)),
105 Category(category, 0, GetNumCharsToLastNonPeriod(category)) {}
106
Chris Lattner0a14eee2008-11-18 07:04:44 +0000107void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
108 const DiagnosticInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000110 // Create a PathDiagnostic with a single piece.
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek55851142008-04-22 16:15:03 +0000112 PathDiagnostic* D = new PathDiagnostic();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattnere837f932008-11-18 04:44:58 +0000114 const char *LevelStr;
Ted Kremenek120187d2008-03-27 06:16:40 +0000115 switch (DiagLevel) {
Mike Stumpe87f5c12009-02-07 03:46:08 +0000116 default:
Chris Lattner41327582009-02-06 03:57:44 +0000117 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattnere837f932008-11-18 04:44:58 +0000118 case Diagnostic::Note: LevelStr = "note: "; break;
119 case Diagnostic::Warning: LevelStr = "warning: "; break;
120 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +0000121 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000122 }
Ted Kremenek120187d2008-03-27 06:16:40 +0000123
Chris Lattnerf4c83962008-11-19 06:51:40 +0000124 llvm::SmallString<100> StrC;
125 StrC += LevelStr;
126 Info.FormatDiagnostic(StrC);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner0a14eee2008-11-18 07:04:44 +0000128 PathDiagnosticPiece *P =
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000129 new PathDiagnosticEventPiece(Info.getLocation(),
Chris Lattnerf4c83962008-11-19 06:51:40 +0000130 std::string(StrC.begin(), StrC.end()));
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner0a14eee2008-11-18 07:04:44 +0000132 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
133 P->addRange(Info.getRange(i));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000134 for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i)
135 P->addCodeModificationHint(Info.getCodeModificationHint(i));
Ted Kremenek55851142008-04-22 16:15:03 +0000136 D->push_front(P);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000137
Mike Stump1eb44332009-09-09 15:08:12 +0000138 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000139}
Ted Kremenek97b40032009-03-26 21:39:39 +0000140
141//===----------------------------------------------------------------------===//
142// PathDiagnosticLocation methods.
143//===----------------------------------------------------------------------===//
144
145FullSourceLoc PathDiagnosticLocation::asLocation() const {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000146 assert(isValid());
Ted Kremenekb7737022009-03-26 21:42:51 +0000147 // Note that we want a 'switch' here so that the compiler can warn us in
148 // case we add more cases.
Ted Kremenek97b40032009-03-26 21:39:39 +0000149 switch (K) {
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000150 case SingleLocK:
151 case RangeK:
Ted Kremeneka5465002009-03-26 21:42:00 +0000152 break;
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000153 case StmtK:
Ted Kremenek6f002042009-03-26 23:12:02 +0000154 return FullSourceLoc(S->getLocStart(), const_cast<SourceManager&>(*SM));
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000155 case DeclK:
156 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
Ted Kremenek97b40032009-03-26 21:39:39 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek6f002042009-03-26 23:12:02 +0000159 return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM));
Ted Kremenek97b40032009-03-26 21:39:39 +0000160}
Ted Kremenekbecf8882009-03-26 21:48:17 +0000161
Ted Kremenek4c5fcd92009-04-22 22:26:10 +0000162PathDiagnosticRange PathDiagnosticLocation::asRange() const {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000163 assert(isValid());
Ted Kremenekbecf8882009-03-26 21:48:17 +0000164 // Note that we want a 'switch' here so that the compiler can warn us in
165 // case we add more cases.
166 switch (K) {
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000167 case SingleLocK:
Ted Kremenek4c5fcd92009-04-22 22:26:10 +0000168 return PathDiagnosticRange(R, true);
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000169 case RangeK:
Ted Kremenekbecf8882009-03-26 21:48:17 +0000170 break;
Ted Kremenek8f332902009-04-22 18:03:00 +0000171 case StmtK: {
172 const Stmt *S = asStmt();
173 switch (S->getStmtClass()) {
174 default:
175 break;
Ted Kremenek1e63c292009-05-15 02:05:25 +0000176 case Stmt::DeclStmtClass: {
177 const DeclStmt *DS = cast<DeclStmt>(S);
178 if (DS->isSingleDecl()) {
179 // Should always be the case, but we'll be defensive.
180 return SourceRange(DS->getLocStart(),
Mike Stump1eb44332009-09-09 15:08:12 +0000181 DS->getSingleDecl()->getLocation());
Ted Kremenek1e63c292009-05-15 02:05:25 +0000182 }
183 break;
184 }
Ted Kremenek8f332902009-04-22 18:03:00 +0000185 // FIXME: Provide better range information for different
186 // terminators.
187 case Stmt::IfStmtClass:
188 case Stmt::WhileStmtClass:
189 case Stmt::DoStmtClass:
190 case Stmt::ForStmtClass:
191 case Stmt::ChooseExprClass:
192 case Stmt::IndirectGotoStmtClass:
193 case Stmt::SwitchStmtClass:
194 case Stmt::ConditionalOperatorClass:
195 case Stmt::ObjCForCollectionStmtClass: {
196 SourceLocation L = S->getLocStart();
197 return SourceRange(L, L);
198 }
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenekbecf8882009-03-26 21:48:17 +0000201 return S->getSourceRange();
Ted Kremenek8f332902009-04-22 18:03:00 +0000202 }
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000203 case DeclK:
204 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
205 return MD->getSourceRange();
Douglas Gregor72971342009-04-18 00:02:19 +0000206 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
207 // FIXME: We would like to always get the function body, even
208 // when it needs to be de-serialized, but getting the
209 // ASTContext here requires significant changes.
Douglas Gregoraf3280f2009-09-12 00:08:48 +0000210 if (Stmt *Body = FD->getBody()) {
Sebastian Redld3a413d2009-04-26 20:35:05 +0000211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
212 return CS->getSourceRange();
213 else
214 return cast<CXXTryStmt>(Body)->getSourceRange();
215 }
Douglas Gregor72971342009-04-18 00:02:19 +0000216 }
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000217 else {
218 SourceLocation L = D->getLocation();
Ted Kremenek4c5fcd92009-04-22 22:26:10 +0000219 return PathDiagnosticRange(SourceRange(L, L), true);
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000220 }
Ted Kremenekbecf8882009-03-26 21:48:17 +0000221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenekbecf8882009-03-26 21:48:17 +0000223 return R;
224}
225
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000226void PathDiagnosticLocation::flatten() {
227 if (K == StmtK) {
228 R = asRange();
229 K = RangeK;
230 S = 0;
231 D = 0;
232 }
233 else if (K == DeclK) {
234 SourceLocation L = D->getLocation();
235 R = SourceRange(L, L);
236 K = SingleLocK;
237 S = 0;
238 D = 0;
239 }
240}
241
Ted Kremenekdb8338a2009-09-18 22:33:39 +0000242//===----------------------------------------------------------------------===//
243// FoldingSet profiling methods.
244//===----------------------------------------------------------------------===//
Ted Kremenekfea5f5a2009-04-06 22:33:35 +0000245
Ted Kremenekdb8338a2009-09-18 22:33:39 +0000246void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const {
247 ID.AddInteger((unsigned) K);
248 switch (K) {
249 case RangeK:
250 ID.AddInteger(R.getBegin().getRawEncoding());
251 ID.AddInteger(R.getEnd().getRawEncoding());
252 break;
253 case SingleLocK:
254 ID.AddInteger(R.getBegin().getRawEncoding());
255 break;
256 case StmtK:
257 ID.Add(S);
258 break;
259 case DeclK:
260 ID.Add(D);
261 break;
262 }
263 return;
264}
265
266void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const {
267 ID.AddInteger((unsigned) getKind());
268 ID.AddString(str);
269 // FIXME: Add profiling support for code hints.
270 ID.AddInteger((unsigned) getDisplayHint());
271 for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) {
272 ID.AddInteger(I->getBegin().getRawEncoding());
273 ID.AddInteger(I->getEnd().getRawEncoding());
274 }
275}
276
277void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const {
278 PathDiagnosticPiece::Profile(ID);
279 ID.Add(Pos);
280}
281
282void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const {
283 PathDiagnosticPiece::Profile(ID);
284 for (const_iterator I = begin(), E = end(); I != E; ++I)
285 ID.Add(*I);
286}
287
288void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const {
289 PathDiagnosticSpotPiece::Profile(ID);
290 for (const_iterator I = begin(), E = end(); I != E; ++I)
291 ID.Add(**I);
292}
293
294void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const {
295 ID.AddInteger(Size);
296 ID.AddString(BugType);
297 ID.AddString(Desc);
298 ID.AddString(Category);
299 for (const_iterator I = begin(), E = end(); I != E; ++I)
300 ID.Add(*I);
301
302 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I)
303 ID.AddString(*I);
304}