BugReporterVisitor.h revision 166b7bd43551964d65bcf4918f51a167b8374e2a
1//===--- BugReporterVisitor.h - Generate PathDiagnostics -------*- 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 declares BugReporterVisitors, which are used to generate enhanced 11// diagnostic traces. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_GR_BUGREPORTERVISITOR 16#define LLVM_CLANG_GR_BUGREPORTERVISITOR 17 18#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 19#include "llvm/ADT/FoldingSet.h" 20 21namespace clang { 22 23namespace ento { 24 25class BugReport; 26class BugReporterContext; 27class ExplodedNode; 28class MemRegion; 29class PathDiagnosticPiece; 30 31/// \brief BugReporterVisitors are used to add custom diagnostics along a path. 32/// 33/// Custom visitors should subclass the BugReporterVisitorImpl class for a 34/// default implementation of the clone() method. 35/// (Warning: if you have a deep subclass of BugReporterVisitorImpl, the 36/// default implementation of clone() will NOT do the right thing, and you 37/// will have to provide your own implementation.) 38class BugReporterVisitor : public llvm::FoldingSetNode { 39public: 40 virtual ~BugReporterVisitor(); 41 42 /// \brief Returns a copy of this BugReporter. 43 /// 44 /// Custom BugReporterVisitors should not override this method directly. 45 /// Instead, they should inherit from BugReporterVisitorImpl and provide 46 /// a protected or public copy constructor. 47 /// 48 /// (Warning: if you have a deep subclass of BugReporterVisitorImpl, the 49 /// default implementation of clone() will NOT do the right thing, and you 50 /// will have to provide your own implementation.) 51 virtual BugReporterVisitor *clone() const = 0; 52 53 /// \brief Return a diagnostic piece which should be associated with the 54 /// given node. 55 /// 56 /// The last parameter can be used to register a new visitor with the given 57 /// BugReport while processing a node. 58 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 59 const ExplodedNode *PrevN, 60 BugReporterContext &BRC, 61 BugReport &BR) = 0; 62 63 /// \brief Provide custom definition for the final diagnostic piece on the 64 /// path - the piece, which is displayed before the path is expanded. 65 /// 66 /// If returns NULL the default implementation will be used. 67 /// Also note that at most one visitor of a BugReport should generate a 68 /// non-NULL end of path diagnostic piece. 69 virtual PathDiagnosticPiece *getEndPath(BugReporterContext &BRC, 70 const ExplodedNode *N, 71 BugReport &BR); 72 73 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0; 74 75 /// \brief Generates the default final diagnostic piece. 76 static PathDiagnosticPiece *getDefaultEndPath(BugReporterContext &BRC, 77 const ExplodedNode *N, 78 BugReport &BR); 79 80}; 81 82/// This class provides a convenience implementation for clone() using the 83/// Curiously-Recurring Template Pattern. If you are implementing a custom 84/// BugReporterVisitor, subclass BugReporterVisitorImpl and provide a public 85/// or protected copy constructor. 86/// 87/// (Warning: if you have a deep subclass of BugReporterVisitorImpl, the 88/// default implementation of clone() will NOT do the right thing, and you 89/// will have to provide your own implementation.) 90template <class DERIVED> 91class BugReporterVisitorImpl : public BugReporterVisitor { 92 virtual BugReporterVisitor *clone() const { 93 return new DERIVED(*static_cast<const DERIVED *>(this)); 94 } 95}; 96 97class FindLastStoreBRVisitor 98 : public BugReporterVisitorImpl<FindLastStoreBRVisitor> 99{ 100 const MemRegion *R; 101 SVal V; 102 bool satisfied; 103 104public: 105 /// \brief Convenience method to create a visitor given only the MemRegion. 106 /// Returns NULL if the visitor cannot be created. For example, when the 107 /// corresponding value is unknown. 108 static BugReporterVisitor *createVisitorObject(const ExplodedNode *N, 109 const MemRegion *R); 110 111 /// Creates a visitor for every VarDecl inside a Stmt and registers it with 112 /// the BugReport. 113 static void registerStatementVarDecls(BugReport &BR, const Stmt *S); 114 115 FindLastStoreBRVisitor(SVal v, const MemRegion *r) 116 : R(r), V(v), satisfied(false) { 117 assert (!V.isUnknown() && "Cannot track unknown value."); 118 119 // TODO: Does it make sense to allow undef values here? 120 // (If not, also see UndefCapturedBlockVarChecker)? 121 } 122 123 void Profile(llvm::FoldingSetNodeID &ID) const; 124 125 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 126 const ExplodedNode *PrevN, 127 BugReporterContext &BRC, 128 BugReport &BR); 129}; 130 131class TrackConstraintBRVisitor 132 : public BugReporterVisitorImpl<TrackConstraintBRVisitor> 133{ 134 DefinedSVal Constraint; 135 const bool Assumption; 136 bool isSatisfied; 137 138public: 139 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) 140 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} 141 142 void Profile(llvm::FoldingSetNodeID &ID) const; 143 144 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 145 const ExplodedNode *PrevN, 146 BugReporterContext &BRC, 147 BugReport &BR); 148}; 149 150class NilReceiverBRVisitor 151 : public BugReporterVisitorImpl<NilReceiverBRVisitor> 152{ 153public: 154 void Profile(llvm::FoldingSetNodeID &ID) const { 155 static int x = 0; 156 ID.AddPointer(&x); 157 } 158 159 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 160 const ExplodedNode *PrevN, 161 BugReporterContext &BRC, 162 BugReport &BR); 163}; 164 165/// Visitor that tries to report interesting diagnostics from conditions. 166class ConditionBRVisitor : public BugReporterVisitorImpl<ConditionBRVisitor> { 167public: 168 void Profile(llvm::FoldingSetNodeID &ID) const { 169 static int x = 0; 170 ID.AddPointer(&x); 171 } 172 173 174 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 175 const ExplodedNode *Prev, 176 BugReporterContext &BRC, 177 BugReport &BR); 178 179 PathDiagnosticPiece *VisitNodeImpl(const ExplodedNode *N, 180 const ExplodedNode *Prev, 181 BugReporterContext &BRC, 182 BugReport &BR); 183 184 PathDiagnosticPiece *VisitTerminator(const Stmt *Term, 185 const ExplodedNode *N, 186 const CFGBlock *srcBlk, 187 const CFGBlock *dstBlk, 188 BugReport &R, 189 BugReporterContext &BRC); 190 191 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 192 bool tookTrue, 193 BugReporterContext &BRC, 194 BugReport &R, 195 const ExplodedNode *N); 196 197 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 198 const DeclRefExpr *DR, 199 const bool tookTrue, 200 BugReporterContext &BRC, 201 BugReport &R, 202 const ExplodedNode *N); 203 204 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 205 const BinaryOperator *BExpr, 206 const bool tookTrue, 207 BugReporterContext &BRC, 208 BugReport &R, 209 const ExplodedNode *N); 210 211 PathDiagnosticPiece *VisitConditionVariable(StringRef LhsString, 212 const Expr *CondVarExpr, 213 const bool tookTrue, 214 BugReporterContext &BRC, 215 BugReport &R, 216 const ExplodedNode *N); 217 218 bool patternMatch(const Expr *Ex, 219 llvm::raw_ostream &Out, 220 BugReporterContext &BRC, 221 BugReport &R, 222 const ExplodedNode *N, 223 llvm::Optional<bool> &prunable); 224}; 225 226namespace bugreporter { 227 228void addTrackNullOrUndefValueVisitor(const ExplodedNode *N, const Stmt *S, 229 BugReport *R); 230 231const Stmt *GetDerefExpr(const ExplodedNode *N); 232const Stmt *GetDenomExpr(const ExplodedNode *N); 233const Stmt *GetRetValExpr(const ExplodedNode *N); 234 235} // end namespace clang 236} // end namespace ento 237} // end namespace bugreporter 238 239 240#endif //LLVM_CLANG_GR__BUGREPORTERVISITOR 241