ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
staticCFG.h
Go to the documentation of this file.
1 #ifndef STATIC_CFG_H
2 #define STATIC_CFG_H
3 
4 #include <sage3basic.h>
6 #include "virtualCFG.h"
7 #include <map>
8 #include <set>
9 #include <string>
10 
11 
13 class SgGraphNode;
15 
16 
17 namespace StaticCFG
18 {
19 
22 
23 
25 {
26 protected:
29 
31  std::map<CFGNode, SgGraphNode*> all_nodes_;
32 
35 
38 
41 
44 
45 public:
46  CFG() : graph_(NULL), start_(NULL), entry_(NULL), exit_(NULL) {}
47 
49  CFGNode toCFGNode(SgGraphNode* node);
50 
53  SgGraphNode *toGraphNode(CFGNode &n) { return ((all_nodes_.count(n)==0) ? NULL : all_nodes_[n]);}
54 
56 
57  CFG(SgNode* node, bool is_filtered = false)
58  : graph_(NULL), start_(node), entry_(NULL), exit_(NULL), is_filtered_(is_filtered)
59  { buildCFG(); }
60 
63  { return graph_; }
64 
65  virtual ~CFG()
66  { clearNodesAndEdges(); }
67 
69 
70  void setStart(SgNode* node) { start_ = node; }
71 
74  { return entry_; }
75 
78  { return exit_; }
79 
80  bool isFilteredCFG() const { return is_filtered_; }
81  void setFiltered(bool flag) { is_filtered_ = flag; }
82 
83 
85  virtual void buildCFG()
86  {
87  if (is_filtered_) buildFilteredCFG();
88  else buildFullCFG();
89  }
90 
92  virtual void buildFullCFG();
93 
95  virtual void buildFilteredCFG();
96 
97 
98 #if 0
99  std::vector<SgDirectedGraphEdge*> getOutEdges(SgNode* node, int index);
100  std::vector<SgDirectedGraphEdge*> getInEdges(SgNode* node, int index);
101 #endif
102 
103  // The following four functions are for getting in/out edges of a given node.
104  std::vector<SgDirectedGraphEdge*> getOutEdges(SgGraphNode* node);
105  std::vector<SgDirectedGraphEdge*> getInEdges(SgGraphNode* node);
106 
107  // Provide the same interface to get the beginning/end graph node for a SgNode
108  SgGraphNode* cfgForBeginning(SgNode* node);
109  SgGraphNode* cfgForEnd(SgNode* node);
110 
112  static int getIndex(SgGraphNode* node);
113 
115  void cfgToDot(SgNode* node, const std::string& file_name);
116 
117 protected:
118  //void buildCFG(CFGNode n);
119  template <class NodeT, class EdgeT>
120  void buildCFG(NodeT n, std::map<NodeT, SgGraphNode*>& all_nodes, std::set<NodeT>& explored);
121 
123  void clearNodesAndEdges();
124 
125 
126 
127  // The following methods are used to build a DOT file.
128  virtual void processNodes(std::ostream & o, SgGraphNode* n, std::set<SgGraphNode*>& explored);
129  virtual void printNodePlusEdges(std::ostream & o, SgGraphNode* node);
130  virtual void printNode(std::ostream & o, SgGraphNode* node);
131  virtual void printEdge(std::ostream & o, SgDirectedGraphEdge* edge, bool isInEdge);
132 };
133 
134 
137 {
138  int index_;
140 
141 public:
142  CFGNodeAttribute(int idx = 0, SgIncidenceDirectedGraph* graph = NULL)
143  : index_(idx), graph_(graph) {}
144 
145  int getIndex() const { return index_; }
146 
147  void setIndex(int idx) { index_ = idx; }
148 
149  const SgIncidenceDirectedGraph* getGraph() const { return graph_; }
151 
153  { graph_ = graph; }
154 };
155 
156 template <class EdgeT>
158 {
159  EdgeT edge_;
160 public:
161  CFGEdgeAttribute(const EdgeT& e) : edge_(e) {}
162 
163  void setEdge(const EdgeT& e)
164  { edge_ = e; }
165 
166  EdgeT getEdge() const
167  { return edge_; }
168 };
169 
170 // The following are some auxiliary functions, since SgGraphNode cannot provide them.
171 std::vector<SgDirectedGraphEdge*> outEdges(SgGraphNode* node);
172 std::vector<SgDirectedGraphEdge*> inEdges(SgGraphNode* node);
173 
174 } // end of namespace StaticCFG
175 
176 #endif