Files
carbon-lang/migrate_cpp/cpp_refactoring/main.cpp
T
Jon Meow 6fa42a7f3c For-range in replacement logic (#631)
This replaces GetAstMatcher with AddMatcher because cxxForRangeStmt is a StatementMatcher. addMatcher has multiple definitions (https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html) and so this approach allows using the right addMatcher without writing per-call overloads.

To handle the `var`, I'm considering something like moving VarDecl logic into a VarMatcherBase so that I can just use CXXForRangeStmt's getLoopVariable. The problem is a for-range statement has multiple VarDecls, and getLoopVariable may be the easiest way to identify the real one.
2021-07-12 16:20:44 -07:00

45 lines
1.8 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "migrate_cpp/cpp_refactoring/for_range.h"
#include "migrate_cpp/cpp_refactoring/matcher_manager.h"
#include "migrate_cpp/cpp_refactoring/var_decl.h"
using clang::tooling::RefactoringTool;
// Initialize the files in replacements. Matcher will restrict replacements to
// initialized files.
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");
}
repl.insert({files.getCanonicalName(*file).str(), {}});
}
}
auto main(int argc, const char** argv) -> int {
llvm::cl::OptionCategory category("C++ refactoring options");
auto parser =
clang::tooling::CommonOptionsParser::create(argc, argv, category);
RefactoringTool tool(parser->getCompilations(), parser->getSourcePathList());
InitReplacements(&tool);
// Set up AST matcher callbacks.
Carbon::MatcherManager matchers(&tool.getReplacements());
matchers.Register(std::make_unique<Carbon::FnInserterFactory>());
matchers.Register(std::make_unique<Carbon::ForRangeFactory>());
matchers.Register(std::make_unique<Carbon::VarDeclFactory>());
return tool.runAndSave(
clang::tooling::newFrontendActionFactory(matchers.GetFinder()).get());
}