diff options
Diffstat (limited to 'include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h')
-rw-r--r-- | include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h | 360 |
1 files changed, 134 insertions, 226 deletions
diff --git a/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h b/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h index 3acbcd685bc0..bfb7ef8964ea 100644 --- a/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h +++ b/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h @@ -8,7 +8,7 @@ //===----------------------------------------------------------------------===// // // This file defines BugReporter, a utility class for generating -// PathDiagnostics for analyses based on GRState. +// PathDiagnostics for analyses based on ProgramState. // //===----------------------------------------------------------------------===// @@ -16,7 +16,9 @@ #define LLVM_CLANG_GR_BUGREPORTER #include "clang/Basic/SourceLocation.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h" +#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/ImmutableList.h" #include "llvm/ADT/ImmutableSet.h" @@ -26,121 +28,138 @@ namespace clang { class ASTContext; -class Diagnostic; +class DiagnosticsEngine; class Stmt; class ParentMap; namespace ento { class PathDiagnostic; -class PathDiagnosticPiece; -class PathDiagnosticClient; class ExplodedNode; class ExplodedGraph; +class BugReport; class BugReporter; class BugReporterContext; class ExprEngine; -class GRState; class BugType; //===----------------------------------------------------------------------===// // Interface for individual bug reports. //===----------------------------------------------------------------------===// -class BugReporterVisitor : public llvm::FoldingSetNode { +/// This class provides an interface through which checkers can create +/// individual bug reports. +class BugReport { public: - virtual ~BugReporterVisitor(); - virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N, - const ExplodedNode* PrevN, - BugReporterContext& BRC) = 0; + class NodeResolver { + public: + virtual ~NodeResolver() {} + virtual const ExplodedNode* + getOriginalNode(const ExplodedNode *N) = 0; + }; - virtual bool isOwnedByReporterContext() { return true; } - virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0; -}; + typedef const SourceRange *ranges_iterator; + typedef llvm::ImmutableList<BugReporterVisitor*>::iterator visitor_iterator; + typedef SmallVector<StringRef, 2> ExtraTextList; -// FIXME: Combine this with RangedBugReport and remove RangedBugReport. -class BugReport : public BugReporterVisitor { protected: + friend class BugReporter; + friend class BugReportEquivClass; + BugType& BT; std::string ShortDescription; std::string Description; + PathDiagnosticLocation Location; const ExplodedNode *ErrorNode; - mutable SourceRange R; - -protected: - friend class BugReporter; - friend class BugReportEquivClass; + SmallVector<SourceRange, 4> Ranges; + ExtraTextList ExtraText; - virtual void Profile(llvm::FoldingSetNodeID& hash) const { - hash.AddPointer(&BT); - hash.AddInteger(getLocation().getRawEncoding()); - hash.AddString(Description); - } + // Not the most efficient data structure, but we use an ImmutableList for the + // Callbacks because it is safe to make additions to list during iteration. + llvm::ImmutableList<BugReporterVisitor*>::Factory F; + llvm::ImmutableList<BugReporterVisitor*> Callbacks; + llvm::FoldingSet<BugReporterVisitor> CallbacksSet; public: - class NodeResolver { - public: - virtual ~NodeResolver() {} - virtual const ExplodedNode* - getOriginalNode(const ExplodedNode* N) = 0; - }; - - BugReport(BugType& bt, llvm::StringRef desc, const ExplodedNode *errornode) - : BT(bt), Description(desc), ErrorNode(errornode) {} + BugReport(BugType& bt, StringRef desc, const ExplodedNode *errornode) + : BT(bt), Description(desc), ErrorNode(errornode), + Callbacks(F.getEmptyList()) {} - BugReport(BugType& bt, llvm::StringRef shortDesc, llvm::StringRef desc, + BugReport(BugType& bt, StringRef shortDesc, StringRef desc, const ExplodedNode *errornode) - : BT(bt), ShortDescription(shortDesc), Description(desc), - ErrorNode(errornode) {} + : BT(bt), ShortDescription(shortDesc), Description(desc), + ErrorNode(errornode), Callbacks(F.getEmptyList()) {} - virtual ~BugReport(); + BugReport(BugType& bt, StringRef desc, PathDiagnosticLocation l) + : BT(bt), Description(desc), Location(l), ErrorNode(0), + Callbacks(F.getEmptyList()) {} - virtual bool isOwnedByReporterContext() { return false; } + virtual ~BugReport(); const BugType& getBugType() const { return BT; } BugType& getBugType() { return BT; } - // FIXME: Perhaps this should be moved into a subclass? - const ExplodedNode* getErrorNode() const { return ErrorNode; } - - // FIXME: Do we need this? Maybe getLocation() should return a ProgramPoint - // object. - // FIXME: If we do need it, we can probably just make it private to - // BugReporter. - const Stmt* getStmt() const; + const ExplodedNode *getErrorNode() const { return ErrorNode; } - const llvm::StringRef getDescription() const { return Description; } + const StringRef getDescription() const { return Description; } - const llvm::StringRef getShortDescription() const { + const StringRef getShortDescription() const { return ShortDescription.empty() ? Description : ShortDescription; } - // FIXME: Is this needed? - virtual std::pair<const char**,const char**> getExtraDescriptiveText() { - return std::make_pair((const char**)0,(const char**)0); + /// \brief This allows for addition of meta data to the diagnostic. + /// + /// Currently, only the HTMLDiagnosticClient knows how to display it. + void addExtraText(StringRef S) { + ExtraText.push_back(S); } - // FIXME: Perhaps move this into a subclass. - virtual PathDiagnosticPiece* getEndPath(BugReporterContext& BRC, - const ExplodedNode* N); + virtual const ExtraTextList &getExtraText() { + return ExtraText; + } - /// getLocation - Return the "definitive" location of the reported bug. + /// \brief Return the "definitive" location of the reported bug. + /// /// While a bug can span an entire path, usually there is a specific /// location that can be used to identify where the key issue occurred. /// This location is used by clients rendering diagnostics. - virtual SourceLocation getLocation() const; + virtual PathDiagnosticLocation getLocation(const SourceManager &SM) const; - typedef const SourceRange *ranges_iterator; + const Stmt *getStmt() const; + + /// \brief Add a range to a bug report. + /// + /// Ranges are used to highlight regions of interest in the source code. + /// They should be at the same source code line as the BugReport location. + /// By default, the source range of the statement corresponding to the error + /// node will be used; add a single invalid range to specify absence of + /// ranges. + void addRange(SourceRange R) { + assert((R.isValid() || Ranges.empty()) && "Invalid range can only be used " + "to specify that the report does not have a range."); + Ranges.push_back(R); + } - /// getRanges - Returns the source ranges associated with this bug. - virtual std::pair<ranges_iterator, ranges_iterator> getRanges() const; + /// \brief Get the SourceRanges associated with the report. + virtual std::pair<ranges_iterator, ranges_iterator> getRanges(); - virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N, - const ExplodedNode* PrevN, - BugReporterContext& BR); + /// \brief Add custom or predefined bug report visitors to this report. + /// + /// The visitors should be used when the default trace is not sufficient. + /// For example, they allow constructing a more elaborate trace. + /// \sa registerConditionVisitor(), registerTrackNullOrUndefValue(), + /// registerFindLastStore(), registerNilReceiverVisitor(), and + /// registerVarDeclsLastStore(). + void addVisitor(BugReporterVisitor *visitor); - virtual void registerInitialVisitors(BugReporterContext& BRC, - const ExplodedNode* N) {} + /// Iterators through the custom diagnostic visitors. + visitor_iterator visitor_begin() { return Callbacks.begin(); } + visitor_iterator visitor_end() { return Callbacks.end(); } + + /// Profile to identify equivalent bug reports for error report coalescing. + /// Reports are uniqued to ensure that we do not emit multiple diagnostics + /// for each bug. + virtual void Profile(llvm::FoldingSetNodeID& hash) const; }; //===----------------------------------------------------------------------===// @@ -148,7 +167,7 @@ public: //===----------------------------------------------------------------------===// class BugReportEquivClass : public llvm::FoldingSetNode { - // List of *owned* BugReport objects. + /// List of *owned* BugReport objects. std::list<BugReport*> Reports; friend class BugReporter; @@ -166,9 +185,9 @@ public: std::list<BugReport*>::iterator impl; public: iterator(std::list<BugReport*>::iterator i) : impl(i) {} - iterator& operator++() { ++impl; return *this; } - bool operator==(const iterator& I) const { return I.impl == impl; } - bool operator!=(const iterator& I) const { return I.impl != impl; } + iterator &operator++() { ++impl; return *this; } + bool operator==(const iterator &I) const { return I.impl == impl; } + bool operator!=(const iterator &I) const { return I.impl != impl; } BugReport* operator*() const { return *impl; } BugReport* operator->() const { return *impl; } }; @@ -177,9 +196,9 @@ public: std::list<BugReport*>::const_iterator impl; public: const_iterator(std::list<BugReport*>::const_iterator i) : impl(i) {} - const_iterator& operator++() { ++impl; return *this; } - bool operator==(const const_iterator& I) const { return I.impl == impl; } - bool operator!=(const const_iterator& I) const { return I.impl != impl; } + const_iterator &operator++() { ++impl; return *this; } + bool operator==(const const_iterator &I) const { return I.impl == impl; } + bool operator!=(const const_iterator &I) const { return I.impl != impl; } const BugReport* operator*() const { return *impl; } const BugReport* operator->() const { return *impl; } }; @@ -191,78 +210,6 @@ public: const_iterator end() const { return const_iterator(Reports.end()); } }; - -//===----------------------------------------------------------------------===// -// Specialized subclasses of BugReport. -//===----------------------------------------------------------------------===// - -// FIXME: Collapse this with the default BugReport class. -class RangedBugReport : public BugReport { - llvm::SmallVector<SourceRange, 4> Ranges; -public: - RangedBugReport(BugType& D, llvm::StringRef description, - ExplodedNode *errornode) - : BugReport(D, description, errornode) {} - - RangedBugReport(BugType& D, llvm::StringRef shortDescription, - llvm::StringRef description, ExplodedNode *errornode) - : BugReport(D, shortDescription, description, errornode) {} - - ~RangedBugReport(); - - // FIXME: Move this out of line. - void addRange(SourceRange R) { - assert(R.isValid()); - Ranges.push_back(R); - } - - virtual std::pair<ranges_iterator, ranges_iterator> getRanges() const { - return std::make_pair(Ranges.begin(), Ranges.end()); - } - - virtual void Profile(llvm::FoldingSetNodeID& hash) const { - BugReport::Profile(hash); - for (llvm::SmallVectorImpl<SourceRange>::const_iterator I = - Ranges.begin(), E = Ranges.end(); I != E; ++I) { - const SourceRange range = *I; - if (!range.isValid()) - continue; - hash.AddInteger(range.getBegin().getRawEncoding()); - hash.AddInteger(range.getEnd().getRawEncoding()); - } - } -}; - -class EnhancedBugReport : public RangedBugReport { -public: - typedef void (*VisitorCreator)(BugReporterContext &BRcC, const void *data, - const ExplodedNode *N); - -private: - typedef std::vector<std::pair<VisitorCreator, const void*> > Creators; - Creators creators; - -public: - EnhancedBugReport(BugType& D, llvm::StringRef description, - ExplodedNode *errornode) - : RangedBugReport(D, description, errornode) {} - - EnhancedBugReport(BugType& D, llvm::StringRef shortDescription, - llvm::StringRef description, ExplodedNode *errornode) - : RangedBugReport(D, shortDescription, description, errornode) {} - - ~EnhancedBugReport() {} - - void registerInitialVisitors(BugReporterContext& BRC, const ExplodedNode* N) { - for (Creators::iterator I = creators.begin(), E = creators.end(); I!=E; ++I) - I->first(BRC, I->second, N); - } - - void addVisitorCreator(VisitorCreator creator, const void *data) { - creators.push_back(std::make_pair(creator, data)); - } -}; - //===----------------------------------------------------------------------===// // BugReporter and friends. //===----------------------------------------------------------------------===// @@ -270,12 +217,15 @@ public: class BugReporterData { public: virtual ~BugReporterData(); - virtual Diagnostic& getDiagnostic() = 0; - virtual PathDiagnosticClient* getPathDiagnosticClient() = 0; - virtual ASTContext& getASTContext() = 0; + virtual DiagnosticsEngine& getDiagnostic() = 0; + virtual PathDiagnosticConsumer* getPathDiagnosticConsumer() = 0; + virtual ASTContext &getASTContext() = 0; virtual SourceManager& getSourceManager() = 0; }; +/// BugReporter is a utility class for generating PathDiagnostics for analysis. +/// It collects the BugReports and BugTypes and knows how to generate +/// and flush the corresponding diagnostics. class BugReporter { public: enum Kind { BaseBRKind, GRBugReporterKind }; @@ -288,9 +238,13 @@ private: const Kind kind; BugReporterData& D; + /// Generate and flush the diagnostics for the given bug report. void FlushReport(BugReportEquivClass& EQ); + /// The set of bug reports tracked by the BugReporter. llvm::FoldingSet<BugReportEquivClass> EQClasses; + /// A vector of BugReports for tracking the allocated pointers and cleanup. + std::vector<BugReportEquivClass *> EQClassesVector; protected: BugReporter(BugReporterData& d, Kind k) : BugTypes(F.getEmptySet()), kind(k), @@ -301,63 +255,71 @@ public: D(d) {} virtual ~BugReporter(); + /// \brief Generate and flush diagnostics for all bug reports. void FlushReports(); Kind getKind() const { return kind; } - Diagnostic& getDiagnostic() { + DiagnosticsEngine& getDiagnostic() { return D.getDiagnostic(); } - PathDiagnosticClient* getPathDiagnosticClient() { - return D.getPathDiagnosticClient(); + PathDiagnosticConsumer* getPathDiagnosticConsumer() { + return D.getPathDiagnosticConsumer(); } + /// \brief Iterator over the set of BugTypes tracked by the BugReporter. typedef BugTypesTy::iterator iterator; iterator begin() { return BugTypes.begin(); } iterator end() { return BugTypes.end(); } + /// \brief Iterator over the set of BugReports tracked by the BugReporter. typedef llvm::FoldingSet<BugReportEquivClass>::iterator EQClasses_iterator; EQClasses_iterator EQClasses_begin() { return EQClasses.begin(); } EQClasses_iterator EQClasses_end() { return EQClasses.end(); } - ASTContext& getContext() { return D.getASTContext(); } + ASTContext &getContext() { return D.getASTContext(); } SourceManager& getSourceManager() { return D.getSourceManager(); } virtual void GeneratePathDiagnostic(PathDiagnostic& pathDiagnostic, - llvm::SmallVectorImpl<BugReport *> &bugReports) {} + SmallVectorImpl<BugReport *> &bugReports) {} void Register(BugType *BT); + /// \brief Add the given report to the set of reports tracked by BugReporter. + /// + /// The reports are usually generated by the checkers. Further, they are + /// folded based on the profile value, which is done to coalesce similar + /// reports. void EmitReport(BugReport *R); - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr, - SourceLocation Loc, + void EmitBasicReport(StringRef BugName, StringRef BugStr, + PathDiagnosticLocation Loc, SourceRange* RangeBeg, unsigned NumRanges); - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory, - llvm::StringRef BugStr, SourceLocation Loc, + void EmitBasicReport(StringRef BugName, StringRef BugCategory, + StringRef BugStr, PathDiagnosticLocation Loc, SourceRange* RangeBeg, unsigned NumRanges); - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr, - SourceLocation Loc) { + void EmitBasicReport(StringRef BugName, StringRef BugStr, + PathDiagnosticLocation Loc) { EmitBasicReport(BugName, BugStr, Loc, 0, 0); } - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugCategory, - llvm::StringRef BugStr, SourceLocation Loc) { + void EmitBasicReport(StringRef BugName, StringRef BugCategory, + StringRef BugStr, PathDiagnosticLocation Loc) { EmitBasicReport(BugName, BugCategory, BugStr, Loc, 0, 0); } - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef BugStr, - SourceLocation Loc, SourceRange R) { + void EmitBasicReport(StringRef BugName, StringRef BugStr, + PathDiagnosticLocation Loc, SourceRange R) { EmitBasicReport(BugName, BugStr, Loc, &R, 1); } - void EmitBasicReport(llvm::StringRef BugName, llvm::StringRef Category, - llvm::StringRef BugStr, SourceLocation Loc, + void EmitBasicReport(StringRef BugName, StringRef Category, + StringRef BugStr, PathDiagnosticLocation Loc, SourceRange R) { EmitBasicReport(BugName, Category, BugStr, Loc, &R, 1); } @@ -369,7 +331,7 @@ private: /// \brief Returns a BugType that is associated with the given name and /// category. - BugType *getBugTypeForName(llvm::StringRef name, llvm::StringRef category); + BugType *getBugTypeForName(StringRef name, StringRef category); }; // FIXME: Get rid of GRBugReporter. It's the wrong abstraction. @@ -392,10 +354,10 @@ public: /// getStateManager - Return the state manager used by the analysis /// engine. - GRStateManager &getStateManager(); + ProgramStateManager &getStateManager(); virtual void GeneratePathDiagnostic(PathDiagnostic &pathDiagnostic, - llvm::SmallVectorImpl<BugReport*> &bugReports); + SmallVectorImpl<BugReport*> &bugReports); void addNotableSymbol(SymbolRef Sym) { NotableSymbols.insert(Sym); @@ -413,20 +375,10 @@ public: class BugReporterContext { GRBugReporter &BR; - // Not the most efficient data structure, but we use an ImmutableList for the - // Callbacks because it is safe to make additions to list during iteration. - llvm::ImmutableList<BugReporterVisitor*>::Factory F; - llvm::ImmutableList<BugReporterVisitor*> Callbacks; - llvm::FoldingSet<BugReporterVisitor> CallbacksSet; public: - BugReporterContext(GRBugReporter& br) : BR(br), Callbacks(F.getEmptyList()) {} - virtual ~BugReporterContext(); - - void addVisitor(BugReporterVisitor* visitor); + BugReporterContext(GRBugReporter& br) : BR(br) {} - typedef llvm::ImmutableList<BugReporterVisitor*>::iterator visitor_iterator; - visitor_iterator visitor_begin() { return Callbacks.begin(); } - visitor_iterator visitor_end() { return Callbacks.end(); } + virtual ~BugReporterContext() {} GRBugReporter& getBugReporter() { return BR; } @@ -442,7 +394,7 @@ public: return BR.isNotable(Sym); } - GRStateManager& getStateManager() { + ProgramStateManager& getStateManager() { return BR.getStateManager(); } @@ -450,7 +402,7 @@ public: return getStateManager().getSValBuilder(); } - ASTContext& getASTContext() { + ASTContext &getASTContext() { return BR.getContext(); } @@ -461,50 +413,6 @@ public: virtual BugReport::NodeResolver& getNodeResolver() = 0; }; -class DiagBugReport : public RangedBugReport { - std::list<std::string> Strs; - FullSourceLoc L; -public: - DiagBugReport(BugType& D, llvm::StringRef desc, FullSourceLoc l) : - RangedBugReport(D, desc, 0), L(l) {} - - virtual ~DiagBugReport() {} - - // FIXME: Move out-of-line (virtual function). - SourceLocation getLocation() const { return L; } - - void addString(llvm::StringRef s) { Strs.push_back(s); } - - typedef std::list<std::string>::const_iterator str_iterator; - str_iterator str_begin() const { return Strs.begin(); } - str_iterator str_end() const { return Strs.end(); } -}; - -//===----------------------------------------------------------------------===// -//===----------------------------------------------------------------------===// - -namespace bugreporter { - -const Stmt *GetDerefExpr(const ExplodedNode *N); -const Stmt *GetDenomExpr(const ExplodedNode *N); -const Stmt *GetCalleeExpr(const ExplodedNode *N); -const Stmt *GetRetValExpr(const ExplodedNode *N); - -void registerTrackNullOrUndefValue(BugReporterContext& BRC, const void *stmt, - const ExplodedNode* N); - -void registerFindLastStore(BugReporterContext& BRC, const void *memregion, - const ExplodedNode *N); - -void registerNilReceiverVisitor(BugReporterContext &BRC); - -void registerVarDeclsLastStore(BugReporterContext &BRC, const void *stmt, - const ExplodedNode *N); - -} // end namespace clang::bugreporter - -//===----------------------------------------------------------------------===// - } // end GR namespace } // end clang namespace |