forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildCFG.C
64 lines (50 loc) · 1.84 KB
/
buildCFG.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
// Example ROSE Translator: used within ROSE/tutorial
#include "rose.h"
#include <GraphUpdate.h>
#include "CFGImpl.h"
#include "GraphDotOutput.h"
#include "preControlFlowGraph.h"
#include "CommandOptions.h"
using namespace std;
// Use the ControlFlowGraph is defined in both PRE
// and the DominatorTreesAndDominanceFrontiers namespaces.
// We want to use the one in the PRE namespace.
using namespace legacy::PRE;
class visitorTraversal : public AstSimpleProcessing
{
public:
virtual void visit(SgNode* n);
};
void visitorTraversal::visit(SgNode* n)
{
SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(n);
if (functionDeclaration != NULL)
{
SgFunctionDefinition* functionDefinition = functionDeclaration->get_definition();
if (functionDefinition != NULL)
{
SgBasicBlock* functionBody = functionDefinition->get_body();
ROSE_ASSERT(functionBody != NULL);
ControlFlowGraph controlflow;
// The CFG can only be called on a function definition (at present)
makeCfg(functionDefinition,controlflow);
string fileName = functionDeclaration->get_name().str();
fileName += ".dot";
ofstream dotfile(fileName.c_str());
printCfgAsDot(dotfile, controlflow);
}
}
}
int main( int argc, char * argv[] )
{
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
// Build the AST used by ROSE
SgProject* project = frontend(argc,argv);
CmdOptions::GetInstance()->SetOptions(argc,argv);
// Build the traversal object
visitorTraversal exampleTraversal;
// Call the traversal starting at the project node of the AST
exampleTraversal.traverseInputFiles(project,preorder);
return 0;
}