fn_inserter.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031
  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 "migrate_cpp/cpp_refactoring/fn_inserter.h"
  5. #include "clang/ASTMatchers/ASTMatchers.h"
  6. namespace cam = ::clang::ast_matchers;
  7. namespace Carbon {
  8. FnInserter::FnInserter(std::map<std::string, Replacements>& in_replacements,
  9. cam::MatchFinder* finder)
  10. : Matcher(in_replacements) {
  11. finder->addMatcher(cam::functionDecl(cam::hasTrailingReturn()).bind(Label),
  12. this);
  13. }
  14. void FnInserter::run(const cam::MatchFinder::MatchResult& result) {
  15. const auto* decl = result.Nodes.getNodeAs<clang::FunctionDecl>(Label);
  16. if (!decl) {
  17. llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
  18. }
  19. auto begin = decl->getBeginLoc();
  20. // Replace the first token in the range, `auto`.
  21. auto range = clang::CharSourceRange::getTokenRange(begin, begin);
  22. AddReplacement(*(result.SourceManager), range, "fn");
  23. }
  24. } // namespace Carbon