Skip to content

Instantly share code, notes, and snippets.

@chichunchen
Created April 20, 2019 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chichunchen/ae3c8a9225f9b8875927d0d1456adc73 to your computer and use it in GitHub Desktop.
Save chichunchen/ae3c8a9225f9b8875927d0d1456adc73 to your computer and use it in GitHub Desktop.
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