forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customGraphGenerationWithFiltering.C
167 lines (138 loc) · 6.78 KB
/
customGraphGenerationWithFiltering.C
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/********************************************************************
* Andreas Saebjoernsen 03/17/06
* Demonstration on how to iterate over a graph constucted by an
* VirtualGraphCreateTemplate. This type of graph is returned by for
* instance the call graph analysis, data flow analysis and all
* the other program analysis work within ROSE.
********************************************************************/
// Example ROSE Preprocessor
// used for testing ROSE infrastructure
#include "rose.h"
#include <assert.h>
#include <string>
#include <iomanip>
#include "AstConsistencyTests.h"
#include <functional>
#include <GraphUpdate.h>
#include <GraphDotOutput.h>
#include <VirtualGraphCreate.h>
using namespace std;
class Node : public MultiGraphElem {
public:
std::string name;
Node( std::string n ) : MultiGraphElem( NULL ), name( n ) {}
virtual std::string toString() const { return name; }
};
class Edge : public MultiGraphElem {
public:
std::string label;
Edge ( std::string label = "default edge" ) : MultiGraphElem( NULL ), label( label ) {};
void Dump() const { printf ("EDGE: label = %s \n",label.c_str()); }
virtual std::string toString() const { return label;}
};
template <class NodeType, class EdgeType>
class GraphBuilder : public VirtualGraphCreateTemplate<NodeType, EdgeType> {
public:
void addNode ( NodeType* node );
void addEdge ( NodeType* src, NodeType* snk, EdgeType* edge );
GraphBuilder () : VirtualGraphCreateTemplate<NodeType, EdgeType> (NULL) {}
~GraphBuilder() { printf ("Inside of ~GraphBuilder() \n"); }
void DeleteNode(Node* node)
{
VirtualGraphCreateTemplate<Node, Edge>::DeleteNode(node);
}
};
template <class NodeType, class EdgeType>
void GraphBuilder<NodeType, EdgeType>::addNode ( NodeType* node )
{ VirtualGraphCreateTemplate<NodeType,EdgeType>::AddNode( node ); }
template <class NodeType, class EdgeType>
void GraphBuilder<NodeType, EdgeType>::addEdge ( NodeType* src, NodeType* snk, EdgeType* edge )
{ VirtualGraphCreateTemplate<NodeType,EdgeType>::AddEdge( src, snk, edge ); }
/***********************************************************************************************
* The function
* void TranslateGraph(_GraphStructure& graph, _NodePredicate& _nodePred, _EdgePredicate& _edgePred);
* performas the action _nodePred to every node and _edgePred to every edge in the graph 'graph'.
* This work is general and should work on any graph constructed from VirtualGraphCreateTemplate which
* is the graph all the program analysis work returns.
***********************************************************************************************/
template<typename _GraphStructure, typename _NodePredicate,typename _EdgePredicate>
void translateGraph(_GraphStructure& graph, _NodePredicate _nodePred, _EdgePredicate _edgePred){
typename _GraphStructure::NodeIterator it1 = graph. GetNodeIterator();
while(!it1.ReachEnd()){
typename _GraphStructure::Node* x1 = *it1;
ROSE_ASSERT(x1!=NULL);
_nodePred(x1);
it1++;
typename _GraphStructure::EdgeIterator it2 = graph. GetNodeEdgeIterator(x1,GraphAccess::EdgeOut );
while(!it2.ReachEnd()){
typename _GraphStructure::Edge* y1 = *it2;
ROSE_ASSERT(y1!=NULL);
_edgePred(y1);
it2++;
}
}
};
struct filterNodes : public unary_function<Node*,bool>{
public:
bool operator()(Node* test){
if("src" == test->toString())
return true;
return false;
}
};
struct testPredNodes : public unary_function<Node*,void>{
public:
void operator()(Node* test)
{
// std::cout << "Found a Node" << std::endl;
}
};
struct testPredEdges : public unary_function<Edge*,void>{
public:
void operator()(Edge* test)
{
// std::cout << "Found an Edge" << std::endl;
}
};
int
main( int argc, char * argv[] )
{
GraphBuilder<Node, Edge> graph;
Node* rose = new Node("ROSE"); graph.addNode(rose);
Node* docs = new Node("docs"); graph.addNode(docs);
Node* scripts = new Node("scripts"); graph.addNode(scripts);
Node* src = new Node("src"); graph.addNode(src);
Node* tests = new Node("tests"); graph.addNode(tests);
Node* tools = new Node("tools"); graph.addNode(tools);
Node* tutorial = new Node("tutorial"); graph.addNode(tutorial);
Node* frontend = new Node("frontend"); graph.addNode(frontend);
Node* midend = new Node("midend"); graph.addNode(midend);
Node* backend = new Node("backend"); graph.addNode(backend);
Node* thirdPartyLibraries = new Node("3rdPartyLibraries"); graph.addNode(thirdPartyLibraries);
Node* roseExtensions = new Node("RoseExtensions"); graph.addNode(roseExtensions);
Node* roseIndependentSupport = new Node("RoseIndependentSupport"); graph.addNode(roseIndependentSupport);
Node* rosetta = new Node("ROSETTA"); graph.addNode(rosetta);
Node* util = new Node("util"); graph.addNode(util);
graph.addEdge(rose,docs, new Edge("subdir1"));
graph.addEdge(rose,scripts, new Edge("subdir2"));
graph.addEdge(rose,src, new Edge("subdir3"));
graph.addEdge(rose,tests, new Edge("subdir4"));
graph.addEdge(rose,tools, new Edge("subdir5"));
graph.addEdge(rose,tutorial, new Edge("subdir6"));
graph.addEdge(src,frontend, new Edge("subdirA"));
graph.addEdge(src,midend, new Edge("subdirB"));
graph.addEdge(src,backend, new Edge("subdirC"));
graph.addEdge(src,thirdPartyLibraries, new Edge("subdirD"));
graph.addEdge(src,roseExtensions, new Edge("subdirE"));
graph.addEdge(src,roseIndependentSupport, new Edge("subdirF"));
graph.addEdge(src,rosetta, new Edge("subdirG"));
graph.addEdge(src,util, new Edge("subdirH"));
// Call function to iterate over the graph
translateGraph(graph,testPredNodes(),testPredEdges());
filterGraph(graph,filterNodes());
// Build a DOT graph internally
GraphDotOutput<GraphBuilder<Node, Edge> > output(graph);
// Write out the DOT graph
output.writeToDOTFile("customGraphWithFiltering.dot", "Custom graph");
return 0;
}