1 //===--- BugReporter.h - Generate PathDiagnostics --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines BugReporter, a utility class for generating
11 // PathDiagnostics for analyses based on GRState.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_ANALYSIS_BUGREPORTER
16 #define LLVM_CLANG_ANALYSIS_BUGREPORTER
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Checker/PathSensitive/GRState.h"
21 #include "clang/Checker/PathSensitive/ExplodedGraph.h"
22 #include "clang/Checker/BugReporter/BugType.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/ImmutableSet.h"
32 class PathDiagnosticPiece;
33 class PathDiagnosticClient;
37 class BugReporterContext;
44 //===----------------------------------------------------------------------===//
45 // Interface for individual bug reports.
46 //===----------------------------------------------------------------------===//
48 class BugReporterVisitor {
50 virtual ~BugReporterVisitor();
51 virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
52 const ExplodedNode* PrevN,
53 BugReporterContext& BRC) = 0;
55 virtual bool isOwnedByReporterContext() { return true; }
58 // FIXME: Combine this with RangedBugReport and remove RangedBugReport.
59 class BugReport : public BugReporterVisitor {
62 std::string ShortDescription;
63 std::string Description;
64 const ExplodedNode *EndNode;
68 friend class BugReporter;
69 friend class BugReportEquivClass;
71 virtual void Profile(llvm::FoldingSetNodeID& hash) const {
72 hash.AddInteger(getLocation().getRawEncoding());
78 virtual ~NodeResolver() {}
79 virtual const ExplodedNode*
80 getOriginalNode(const ExplodedNode* N) = 0;
83 BugReport(BugType& bt, llvm::StringRef desc, const ExplodedNode *n)
84 : BT(bt), Description(desc), EndNode(n) {}
86 BugReport(BugType& bt, llvm::StringRef shortDesc, llvm::StringRef desc,
87 const ExplodedNode *n)
88 : BT(bt), ShortDescription(shortDesc), Description(desc), EndNode(n) {}
92 virtual bool isOwnedByReporterContext() { return false; }
94 const BugType& getBugType() const { return BT; }
95 BugType& getBugType() { return BT; }
97 // FIXME: Perhaps this should be moved into a subclass?
98 const ExplodedNode* getEndNode() const { return EndNode; }
100 // FIXME: Do we need this? Maybe getLocation() should return a ProgramPoint
102 // FIXME: If we do need it, we can probably just make it private to
104 const Stmt* getStmt() const;
106 const llvm::StringRef getDescription() const { return Description; }
108 const llvm::StringRef getShortDescription() const {
109 return ShortDescription.empty() ? Description : ShortDescription;
112 // FIXME: Is this needed?
113 virtual std::pair<const char**,const char**> getExtraDescriptiveText() {
114 return std::make_pair((const char**)0,(const char**)0);
117 // FIXME: Perhaps move this into a subclass.
118 virtual PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
119 const ExplodedNode* N);
121 /// getLocation - Return the "definitive" location of the reported bug.
122 /// While a bug can span an entire path, usually there is a specific
123 /// location that can be used to identify where the key issue occured.
124 /// This location is used by clients rendering diagnostics.
125 virtual SourceLocation getLocation() const;
127 /// getRanges - Returns the source ranges associated with this bug.
128 virtual void getRanges(const SourceRange*& beg, const SourceRange*& end);
130 virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
131 const ExplodedNode* PrevN,
132 BugReporterContext& BR);
134 virtual void registerInitialVisitors(BugReporterContext& BRC,
135 const ExplodedNode* N) {}
138 //===----------------------------------------------------------------------===//
139 // BugTypes (collections of related reports).
140 //===----------------------------------------------------------------------===//
142 class BugReportEquivClass : public llvm::FoldingSetNode {
143 // List of *owned* BugReport objects.
144 std::list<BugReport*> Reports;
146 friend class BugReporter;
147 void AddReport(BugReport* R) { Reports.push_back(R); }
149 BugReportEquivClass(BugReport* R) { Reports.push_back(R); }
150 ~BugReportEquivClass();
152 void Profile(llvm::FoldingSetNodeID& ID) const {
153 assert(!Reports.empty());
154 (*Reports.begin())->Profile(ID);
158 std::list<BugReport*>::iterator impl;
160 iterator(std::list<BugReport*>::iterator i) : impl(i) {}
161 iterator& operator++() { ++impl; return *this; }
162 bool operator==(const iterator& I) const { return I.impl == impl; }
163 bool operator!=(const iterator& I) const { return I.impl != impl; }
164 BugReport* operator*() const { return *impl; }
165 BugReport* operator->() const { return *impl; }
168 class const_iterator {
169 std::list<BugReport*>::const_iterator impl;
171 const_iterator(std::list<BugReport*>::const_iterator i) : impl(i) {}
172 const_iterator& operator++() { ++impl; return *this; }
173 bool operator==(const const_iterator& I) const { return I.impl == impl; }
174 bool operator!=(const const_iterator& I) const { return I.impl != impl; }
175 const BugReport* operator*() const { return *impl; }
176 const BugReport* operator->() const { return *impl; }
179 iterator begin() { return iterator(Reports.begin()); }
180 iterator end() { return iterator(Reports.end()); }
182 const_iterator begin() const { return const_iterator(Reports.begin()); }
183 const_iterator end() const { return const_iterator(Reports.end()); }
187 //===----------------------------------------------------------------------===//
188 // Specialized subclasses of BugReport.
189 //===----------------------------------------------------------------------===//
191 // FIXME: Collapse this with the default BugReport class.
192 class RangedBugReport : public BugReport {
193 std::vector<SourceRange> Ranges;
195 RangedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
196 : BugReport(D, description, n) {}
198 RangedBugReport(BugType& D, llvm::StringRef shortDescription,
199 llvm::StringRef description, ExplodedNode *n)
200 : BugReport(D, shortDescription, description, n) {}
204 // FIXME: Move this out of line.
205 void addRange(SourceRange R) {
210 // FIXME: Move this out of line.
211 void getRanges(const SourceRange*& beg, const SourceRange*& end) {
213 if (Ranges.empty()) {
219 end = beg + Ranges.size();
224 class EnhancedBugReport : public RangedBugReport {
226 typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data,
227 const ExplodedNode *N);
230 typedef std::vector<std::pair<VisitorCreator, const void*> > Creators;
234 EnhancedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
235 : RangedBugReport(D, description, n) {}
237 EnhancedBugReport(BugType& D, llvm::StringRef shortDescription,
238 llvm::StringRef description, ExplodedNode *n)
239 : RangedBugReport(D, shortDescription, description, n) {}
241 ~EnhancedBugReport() {}
243 void registerInitialVisitors(BugReporterContext& BRC, const ExplodedNode* N) {
244 for (Creators::iterator I = creators.begin(), E = creators.end(); I!=E; ++I)
245 I->first(BRC, I->second, N);
248 void addVisitorCreator(VisitorCreator creator, const void *data) {
249 creators.push_back(std::make_pair(creator, data));
253 //===----------------------------------------------------------------------===//
254 // BugReporter and friends.
255 //===----------------------------------------------------------------------===//
257 class BugReporterData {
259 virtual ~BugReporterData();
260 virtual Diagnostic& getDiagnostic() = 0;
261 virtual PathDiagnosticClient* getPathDiagnosticClient() = 0;
262 virtual ASTContext& getASTContext() = 0;
263 virtual SourceManager& getSourceManager() = 0;
268 enum Kind { BaseBRKind, GRBugReporterKind };
271 typedef llvm::ImmutableSet<BugType*> BugTypesTy;
272 BugTypesTy::Factory F;
278 void FlushReport(BugReportEquivClass& EQ);
281 BugReporter(BugReporterData& d, Kind k) : BugTypes(F.GetEmptySet()), kind(k), D(d) {}
284 BugReporter(BugReporterData& d) : BugTypes(F.GetEmptySet()), kind(BaseBRKind), D(d) {}
285 virtual ~BugReporter();
289 Kind getKind() const { return kind; }
291 Diagnostic& getDiagnostic() {
292 return D.getDiagnostic();
295 PathDiagnosticClient* getPathDiagnosticClient() {
296 return D.getPathDiagnosticClient();
299 typedef BugTypesTy::iterator iterator;
300 iterator begin() { return BugTypes.begin(); }
301 iterator end() { return BugTypes.end(); }
303 ASTContext& getContext() { return D.getASTContext(); }
305 SourceManager& getSourceManager() { return D.getSourceManager(); }
307 virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
308 BugReportEquivClass& EQ) {}
310 void Register(BugType *BT);
312 void EmitReport(BugReport *R);
314 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
316 SourceRange* RangeBeg, unsigned NumRanges);
318 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory,
319 llvm::StringRef BugStr, SourceLocation Loc,
320 SourceRange* RangeBeg, unsigned NumRanges);
323 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
324 SourceLocation Loc) {
325 EmitBasicReport(BugName, BugStr, Loc, 0, 0);
328 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory,
329 llvm::StringRef BugStr, SourceLocation Loc) {
330 EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0);
333 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr,
334 SourceLocation Loc, SourceRange R) {
335 EmitBasicReport(BugName, BugStr, Loc, &R, 1);
338 void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef Category,
339 llvm::StringRef BugStr, SourceLocation Loc,
341 EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1);
344 static bool classof(const BugReporter* R) { return true; }
347 // FIXME: Get rid of GRBugReporter. It's the wrong abstraction.
348 class GRBugReporter : public BugReporter {
350 llvm::SmallSet<SymbolRef, 10> NotableSymbols;
352 GRBugReporter(BugReporterData& d, GRExprEngine& eng)
353 : BugReporter(d, GRBugReporterKind), Eng(eng) {}
355 virtual ~GRBugReporter();
357 /// getEngine - Return the analysis engine used to analyze a given
358 /// function or method.
359 GRExprEngine &getEngine() { return Eng; }
361 /// getGraph - Get the exploded graph created by the analysis engine
362 /// for the analyzed method or function.
363 ExplodedGraph &getGraph();
365 /// getStateManager - Return the state manager used by the analysis
367 GRStateManager &getStateManager();
369 virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
370 BugReportEquivClass& R);
372 void addNotableSymbol(SymbolRef Sym) {
373 NotableSymbols.insert(Sym);
376 bool isNotable(SymbolRef Sym) const {
377 return (bool) NotableSymbols.count(Sym);
380 /// classof - Used by isa<>, cast<>, and dyn_cast<>.
381 static bool classof(const BugReporter* R) {
382 return R->getKind() == GRBugReporterKind;
386 class BugReporterContext {
388 std::vector<BugReporterVisitor*> Callbacks;
390 BugReporterContext(GRBugReporter& br) : BR(br) {}
391 virtual ~BugReporterContext();
393 void addVisitor(BugReporterVisitor* visitor) {
394 if (visitor) Callbacks.push_back(visitor);
397 typedef std::vector<BugReporterVisitor*>::iterator visitor_iterator;
398 visitor_iterator visitor_begin() { return Callbacks.begin(); }
399 visitor_iterator visitor_end() { return Callbacks.end(); }
401 GRBugReporter& getBugReporter() { return BR; }
403 ExplodedGraph &getGraph() { return BR.getGraph(); }
405 void addNotableSymbol(SymbolRef Sym) {
406 // FIXME: For now forward to GRBugReporter.
407 BR.addNotableSymbol(Sym);
410 bool isNotable(SymbolRef Sym) const {
411 // FIXME: For now forward to GRBugReporter.
412 return BR.isNotable(Sym);
415 GRStateManager& getStateManager() {
416 return BR.getStateManager();
419 ValueManager& getValueManager() {
420 return getStateManager().getValueManager();
423 ASTContext& getASTContext() {
424 return BR.getContext();
427 SourceManager& getSourceManager() {
428 return BR.getSourceManager();
431 virtual BugReport::NodeResolver& getNodeResolver() = 0;
434 class DiagBugReport : public RangedBugReport {
435 std::list<std::string> Strs;
438 DiagBugReport(BugType& D, llvm::StringRef desc, FullSourceLoc l) :
439 RangedBugReport(D, desc, 0), L(l) {}
441 virtual ~DiagBugReport() {}
443 // FIXME: Move out-of-line (virtual function).
444 SourceLocation getLocation() const { return L; }
446 void addString(llvm::StringRef s) { Strs.push_back(s); }
448 typedef std::list<std::string>::const_iterator str_iterator;
449 str_iterator str_begin() const { return Strs.begin(); }
450 str_iterator str_end() const { return Strs.end(); }
453 //===----------------------------------------------------------------------===//
454 //===----------------------------------------------------------------------===//
456 namespace bugreporter {
458 const Stmt *GetDerefExpr(const ExplodedNode *N);
459 const Stmt *GetReceiverExpr(const ExplodedNode *N);
460 const Stmt *GetDenomExpr(const ExplodedNode *N);
461 const Stmt *GetCalleeExpr(const ExplodedNode *N);
462 const Stmt *GetRetValExpr(const ExplodedNode *N);
464 void registerTrackNullOrUndefValue(BugReporterContext& BRC, const void *stmt,
465 const ExplodedNode* N);
467 void registerFindLastStore(BugReporterContext& BRC, const void *memregion,
468 const ExplodedNode *N);
471 } // end namespace clang::bugreporter
473 //===----------------------------------------------------------------------===//
475 } // end clang namespace