Created
April 20, 2019 20:02
-
-
Save chichunchen/ae3c8a9225f9b8875927d0d1456adc73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Consumer : public clang::ASTConsumer { | |
public: | |
explicit Consumer(bool RewriteOption, clang::Rewriter &Rewriter) | |
: Checker(RewriteOption, Rewriter) { | |
using namespace clang::ast_matchers; | |
const auto CXXMethodMatcher = | |
cxxMethodDecl(unless(isExpansionInSystemHeader())).bind("target"); | |
Finder.addMatcher(CXXMethodMatcher, &Checker); | |
} | |
void HandleTranslationUnit(clang::ASTContext &Context) override { | |
Finder.matchAST(Context); | |
} | |
private: | |
clang::ast_matchers::MatchFinder Finder; | |
Checker Checker; | |
}; | |
class Action : public clang::ASTFrontendAction { | |
public: | |
using ASTConsumerPointer = std::unique_ptr<clang::ASTConsumer>; | |
explicit Action(bool RewriteOption) : RewriteOption(RewriteOption) {} | |
ASTConsumerPointer CreateASTConsumer(clang::CompilerInstance &Compiler, | |
llvm::StringRef Filename) override { | |
Rewriter.setSourceMgr(Compiler.getSourceManager(), Compiler.getLangOpts()); | |
return std::make_unique<Consumer>(RewriteOption, Rewriter); | |
} | |
bool BeginSourceFileAction(clang::CompilerInstance &Compiler) override { | |
llvm::errs() << "Processing " << getCurrentFile() << "\n\n"; | |
return true; | |
} | |
void EndSourceFileAction() override { | |
if (!RewriteOption) | |
return; | |
const auto File = Rewriter.getSourceMgr().getMainFileID(); | |
Rewriter.getEditBuffer(File).write(llvm::outs()); | |
} | |
private: | |
bool RewriteOption; | |
clang::Rewriter Rewriter; | |
}; | |
} // namespace UseOverride |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment