Files
carbon-lang/migrate_cpp/cpp_refactoring/fn_inserter.cpp
T
Jon Meow 29bf305539 Add tests to the cpp_refactoring tool. (#539)
This refactors the main.cpp out into a file structure that should make it easier to add more matchers/tests.
2021-05-17 16:55:50 -07:00

37 lines
1.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "clang/ASTMatchers/ASTMatchers.h"
namespace cam = ::clang::ast_matchers;
namespace Carbon {
FnInserter::FnInserter(std::map<std::string, Replacements>& in_replacements,
cam::MatchFinder* finder)
: Matcher(in_replacements) {
// TODO: Switch from isExpansionInMainFile to isDefinition. That should then
// include `for (const auto* redecl : func->redecls())` to generate
// replacements.
finder->addMatcher(
cam::functionDecl(cam::isExpansionInMainFile(), cam::hasTrailingReturn())
.bind(Label),
this);
}
void FnInserter::run(const cam::MatchFinder::MatchResult& result) {
const auto* decl = result.Nodes.getNodeAs<clang::FunctionDecl>(Label);
if (!decl) {
llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
}
auto begin = decl->getBeginLoc();
// Replace the first token in the range, `auto`.
auto range = clang::CharSourceRange::getTokenRange(begin, begin);
AddReplacement(*(result.SourceManager), range, "fn");
}
} // namespace Carbon