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 ProgramState.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_GR_BUGREPORTER
16 #define LLVM_CLANG_GR_BUGREPORTER
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/ImmutableList.h"
22 #include "llvm/ADT/ImmutableSet.h"
23 #include "llvm/ADT/SmallSet.h"
36 class PathDiagnosticPiece;
37 class PathDiagnosticClient;
41 class BugReporterContext;
46 //===----------------------------------------------------------------------===//
47 // Interface for individual bug reports.
48 //===----------------------------------------------------------------------===//
50 class BugReporterVisitor : public llvm::FoldingSetNode {
52 virtual ~BugReporterVisitor();
53 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
54 const ExplodedNode *PrevN,
55 BugReporterContext &BRC) = 0;
57 virtual bool isOwnedByReporterContext() { return true; }
58 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0;
61 class BugReport : public BugReporterVisitor {
65 virtual ~NodeResolver() {}
66 virtual const ExplodedNode*
67 getOriginalNode(const ExplodedNode *N) = 0;
70 typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data,
71 const ExplodedNode *N);
72 typedef const SourceRange *ranges_iterator;
75 friend class BugReporter;
76 friend class BugReportEquivClass;
77 typedef SmallVector<std::pair<VisitorCreator, const void*>, 2> Creators;
80 std::string ShortDescription;
81 std::string Description;
82 const ExplodedNode *ErrorNode;
83 SmallVector<SourceRange, 4> Ranges;
86 /// Profile to identify equivalent bug reports for error report coalescing.
87 virtual void Profile(llvm::FoldingSetNodeID& hash) const;
89 const Stmt *getStmt() const;
92 BugReport(BugType& bt, StringRef desc, const ExplodedNode *errornode)
93 : BT(bt), Description(desc), ErrorNode(errornode) {}
95 BugReport(BugType& bt, StringRef shortDesc, StringRef desc,
96 const ExplodedNode *errornode)
97 : BT(bt), ShortDescription(shortDesc), Description(desc),
98 ErrorNode(errornode) {}
100 virtual ~BugReport();
102 bool isOwnedByReporterContext() { return false; }
104 const BugType& getBugType() const { return BT; }
105 BugType& getBugType() { return BT; }
107 const ExplodedNode *getErrorNode() const { return ErrorNode; }
109 const StringRef getDescription() const { return Description; }
111 const StringRef getShortDescription() const {
112 return ShortDescription.empty() ? Description : ShortDescription;
115 /// \brief This allows for addition of meta data to the diagnostic.
117 /// Currently, only the HTMLDiagnosticClient knows how to display it.
118 virtual std::pair<const char**,const char**> getExtraDescriptiveText() {
119 return std::make_pair((const char**)0,(const char**)0);
122 /// Provide custom definition for the last diagnostic piece on the path.
123 virtual PathDiagnosticPiece *getEndPath(BugReporterContext &BRC,
124 const ExplodedNode *N);
126 /// \brief Return the "definitive" location of the reported bug.
128 /// While a bug can span an entire path, usually there is a specific
129 /// location that can be used to identify where the key issue occurred.
130 /// This location is used by clients rendering diagnostics.
131 virtual SourceLocation getLocation() const;
133 /// \brief Add a range to a bug report.
135 /// Ranges are used to highlight regions of interest in the source code.
136 /// They should be at the same source code line as the BugReport location.
137 void addRange(SourceRange R) {
142 /// \brief Get the SourceRanges associated with the report.
143 virtual std::pair<ranges_iterator, ranges_iterator> getRanges();
145 /// \brief Add custom or predefined bug report visitors to this report.
147 /// The visitors should be used when the default trace is not sufficient.
148 /// For example, they allow constructing a more elaborate trace.
149 /// \sa registerConditionVisitor(), registerTrackNullOrUndefValue(),
150 /// registerFindLastStore(), registerNilReceiverVisitor(), and
151 /// registerVarDeclsLastStore().
152 void addVisitorCreator(VisitorCreator creator, const void *data) {
153 creators.push_back(std::make_pair(creator, data));
156 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
157 const ExplodedNode *PrevN,
158 BugReporterContext &BR);
160 virtual void registerInitialVisitors(BugReporterContext &BRC,
161 const ExplodedNode *N) {}
164 //===----------------------------------------------------------------------===//
165 // BugTypes (collections of related reports).
166 //===----------------------------------------------------------------------===//
168 class BugReportEquivClass : public llvm::FoldingSetNode {
169 /// List of *owned* BugReport objects.
170 std::list<BugReport*> Reports;
172 friend class BugReporter;
173 void AddReport(BugReport* R) { Reports.push_back(R); }
175 BugReportEquivClass(BugReport* R) { Reports.push_back(R); }
176 ~BugReportEquivClass();
178 void Profile(llvm::FoldingSetNodeID& ID) const {
179 assert(!Reports.empty());
180 (*Reports.begin())->Profile(ID);
184 std::list<BugReport*>::iterator impl;
186 iterator(std::list<BugReport*>::iterator i) : impl(i) {}
187 iterator &operator++() { ++impl; return *this; }
188 bool operator==(const iterator &I) const { return I.impl == impl; }
189 bool operator!=(const iterator &I) const { return I.impl != impl; }
190 BugReport* operator*() const { return *impl; }
191 BugReport* operator->() const { return *impl; }
194 class const_iterator {
195 std::list<BugReport*>::const_iterator impl;
197 const_iterator(std::list<BugReport*>::const_iterator i) : impl(i) {}
198 const_iterator &operator++() { ++impl; return *this; }
199 bool operator==(const const_iterator &I) const { return I.impl == impl; }
200 bool operator!=(const const_iterator &I) const { return I.impl != impl; }
201 const BugReport* operator*() const { return *impl; }
202 const BugReport* operator->() const { return *impl; }
205 iterator begin() { return iterator(Reports.begin()); }
206 iterator end() { return iterator(Reports.end()); }
208 const_iterator begin() const { return const_iterator(Reports.begin()); }
209 const_iterator end() const { return const_iterator(Reports.end()); }
212 //===----------------------------------------------------------------------===//
213 // BugReporter and friends.
214 //===----------------------------------------------------------------------===//
216 class BugReporterData {
218 virtual ~BugReporterData();
219 virtual Diagnostic& getDiagnostic() = 0;
220 virtual PathDiagnosticClient* getPathDiagnosticClient() = 0;
221 virtual ASTContext &getASTContext() = 0;
222 virtual SourceManager& getSourceManager() = 0;
225 /// BugReporter is a utility class for generating PathDiagnostics for analysis.
226 /// It collects the BugReports and BugTypes and knows how to generate
227 /// and flush the corresponding diagnostics.
230 enum Kind { BaseBRKind, GRBugReporterKind };
233 typedef llvm::ImmutableSet<BugType*> BugTypesTy;
234 BugTypesTy::Factory F;
240 /// Generate and flush the diagnostics for the given bug report.
241 void FlushReport(BugReportEquivClass& EQ);
243 /// The set of bug reports tracked by the BugReporter.
244 llvm::FoldingSet<BugReportEquivClass> EQClasses;
247 BugReporter(BugReporterData& d, Kind k) : BugTypes(F.getEmptySet()), kind(k),
251 BugReporter(BugReporterData& d) : BugTypes(F.getEmptySet()), kind(BaseBRKind),
253 virtual ~BugReporter();
255 /// \brief Generate and flush diagnostics for all bug reports.
258 Kind getKind() const { return kind; }
260 Diagnostic& getDiagnostic() {
261 return D.getDiagnostic();
264 PathDiagnosticClient* getPathDiagnosticClient() {
265 return D.getPathDiagnosticClient();
268 /// \brief Iterator over the set of BugTypes tracked by the BugReporter.
269 typedef BugTypesTy::iterator iterator;
270 iterator begin() { return BugTypes.begin(); }
271 iterator end() { return BugTypes.end(); }
273 /// \brief Iterator over the set of BugReports tracked by the BugReporter.
274 typedef llvm::FoldingSet<BugReportEquivClass>::iterator EQClasses_iterator;
275 EQClasses_iterator EQClasses_begin() { return EQClasses.begin(); }
276 EQClasses_iterator EQClasses_end() { return EQClasses.end(); }
278 ASTContext &getContext() { return D.getASTContext(); }
280 SourceManager& getSourceManager() { return D.getSourceManager(); }
282 virtual void GeneratePathDiagnostic(PathDiagnostic& pathDiagnostic,
283 SmallVectorImpl<BugReport *> &bugReports) {}
285 void Register(BugType *BT);
287 /// \brief Add the given report to the set of reports tracked by BugReporter.
289 /// The reports are usually generated by the checkers. Further, they are
290 /// folded based on the profile value, which is done to coalesce similar
292 void EmitReport(BugReport *R);
294 void EmitBasicReport(StringRef BugName, StringRef BugStr,
296 SourceRange* RangeBeg, unsigned NumRanges);
298 void EmitBasicReport(StringRef BugName, StringRef BugCategory,
299 StringRef BugStr, SourceLocation Loc,
300 SourceRange* RangeBeg, unsigned NumRanges);
303 void EmitBasicReport(StringRef BugName, StringRef BugStr,
304 SourceLocation Loc) {
305 EmitBasicReport(BugName, BugStr, Loc, 0, 0);
308 void EmitBasicReport(StringRef BugName, StringRef BugCategory,
309 StringRef BugStr, SourceLocation Loc) {
310 EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0);
313 void EmitBasicReport(StringRef BugName, StringRef BugStr,
314 SourceLocation Loc, SourceRange R) {
315 EmitBasicReport(BugName, BugStr, Loc, &R, 1);
318 void EmitBasicReport(StringRef BugName, StringRef Category,
319 StringRef BugStr, SourceLocation Loc,
321 EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1);
324 static bool classof(const BugReporter* R) { return true; }
327 llvm::StringMap<BugType *> StrBugTypes;
329 /// \brief Returns a BugType that is associated with the given name and
331 BugType *getBugTypeForName(StringRef name, StringRef category);
334 // FIXME: Get rid of GRBugReporter. It's the wrong abstraction.
335 class GRBugReporter : public BugReporter {
337 llvm::SmallSet<SymbolRef, 10> NotableSymbols;
339 GRBugReporter(BugReporterData& d, ExprEngine& eng)
340 : BugReporter(d, GRBugReporterKind), Eng(eng) {}
342 virtual ~GRBugReporter();
344 /// getEngine - Return the analysis engine used to analyze a given
345 /// function or method.
346 ExprEngine &getEngine() { return Eng; }
348 /// getGraph - Get the exploded graph created by the analysis engine
349 /// for the analyzed method or function.
350 ExplodedGraph &getGraph();
352 /// getStateManager - Return the state manager used by the analysis
354 ProgramStateManager &getStateManager();
356 virtual void GeneratePathDiagnostic(PathDiagnostic &pathDiagnostic,
357 SmallVectorImpl<BugReport*> &bugReports);
359 void addNotableSymbol(SymbolRef Sym) {
360 NotableSymbols.insert(Sym);
363 bool isNotable(SymbolRef Sym) const {
364 return (bool) NotableSymbols.count(Sym);
367 /// classof - Used by isa<>, cast<>, and dyn_cast<>.
368 static bool classof(const BugReporter* R) {
369 return R->getKind() == GRBugReporterKind;
373 class BugReporterContext {
375 // Not the most efficient data structure, but we use an ImmutableList for the
376 // Callbacks because it is safe to make additions to list during iteration.
377 llvm::ImmutableList<BugReporterVisitor*>::Factory F;
378 llvm::ImmutableList<BugReporterVisitor*> Callbacks;
379 llvm::FoldingSet<BugReporterVisitor> CallbacksSet;
381 BugReporterContext(GRBugReporter& br) : BR(br), Callbacks(F.getEmptyList()) {}
382 virtual ~BugReporterContext();
384 void addVisitor(BugReporterVisitor* visitor);
386 typedef llvm::ImmutableList<BugReporterVisitor*>::iterator visitor_iterator;
387 visitor_iterator visitor_begin() { return Callbacks.begin(); }
388 visitor_iterator visitor_end() { return Callbacks.end(); }
390 GRBugReporter& getBugReporter() { return BR; }
392 ExplodedGraph &getGraph() { return BR.getGraph(); }
394 void addNotableSymbol(SymbolRef Sym) {
395 // FIXME: For now forward to GRBugReporter.
396 BR.addNotableSymbol(Sym);
399 bool isNotable(SymbolRef Sym) const {
400 // FIXME: For now forward to GRBugReporter.
401 return BR.isNotable(Sym);
404 ProgramStateManager& getStateManager() {
405 return BR.getStateManager();
408 SValBuilder& getSValBuilder() {
409 return getStateManager().getSValBuilder();
412 ASTContext &getASTContext() {
413 return BR.getContext();
416 SourceManager& getSourceManager() {
417 return BR.getSourceManager();
420 virtual BugReport::NodeResolver& getNodeResolver() = 0;
423 class DiagBugReport : public BugReport {
424 std::list<std::string> Strs;
427 DiagBugReport(BugType& D, StringRef desc, FullSourceLoc l) :
428 BugReport(D, desc, 0), L(l) {}
430 virtual ~DiagBugReport() {}
432 // FIXME: Move out-of-line (virtual function).
433 SourceLocation getLocation() const { return L; }
435 void addString(StringRef s) { Strs.push_back(s); }
437 typedef std::list<std::string>::const_iterator str_iterator;
438 str_iterator str_begin() const { return Strs.begin(); }
439 str_iterator str_end() const { return Strs.end(); }
442 //===----------------------------------------------------------------------===//
443 //===----------------------------------------------------------------------===//
445 namespace bugreporter {
447 const Stmt *GetDerefExpr(const ExplodedNode *N);
448 const Stmt *GetDenomExpr(const ExplodedNode *N);
449 const Stmt *GetCalleeExpr(const ExplodedNode *N);
450 const Stmt *GetRetValExpr(const ExplodedNode *N);
452 void registerConditionVisitor(BugReporterContext &BRC);
454 void registerTrackNullOrUndefValue(BugReporterContext &BRC, const void *stmt,
455 const ExplodedNode *N);
457 void registerFindLastStore(BugReporterContext &BRC, const void *memregion,
458 const ExplodedNode *N);
460 void registerNilReceiverVisitor(BugReporterContext &BRC);
462 void registerVarDeclsLastStore(BugReporterContext &BRC, const void *stmt,
463 const ExplodedNode *N);
465 } // end namespace clang::bugreporter
467 //===----------------------------------------------------------------------===//
469 } // end GR namespace
471 } // end clang namespace