forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acumulatorAttributeTraversal.C
57 lines (44 loc) · 1.67 KB
/
acumulatorAttributeTraversal.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
// ROSE is a tool for building preprocessors, this file is an example preprocessor built with ROSE.
// rose.C: Example (default) ROSE Preprocessor: used for testing ROSE infrastructure
#include "rose.h"
// Build an accumulator attribute, fancy name for what is essentially a global variable :-).
class AccumulatorAttribute
{
public:
int loopCounter;
// Specific constructors are optional
AccumulatorAttribute () : loopCounter(0) {}
AccumulatorAttribute ( const AccumulatorAttribute & X ) {}
AccumulatorAttribute & operator= ( const AccumulatorAttribute & X ) { return *this; }
};
class visitorTraversal : public AstSimpleProcessing
{
public:
static AccumulatorAttribute accumulatorAttribute;
virtual void visit(SgNode* n);
};
// declaration required for static data member
AccumulatorAttribute avisitorTraversal::accumulatorAttribute;
void visitorTraversal::visit(SgNode* n)
{
if (isSgForStatement(n) != NULL)
{
printf ("Found a for loop ... \n");
accumulatorAttribute.loopCounter++;
}
}
int
main ( int argc, char* argv[] )
{
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
SgProject* project = frontend(argc,argv);
ROSE_ASSERT (project != NULL);
// Build the traversal object
visitorTraversal exampleTraversal;
// Call the traversal starting at the project node of the AST
// can be specified to be preorder or postorder).
exampleTraversal.traverseInputFiles(project,preorder);
printf ("Number of loops = %d \n",exampleTraversal.accumulatorAttribute.loopCounter);
return 0;
}