forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectDefineDirectives.C
105 lines (85 loc) · 3.87 KB
/
collectDefineDirectives.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
// Example ROSE Translator: used within ROSE/tutorial
#include "rose.h"
using namespace std;
// Build a synthesized attribute for the tree traversal
class SynthesizedAttribute
{
public:
// List of #define directives (save the PreprocessingInfo objects
// so that we have all the source code position information).
list<PreprocessingInfo*> accumulatedList;
void display() const;
};
void
SynthesizedAttribute::display() const
{
list<PreprocessingInfo*>::const_iterator i = accumulatedList.begin();
while (i != accumulatedList.end())
{
printf ("CPP define directive = %s \n",(*i)->getString().c_str());
i++;
}
}
class visitorTraversal : public AstBottomUpProcessing<SynthesizedAttribute>
{
public:
// virtual function must be defined
virtual SynthesizedAttribute evaluateSynthesizedAttribute (
SgNode* n, SynthesizedAttributesList childAttributes );
};
SynthesizedAttribute
visitorTraversal::evaluateSynthesizedAttribute ( SgNode* n, SynthesizedAttributesList childAttributes )
{
SynthesizedAttribute localResult;
// printf ("In evaluateSynthesizedAttribute(n = %p = %s) \n",n,n->class_name().c_str());
// Build the list from children (in reverse order to preserve the final ordering)
for (SynthesizedAttributesList::reverse_iterator child = childAttributes.rbegin(); child != childAttributes.rend(); child++)
{
localResult.accumulatedList.splice(localResult.accumulatedList.begin(),child->accumulatedList);
}
// Add in the information from the current node
SgLocatedNode* locatedNode = isSgLocatedNode(n);
if (locatedNode != NULL)
{
AttachedPreprocessingInfoType* commentsAndDirectives = locatedNode->getAttachedPreprocessingInfo();
if (commentsAndDirectives != NULL)
{
// printf ("Found attached comments (to IR node at %p of type: %s): \n",locatedNode,locatedNode->class_name().c_str());
// int counter = 0;
// Use a reverse iterator so that we preserve the order when using push_front to add each directive to the accumulatedList
AttachedPreprocessingInfoType::reverse_iterator i;
for (i = commentsAndDirectives->rbegin(); i != commentsAndDirectives->rend(); i++)
{
// The different classifications of comments and directives are in ROSE/src/frontend/SageIII/rose_attributes_list.h
if ((*i)->getTypeOfDirective() == PreprocessingInfo::CpreprocessorDefineDeclaration)
{
#if 0
printf (" Attached Comment #%d in file %s (relativePosition=%s): classification %s :\n%s\n",
counter++,(*i)->get_file_info()->get_filenameString().c_str(),
((*i)->getRelativePosition() == PreprocessingInfo::before) ? "before" : "after",
PreprocessingInfo::directiveTypeName((*i)->getTypeOfDirective()).c_str(),
(*i)->getString().c_str());
#endif
// use push_front() to end up with source ordering of final list of directives
localResult.accumulatedList.push_front(*i);
}
}
}
}
// printf ("localResult after adding current node info \n");
// localResult.display();
return localResult;
}
int
main ( int argc, char* argv[] )
{
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
SgProject* project = frontend(argc,argv);
// Build the traversal object and call "traverse" member function
visitorTraversal exampleTraversal;
SynthesizedAttribute results = exampleTraversal.traverse(project);
// Output the results
results.display();
return 0;
}