-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.cpp
71 lines (57 loc) · 2.06 KB
/
main.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
#include <iostream>
#include <sstream>
#include <llvm/Support/CommandLine.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>
#include <clang/Rewrite/Core/Rewriter.h>
#include <clang/Frontend/CompilerInstance.h>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
class CastCallBack : public MatchFinder::MatchCallback {
public:
CastCallBack(Rewriter& rewriter) {
// Your code goes here
};
virtual void run(const MatchFinder::MatchResult &Result) {
// Your code goes here
}
};
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(Rewriter &rewriter) : callback_(rewriter) {
matcher_.addMatcher(
cStyleCastExpr(unless(isExpansionInSystemHeader())).bind("cast"), &callback_);
}
void HandleTranslationUnit(ASTContext &Context) override {
matcher_.matchAST(Context);
}
private:
CastCallBack callback_;
MatchFinder matcher_;
};
class CStyleCheckerFrontendAction : public ASTFrontendAction {
public:
CStyleCheckerFrontendAction() = default;
void EndSourceFileAction() override {
rewriter_.getEditBuffer(rewriter_.getSourceMgr().getMainFileID())
.write(llvm::outs());
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef /* file */) override {
rewriter_.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return std::make_unique<MyASTConsumer>(rewriter_);
}
private:
Rewriter rewriter_;
};
static llvm::cl::OptionCategory CastMatcherCategory("cast-matcher options");
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, CastMatcherCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<CStyleCheckerFrontendAction>().get());
}