BugReporterVisitor.h revision 50bbc165b063155cc23c360deb7b865502e068e2
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  virtual bool isOwnedByReporterContext() { return true; }
46  virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0;
47};
48
49class FindLastStoreBRVisitor : public BugReporterVisitor {
50  const MemRegion *R;
51  SVal V;
52  bool satisfied;
53  const ExplodedNode *StoreSite;
54
55public:
56  /// \brief Convenience method to create a visitor given only the MemRegion.
57  /// Returns NULL if the visitor cannot be created. For example, when the
58  /// corresponding value is unknown.
59  static BugReporterVisitor *createVisitorObject(const ExplodedNode *N,
60                                                 const MemRegion *R);
61
62  /// Creates a visitor for every VarDecl inside a Stmt and registers it with
63  /// the BugReport.
64  static void registerStatementVarDecls(BugReport &BR, const Stmt *S);
65
66  FindLastStoreBRVisitor(SVal v, const MemRegion *r)
67  : R(r), V(v), satisfied(false), StoreSite(0) {
68    assert (!V.isUnknown() && "Cannot track unknown value.");
69
70    // TODO: Does it make sense to allow undef values here?
71    // (If not, also see UndefCapturedBlockVarChecker)?
72  }
73
74  void Profile(llvm::FoldingSetNodeID &ID) const;
75
76  PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
77                                 const ExplodedNode *PrevN,
78                                 BugReporterContext &BRC,
79                                 BugReport &BR);
80};
81
82class TrackConstraintBRVisitor : public BugReporterVisitor {
83  DefinedSVal Constraint;
84  const bool Assumption;
85  bool isSatisfied;
86
87public:
88  TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
89  : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
90
91  void Profile(llvm::FoldingSetNodeID &ID) const;
92
93  PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
94                                 const ExplodedNode *PrevN,
95                                 BugReporterContext &BRC,
96                                 BugReport &BR);
97};
98
99class NilReceiverBRVisitor : public BugReporterVisitor {
100public:
101  void Profile(llvm::FoldingSetNodeID &ID) const {
102    static int x = 0;
103    ID.AddPointer(&x);
104  }
105
106  PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
107                                 const ExplodedNode *PrevN,
108                                 BugReporterContext &BRC,
109                                 BugReport &BR);
110};
111
112/// Visitor that tries to report interesting diagnostics from conditions.
113class ConditionBRVisitor : public BugReporterVisitor {
114public:
115  void Profile(llvm::FoldingSetNodeID &ID) const {
116    static int x = 0;
117    ID.AddPointer(&x);
118  }
119
120  virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
121                                         const ExplodedNode *Prev,
122                                         BugReporterContext &BRC,
123                                         BugReport &BR);
124
125  PathDiagnosticPiece *VisitTerminator(const Stmt *Term,
126                                       const ProgramState *CurrentState,
127                                       const ProgramState *PrevState,
128                                       const CFGBlock *srcBlk,
129                                       const CFGBlock *dstBlk,
130                                       BugReporterContext &BRC);
131
132  PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
133                                     bool tookTrue,
134                                     BugReporterContext &BRC);
135
136  PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
137                                     const DeclRefExpr *DR,
138                                     const bool tookTrue,
139                                     BugReporterContext &BRC);
140
141  PathDiagnosticPiece *VisitTrueTest(const Expr *Cond,
142                                     const BinaryOperator *BExpr,
143                                     const bool tookTrue,
144                                     BugReporterContext &BRC);
145
146  bool patternMatch(const Expr *Ex,
147                    llvm::raw_ostream &Out,
148                    BugReporterContext &BRC);
149};
150
151namespace bugreporter {
152
153BugReporterVisitor *getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
154                                                    const Stmt *S);
155
156const Stmt *GetDerefExpr(const ExplodedNode *N);
157const Stmt *GetDenomExpr(const ExplodedNode *N);
158const Stmt *GetCalleeExpr(const ExplodedNode *N);
159const Stmt *GetRetValExpr(const ExplodedNode *N);
160
161} // end namespace clang
162} // end namespace ento
163} // end namespace bugreporter
164
165
166#endif //LLVM_CLANG_GR__BUGREPORTERVISITOR
167