forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addVariableDeclaration.C
68 lines (51 loc) · 2.44 KB
/
addVariableDeclaration.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
// ROSE is a tool for building preprocessors, this file is an example preprocessor built with ROSE.
// Specifically it shows the design of a transformation to instrument source code, placing source code
// at the top and bottom of each basic block.
// Member functions of SAGE III AST node classes are directly used.
// So all details for Sg_File_Info, scope, parent, symbol tables have to be explicitly handled.
#include "rose.h"
class SimpleInstrumentation : public SgSimpleProcessing
{
public:
void visit ( SgNode* astNode );
};
void
SimpleInstrumentation::visit ( SgNode* astNode )
{
SgBasicBlock* block = isSgBasicBlock(astNode);
if (block != NULL)
{
// Mark this as a transformation (required)
Sg_File_Info* sourceLocation = Sg_File_Info::generateDefaultFileInfoForTransformationNode();
ROSE_ASSERT(sourceLocation != NULL);
SgType* type = new SgTypeInt();
ROSE_ASSERT(type != NULL);
SgName name = "newVariable";
SgVariableDeclaration* variableDeclaration = new SgVariableDeclaration(sourceLocation,name,type);
ROSE_ASSERT(variableDeclaration != NULL);
SgInitializedName* initializedName = *(variableDeclaration->get_variables().begin());
initializedName->set_file_info(Sg_File_Info::generateDefaultFileInfoForTransformationNode());
// DQ (6/18/2007): The unparser requires that the scope be set (for name qualification to work).
initializedName->set_scope(block);
// Liao (2/13/2008): AstTests requires this to be set
variableDeclaration->set_firstNondefiningDeclaration(variableDeclaration);
ROSE_ASSERT(block->get_statements().size() > 0);
block->get_statements().insert(block->get_statements().begin(),variableDeclaration);
variableDeclaration->set_parent(block);
// Add a symbol to the sybol table for the new variable
SgVariableSymbol* variableSymbol = new SgVariableSymbol(initializedName);
block->insert_symbol(name,variableSymbol);
}
}
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);
}