Files
carbon-lang/migrate_cpp/cpp_refactoring/fn_inserter.cpp
T
Jon Meow f180c64454 Change how replacements are filtered (#541)
This replaces the use of `isExpansionInMainFile` with instead taking the list of input files, and only touching them. This doesn't keep `isExpansionInMainFile` because it should be redundant, and as such it'd be easy to forget. Unless it starts being a performance issue, it's probably better to omit.
2021-06-03 16:35:55 -07:00

32 lines
1.1 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) {
finder->addMatcher(cam::functionDecl(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