BugReporterVisitor.h revision 76aadc346c3a4c363238a1e1232f324c3355d9e0
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 31class BugReporterVisitor : public llvm::FoldingSetNode { 32public: 33 virtual ~BugReporterVisitor(); 34 35 /// \brief Return a diagnostic piece which should be associated with the 36 /// given node. 37 /// 38 /// The last parameter can be used to register a new visitor with the given 39 /// BugReport while processing a node. 40 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 41 const ExplodedNode *PrevN, 42 BugReporterContext &BRC, 43 BugReport &BR) = 0; 44 45 /// \brief Provide custom definition for the final diagnostic piece on the 46 /// path - the piece, which is displayed before the path is expanded. 47 /// 48 /// If returns NULL the default implementation will be used. 49 /// Also note that at most one visitor of a BugReport should generate a 50 /// non-NULL end of path diagnostic piece. 51 virtual PathDiagnosticPiece *getEndPath(BugReporterContext &BRC, 52 const ExplodedNode *N, 53 BugReport &BR); 54 55 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0; 56 57 /// \brief Generates the default final diagnostic piece. 58 static PathDiagnosticPiece *getDefaultEndPath(BugReporterContext &BRC, 59 const ExplodedNode *N, 60 BugReport &BR); 61 62}; 63 64class FindLastStoreBRVisitor : public BugReporterVisitor { 65 const MemRegion *R; 66 SVal V; 67 bool satisfied; 68 const ExplodedNode *StoreSite; 69 70public: 71 /// \brief Convenience method to create a visitor given only the MemRegion. 72 /// Returns NULL if the visitor cannot be created. For example, when the 73 /// corresponding value is unknown. 74 static BugReporterVisitor *createVisitorObject(const ExplodedNode *N, 75 const MemRegion *R); 76 77 /// Creates a visitor for every VarDecl inside a Stmt and registers it with 78 /// the BugReport. 79 static void registerStatementVarDecls(BugReport &BR, const Stmt *S); 80 81 FindLastStoreBRVisitor(SVal v, const MemRegion *r) 82 : R(r), V(v), satisfied(false), StoreSite(0) { 83 assert (!V.isUnknown() && "Cannot track unknown value."); 84 85 // TODO: Does it make sense to allow undef values here? 86 // (If not, also see UndefCapturedBlockVarChecker)? 87 } 88 89 void Profile(llvm::FoldingSetNodeID &ID) const; 90 91 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 92 const ExplodedNode *PrevN, 93 BugReporterContext &BRC, 94 BugReport &BR); 95}; 96 97class TrackConstraintBRVisitor : public BugReporterVisitor { 98 DefinedSVal Constraint; 99 const bool Assumption; 100 bool isSatisfied; 101 102public: 103 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) 104 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} 105 106 void Profile(llvm::FoldingSetNodeID &ID) const; 107 108 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 109 const ExplodedNode *PrevN, 110 BugReporterContext &BRC, 111 BugReport &BR); 112}; 113 114class NilReceiverBRVisitor : public BugReporterVisitor { 115public: 116 void Profile(llvm::FoldingSetNodeID &ID) const { 117 static int x = 0; 118 ID.AddPointer(&x); 119 } 120 121 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 122 const ExplodedNode *PrevN, 123 BugReporterContext &BRC, 124 BugReport &BR); 125}; 126 127/// Visitor that tries to report interesting diagnostics from conditions. 128class ConditionBRVisitor : public BugReporterVisitor { 129public: 130 void Profile(llvm::FoldingSetNodeID &ID) const { 131 static int x = 0; 132 ID.AddPointer(&x); 133 } 134 135 136 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 137 const ExplodedNode *Prev, 138 BugReporterContext &BRC, 139 BugReport &BR); 140 141 PathDiagnosticPiece *VisitNodeImpl(const ExplodedNode *N, 142 const ExplodedNode *Prev, 143 BugReporterContext &BRC, 144 BugReport &BR); 145 146 PathDiagnosticPiece *VisitTerminator(const Stmt *Term, 147 const ExplodedNode *N, 148 const CFGBlock *srcBlk, 149 const CFGBlock *dstBlk, 150 BugReport &R, 151 BugReporterContext &BRC); 152 153 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 154 bool tookTrue, 155 BugReporterContext &BRC, 156 BugReport &R, 157 const ExplodedNode *N); 158 159 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 160 const DeclRefExpr *DR, 161 const bool tookTrue, 162 BugReporterContext &BRC, 163 BugReport &R, 164 const ExplodedNode *N); 165 166 PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, 167 const BinaryOperator *BExpr, 168 const bool tookTrue, 169 BugReporterContext &BRC, 170 BugReport &R, 171 const ExplodedNode *N); 172 173 PathDiagnosticPiece *VisitConditionVariable(StringRef LhsString, 174 const Expr *CondVarExpr, 175 const bool tookTrue, 176 BugReporterContext &BRC, 177 BugReport &R, 178 const ExplodedNode *N); 179 180 bool patternMatch(const Expr *Ex, 181 llvm::raw_ostream &Out, 182 BugReporterContext &BRC, 183 BugReport &R, 184 const ExplodedNode *N, 185 llvm::Optional<bool> &prunable); 186}; 187 188namespace bugreporter { 189 190BugReporterVisitor *getTrackNullOrUndefValueVisitor(const ExplodedNode *N, 191 const Stmt *S, 192 BugReport *R); 193 194const Stmt *GetDerefExpr(const ExplodedNode *N); 195const Stmt *GetDenomExpr(const ExplodedNode *N); 196const Stmt *GetCalleeExpr(const ExplodedNode *N); 197const Stmt *GetRetValExpr(const ExplodedNode *N); 198 199} // end namespace clang 200} // end namespace ento 201} // end namespace bugreporter 202 203 204#endif //LLVM_CLANG_GR__BUGREPORTERVISITOR 205