forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addFunctionDeclaration2.C
66 lines (55 loc) · 2.43 KB
/
addFunctionDeclaration2.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
// This example shows how to construct a defining function (with a function body)
// using high level AST construction interfaces.
//
#include "rose.h"
using namespace SageBuilder;
using namespace SageInterface;
class SimpleInstrumentation : public SgSimpleProcessing
{
public:
void visit ( SgNode* astNode );
};
void
SimpleInstrumentation::visit ( SgNode* astNode )
{
SgGlobal* globalScope = isSgGlobal(astNode);
if (globalScope != NULL)
{
// ********************************************************************
// Create a parameter list with a parameter
// ********************************************************************
SgName var1_name = "var_name";
SgReferenceType *ref_type = buildReferenceType(buildIntType());
SgInitializedName *var1_init_name = buildInitializedName(var1_name, ref_type);
SgFunctionParameterList* parameterList = buildFunctionParameterList();
appendArg(parameterList,var1_init_name);
// *****************************************************
// Create a defining functionDeclaration (with a function body)
// *****************************************************
SgName func_name = "my_function";
SgFunctionDeclaration * func = buildDefiningFunctionDeclaration
(func_name, buildIntType(), parameterList,globalScope);
SgBasicBlock* func_body = func->get_definition()->get_body();
// ********************************************************
// Insert a statement in the function body
// *******************************************************
SgVarRefExp *var_ref = buildVarRefExp(var1_name,func_body);
SgPlusPlusOp *pp_expression = buildPlusPlusOp(var_ref);
SgExprStatement* new_stmt = buildExprStatement(pp_expression);
// insert a statement into the function body
prependStatement(new_stmt,func_body);
prependStatement(func,globalScope);
}
}
int
main ( int argc, char * argv[] )
{
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
SgProject* project = frontend(argc,argv);
ROSE_ASSERT(project != NULL);
SimpleInstrumentation treeTraversal;
treeTraversal.traverseInputFiles ( project, preorder );
AstTests::runAllTests(project);
return backend(project);
}