-
Notifications
You must be signed in to change notification settings - Fork 0
/
sea-cut.cpp
294 lines (234 loc) · 9.07 KB
/
sea-cut.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/RefactoringCallbacks.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
static llvm::cl::OptionCategory SeaCutCategory("SeaCut options");
static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static llvm::cl::extrahelp MoreHelp("\nMoreHelpText");
cl::opt<std::string> NamespaceName("namespace", cl::cat(SeaCutCategory));
cl::opt<std::string> ClassName("class", cl::cat(SeaCutCategory));
cl::opt<std::string> FileName("fileName", cl::cat(SeaCutCategory));
RefactoringCallback::RefactoringCallback() {}
tooling::Replacements &RefactoringCallback::getReplacements() {
return Replace;
}
class DeleteBodyConsumer : public ASTConsumer {
std::map<std::string, tooling::Replacements> &m_replacements;
std::error_code code;
raw_fd_ostream rawFdOstream;
public:
DeleteBodyConsumer(std::map<std::string, tooling::Replacements> &replacements)
: m_replacements(replacements), rawFdOstream(FileName, code) {
}
DeleteBodyConsumer(const DeleteBodyConsumer &) = delete;
DeleteBodyConsumer &operator=(const DeleteBodyConsumer &) = delete;
static const SmallVector<BoundNodes, 1> findDefinition(ASTContext &Context,
StringRef BindName) {
StringRef parentName;
StringRef AncestorName;
parentName = ClassName;
AncestorName = NamespaceName;
SmallVector<BoundNodes, 1> Results =
match(cxxMethodDecl(hasParent(cxxRecordDecl(hasName(parentName))),
hasAncestor(namespaceDecl(hasName(AncestorName))))
.bind(BindName),
Context);
return Results;
}
void ExtractMethod(SmallVector<BoundNodes, 1> Matches, ASTContext &Context) {
llvm::errs() << "New File:\n";
auto *MD = selectFirst<CXXMethodDecl>("methods", Matches);
const DeclContext *parent = MD->getParent();
// Get all namespace;
unsigned nameSpaceCount = 0;
std::stack<StringRef> s;
while (parent) {
if (parent->isNamespace()) {
++nameSpaceCount;
auto *namespaceDecl = cast<NamespaceDecl>(parent);
s.push(namespaceDecl->getName());
}
parent = parent->getParent();
}
while (!s.empty()) {
StringRef ns = s.top();
s.pop();
errs() << "namespace " << ns << " {\n";
rawFdOstream << "namespace " << ns << " {\n";
}
for (const BoundNodes &N : Matches) {
auto *MD = N.getNodeAs<CXXMethodDecl>("methods");
if (!MD)
return;
PrintMethod(MD, Context);
}
for (unsigned i = 0; i < nameSpaceCount; i++) {
errs() << "}\n";
rawFdOstream << "}\n";
}
llvm::errs() << "\n";
rawFdOstream.close();
}
void PrintMethod(const CXXMethodDecl *MD, ASTContext &Context) {
PrintTemplateDecl(MD, Context);
if (MD->isInlined()) {
errs() << "inline ";
rawFdOstream << "inline ";
}
MD->getReturnType().print(errs(), PrintingPolicy(LangOptions()));
MD->getReturnType().print(rawFdOstream, PrintingPolicy(LangOptions()));
errs() << " ";
rawFdOstream << " ";
PrintClass(MD);
errs() << MD->getName();
rawFdOstream << MD->getName();
PrintParameters(MD, Context);
if (MD->isConst()) {
errs() << " const ";
rawFdOstream << " const ";
}
MD->getBody()->printPretty(errs(), nullptr, PrintingPolicy(LangOptions()));
MD->getBody()->printPretty(rawFdOstream, nullptr, PrintingPolicy(LangOptions()));
}
void PrintTemplateDecl(const CXXMethodDecl *MD, ASTContext &Context) {
const DeclContext *methodParent = MD->getParent();
while (methodParent) {
if (!methodParent->isRecord()) {
methodParent = methodParent->getParent();
continue;
}
auto *record = cast<RecordDecl>(methodParent);
if (record->isTemplated() && record->getDescribedTemplate()) {
auto *templateParameters = record->getDescribedTemplate()
->getTemplateParameters();
errs() << getTextFromSourceRange(templateParameters->getSourceRange(),
Context)
<< ">\n";
rawFdOstream << getTextFromSourceRange(templateParameters->getSourceRange(),
Context)
<< ">\n";
}
methodParent = methodParent->getParent();
}
}
void PrintParameters(const CXXMethodDecl *MD, ASTContext &Context) {
errs() << "(";
rawFdOstream << "(";
for (size_t i = 0; i < MD->param_size(); i++) {
MD->getParamDecl(i)->print(errs());
MD->getParamDecl(i)->print(rawFdOstream);
if (i < MD->param_size() - 1) {
errs() << ", ";
rawFdOstream << ", ";
}
}
errs() << ")";
rawFdOstream << ")";
}
void PrintClass(const CXXMethodDecl *MD) {
const DeclContext *methodParent = MD->getParent();
while (methodParent) {
if (methodParent->isRecord()) {
auto *recordDecl = cast<RecordDecl>(methodParent);
errs() << recordDecl->getName();
rawFdOstream << recordDecl->getName();
PrintTemplate(recordDecl);
errs() << "::";
rawFdOstream << "::";
}
methodParent = methodParent->getParent();
}
}
void PrintTemplate(const RecordDecl *recordDecl) {
if (!recordDecl->isTemplated() || !recordDecl->getDescribedTemplate()) return;
TemplateParameterList *templateParameterList =
recordDecl->getDescribedTemplate()->getTemplateParameters();
errs() << "<";
rawFdOstream << "<";
for (unsigned i = 0; i < templateParameterList->size(); i++) {
errs() << templateParameterList->getParam(i)->getName();
rawFdOstream << templateParameterList->getParam(i)->getName();
templateParameterList->getParam(i) -> getSourceRange();
if (i < templateParameterList->size() - 1) {
errs() << ", ";
rawFdOstream << ", ";
}
}
errs() << ">";
rawFdOstream << ">";
}
static StringRef getTextFromSourceRange(SourceRange R, ASTContext &Context) {
return Lexer::getSourceText(CharSourceRange::getCharRange(R), Context.getSourceManager(), LangOptions());
}
void HandleTranslationUnit(ASTContext &Context) override {
auto Matches = findDefinition(Context, "methods");
if (Matches.size() < 1)
return;
ExtractMethod(Matches, Context);
for (const BoundNodes &N : Matches) {
auto *MD = N.getNodeAs<CXXMethodDecl>("methods");
if (!MD)
return;
llvm::errs() << "Match found:\n";
MD->dump();
SourceRange body = MD->getBody()->getSourceRange();
StringRef newBody = ";";
tooling::Replacement replacement(Context.getSourceManager(),
CharSourceRange::getTokenRange(body),
newBody, Context.getLangOpts());
llvm::errs() << "New replacement: \n" << replacement.toString() <<
"\n";
consumeError(m_replacements[replacement.getFilePath()].add(replacement));
}
}
};
class DeleteBodyAction {
std::map<std::string, tooling::Replacements> &m_replacements;
public:
DeleteBodyAction(std::map<std::string, tooling::Replacements> &replacements)
: m_replacements(replacements) {}
DeleteBodyAction(const DeleteBodyAction &) = delete;
DeleteBodyAction &operator=(const DeleteBodyAction &) = delete;
std::unique_ptr<ASTConsumer> newASTConsumer() {
return llvm::make_unique<DeleteBodyConsumer>(m_replacements);
}
};
int main(int argc, const char **argv) {
CommonOptionsParser parser(argc, argv, SeaCutCategory);
auto files = parser.getSourcePathList();
RefactoringTool reTool(parser.getCompilations(), parser.getSourcePathList());
DeleteBodyAction action(reTool.getReplacements());
auto factory = tooling::newFrontendActionFactory(&action);
if (int result = reTool.run(factory.get()))
return result;
auto &fileMgr = reTool.getFiles();
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
clang::TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
&DiagnosticPrinter, false);
SourceManager sources(Diagnostics, fileMgr);
Rewriter rewriter(sources, LangOptions());
reTool.applyAllReplacements(rewriter);
llvm::errs() << "File after applying replacements:";
size_t i = 0;
for (const auto &file : files) {
llvm::errs() << "\n#" << (i++) << " " << file << "\n";
const auto *entry = fileMgr.getFile(file);
const auto ID = sources.getOrCreateFileID(entry, SrcMgr::C_User);
rewriter.getEditBuffer(ID).write(llvm::outs());
llvm::outs() << "\n";
}
return 0;
}