aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Analysis/PathSensitive/Checker.h
blob: 3bef08d7545fae40c13f16effb484b5846df21d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//== Checker.h - Abstract interface for checkers -----------------*- C++ -*--=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines Checker and CheckerVisitor, classes used for creating
//  domain-specific checks.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ANALYSIS_CHECKER
#define LLVM_CLANG_ANALYSIS_CHECKER
#include "clang/Analysis/Support/SaveAndRestore.h"
#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
#include "clang/Analysis/PathSensitive/GRState.h"
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtObjC.h"

//===----------------------------------------------------------------------===//
// Checker interface.
//===----------------------------------------------------------------------===//

namespace clang {
  class GRExprEngine;

class CheckerContext {
  ExplodedNodeSet &Dst;
  GRStmtNodeBuilder &B;
  GRExprEngine &Eng;
  ExplodedNode *Pred;
  SaveAndRestore<bool> OldSink;
  SaveAndRestore<const void*> OldTag;
  SaveAndRestore<ProgramPoint::Kind> OldPointKind;
  SaveOr OldHasGen;

public:
  CheckerContext(ExplodedNodeSet &dst,
                 GRStmtNodeBuilder &builder,
                 GRExprEngine &eng,
                 ExplodedNode *pred,
                 const void *tag, bool preVisit)
    : Dst(dst), B(builder), Eng(eng), Pred(pred),
      OldSink(B.BuildSinks), OldTag(B.Tag),
      OldPointKind(B.PointKind), OldHasGen(B.HasGeneratedNode) {
        //assert(Dst.empty()); // This is a fake assertion.
              // See GRExprEngine::CheckerVisit(), CurrSet is repeatedly used.
        B.Tag = tag;
        if (preVisit)
          B.PointKind = ProgramPoint::PreStmtKind;
      }

  ~CheckerContext() {
    if (!B.BuildSinks && !B.HasGeneratedNode)
      Dst.Add(Pred);
  }

  ConstraintManager &getConstraintManager() {
      return Eng.getConstraintManager();
  }
  ExplodedNodeSet &getNodeSet() { return Dst; }
  GRStmtNodeBuilder &getNodeBuilder() { return B; }
  ExplodedNode *&getPredecessor() { return Pred; }
  const GRState *getState() { return B.GetState(Pred); }

  ASTContext &getASTContext() {
    return Eng.getContext();
  }
  
  BugReporter &getBugReporter() {
    return Eng.getBugReporter();
  }

  ExplodedNode *GenerateNode(const Stmt *S, bool markAsSink = false) {
    return GenerateNode(S, getState(), markAsSink);
  }

  ExplodedNode *GenerateNode(const Stmt* S, const GRState *state,
                             bool markAsSink = false) {
    ExplodedNode *node = B.generateNode(S, state, Pred);

    if (markAsSink && node)
      node->markAsSink();

    return node;
  }

  void addTransition(ExplodedNode *node) {
    Dst.Add(node);
  }

  void EmitReport(BugReport *R) {
    Eng.getBugReporter().EmitReport(R);
  }
};

class Checker {
private:
  friend class GRExprEngine;

  void GR_Visit(ExplodedNodeSet &Dst,
                GRStmtNodeBuilder &Builder,
                GRExprEngine &Eng,
                const Stmt *stmt,
                ExplodedNode *Pred, void *tag, bool isPrevisit) {
    CheckerContext C(Dst, Builder, Eng, Pred, tag, isPrevisit);
    assert(isPrevisit && "Only previsit supported for now.");
    _PreVisit(C, stmt);
  }

  void GR_VisitBind(ExplodedNodeSet &Dst,
                    GRStmtNodeBuilder &Builder, GRExprEngine &Eng,
                    const Stmt *stmt, ExplodedNode *Pred, void *tag, 
                    SVal location, SVal val,
                    bool isPrevisit) {
    CheckerContext C(Dst, Builder, Eng, Pred, tag, isPrevisit);
    assert(isPrevisit && "Only previsit supported for now.");
    PreVisitBind(C, stmt, location, val);
  }

public:
  virtual ~Checker() {}
  virtual void _PreVisit(CheckerContext &C, const Stmt *ST) {}
  
  // This is a previsit which takes a node returns a node.
  virtual ExplodedNode *CheckLocation(const Stmt *S, ExplodedNode *Pred,
                                      const GRState *state, SVal V,
                                      GRExprEngine &Eng) {
    return Pred;
  }
  
  virtual void PreVisitBind(CheckerContext &C, const Stmt *ST, 
                            SVal location, SVal val) {}

  virtual ExplodedNode *CheckType(QualType T, ExplodedNode *Pred, 
                                  const GRState *state, Stmt *S, 
                                  GRExprEngine &Eng) {
    return Pred;
  }

};

} // end clang namespace

#endif