main.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "clang/Tooling/CommonOptionsParser.h"
  5. #include "clang/Tooling/Refactoring.h"
  6. #include "migrate_cpp/cpp_refactoring/fn_inserter.h"
  7. namespace cam = ::clang::ast_matchers;
  8. namespace ct = ::clang::tooling;
  9. // Initialize the files in replacements. Matcher will restrict replacements to
  10. // initialized files.
  11. static void InitReplacements(ct::RefactoringTool* tool) {
  12. auto& files = tool->getFiles();
  13. auto& repl = tool->getReplacements();
  14. for (const auto& path : tool->getSourcePaths()) {
  15. auto file = files.getFile(path);
  16. if (file.getError()) {
  17. llvm::report_fatal_error("Error accessing `" + path +
  18. "`: " + file.getError().message() + "\n");
  19. }
  20. repl.insert({files.getCanonicalName(*file).str(), {}});
  21. }
  22. }
  23. auto main(int argc, const char** argv) -> int {
  24. llvm::cl::OptionCategory category("C++ refactoring options");
  25. auto parser = ct::CommonOptionsParser::create(argc, argv, category);
  26. ct::RefactoringTool tool(parser->getCompilations(),
  27. parser->getSourcePathList());
  28. InitReplacements(&tool);
  29. // Set up AST matcher callbacks.
  30. cam::MatchFinder finder;
  31. Carbon::FnInserter fn_inserter(tool.getReplacements(), &finder);
  32. return tool.runAndSave(
  33. clang::tooling::newFrontendActionFactory(&finder).get());
  34. }