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/Analysis/PathSensitive/GRState.h"
21 #include "clang/Analysis/PathSensitive/ExplodedGraph.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.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, const char* desc, const ExplodedNode *n)
84 : BT(bt), Description(desc), EndNode(n) {}
86 BugReport(BugType& bt, const char* shortDesc, const char* 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 std::string& getDescription() const { return Description; }
108 const std::string& 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()); }
188 const std::string Name;
189 const std::string Category;
190 llvm::FoldingSet<BugReportEquivClass> EQClasses;
191 friend class BugReporter;
194 BugType(const char *name, const char* cat)
195 : Name(name), Category(cat), SuppressonSink(false) {}
198 // FIXME: Should these be made strings as well?
199 const std::string& getName() const { return Name; }
200 const std::string& getCategory() const { return Category; }
202 /// isSuppressOnSink - Returns true if bug reports associated with this bug
203 /// type should be suppressed if the end node of the report is post-dominated
205 bool isSuppressOnSink() const { return SuppressonSink; }
206 void setSuppressOnSink(bool x) { SuppressonSink = x; }
208 virtual void FlushReports(BugReporter& BR);
210 typedef llvm::FoldingSet<BugReportEquivClass>::iterator iterator;
211 iterator begin() { return EQClasses.begin(); }
212 iterator end() { return EQClasses.end(); }
214 typedef llvm::FoldingSet<BugReportEquivClass>::const_iterator const_iterator;
215 const_iterator begin() const { return EQClasses.begin(); }
216 const_iterator end() const { return EQClasses.end(); }
219 //===----------------------------------------------------------------------===//
220 // Specialized subclasses of BugReport.
221 //===----------------------------------------------------------------------===//
223 // FIXME: Collapse this with the default BugReport class.
224 class RangedBugReport : public BugReport {
225 std::vector<SourceRange> Ranges;
227 RangedBugReport(BugType& D, const char* description, ExplodedNode *n)
228 : BugReport(D, description, n) {}
230 RangedBugReport(BugType& D, const char *shortDescription,
231 const char *description, ExplodedNode *n)
232 : BugReport(D, shortDescription, description, n) {}
236 // FIXME: Move this out of line.
237 void addRange(SourceRange R) { Ranges.push_back(R); }
239 // FIXME: Move this out of line.
240 void getRanges(const SourceRange*& beg, const SourceRange*& end) {
242 if (Ranges.empty()) {
248 end = beg + Ranges.size();
253 class EnhancedBugReport : public RangedBugReport {
255 typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data,
256 const ExplodedNode *N);
259 typedef std::vector<std::pair<VisitorCreator, const void*> > Creators;
263 EnhancedBugReport(BugType& D, const char* description, ExplodedNode *n)
264 : RangedBugReport(D, description, n) {}
266 EnhancedBugReport(BugType& D, const char *shortDescription,
267 const char *description, ExplodedNode *n)
268 : RangedBugReport(D, shortDescription, description, n) {}
270 ~EnhancedBugReport() {}
272 void registerInitialVisitors(BugReporterContext& BRC, const ExplodedNode* N) {
273 for (Creators::iterator I = creators.begin(), E = creators.end(); I!=E; ++I)
274 I->first(BRC, I->second, N);
277 void addVisitorCreator(VisitorCreator creator, const void *data) {
278 creators.push_back(std::make_pair(creator, data));
282 //===----------------------------------------------------------------------===//
283 // BugReporter and friends.
284 //===----------------------------------------------------------------------===//
286 class BugReporterData {
288 virtual ~BugReporterData();
289 virtual Diagnostic& getDiagnostic() = 0;
290 virtual PathDiagnosticClient* getPathDiagnosticClient() = 0;
291 virtual ASTContext& getASTContext() = 0;
292 virtual SourceManager& getSourceManager() = 0;
297 enum Kind { BaseBRKind, GRBugReporterKind };
300 typedef llvm::ImmutableSet<BugType*> BugTypesTy;
301 BugTypesTy::Factory F;
307 void FlushReport(BugReportEquivClass& EQ);
310 BugReporter(BugReporterData& d, Kind k) : BugTypes(F.GetEmptySet()), kind(k), D(d) {}
313 BugReporter(BugReporterData& d) : BugTypes(F.GetEmptySet()), kind(BaseBRKind), D(d) {}
314 virtual ~BugReporter();
318 Kind getKind() const { return kind; }
320 Diagnostic& getDiagnostic() {
321 return D.getDiagnostic();
324 PathDiagnosticClient* getPathDiagnosticClient() {
325 return D.getPathDiagnosticClient();
328 typedef BugTypesTy::iterator iterator;
329 iterator begin() { return BugTypes.begin(); }
330 iterator end() { return BugTypes.end(); }
332 ASTContext& getContext() { return D.getASTContext(); }
334 SourceManager& getSourceManager() { return D.getSourceManager(); }
336 virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
337 BugReportEquivClass& EQ) {}
339 void Register(BugType *BT);
341 void EmitReport(BugReport *R);
343 void EmitBasicReport(const char* BugName, const char* BugStr,
345 SourceRange* RangeBeg, unsigned NumRanges);
347 void EmitBasicReport(const char* BugName, const char* BugCategory,
348 const char* BugStr, SourceLocation Loc,
349 SourceRange* RangeBeg, unsigned NumRanges);
352 void EmitBasicReport(const char* BugName, const char* BugStr,
353 SourceLocation Loc) {
354 EmitBasicReport(BugName, BugStr, Loc, 0, 0);
357 void EmitBasicReport(const char* BugName, const char* BugCategory,
358 const char* BugStr, SourceLocation Loc) {
359 EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0);
362 void EmitBasicReport(const char* BugName, const char* BugStr,
363 SourceLocation Loc, SourceRange R) {
364 EmitBasicReport(BugName, BugStr, Loc, &R, 1);
367 void EmitBasicReport(const char* BugName, const char* Category,
368 const char* BugStr, SourceLocation Loc, SourceRange R) {
369 EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1);
372 static bool classof(const BugReporter* R) { return true; }
375 // FIXME: Get rid of GRBugReporter. It's the wrong abstraction.
376 class GRBugReporter : public BugReporter {
378 llvm::SmallSet<SymbolRef, 10> NotableSymbols;
380 GRBugReporter(BugReporterData& d, GRExprEngine& eng)
381 : BugReporter(d, GRBugReporterKind), Eng(eng) {}
383 virtual ~GRBugReporter();
385 /// getEngine - Return the analysis engine used to analyze a given
386 /// function or method.
387 GRExprEngine &getEngine() { return Eng; }
389 /// getGraph - Get the exploded graph created by the analysis engine
390 /// for the analyzed method or function.
391 ExplodedGraph &getGraph();
393 /// getStateManager - Return the state manager used by the analysis
395 GRStateManager &getStateManager();
397 virtual void GeneratePathDiagnostic(PathDiagnostic& PD,
398 BugReportEquivClass& R);
400 void addNotableSymbol(SymbolRef Sym) {
401 NotableSymbols.insert(Sym);
404 bool isNotable(SymbolRef Sym) const {
405 return (bool) NotableSymbols.count(Sym);
408 /// classof - Used by isa<>, cast<>, and dyn_cast<>.
409 static bool classof(const BugReporter* R) {
410 return R->getKind() == GRBugReporterKind;
414 class BugReporterContext {
416 std::vector<BugReporterVisitor*> Callbacks;
418 BugReporterContext(GRBugReporter& br) : BR(br) {}
419 virtual ~BugReporterContext();
421 void addVisitor(BugReporterVisitor* visitor) {
422 if (visitor) Callbacks.push_back(visitor);
425 typedef std::vector<BugReporterVisitor*>::iterator visitor_iterator;
426 visitor_iterator visitor_begin() { return Callbacks.begin(); }
427 visitor_iterator visitor_end() { return Callbacks.end(); }
429 GRBugReporter& getBugReporter() { return BR; }
431 ExplodedGraph &getGraph() { return BR.getGraph(); }
433 void addNotableSymbol(SymbolRef Sym) {
434 // FIXME: For now forward to GRBugReporter.
435 BR.addNotableSymbol(Sym);
438 bool isNotable(SymbolRef Sym) const {
439 // FIXME: For now forward to GRBugReporter.
440 return BR.isNotable(Sym);
443 GRStateManager& getStateManager() {
444 return BR.getStateManager();
447 ValueManager& getValueManager() {
448 return getStateManager().getValueManager();
451 ASTContext& getASTContext() {
452 return BR.getContext();
455 SourceManager& getSourceManager() {
456 return BR.getSourceManager();
459 virtual BugReport::NodeResolver& getNodeResolver() = 0;
462 class DiagBugReport : public RangedBugReport {
463 std::list<std::string> Strs;
466 DiagBugReport(BugType& D, const char* desc, FullSourceLoc l) :
467 RangedBugReport(D, desc, 0), L(l) {}
469 virtual ~DiagBugReport() {}
471 // FIXME: Move out-of-line (virtual function).
472 SourceLocation getLocation() const { return L; }
474 void addString(const std::string& s) { Strs.push_back(s); }
476 typedef std::list<std::string>::const_iterator str_iterator;
477 str_iterator str_begin() const { return Strs.begin(); }
478 str_iterator str_end() const { return Strs.end(); }
481 //===----------------------------------------------------------------------===//
482 //===----------------------------------------------------------------------===//
484 namespace bugreporter {
486 const Stmt *GetDerefExpr(const ExplodedNode *N);
487 const Stmt *GetReceiverExpr(const ExplodedNode *N);
488 const Stmt *GetDenomExpr(const ExplodedNode *N);
489 const Stmt *GetCalleeExpr(const ExplodedNode *N);
490 const Stmt *GetRetValExpr(const ExplodedNode *N);
492 void registerTrackNullOrUndefValue(BugReporterContext& BRC, const void *stmt,
493 const ExplodedNode* N);
495 } // end namespace clang::bugreporter
497 //===----------------------------------------------------------------------===//
499 } // end clang namespace