main.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/matcher_manager.h"
  8. #include "migrate_cpp/cpp_refactoring/var_decl.h"
  9. using clang::tooling::RefactoringTool;
  10. // Initialize the files in replacements. Matcher will restrict replacements to
  11. // initialized files.
  12. static void InitReplacements(RefactoringTool* tool) {
  13. clang::FileManager& files = tool->getFiles();
  14. Carbon::Matcher::ReplacementMap& repl = tool->getReplacements();
  15. for (const std::string& path : tool->getSourcePaths()) {
  16. llvm::ErrorOr<const clang::FileEntry*> 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 =
  27. clang::tooling::CommonOptionsParser::create(argc, argv, category);
  28. RefactoringTool tool(parser->getCompilations(), parser->getSourcePathList());
  29. InitReplacements(&tool);
  30. // Set up AST matcher callbacks.
  31. Carbon::MatcherManager matchers(&tool.getReplacements());
  32. matchers.Register(std::make_unique<Carbon::FnInserterFactory>());
  33. matchers.Register(std::make_unique<Carbon::VarDeclFactory>());
  34. return tool.runAndSave(
  35. clang::tooling::newFrontendActionFactory(matchers.GetFinder()).get());
  36. }