-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewriter.cpp
143 lines (120 loc) · 4.87 KB
/
rewriter.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
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/CommandLine.h>
#include <clang/AST/AST.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Frontend/CompilerInstance.h>
#include <iostream>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>
#include <clang/Rewrite/Core/Rewriter.h>
using namespace clang;
class NlanesVisitor : public RecursiveASTVisitor<NlanesVisitor> {
public:
explicit NlanesVisitor(ASTContext *Context, Rewriter &TheRewriter)
: Context(Context), TheRewriter(TheRewriter) {}
bool VisitDeclRefExpr(DeclRefExpr *Expr) {
if (EnumConstantDecl *EnumDecl = dyn_cast<EnumConstantDecl>(Expr->getDecl())) {
if (EnumDecl->getNameAsString() == "nlanes") {
printNlanesRefInfo(Expr);
rewriteNlanesRef(Expr);
}
}
return true;
}
private:
void printNlanesRefInfo(DeclRefExpr *Expr);
void rewriteNlanesRef(DeclRefExpr *Expr);
ASTContext *Context;
Rewriter &TheRewriter;
};
void NlanesVisitor::printNlanesRefInfo(DeclRefExpr *Expr) {
SourceManager &SM = Context->getSourceManager();
SourceLocation startLoc = Expr->getBeginLoc();
SourceLocation endLoc = Expr->getEndLoc();
FullSourceLoc FullLocation = Context->getFullLoc(Expr->getBeginLoc());
if (FullLocation.isValid() && SM.isWrittenInMainFile(FullLocation)) {
std::string code = Lexer::getSourceText(CharSourceRange::getTokenRange(startLoc, endLoc),
SM, Context->getLangOpts()).str();
llvm::outs() << "Found " << code << "\t" << "reference at "
<< SM.getFilename(FullLocation)
<< ":" << FullLocation.getSpellingLineNumber()
<< "\n";
}
}
void NlanesVisitor::rewriteNlanesRef(DeclRefExpr *Expr) {
SourceManager &SM = Context->getSourceManager();
FullSourceLoc FullLocation = Context->getFullLoc(Expr->getBeginLoc());
if (FullLocation.isValid() && SM.isWrittenInMainFile(FullLocation)) {
// Get the type of the enclosing class
if (auto *ParentDecl = dyn_cast<CXXRecordDecl>(Expr->getDecl()->getDeclContext()->getParent())) {
std::string vecTypeName = Lexer::getSourceText(CharSourceRange::getTokenRange(Context->getFullLoc(Expr->getBeginLoc())),
SM, Context->getLangOpts()).str();
std::string Replacement = "VTraits<" + vecTypeName + ">::vlanes()";
TheRewriter.ReplaceText(Expr->getSourceRange(), Replacement);
}
}
}
class NlanesASTConsumer : public clang::ASTConsumer {
public:
explicit NlanesASTConsumer(ASTContext *Context, Rewriter &R)
: Visitor(Context, R) {}
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
private:
NlanesVisitor Visitor;
};
class NlanesFrontendAction : public clang::ASTFrontendAction {
public:
NlanesFrontendAction() {}
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &CI, StringRef file) override {
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return std::make_unique<NlanesASTConsumer>(&CI.getASTContext(), TheRewriter);
}
void EndSourceFileAction() override {
TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID())
.write(llvm::outs());
}
private:
Rewriter TheRewriter;
};
using namespace clang::tooling;
int main(int argc, const char **argv) {
llvm::cl::OptionCategory MyToolCategory("My tool options");
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser &OptionsParser = ExpectedParser.get();
std::vector<std::string> sourceFiles;
std::string inputPath = OptionsParser.getSourcePathList()[0];
if (llvm::sys::fs::is_directory(inputPath)) {
// This lambda function checks if a file has a .cpp or .hpp extension
auto fileFilter = [](const llvm::StringRef &filename) -> bool {
std::string ext = filename.str();
ext = llvm::sys::path::extension(ext);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
return ext == ".cpp" || ext == ".hpp";
};
// Recursively find files with .cpp and .hpp extensions in the input directory
std::error_code ec;
llvm::sys::fs::recursive_directory_iterator dir_itr(inputPath, ec), dir_end;
while (dir_itr != dir_end) {
llvm::StringRef pathStr = dir_itr->path();
if (fileFilter(pathStr)) {
sourceFiles.push_back(pathStr.str());
}
dir_itr.increment(ec);
}
} else {
// Single file input
sourceFiles = OptionsParser.getSourcePathList();
}
ClangTool Tool(OptionsParser.getCompilations(), sourceFiles);
return Tool.run(newFrontendActionFactory<NlanesFrontendAction>().get());
}