main.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "migrate_cpp/cpp_refactoring/var_decl.h"
  8. namespace cam = ::clang::ast_matchers;
  9. namespace ct = ::clang::tooling;
  10. // Initialize the files in replacements. Matcher will restrict replacements to
  11. // initialized files.
  12. static void InitReplacements(ct::RefactoringTool* tool) {
  13. auto& files = tool->getFiles();
  14. auto& repl = tool->getReplacements();
  15. for (const auto& path : tool->getSourcePaths()) {
  16. auto file = files.getFile(path);
  17. if (file.getError()) {
  18. llvm::report_fatal_error("Error accessing `" + path +
  19. "`: " + file.getError().message() + "\n");
  20. }
  21. repl.insert({files.getCanonicalName(*file).str(), {}});
  22. }
  23. }
  24. auto main(int argc, const char** argv) -> int {
  25. llvm::cl::OptionCategory category("C++ refactoring options");
  26. auto parser = ct::CommonOptionsParser::create(argc, argv, category);
  27. ct::RefactoringTool tool(parser->getCompilations(),
  28. parser->getSourcePathList());
  29. InitReplacements(&tool);
  30. // Set up AST matcher callbacks.
  31. auto& repl = tool.getReplacements();
  32. cam::MatchFinder finder;
  33. Carbon::FnInserter fn_inserter(repl, &finder);
  34. Carbon::VarDecl var_decl(repl, &finder);
  35. return tool.runAndSave(
  36. clang::tooling::newFrontendActionFactory(&finder).get());
  37. }