Refactor Matcher code to reduce per-Matcher boilerplate (#615)

This is in part to eliminate the "sm" and "lang_opts" variables that received comment, but also cuts back on the use of auto.
This commit is contained in:
Jon Meow
2021-07-08 12:42:24 -07:00
committed by GitHub
parent db2efeafeb
commit 4f1b2198ff
13 changed files with 252 additions and 162 deletions
+14 -15
View File
@@ -5,18 +5,18 @@
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "migrate_cpp/cpp_refactoring/matcher_manager.h"
#include "migrate_cpp/cpp_refactoring/var_decl.h"
namespace cam = ::clang::ast_matchers;
namespace ct = ::clang::tooling;
using clang::tooling::RefactoringTool;
// Initialize the files in replacements. Matcher will restrict replacements to
// initialized files.
static void InitReplacements(ct::RefactoringTool* tool) {
auto& files = tool->getFiles();
auto& repl = tool->getReplacements();
for (const auto& path : tool->getSourcePaths()) {
auto file = files.getFile(path);
static void InitReplacements(RefactoringTool* tool) {
clang::FileManager& files = tool->getFiles();
Carbon::Matcher::ReplacementMap& repl = tool->getReplacements();
for (const std::string& path : tool->getSourcePaths()) {
llvm::ErrorOr<const clang::FileEntry*> file = files.getFile(path);
if (file.getError()) {
llvm::report_fatal_error("Error accessing `" + path +
"`: " + file.getError().message() + "\n");
@@ -27,17 +27,16 @@ static void InitReplacements(ct::RefactoringTool* tool) {
auto main(int argc, const char** argv) -> int {
llvm::cl::OptionCategory category("C++ refactoring options");
auto parser = ct::CommonOptionsParser::create(argc, argv, category);
ct::RefactoringTool tool(parser->getCompilations(),
parser->getSourcePathList());
auto parser =
clang::tooling::CommonOptionsParser::create(argc, argv, category);
RefactoringTool tool(parser->getCompilations(), parser->getSourcePathList());
InitReplacements(&tool);
// Set up AST matcher callbacks.
auto& repl = tool.getReplacements();
cam::MatchFinder finder;
Carbon::FnInserter fn_inserter(repl, &finder);
Carbon::VarDecl var_decl(repl, &finder);
Carbon::MatcherManager matchers(&tool.getReplacements());
matchers.Register(std::make_unique<Carbon::FnInserterFactory>());
matchers.Register(std::make_unique<Carbon::VarDeclFactory>());
return tool.runAndSave(
clang::tooling::newFrontendActionFactory(&finder).get());
clang::tooling::newFrontendActionFactory(matchers.GetFinder()).get());
}