summaryrefslogtreecommitdiffstats
path: root/include/clang/Analysis/PathSensitive/GRExprEngineBuilders.h
blob: 6c23745de23a5a44eb5f30105b3c20871efafe4d (plain)
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
//===-- GRExprEngineBuilders.h - "Builder" classes for GRExprEngine -*- 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 smart builder "references" which are used to marshal
//  builders between GRExprEngine objects and their related components.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ANALYSIS_GREXPRENGINE_BUILDERS
#define LLVM_CLANG_ANALYSIS_GREXPRENGINE_BUILDERS
#include "clang/Analysis/PathSensitive/GRExprEngine.h"

namespace clang {
  

// SaveAndRestore - A utility class that uses RAII to save and restore
//  the value of a variable.
template<typename T>
struct SaveAndRestore {
  SaveAndRestore(T& x) : X(x), old_value(x) {}
  ~SaveAndRestore() { X = old_value; }
  T get() { return old_value; }
private:  
  T& X;
  T old_value;
};

// SaveOr - Similar to SaveAndRestore.  Operates only on bools; the old
//  value of a variable is saved, and during the dstor the old value is
//  or'ed with the new value.
struct SaveOr {
  SaveOr(bool& x) : X(x), old_value(x) { x = false; }
  ~SaveOr() { X |= old_value; }
private:
  bool& X;
  const bool old_value;
};

class GRStmtNodeBuilderRef {
  GRExprEngine::NodeSet &Dst;
  GRExprEngine::StmtNodeBuilder &B;
  GRExprEngine& Eng;
  GRExprEngine::NodeTy* Pred;
  const GRState* state;
  const Stmt* stmt;
  const unsigned OldSize;
  const bool AutoCreateNode;
  SaveAndRestore<bool> OldSink;
  SaveAndRestore<const void*> OldTag;
  SaveOr OldHasGen;

private:
  friend class GRExprEngine;
  
  GRStmtNodeBuilderRef(); // do not implement
  void operator=(const GRStmtNodeBuilderRef&); // do not implement
  
  GRStmtNodeBuilderRef(GRExprEngine::NodeSet &dst,
                       GRExprEngine::StmtNodeBuilder &builder,
                       GRExprEngine& eng,
                       GRExprEngine::NodeTy* pred,
                       const GRState *st,
                       const Stmt* s, bool auto_create_node)
  : Dst(dst), B(builder), Eng(eng), Pred(pred),
    state(st), stmt(s), OldSize(Dst.size()), AutoCreateNode(auto_create_node),
    OldSink(B.BuildSinks), OldTag(B.Tag), OldHasGen(B.HasGeneratedNode) {}
  
public:

  ~GRStmtNodeBuilderRef() {
    // Handle the case where no nodes where generated.  Auto-generate that
    // contains the updated state if we aren't generating sinks.  
    if (!B.BuildSinks && Dst.size() == OldSize && !B.HasGeneratedNode) {
      if (AutoCreateNode)
        B.MakeNode(Dst, const_cast<Stmt*>(stmt), Pred, state);
      else
        Dst.Add(Pred);
    }
  }
  
  GRStateRef getState() {
    return GRStateRef(state, Eng.getStateManager());
  }

  GRStateManager& getStateManager() {
    return Eng.getStateManager();
  }
  
  GRExprEngine::NodeTy* MakeNode(const GRState* state) {
    return B.MakeNode(Dst, const_cast<Stmt*>(stmt), Pred, state);    
  }    
};

} // end clang namespace
#endif
OpenPOWER on IntegriCloud