main.cpp 1.8 KB

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