-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLTRS.cpp
149 lines (114 loc) · 3.65 KB
/
CLTRS.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <string>
#include "include/CLTRS.h"
#include "include/ScriptWriter.h"
#include "include/StringModifier.h"
using namespace clang::CLTRS;
using namespace std;
void CLTRSConsumer::Initialize(ASTContext &Ctx)
{
SWriter = new ScriptWriter();
Modifier = new StringModifier();
Context = &Ctx;
SM = &Context->getSourceManager();
//TUDecl = Context->getTranslationUnitDecl();
MainFileID = SM->getMainFileID();
const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
MainFileStart = MainBuf->getBufferStart();
MainFileEnd = MainBuf->getBufferEnd();
Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions());
SWriter->initialize(this);
//SWriter.setRewriter(&Rewrite);
//SWriter.setContext(Context);
/*
if (RewriteBuffer const *RewriteBuf =
Rewrite.getRewriteBufferFor(MainFileID)) {
llvm::errs() << "Rewriting...\n";
llvm::errs() << std::string(RewriteBuf->begin(), RewriteBuf->end());
}
*/
}
void CLTRSConsumer::HandleTranslationUnit(ASTContext &Context) {
SWriter->HandleTranslationUnit();
//llvm::errs()<<"handling translation unit \n";
//pritn all type test
/* llvm::errs() << " In handleTranslationUnit dump\n";
for(ASTContext::type_iterator I = Context.types_begin(),E = Context.types_end();I != E; I++)
(*I)->dump();
*/
}
bool CLTRSConsumer::HandleTopLevelDecl(DeclGroupRef DG)
{
/*
Rewrite.InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
*/
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
HandleTopLevelSingleDecl(*I);
}
/*
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
const Decl *D = *i;
D->print(llvm::errs());
llvm::errs() << "\ntop-level-decl type: \"" << D->getDeclKindName() << "\"\n";
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
// llvm::errs() << "top-level-decl: \"" << ND->getName()<< " ||| " << "\"\n";
llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n\n";
}
//*/
return true;
}
#if 0
virtual void HandleTranslationUnit(ASTContext &Context) {
//pritn all type test
llvm::errs() << " In handleTranslationUnit dump\n";
for(ASTContext::type_iterator I = Context.types_begin(),E = Context.types_end();I != E; I++)
(*I)->dump();
}
#endif
void CLTRSConsumer::HandleTopLevelSingleDecl(Decl *D)
{
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
SWriter->handleFuncDefinition(FD);
}
else
{
SWriter->recordGlobalDecl(D);
}
}
//unused function should be deleted
void CLTRSConsumer::HandleFuncDefinition(FunctionDecl *FD)
{
if(FD->hasBody())
{
if(NamedDecl *ND = dyn_cast<NamedDecl>(FD))
{
llvm::errs() << "\ndumping name:\n";
// FD->dump();
//llvm::errs() << ND->getAttrs() << "\n";
llvm::errs() << ND->getName() << "\n";
Stmt *ST = FD->getBody();
if(ST)
HandleStmt(ST);
}
}
}
//unused function should be deleted
void CLTRSConsumer::HandleStmt(Stmt *ST)
{
for (Stmt::child_range CI = ST->children(); CI; ++CI) {
if(*CI)
{
llvm::errs() << "\ndumping stmt:----------------------------\n";
//CI->dump();
llvm::errs() << CI->getStmtClassName() <<"\n";
for (Stmt::child_range CII = CI->children(); CII; ++CII)
{
if(*CII)
{
llvm::errs() << "\ndumping single stmt:\n";
CII->dumpAll();
}
}
}
}
// ST->dump();
}