fn_inserter.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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(
  12. cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(),
  13. cam::returns(cam::asString("void"))),
  14. cam::unless(cam::anyOf(cam::cxxConstructorDecl(),
  15. cam::cxxDestructorDecl())))
  16. .bind(Label),
  17. this);
  18. }
  19. void FnInserter::run(const cam::MatchFinder::MatchResult& result) {
  20. const auto* decl = result.Nodes.getNodeAs<clang::FunctionDecl>(Label);
  21. if (!decl) {
  22. llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
  23. }
  24. auto begin = decl->getBeginLoc();
  25. // Replace the first token in the range, `auto`.
  26. auto range = clang::CharSourceRange::getTokenRange(begin, begin);
  27. AddReplacement(*(result.SourceManager), range, "fn");
  28. }
  29. } // namespace Carbon